generateur_v3/backend/api_old/tests/test_exercices.py
2022-09-16 21:50:55 +02:00

106 lines
4.4 KiB
Python

from tortoise.contrib.fastapi import register_tortoise
import datetime
from fastapi.testclient import TestClient
import sys, os
import requests
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from faker import Faker
from main import app
fake = Faker()
client = TestClient(app)
fake_exercices = []
token = ''
def test_register():
global token
fail_response = requests.post('http://localhost:8001/register', data={
'username': "", 'password': 't', "password_confirm": "t"})
assert fail_response.status_code == 422
assert fail_response.json()['detail'][0]['msg'] == 'field required'
fail_response = requests.post('http://localhost:8001/register', data={
'username': "test", 'password': 'tt', "password_confirm": "t"})
assert fail_response.status_code == 400
assert fail_response.json()['detail'] == 'Les mots de passe ne correspondent pas !'
fail_response = requests.post('http://localhost:8001/register', data={
'username': "test", 'password': 'tt', "password_confirm": "tt"})
assert fail_response.status_code == 400
assert fail_response.json(
)['detail'] == 'Invalid password : Password too short'
fail_response = requests.post('http://localhost:8001/register', data={
'username': "test", 'password': 'testtest', "password_confirm": "testtest"})
assert fail_response.status_code == 400
assert fail_response.json(
)['detail'] == 'Invalid password : Password must have a figure'
fail_response = requests.post('http://localhost:8001/register', data={
'username': "test", 'password': 'testtest1', "password_confirm": "testtest1"})
assert fail_response.status_code == 400
assert fail_response.json(
)['detail'] == 'Invalid password : Password must have capital letter'
success_response = requests.post('http://localhost:8001/register', data={'username': "test", 'password': 'Testtest1', "password_confirm": "Testtest1"})
print(success_response.json())
assert success_response.status_code == 200
assert 'access_token' in success_response.json()
assert success_response.json()['token_type'] == 'bearer'
fail_response = requests.post('http://localhost:8001/register', data={
'username': "test", 'password': 'Testtest1', "password_confirm": "Testtest1"})
assert fail_response.status_code == 422
assert 'UNIQUE constraint failed' in fail_response.json()['detail'][0]['msg']
token = success_response.json()['access_token']
def test_login():
global token
r = requests.post('http://localhost:8001/login', data = {"username": "teste", 'password': 'Testtest1'})
assert r.status_code == 401
assert r.json()['detail'] == 'Incorrect username or password'
r = requests.post('http://localhost:8001/login', data = {"username": "test", 'password': 'Testtest'})
assert r.status_code == 401
assert r.json()['detail'] == 'Incorrect username or password'
r = requests.post('http://localhost:8001/login', data = {"username": "test", 'password': 'Testtest1'})
assert r.status_code == 200
assert 'access_token' in r.json()
assert r.json()['token_type'] == 'bearer'
token = r.json()['access_token']
def test_delete_user():
r = requests.delete('http://localhost:8001/user', headers={'Authorization': f"Bearer {token}"})
print(r.json())
assert r.status_code == 200
def test_create_exo():
print('TOKEN', token)
headers = {'Authorization': f'Bearer ${token}'}
response = requests.post('http://localhost:8001/exercices/', params= {"name": "test", "consigne": "test", 'private': False}, files = {'file': ('1test_model.py', open("/home/lilian/1test_model.py", 'rb'), "text/x-python")}, headers=headers)
data = response.json()
fake_exercices.append(data)
assert 'id_code' in data
assert 'updated_at' in data
id_code = data.pop('id_code')
data.pop('updated_at')
assert response.status_code == 200
assert data == {'name': 'test', 'consigne': 'test', 'private': False,
'tags': [], 'origin': None, 'isOriginal': True, 'pdfSupport': True, 'csvSupport': False, 'webSupport': True}
def delete_exo():
response = requests.delete('http://localhost:8001/exercices/DATEMY')
assert response.status_code == 200
def test_ws():
pass