tests: refactor and added user token tests

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-06 20:03:15 +03:00
parent 1006baa212
commit 7c94abebe5
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -3,6 +3,7 @@ from sys import path
import random import random
import string import string
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from requests import Response
from ..main import app from ..main import app
from ..schemas import UserCreate from ..schemas import UserCreate
@ -24,6 +25,19 @@ def gen_new_user_dict() -> UserCreate:
password=noune) password=noune)
return new_user return new_user
def get_user_json(user: UserCreate) -> dict:
if type(user) != UserCreate: assert False
new_user_json = {
'email' : user.email,
'username': user.username,
'password': user.password
}
return new_user_json
def post_request(response: Response):
print(response.text)
print(response.reason)
test_user : UserCreate = gen_new_user_dict() test_user : UserCreate = gen_new_user_dict()
common_headres = { common_headres = {
@ -33,27 +47,46 @@ common_headres = {
def test_create_user(): def test_create_user():
new_user_json = {
"email" : test_user.email,
"username": test_user.username,
"password": test_user.password
}
response = client.request("POST", "/users/reg", response = client.request("POST", "/users/reg",
json=new_user_json, json=get_user_json(test_user),
headers=common_headres) headers=common_headres)
assert response.status_code == 200 assert response.status_code == 200
post_request(response)
def test_create_user_duplicate_fields(): def test_create_user_duplicate_fields():
# Assumed that this test runs after test_create_user() # Assumed that this test runs after test_create_user()
new_user_json = {
"email" : test_user.email,
"username": test_user.username,
"password": test_user.password
}
response = client.request("POST", "/users/reg", response = client.request("POST", "/users/reg",
json=new_user_json, json=get_user_json(test_user),
headers=common_headres) headers=common_headres)
assert response.status_code == 400 assert response.status_code == 400
post_request(response)
def test_obtain_user_token():
headers = {
'accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded'
}
data = f"grant_type=&username={test_user.username}&password={test_user.password}&scope=&client_id=&client_secret="
response = client.request("POST", "/users/tkn", headers=headers, data=data)
# if response.status_code == 200 and 'application/json' in response.headers.get('Content-Type',''):
# print(response.json())
assert response.status_code == 200
post_request(response)
def test_reject_false_creds():
headers = {
'accept': 'application/json',
'Content-type': 'application/x-www-form-urlencoded'
}
data = f"grant_type=&username={test_user.username}flaty&password=badpass{test_user.password}&scope=&client_id=&client_secret="
response = client.request("POST", "/users/tkn", headers=headers, data=data)
assert response.status_code == 401
post_request(response)
def test_create_iot_entity(): def test_create_iot_entity():
pass pass