add basic user integration test
This commit is contained in:
parent
bddc6f2b42
commit
c011491962
88
tests/api.py
Normal file
88
tests/api.py
Normal file
@ -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')
|
||||
33
tests/integration/test_user.py
Normal file
33
tests/integration/test_user.py
Normal file
@ -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)
|
||||
Loading…
x
Reference in New Issue
Block a user