freechat/json-api/client-tests/main.py
shockrah e2e68c51ba - Removing old client test code in favor of new structure
+ Moving tests to the new client
- Removing web/ module
! Currently all tests are passing 17/17 but the real trickery comes with doing this
on CI which should will likely take some magic somewhere
Or we'll just extend the freechat docker image to finally have all the required
dependancies and just test on that with diesel and what not
2021-04-21 21:38:25 -07:00

117 lines
3.9 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 req(admin: Admin, method: str, path: str, qs: dict, hope: int, headers: dict={}, body=None, verbose=False) -> 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, verbose)
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([
req(fake_user, 'get', '/channels/list', {}, 401),
req(admin, 'post', '/channels/list', {'kind': TEXT_CHAN}, 404),
req(admin, 'get' , '/channels/list', {'kind': TEXT_CHAN}, 200),
req(admin , 'delete', '/channels/delete', {'channel_id':del_chan_id}, 200),
req(admin, 'post', '/message/send', {'channel_id': chan_id},200,{'content-type':'text/plain'}, 'asdf'),
req(admin, 'post', '/message/send', {'channel_id': 123}, 400, {'content-type': 'text/plain'}, 'asdf'),
req(admin , 'post', '/message/send', {'channel_id': chan_id}, 200, {'content-type': 'image/png'}, 'asdf'),
req(admin , 'post', '/message/send', {'channel_id': 123}, 400, {'content-type': 'image/png'}, 'asdf'),
req(admin, 'get', '/message/recent', {'channel_id': chan_id, 'limit': 20}, 200, verbose=True),
req(admin, 'get', '/message/recent', {'channel_id': 123, 'limit': 20}, 404),
req(admin, 'get', '/members/me', {}, 200),
req(admin, 'get', '/members/get_online', {}, 200),
req(admin, 'post', '/members/me/nickname', {'nick': f'randy-{time()}'}, 200),
req(admin , 'post', '/invite/create', {}, 200, verbose=True)
])
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)
pass_count = 0
for r in requests:
r.show_response()
if r.passing:
pass_count += 1
req_count = len(requests)
print(f'Passing {pass_count}/{req_count}')