From c011491962b6abbf23d9430e5030c2840d60728d Mon Sep 17 00:00:00 2001 From: Sergey Chubaryan Date: Sat, 15 Feb 2025 06:50:25 +0300 Subject: [PATCH] add basic user integration test --- tests/api.py | 88 ++++++++++++++++++++++++++++++++++ tests/integration/test_user.py | 33 +++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 tests/api.py create mode 100644 tests/integration/test_user.py diff --git a/tests/api.py b/tests/api.py new file mode 100644 index 0000000..b0e132f --- /dev/null +++ b/tests/api.py @@ -0,0 +1,88 @@ +import random +import string +import requests + +class Requests(): + def __init__(self, baseUrl): + self.baseUrl = baseUrl + + def post(self, path, json = {}): + return requests.post(self.baseUrl + path, json=json) + +class Auth(): + token: string + + def __init__(self, token): + self.token = token + +class User(): + id: string + email: string + name: string + password: string + + def __init__(self, email, password, name, id="", token = ""): + self.email = email + self.password = password + self.name = name + self.token = token + + +class BackendApi(): + def __init__(self, httpClient): + self.httpClient = httpClient + + def parse_response(self, response): + if response.status != 200: + raise AssertionError('Request error') + + 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: + res = self.parse_response( + self.httpClient.post( + "/v1/user/login", json={ + "email": user.email+"a", + "password": user.password, + }, + ) + ) + + return Auth(res['status']) + + def dummy_get(self, auth: Auth): + headers = {"X-Auth": auth.token} + response = self.httpClient.get("/v1/dummy", headers=headers) + if response.status_code != 200: + raise AssertionError('something wrong') + + def health_get(self): + response = self.httpClient.get("/health") + if response.status_code != 200: + raise AssertionError('something wrong') diff --git a/tests/integration/test_user.py b/tests/integration/test_user.py new file mode 100644 index 0000000..b63c935 --- /dev/null +++ b/tests/integration/test_user.py @@ -0,0 +1,33 @@ +import pytest +from api import BackendApi, Requests, User + +backendUrl = "http://localhost:8080" + +class TestUser: + def test_create_user(self): + api = BackendApi(Requests(backendUrl)) + + user = User("user@example.com", "aaaaaA1!", "SomeName") + userWithBadEmail = User("example.com", "aaaaaA1!", "SomeName") + userWithBadPassword = User("user@example.com", "badPassword", "SomeName") + userWithBadName = User("user@example.com", "aaaaaA1!", "") + + with pytest.raises(Exception) as e: + api.user_create(userWithBadEmail) + raise e + + with pytest.raises(Exception) as e: + api.user_create(userWithBadPassword) + raise e + + with pytest.raises(Exception) as e: + api.user_create(userWithBadName) + raise e + + api.user_create(user) + api.user_login(user) + + def test_login_user(self): + api = BackendApi(Requests(backendUrl)) + user = api.user_create() + api.user_login(user) \ No newline at end of file