713 lines
32 KiB
Python
713 lines
32 KiB
Python
from typing import List
|
|
|
|
from fastapi.testclient import TestClient
|
|
from pydantic import BaseModel
|
|
|
|
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})
|
|
|
|
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': False, 'csv': True, 'web': True}, 'examples': {'type': 'csv', 'data': [{'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}]}, 'is_author': True}
|
|
return r.json()
|
|
|
|
|
|
def test_create_bad_import(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_bad_import.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
|
|
assert r.status_code == 422
|
|
return r.json()
|
|
|
|
|
|
def test_create_bad_use__(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_use__.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
|
|
assert r.status_code == 422
|
|
return r.json()
|
|
|
|
|
|
def test_create_invalid(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})
|
|
|
|
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_too_long(client: TestClient):
|
|
|
|
token = test_register(client)['access']
|
|
|
|
r = client.post('/exercices', data={"name": "e", "consigne": "e", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_infinite.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
|
|
assert r.status_code == 422
|
|
assert r.json()['detail'] == {'exo_source_error': 'Script took too long', }
|
|
|
|
|
|
def test_create_name_missing(client: TestClient):
|
|
token = test_register(client)['access']
|
|
|
|
r = client.post('/exercices', headers={"Authorization": "Bearer " + token})
|
|
|
|
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})
|
|
|
|
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})
|
|
|
|
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})
|
|
|
|
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'], 'author': 'lilian'}, 'tags': [], 'exo_source': 'test.py', 'supports': {
|
|
'pdf': False, 'csv': True, 'web': True}, 'examples': {'type': 'csv', 'data': [{'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}]}, 'is_author': True}
|
|
|
|
|
|
def test_clone_private(client: TestClient):
|
|
create = test_create(client, private=True)
|
|
id_code = create['id_code']
|
|
token = test_register(client, username="lilian2")['access']
|
|
rr = client.post('/clone/' + id_code,
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
|
|
assert rr.status_code == 401
|
|
assert rr.json() == {'detail': 'Cet exercice est privé'}
|
|
|
|
|
|
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})
|
|
|
|
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': False, 'csv': True, 'web': True}, 'examples': {'type': 'csv', 'data': [{'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}]}, 'is_author': True}
|
|
return r.json()
|
|
|
|
def test_update_no_file(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}, headers={"Authorization": "Bearer " + token})
|
|
|
|
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': 'test.py', 'supports': {
|
|
'pdf': False, 'csv': True, 'web': True}, 'examples': {'type': 'csv', 'data': [{'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}, {'calcul': '1 + ... = 2', "correction": "1 + [1] = 2"}]}, '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})
|
|
|
|
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})
|
|
|
|
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})
|
|
|
|
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})
|
|
|
|
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})
|
|
|
|
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})
|
|
|
|
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': None}], user=None, exo =None):
|
|
if user == None:
|
|
token = test_register(client, username="lilian")['access']
|
|
user = {"token": token, 'username': "lilian"}
|
|
else:
|
|
token = user['token']
|
|
if exo == None:
|
|
exo = test_create(client, name=name, user=user)
|
|
id_code = exo['id_code']
|
|
else:
|
|
id_code = exo['id_code']
|
|
r = client.post(f'/exercice/{id_code}/tags', json=tags,
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
|
|
data = r.json()
|
|
labels = [l['label'] for l in tags]
|
|
assert r.status_code == 200
|
|
assert {'exo': {**data['exo'], 'tags': [{**t, "id_code": "test"} for t in data['exo']['tags'] if t['id_code'] != None]}, "tags": [{**t, "id_code": "test"}
|
|
for t in data['tags'] if t['id_code'] != None]} == {"exo": {**exo, 'tags': [{**t, "id_code": "test"} for t in tags]}, 'tags': [*exo['tags'], *[{**t, 'id_code': "test"} for t in tags if t['id_code'] == None]]}
|
|
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})
|
|
|
|
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})
|
|
|
|
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['exo']['id_code']
|
|
tag_id = exo["tags"][0]["id_code"]
|
|
r = client.delete(f'/exercice/{id_code}/tags/{tag_id}',
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
|
|
assert r.json() == {
|
|
**exo['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['exo']['id_code']
|
|
tag_id = "none"
|
|
r = client.delete(f'/exercice/{id_code}/tags/{tag_id}',
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
|
|
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})
|
|
|
|
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['exo']['id_code']
|
|
tag_id = exo['tags'][0]['id_code']
|
|
r = client.delete(f'/exercice/{id_code}/tags/{tag_id}',
|
|
headers={'Authorization': 'Bearer ' + token2})
|
|
|
|
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})
|
|
|
|
|
|
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})
|
|
|
|
assert r.json()['page'] == 1
|
|
assert r.json()['size'] == 50
|
|
assert r.json()["items"] == [prv]
|
|
|
|
|
|
def test_get_users_exos_page(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"})
|
|
prv2 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv3 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv4 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv5 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv6 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv7 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv8 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv9 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv10 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv11 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv12 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
|
|
r = client.get('/exercices/user',
|
|
headers={'Authorization': 'Bearer ' + token1}, params={"page": 2, "size": 10})
|
|
|
|
assert r.json()['page'] == 2
|
|
assert r.json()['size'] == 10
|
|
assert r.json()["items"] == [prv11, prv12]
|
|
|
|
|
|
def test_get_users_exos_page_up(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"})
|
|
prv2 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv3 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv4 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv5 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv6 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv7 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv8 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv9 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv10 = test_create(client, private=True, user={
|
|
'token': token1, 'username': "lilian"})
|
|
prv11 = test_create(client, private=True, user={
|
|
'token': token2, 'username': "lilian2"})
|
|
prv12 = test_create(client, private=True, user={
|
|
'token': token2, 'username': "lilian2"})
|
|
|
|
r = client.get('/exercices/user',
|
|
headers={'Authorization': 'Bearer ' + token1}, params={"page": 2, "size": 10})
|
|
|
|
assert r.json()['page'] == 2
|
|
assert r.json()['size'] == 10
|
|
assert r.json()["items"] == []
|
|
|
|
|
|
|
|
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})
|
|
|
|
assert r.json()['items'] == [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})
|
|
|
|
assert r.json()['items'] == [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': "tag2", 'color': "#ff0000", 'id_code': None}]
|
|
|
|
tags3 = [{'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)
|
|
tags2 = [*exo1['tags'], *tags2]
|
|
exo2 = test_add_tags(client, user={
|
|
'token': token1, 'username': "lilian"}, tags=tags2)
|
|
|
|
tags3 = [*exo2['tags'], *tags3]
|
|
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], 'notexisting']},
|
|
headers={'Authorization': 'Bearer ' + token1})
|
|
|
|
assert r.json()['items'] == [exo2['exo'], exo3['exo']]
|
|
|
|
|
|
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': "tag2", 'color': "#ff0000", 'id_code': None}]
|
|
tags3 = [{'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")
|
|
tags2 = [*exo1['tags'], *tags2]
|
|
exo2 = test_add_tags(client, user={
|
|
'token': token1, 'username': "lilian"}, tags=tags2, name="no")
|
|
tags3 = [*exo2['tags'], *tags3]
|
|
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})
|
|
|
|
assert r.json()['items'] == [exo3['exo']]
|
|
|
|
|
|
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})
|
|
|
|
assert r.json()['items'] == [{**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})
|
|
|
|
assert r.json()['items'] == [{**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')
|
|
|
|
assert r.json()['items'] == [{**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['exo']['id_code'])
|
|
assert r.json() == {**exo['exo'], "tags": [], 'is_author': False}
|
|
|
|
|
|
def test_get_exo_no_auth_private(client: TestClient):
|
|
token = test_register(client, username="lilian")['access']
|
|
exo = test_create(
|
|
client, user={'token': token, 'username': "lilian"}, private=True)
|
|
|
|
r = client.get('/exercice/' + exo['id_code'])
|
|
assert r.json() == {"detail": "Cet exercice est privé"}
|
|
|
|
|
|
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['exo']['id_code'],
|
|
headers={'Authorization': 'Bearer ' + token2})
|
|
|
|
assert r.json() == {**exo['exo'], "tags": [], 'is_author': False}
|
|
|
|
|
|
def test_get_exo_auth_with_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"})
|
|
test_add_tags(client, user={'token': token2, 'username': "lilian2"}, exo={**exo['exo'], "is_author": False, "tags": []})
|
|
|
|
r = client.get('/exercice/' + exo['exo']['id_code'],
|
|
headers={'Authorization': 'Bearer ' + token})
|
|
|
|
|
|
assert r.json() == {**exo['exo']}
|
|
|
|
|
|
def test_get_exo_auth_private(client: TestClient):
|
|
token = test_register(client, username="lilian")['access']
|
|
token2 = test_register(client, username="lilian2")['access']
|
|
exo = test_create(
|
|
client, user={'token': token, 'username': "lilian"}, private=True)
|
|
|
|
r = client.get('/exercice/' + exo['id_code'],
|
|
headers={'Authorization': 'Bearer ' + token2})
|
|
assert r.json() == {"detail": "Cet exercice est privé"}
|
|
|
|
|
|
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})
|
|
|
|
assert r.json() == tags2
|
|
|
|
|
|
def test_get_csv(client: TestClient):
|
|
token = test_register(client)['access']
|
|
exoCsv = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_csv_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
exoPdf = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_pdf_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
exoWeb = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_web_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
|
|
r = client.get('/exercices/public', params={"type": "csv"})
|
|
|
|
assert r.json()['items'] == [{**exoCsv.json(), 'is_author': False}]
|
|
|
|
|
|
''' def test_get_pdf(client: TestClient):
|
|
token = test_register(client)['access']
|
|
exoCsv = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_csv_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
exoPdf = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_pdf_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
exoWeb = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_web_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
|
|
r = client.get('/exercices/public', params={"type": "pdf"})
|
|
|
|
assert r.json()['items'] == [{**exoPdf.json(), 'is_author': False}]
|
|
'''
|
|
|
|
def test_get_web(client: TestClient):
|
|
token = test_register(client)['access']
|
|
exoCsv = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_csv_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
exoPdf = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_pdf_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
exoWeb = client.post('/exercices', data={"name": "name", "consigne": "consigne", "private": False}, files={
|
|
'file': ('test.py', open('tests/testing_exo_source/exo_source_web_only.py', 'rb'))}, headers={"Authorization": "Bearer " + token})
|
|
|
|
r = client.get('/exercices/public', params={"type": "web"})
|
|
|
|
assert r.json()['items'] == [{**exoWeb.json(), 'is_author': False}]
|
|
|
|
|
|
def test_get_invalid_type(client: TestClient):
|
|
|
|
r = client.get('/exercices/public', params={"type": "lol"})
|
|
|
|
assert r.json() == {"detail": {
|
|
'type_error': "value is not a valid enumeration member; permitted: 'csv', 'pdf', 'web'"}}
|