From 79d77e0007a5e356a52f7a76791b7909d09b76e4 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 20 Jan 2021 13:50:09 -0800 Subject: [PATCH] Beginning refactor of test client to more cleanly create new tests Body parameters were being polluted so new logic is required to make the worker more stable and ignore its state properly --- server-api/client-tests/client.py | 58 +++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/server-api/client-tests/client.py b/server-api/client-tests/client.py index 416331f..038e070 100644 --- a/server-api/client-tests/client.py +++ b/server-api/client-tests/client.py @@ -2,6 +2,9 @@ import time import subprocess, os, sys import json, requests +class RequestError(Exception): + pass + class Response: def __init__(self, body, code): self.body = body @@ -10,6 +13,45 @@ class Response: def __str__(self): return f'{self.code} => {self.body}' +class Request: + def __init__(self, domain: str, params: dict): + assert(path[len(path) - 1] != '/') + + self.domain = domain + self.params = params + + def _make_request(self, method: str, path: str, hope: int): + # Lower driver for actuall making the request we are looking for + assert(path.startswith('/')) + method = method.lower() + + url = self.domain + path + if method == 'get': + resp = requests.get(url, data=self.params) + return Response(resp.body, resp.status_code, hope) + elif method == 'post': + resp = requests.post(url, data=self.params) + return Response(resp.body, resp.status_code, hope) + elif: method == 'delete': + resp = requests.delete(url, data=self.params) + return Response(resp.body, resp.status_code, hope) + + else: + raise RequestError('Invalid method passed') + + + + def get(self, path: str, hope: int): + return self._make_request('get', path, hope) + + def post(self, path: str, hope: int): + return self._make_request('post', path, hope) + + def delete(self, path: str, hope: int): + return self._make_request('delete', path, hope) + + + class Test: def __init__(self, base='http://localhost:8888', create_admin=False, admin=None, init_verbose=False): ''' @@ -36,12 +78,16 @@ class Test: # for now we use this because my dev server has this fake ass acc on it self.secret = 'mH7DfAfSLHhVbo9OW4dmqTbzzLnFaZQLxoZFIlKvKHm4NfpLyZsZrYdf12aACZXyEQxELOlBTe2rc36vTPVd8A==', self.id = 2 + # Required params self.body = { 'name': 'owner sama uwu', 'joindate': 69, 'status': 0, 'permissions': 18446744073709551615 } + self.default_body = {} + # deep copy of body to prevent side-channel writes + for k in self.body: self.default_body[k] = self.body[k] self.base = base @@ -72,7 +118,7 @@ class Test: print(f'TESTBOT\t\t[Body]: {request.text}\n', file=self.out) def _build_req_body(self, **options): - _map = self.body + _map = self.default_body for key in options: _map[key] = options[key] @@ -188,11 +234,19 @@ def run(worker): # pass 200(all) spam_messages(send_chan['id'], jwt, worker) now = time.time() - worker.get('/message/get_range', jwt=jwt, **{ + messages = json.loads(worker.get('/message/get_range', jwt=jwt, **{ 'start-time': int(now - 15), 'end-time': int(now + 1), 'channel': send_chan['id'] + })) + + # 200 pass + from_id = worker.get('/message/from_id', jwt=jwt, **{ + 'channel': send_chan['id'], + 'start': messages['messages'][0]['id'] }) + print('done') + if __name__ == '__main__':