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

482 lines
21 KiB
Python

from typing import List
from pydantic import BaseModel
import time
from fastapi.testclient import TestClient
from tests.test_auth import test_register
def test_create(client: TestClient, name="test_exo", consigne="consigne", private=False, user=None):
if user == None:
token = test_register(client, username="lilian")['access']
username = 'lilian'
else:
token = user['token']
username = user['username']
r = client.post('/exercices', data={"name": name, "consigne": consigne, "private": private}, files={
'file': ('test.py', open('tests/testing_exo_source/exo_source.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 201
assert 'id_code' in r.json()
assert {**r.json(), 'id_code': None} == {'name': name, 'consigne': consigne, 'private': private, 'id_code': None, 'author': {'username': username}, 'original': None, 'tags': [], 'exo_source': 'test.py', 'supports': {
'pdf': True, 'csv': True, 'web': False}, 'examples': {'type': 'csv', 'data': [{'calcul': 'None ++', 'correction': 'None ++'}, {'calcul': 'None ++', 'correction': 'None ++'}, {'calcul': 'None ++', 'correction': 'None ++'}]}, 'is_author': True}
return r.json()
def test_create_too_long(client: TestClient):
token = test_register(client)['access']
r = client.post('/exercices', data={"name": "e"*51, "consigne": "e"*201, "private": False}, files={
'file': ('test.py', open('tests/testing_exo_source/exo_source.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print('RESP', r.json())
assert r.status_code == 422
assert r.json()['detail'] == {'name_error': 'ensure this value has at most 50 characters',
'consigne_error': 'ensure this value has at most 200 characters'}
def test_create_name_missing(client: TestClient):
token = test_register(client)['access']
r = client.post('/exercices', headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 422
assert r.json()['detail'] == {
'name_error': 'field required', 'file_error': 'field required'}
def test_create_missing_main(client: TestClient):
token = test_register(client)['access']
r = client.post('/exercices', data={"name": "test_exo", "consigne": "consigne", "private": False}, files={
'file': ('test.py', open('tests/testing_exo_source/exo_source_missing_main.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 422
assert r.json()['detail'] == {
'exo_source_error': "Fonction 'main' introuvable"}
def test_create_invalid_source(client: TestClient):
token = test_register(client)['access']
r = client.post('/exercices', data={"name": "test_exo", "consigne": "consigne", "private": False}, files={
'file': ('test.py', open('tests/testing_exo_source/exo_source_invalid.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 422
assert r.json()['detail'] == {
'exo_source_error': "Exercice non valide (compatible avec aucun support)"}
def test_clone(client: TestClient):
create = test_create(client)
id_code = create['id_code']
token = test_register(client, username="lilian2")['access']
rr = client.post('/clone/' + id_code,
headers={'Authorization': 'Bearer ' + token})
print(rr.json())
assert rr.status_code == 200
assert 'id_code' in rr.json()
assert {**rr.json(), 'id_code': None} == {'name': 'test_exo', 'consigne': 'consigne', 'private': False, 'id_code': None, 'author': {'username': 'lilian2'}, 'original': {"id_code": id_code, "name": create['name']}, 'tags': [], 'exo_source': 'test.py', 'supports': {
'pdf': True, 'csv': True, 'web': False}, 'examples': {'type': 'csv', 'data': [{'calcul': 'None ++', 'correction': 'None ++'}, {'calcul': 'None ++', 'correction': 'None ++'}, {'calcul': 'None ++', 'correction': 'None ++'}]}, 'is_author': True}
def test_update(client: TestClient):
token = test_register(client, username="lilian")['access']
id_code = test_create(client, user={'token': token, 'username': "lilian"},
consigne="testconsigne")['id_code']
r = client.put('/exercice/' + id_code, data={"name": "name", "private": True}, files={
'file': ('test2.py', open('tests/testing_exo_source/exo_source.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 200
assert 'id_code' in r.json()
assert r.json() == {'name': "name", 'consigne': "testconsigne", 'private': True, 'id_code': id_code, 'author': {'username': 'lilian'}, 'original': None, 'tags': [], 'exo_source': 'test2.py', 'supports': {
'pdf': True, 'csv': True, 'web': False}, 'examples': {'type': 'csv', 'data': [{'calcul': 'None ++', 'correction': 'None ++'}, {'calcul': 'None ++', 'correction': 'None ++'}, {'calcul': 'None ++', 'correction': 'None ++'}]}, 'is_author': True}
return r.json()
def test_update_missing_name(client: TestClient):
token = test_register(client, username="lilian")['access']
id_code = test_create(client, user={'token': token, 'username': "lilian"})[
'id_code']
r = client.put('/exercice/' + id_code, data={"consigne": "consigne", "private": False}, files={
'file': ('test2.py', open('tests/testing_exo_source/exo_source.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 422
assert r.json()['detail'] == {'name_error': 'field required'}
def test_update_missing_main(client: TestClient):
token = test_register(client, username="lilian")['access']
id_code = test_create(client, user={'token': token, 'username': "lilian"})[
'id_code']
r = client.put('/exercice/' + id_code, data={"consigne": "consigne", "private": False}, files={
'file': ('test2.py', open('tests/testing_exo_source/exo_source_missing_main.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 422
assert r.json()['detail'] == {
'exo_source_error': "Fonction 'main' introuvable"}
def test_update_invalid(client: TestClient):
token = test_register(client, username="lilian")['access']
id_code = test_create(client, user={'token': token, 'username': "lilian"})[
'id_code']
r = client.put('/exercice/' + id_code, data={"consigne": "consigne", "private": False}, files={
'file': ('test2.py', open('tests/testing_exo_source/exo_source_invalid.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 422
assert r.json()['detail'] == {
'exo_source_error': "Exercice non valide (compatible avec aucun support)"}
def test_update_too_long(client: TestClient):
token = test_register(client, username="lilian")['access']
id_code = test_create(client, user={'token': token, 'username': "lilian"})[
'id_code']
r = client.put('/exercice/' + id_code, data={'name': 'e'*51, "consigne": "e"*201, "private": False}, files={
'file': ('test2.py', open('tests/testing_exo_source/exo_source.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert r.status_code == 422
assert r.json()['detail'] == {'name_error': 'ensure this value has at most 50 characters',
'consigne_error': 'ensure this value has at most 200 characters'}
def test_delete(client: TestClient):
token = test_register(client, username="lilian")['access']
id_code = test_create(client, user={'token': token, 'username': "lilian"})[
'id_code']
r = client.delete('/exercice/' + id_code,
headers={'Authorization': 'Bearer ' + token})
print(r.json())
assert r.status_code == 200
assert r.json()['detail'] == 'Exercice supprimé avec succès'
def test_delete_not_found(client: TestClient):
token = test_register(client, username="lilian")['access']
r = client.delete('/exercice/' + "test",
headers={'Authorization': 'Bearer ' + token})
print(r.json())
assert r.status_code == 404
assert r.json()['detail'] == 'Exercice introuvable'
# TAGS
class Tags(BaseModel):
id_code: str | None
color: str
label: str
def test_add_tags(client: TestClient, name='name', tags: List[Tags] = [{'label': "name", 'color': "#ff0000",
'id_code': "tag_id"}], user=None):
if user == None:
token = test_register(client, username="lilian")['access']
user = {"token": token, 'username': "lilian"}
else:
token = user['token']
exo = test_create(client, name=name, user=user)
id_code = exo['id_code']
r = client.post(f'/exercice/{id_code}/tags', json=tags,
headers={'Authorization': 'Bearer ' + token})
print(r.json())
data = r.json()
assert r.status_code == 200
assert {**data, "tags": [{**t, "id_code": None}
for t in data['tags']]} == {**exo, 'tags': [*exo['tags'], *[{**t, 'id_code': None} for t in tags]]}
return r.json()
def test_add_tags_invalid_color(client: TestClient):
token = test_register(client, username="lilian")['access']
exo = test_create(client, user={'token': token, 'username': "lilian"})
id_code = exo['id_code']
r = client.post(f'/exercice/{id_code}/tags', json=[
{'label': "name", 'color': "color", 'id_code': "id_code"}], headers={'Authorization': 'Bearer ' + token})
print(r.json())
data = r.json()
assert r.status_code == 422
assert data['detail'] == {
'color_error': "value is not a valid enumeration member; permitted: '#00ff00', '#ff0000', '#0000ff', 'string'"}
def test_add_tags_too_long(client: TestClient):
token = test_register(client, username="lilian")['access']
exo = test_create(client, user={'token': token, 'username': "lilian"})
id_code = exo['id_code']
r = client.post(f'/exercice/{id_code}/tags', json=[
{'label': "n"*21, 'color': "#ff0000", 'id_code': "id_code"}], headers={'Authorization': 'Bearer ' + token})
print(r.json())
data = r.json()
assert r.status_code == 422
assert data['detail'] == {
'label_error': "ensure this value has at most 20 characters"}
def test_remove_tag(client: TestClient):
token = test_register(client, username="lilian")['access']
exo = test_add_tags(client, user={"token": token, 'username': "lilian"})
id_code = exo['id_code']
tag_id = exo["tags"][0]["id_code"]
r = client.delete(f'/exercice/{id_code}/tags/{tag_id}',
headers={'Authorization': 'Bearer ' + token})
print(r.json())
assert r.json() == {
**exo, 'tags': exo['tags'][1:]}
def test_remove_tag_not_found(client: TestClient):
token = test_register(client, username="lilian")['access']
exo = test_add_tags(client, user={"token": token, 'username': "lilian"})
id_code = exo['id_code']
tag_id = "none"
r = client.delete(f'/exercice/{id_code}/tags/{tag_id}',
headers={'Authorization': 'Bearer ' + token})
print(r.json())
assert r.json()['detail'] == 'Tag introuvable'
def test_remove_tag_exo_not_found(client: TestClient):
token = test_register(client, username="lilian")['access']
tag_id = "none"
id_code = "tets"
r = client.delete(f'/exercice/{id_code}/tags/{tag_id}',
headers={'Authorization': 'Bearer ' + token})
print(r.json())
assert r.json()['detail'] == 'Exercice introuvable'
def test_remove_tag_not_owner(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
exo = test_add_tags(client, user={"token": token, 'username': "lilian"})
id_code = exo['id_code']
tag_id = exo['tags'][0]['id_code']
r = client.delete(f'/exercice/{id_code}/tags/{tag_id}',
headers={'Authorization': 'Bearer ' + token2})
print(r.json())
assert r.json()['detail'] == "Vous n'êtes pas le propriétaire du tag"
def test_exo_exo_source(client: TestClient):
token = test_register(client, username="lilian")['access']
exo = test_create(client, user={'token': token, 'username': "lilian"})
id_code = exo['id_code']
r = client.get(f"/exercice/{id_code}/exo_source",
headers={'Authorization': 'Bearer ' + token})
print(r.text)
print(r.headers)
assert r.text == open('tests/testing_exo_source/exo_source.py', 'r').read()
assert r.headers['content-disposition'].split('filename=')[-1] == 'test.py'
def test_get_users_exos(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
prv = test_create(client, private=True, user={
'token': token1, 'username': "lilian"})
exo_other_user = test_create(
client, user={'token': token2, 'username': "lilian2"})
r = client.get('/exercices/user',
headers={'Authorization': 'Bearer ' + token1})
print(r.json())
assert r.json() == [prv]
def test_get_user_with_search(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
exo_other_user = test_create(
client, user={'token': token2, 'username': "lilian2"})
exo1 = test_create(client, name='test1', user={
'token': token1, 'username': "lilian"})
exo2 = test_create(client, name='test2', user={
'token': token1, 'username': "lilian"})
exo3 = test_create(client, name='text', user={
'token': token1, 'username': "lilian"})
exo4 = test_create(client, name='autre', user={
'token': token1, 'username': "lilian"})
exo5 = test_create(client, name='tes', user={
'token': token1, 'username': "lilian"})
r = client.get('/exercices/user', params={"search": "test"},
headers={'Authorization': 'Bearer ' + token1})
print(r.json())
assert r.json() == [exo1, exo2]
def test_get_user_with_search(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
exo_other_user = test_create(
client, user={'token': token2, 'username': "lilian2"})
exo1 = test_create(client, name='test1', user={
'token': token1, 'username': "lilian"})
exo2 = test_create(client, name='test2', user={
'token': token1, 'username': "lilian"})
exo3 = test_create(client, name='text', user={
'token': token1, 'username': "lilian"})
exo4 = test_create(client, name='autre', user={
'token': token1, 'username': "lilian"})
exo5 = test_create(client, name='tes', user={
'token': token1, 'username': "lilian"})
r = client.get('/exercices/user', params={"search": "test"},
headers={'Authorization': 'Bearer ' + token1})
print(r.json())
assert r.json() == [exo1, exo2]
def test_get_user_with_tags(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
tags1 = [{'label': "tag1", 'color': "#ff0000", 'id_code': None}]
tags2 = [{'label': "tag1", 'color': "#ff0000", 'id_code': None},
{'label': "tag2", 'color': "#ff0000", 'id_code': None}]
tags3 = [{'label': "tag1", 'color': "#ff0000", 'id_code': None},
{'label': "tag2", 'color': "#ff0000", 'id_code': None}, {'label': "tag3", 'color': "#ff0000", 'id_code': None}]
exo_other_user = test_create(
client, user={'token': token2, 'username': "lilian2"})
exo1 = test_add_tags(client, user={
'token': token1, 'username': "lilian"}, tags=tags1)
exo2 = test_add_tags(client, user={
'token': token1, 'username': "lilian"}, tags=tags2)
exo3 = test_add_tags(client, user={
'token': token1, 'username': "lilian"}, tags=tags3)
tags1 = exo1['tags']
tags2 = exo2['tags']
tags3 = exo3['tags']
r = client.get('/exercices/user', params={'tags': [*[t['id_code'] for t in tags2], 'notexist']},
headers={'Authorization': 'Bearer ' + token1})
print(r.json())
assert r.json() == [exo2, exo3]
def test_get_user_with_tags_and_search(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
tags1 = [{'label': "tag1", 'color': "#ff0000", 'id_code': None}]
tags2 = [{'label': "tag1", 'color': "#ff0000", 'id_code': None},
{'label': "tag2", 'color': "#ff0000", 'id_code': None}]
tags3 = [{'label': "tag1", 'color': "#ff0000", 'id_code': None},
{'label': "tag2", 'color': "#ff0000", 'id_code': None}, {'label': "tag3", 'color': "#ff0000", 'id_code': None}]
exo_other_user = test_create(
client, user={'token': token2, 'username': "lilian2"})
exo1 = test_add_tags(client, user={
'token': token1, 'username': "lilian"}, tags=tags1, name="yes")
exo2 = test_add_tags(client, user={
'token': token1, 'username': "lilian"}, tags=tags2, name="no")
exo3 = test_add_tags(client, user={
'token': token1, 'username': "lilian"}, tags=tags3, name="yes")
tags1 = exo1['tags']
tags2 = exo2['tags']
tags3 = exo3['tags']
r = client.get('/exercices/user', params={"search": "yes", 'tags': [t['id_code'] for t in tags2]},
headers={'Authorization': 'Bearer ' + token1})
print(r.json())
assert r.json() == [exo3]
def test_get_public_auth(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
prv = test_create(client, private=True, user={
'token': token1, 'username': "lilian"})
public1 = test_create(
client, user={'token': token2, 'username': "lilian2"})
public2 = test_create(
client, user={'token': token1, 'username': "lilian"})
r = client.get('/exercices/public',
headers={'Authorization': 'Bearer ' + token2})
print(r.json())
assert r.json() == [{**public2, 'is_author': False}]
def test_get_public_auth_with_search(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
prv = test_create(client, private=True, user={
'token': token1, 'username': "lilian"})
public1 = test_create(
client, user={'token': token2, 'username': "lilian2"})
public2 = test_create(
client, user={'token': token1, 'username': "lilian"}, name="yes")
public3 = test_create(
client, user={'token': token1, 'username': "lilian"}, name="no")
r = client.get('/exercices/public',
params={'search': "yes"}, headers={'Authorization': 'Bearer ' + token2})
print(r.json())
assert r.json() == [{**public2, 'is_author': False}]
def test_get_public_no_auth(client: TestClient):
token1 = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
prv = test_create(client, private=True, user={
'token': token1, 'username': "lilian"})
public1 = test_create(
client, user={'token': token2, 'username': "lilian2"})
public2 = test_create(
client, user={'token': token1, 'username': "lilian"})
r = client.get('/exercices/public')
print(r.json())
assert r.json() == [{**public1, 'is_author': False},
{**public2, 'is_author': False}]
def test_get_exo_no_auth(client: TestClient):
token = test_register(client, username="lilian")['access']
exo = test_add_tags(client, user={'token': token, 'username': "lilian"})
r = client.get('/exercice/' + exo['id_code'])
assert r.json() == {**exo, "tags": [], 'is_author': False}
def test_get_exo_auth(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
exo = test_add_tags(client, user={'token': token, 'username': "lilian"})
r = client.get('/exercice/' + exo['id_code'],
headers={'Authorization': 'Bearer ' + token2})
print(r.json(), exo)
assert r.json() == {**exo, "tags": [], 'is_author': False}
def test_get_tags(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
exo = test_add_tags(client, user={'token': token, 'username': "lilian"})
exo2 = test_add_tags(client, user={'token': token2, 'username': "lilian2"})
tags1 = exo['tags']
tags2 = exo2['tags']
r = client.get('/tags', headers={'Authorization': 'Bearer ' + token2})
print(r.json())
assert r.json() == tags2