freechat/json-api/client-tests/main.py
shockrah 38ff0edd39 ! Massive test suite overhaul see details below !
Problem: the old test suite was extremely inflexible
This meant that making new tests took way too much time.

+ This new rework makes the new client's backend much thinner and less "magical"
With less magic going on we can pass way more data more easily to the actual
http-request engine making the convenience wrapper over top it much more flexible

Translating old tests to the new engine might take a while but for now the old
client codebase is completely deprecated and will no longer be used+updated
2021-04-21 17:22:16 -07:00

105 lines
3.4 KiB
Python

from time import time
from request import Request
from config import create_admin, Admin
from config import Server
RESPONSES = []
VOICE_CHAN = 1
TEXT_CHAN = 2
def login() -> (Request, str):
req = Request(
admin.server.url + '/login',
'post',
{'id':admin.id, 'secret': admin.secret},
{},
200
)
response = req.fire()
jwt = response.json().get('jwt')
return (req, jwt)
def make_channel(url: str, id: int, jwt: str) -> (Request, int):
# making a text channel
channel_name = str(time())
qs = {'id': id, 'jwt': jwt, 'kind': 2, 'name': channel_name}
req = Request(url + '/channels/create', 'post', qs, {}, 200)
resp = req.fire()
try:
channel = resp.json().get('channel')
chan_id = 0 if channel is None else channel.get('id')
return (req, chan_id)
except Exception as e:
print(e)
print(f'Actual value: {resp.text}: {resp.status_code}')
return (req, 0)
def bs_admin(server: Server) -> Admin:
user_ = {
'id': 123,
'jwt': None,
'secret': 'no thanks'
}
server_ = {
'url': server.url,
'wsurl': server.wsurl,
'name': server.serv_name
}
return Admin(user_, server_)
def std_request(admin: Admin, method: str, path: str, qs: dict, hope: int, headers: dict={}, body=None) -> Request:
url = admin.server.url + path
# Copy over the additional params into the query string
params = {'id': admin.id, 'jwt': admin.jwt}
for key in qs:
params[key] = qs[key]
return Request(url, method, params, headers, hope, body)
if __name__ == '__main__':
admin = create_admin()
if admin is None:
print('Unable to parse/create admin account')
exit(1)
fake_user = bs_admin(admin.server)
requests = []
# First a quick sanity check for login
# add this after we fire the generic tests
login_req, jwt = login()
if jwt is None:
print('Unable to /login - stopping now to avoid pointless failure')
exit(1)
admin.jwt = jwt
# add this after we fire the generic tests
mk_chan_sanity, chan_id = make_channel(admin.server.url, admin.id, admin.jwt)
mk_chan_to_delete, del_chan_id = make_channel(admin.server.url, admin.id, admin.jwt)
# Container for most/all the generic requests we want to fire off
requests.extend([
std_request(fake_user, 'get', '/channels/list', {}, 401),
std_request(admin, 'post', '/channels/list', {'kind': TEXT_CHAN}, 404),
std_request(admin, 'get' , '/channels/list', {'kind': TEXT_CHAN}, 200),
std_request(admin , 'delete', '/channels/delete', {'channel_id':del_chan_id}, 200),
std_request(admin, 'post', '/message/send', {'channel_id': chan_id},200,{'content-type':'text/plain'}, 'asdf'),
std_request(admin, 'post', '/message/send', {'channel_id': 123}, 400, {'content-type': 'text/plain'}, 'asdf'),
std_request(admin , 'post', '/message/send', {'channel_id': chan_id}, 200, {'content-type': 'image/png'}, 'asdf'),
std_request(admin , 'post', '/message/send', {'channel_id': 123}, 400, {'content-type': 'image/png'}, 'asdf'),
])
for req in requests:
req.fire()
# Prepend the sanity checks we did early on
requests.insert(0, login_req)
requests.insert(0, mk_chan_sanity)
requests.insert(0, mk_chan_to_delete)
for req in requests:
req.show_response()