diff --git a/tests/api.py b/tests/api.py index 338e043..d9d1738 100644 --- a/tests/api.py +++ b/tests/api.py @@ -21,7 +21,7 @@ class User(): name: string password: string - def __init__(self, email, password, name, token = ""): + def __init__(self, email, password, name, id="", token = ""): self.email = email self.password = password self.name = name @@ -32,45 +32,49 @@ class BackendApi(): def __init__(self, httpClient): self.httpClient = httpClient - def user_create(self) -> User: - email = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10)) + '@test.test' - name = ''.join(random.choices(string.ascii_letters, k=10)) - password = 'Abcdef1!!1' - - response = self.httpClient.post( - "/v1/user/create", - json={ - "email": email, - "password": password, - "name": name, - }, - ) - if response.status_code != 200: - raise AssertionError('can not create user') + def parse_response(self, response): + if response.status != 200: + raise AssertionError('something wrong') - return User(email, password, name) + json = response.json() + if json['status'] == 'success': + if 'result' in json: + return json['result'] + return None + + error = json['error'] + raise AssertionError(error['id'], error['message']) + + def user_create(self, user: User | None) -> User: + if user == None: + email = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10)) + '@test.test' + name = ''.join(random.choices(string.ascii_letters, k=10)) + password = 'Abcdef1!!1' + user = User(email, password, name) + + res = self.parse_response( + self.httpClient.post( + "/v1/user/create", json={ + "email": user.email, + "password": user.password, + "name": user.name, + } + ) + ) + + return User(res['email'], res['password'], res['name'], res['id']) def user_login(self, user: User) -> Auth: - response = self.httpClient.post( - "/v1/user/login", - json={ - "email": user.email+"a", - "password": user.password, - }, + res = self.parse_response( + self.httpClient.post( + "/v1/user/login", json={ + "email": user.email+"a", + "password": user.password, + }, + ) ) - - status = response.json()['status'] - if status == 'error': - raise AssertionError(response.json()['error']['message']) - - if response.status_code != 200: - raise AssertionError('can not login user') - - token = response.json()['result']['token'] - if token == '': - raise AssertionError('empty user token') - return Auth(token) + return Auth(res['status']) def dummy_get(self, auth: Auth): headers = {"X-Auth": auth.token}