generateur_v3/backend/api/tests/test_room.py
Kilton937342 4941b5a154 tests
2023-02-22 12:43:39 +01:00

1399 lines
68 KiB
Python

from fastapi import HTTPException
from fastapi.testclient import TestClient
from tests.test_auth import test_register
from tests.test_exos import test_create
def test_create_room_no_auth(client: TestClient, public=False):
r = client.post('/room', json={"name": "test_room",
"public": public}, params={'username': "lilian"})
print(r.json())
assert "room" in r.json()
assert "member" in r.json()
assert r.json()['member'] is not None
return r.json()
def test_create_room_no_auth_invalid(client: TestClient):
r = client.post('/room', json={"name": "test_room" * 21,
"public": False}, params={'username': "lilian" * 21})
print(r.json())
assert r.json() == {'detail': {'username_error': 'ensure this value has at most 20 characters',
'name_error': 'ensure this value has at most 20 characters'}}
def test_create_room_auth(client: TestClient, token=None, public=False):
if token is None:
token = test_register(client=client)['access']
r = client.post('/room', json={"name": "test_room",
"public": public}, headers={"Authorization": "Bearer " + token})
print(r.json())
assert "room" in r.json()
assert "member" in r.json()
assert r.json()['member'] == None
return r.json()
def test_room_not_found(client: TestClient):
try:
with client.websocket_connect('/ws/room/eee') as r:
pass
except HTTPException as e:
assert True
except Exception:
assert False
def test_login_no_auth(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
ws.send_json({"type": "login", "data": {
"reconnect_code": member}})
data = ws.receive_json()
assert "id_code" in data['data']['member']
assert "clientId" in data['data']['member']
assert data == {'type': "loggedIn", "data": {"member": {**data['data']['member'],
"reconnect_code": member, "isAdmin": True,
"isUser": False}}}
def test_login_no_auth_different_clientId(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
ws.send_json({"type": "login", "data": {
"reconnect_code": member}})
data = ws.receive_json()
clientId = data['data']['member']['clientId']
ws.close()
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
ws.send_json({"type": "login", "data": {
"reconnect_code": member}})
data = ws.receive_json()
assert data['data']['member']['clientId'] != clientId
def test_login_no_auth_not_in_room(client: TestClient):
room = test_create_room_no_auth(client=client)
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
ws.send_json({"type": "login", "data": {
"reconnect_code": "lol"}})
data = ws.receive_json()
print(data)
assert data == {'type': "error", "data": {"code": 401,
"msg": "Utilisateur introuvable dans cette salle"}}
def test_login_auth(client: TestClient):
token = test_register(client=client)['access']
room = test_create_room_auth(client=client, token=token)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
ws.send_json({"type": "login", "data": {"token": token}})
data = ws.receive_json()
print(data)
assert "id_code" in data["data"]['member']
assert data == {'type': "loggedIn", "data": {"member": {**data["data"]['member'],
"username": "lilian", "isAdmin": True, "isUser": True,
'reconnect_code': "", "clientId": ""}}}
def test_login_auth_not_in_room(client: TestClient):
token = test_register(client=client, username="lilian2")['access']
room = test_create_room_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
ws.send_json({"type": "login", "data": {"token": token}})
data = ws.receive_json()
print(data)
assert data == {'type': "error", "data": {"code": 401,
"msg": "Utilisateur introuvable dans cette salle"}}
def test_join_auth(client: TestClient):
token = test_register(client, username="lilian2")['access']
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
assert "waiter_id" in mdata['data']['waiter']
assert mdata == {"type": "waiting", "data": {"waiter": {
"username": "lilian2", "waiter_id": mdata['data']['waiter']['waiter_id']}}}
admin.send_json({"type": "ping_room"})
adata = admin.receive_json()
assert adata == {'type': "waiter", 'data': {
"waiter": {"waiter_id": mdata['data']['waiter']['waiter_id'], "username": "lilian2"}}}
admin.send_json({"type": "accept", "data": {
"waiter_id": mdata['data']['waiter']['waiter_id']}})
mdata = member.receive_json()
assert mdata == {"type": "accepted", "data": {"member": {
"username": "lilian2", "isUser": True, "isAdmin": False, "reconnect_code": "",
"id_code": mdata['data']["member"]["id_code"], "clientId": "", "online": True}}}
print("MDATABUS", mdata)
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
"member": {"reconnect_code": "", "clientId": "", "online": True, "username": "lilian2", "isUser": True,
"isAdmin": False, "id_code": adata['data']["member"]["id_code"]}}}
admin.send_json({"type": "ping_room"})
mdata = member.receive_json()
assert mdata == {"type": "ping", "data": {}}
def test_join_waiter_not_found(client: TestClient):
token = test_register(client, username="lilian2")['access']
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
admin.send_json({"type": "accept", "data": {"waiter_id": "OOOO"}})
data = admin.receive_json()
assert data == {"type": "error", "data": {"code": 400,
"msg": "Utilisateur en liste d'attente introuvable"}}
# TODO = Maybe check the joined event
def test_join_no_auth(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"username": "member"}})
mdata = memberws.receive_json()
assert "waiter_id" in mdata['data']['waiter']
assert mdata == {"type": "waiting", "data": {"waiter": {
"username": "member", "waiter_id": mdata['data']['waiter']['waiter_id']}}}
adata = admin.receive_json()
assert adata == {'type': "waiter", 'data': {
"waiter": {"waiter_id": mdata['data']['waiter']['waiter_id'], "username": "member"}}}
admin.send_json({"type": "accept", "data": {
"waiter_id": mdata['data']['waiter']['waiter_id']}})
mdata = memberws.receive_json()
new_reconnect = mdata['data']['member']['reconnect_code']
clientId = mdata['data']['member']['clientId']
assert clientId != ""
assert 'reconnect_code' in mdata['data']['member']
assert mdata == {"type": "accepted", "data": {"member": {
"username": "member", "reconnect_code": new_reconnect, "isUser": False, "isAdmin": False,
"id_code": mdata['data']["member"]["id_code"], "clientId": clientId, "online": True}}}
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
"member": {"reconnect_code": new_reconnect, "clientId": "", "online": True, "username": "member",
"isUser": False, "isAdmin": False, "id_code": adata['data']["member"]["id_code"]}}}
admin.send_json({"type": "ping_room"})
mdata = memberws.receive_json()
assert mdata == {"type": "ping", "data": {}}
return {"room": room['room'], "members": [member, new_reconnect]}
def test_join_no_auth_username_error(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"username": "lilian"}})
mdata = member.receive_json()
assert mdata == {"type": "error", "data": {"code": 400,
"msg": "Nom d'utilisateur invalide ou indisponible"}}
def test_join_no_auth_username_too_long(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"username": "lilian" * 21}})
mdata = member.receive_json()
assert mdata == {"type": "error", "data": {"code": 400,
"msg": "Nom d'utilisateur invalide ou indisponible"}}
def test_join_auth_refused(client: TestClient):
token = test_register(client, username="lilian2")['access']
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
assert "waiter_id" in mdata['data']['waiter']
assert mdata == {"type": "waiting", "data": {"waiter": {
"username": "lilian2", "waiter_id": mdata['data']['waiter']['waiter_id']}}}
waiter_id = mdata['data']['waiter']['waiter_id']
adata = admin.receive_json()
assert adata == {'type': "waiter", 'data': {
"waiter": {"waiter_id": waiter_id, "username": "lilian2"}}}
admin.send_json({"type": "refuse", "data": {
"waiter_id": waiter_id}})
adata = admin.receive_json()
assert adata == {"type": "successfullyRefused",
"data": {"waiter_id": waiter_id}}
mdata = member.receive_json()
assert mdata == {"type": "refused", "data": {
"waiter_id": waiter_id}}
def test_join_auth_in_room_yet(client: TestClient):
token = test_register(client, username="lilian")['access']
room = test_create_room_auth(client=client, token=token)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
assert "id_code" in mdata['data']['member']
assert "clientId" in mdata['data']['member']
assert mdata == {"type": "loggedIn", "data": {"member": {**mdata['data']['member'],
"username": "lilian", "isAdmin": True, "isUser": True,
'reconnect_code': ""}}}
def test_join_auth_public(client: TestClient):
token = test_register(client, username="lilian2")['access']
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
assert mdata == {"type": "accepted", "data": {"member": {
"username": "lilian2", "isUser": True, "isAdmin": False, "reconnect_code": "",
"id_code": mdata['data']["member"]["id_code"], "clientId": "", "online": True}}}
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
"member": {"reconnect_code": "", "username": "lilian2", "isUser": True, "isAdmin": False,
"id_code": mdata['data']["member"]["id_code"],
"clientId": "", "online": True}}}
admin.send_json({"type": "ping_room"})
mdata = member.receive_json()
assert mdata == {"type": "ping", "data": {}}
def test_join_no_auth_public(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"username": "member"}})
mdata = member.receive_json()
assert 'reconnect_code' in mdata['data']['member']
assert "clientId" in mdata['data']['member'] and mdata['data']['member']['clientId'] != ""
assert mdata == {"type": "accepted", "data": {"member": {
"username": "member", "reconnect_code": mdata['data']['member']['reconnect_code'], "isUser": False,
"isAdmin": False, "id_code": mdata['data']["member"]["id_code"],
"clientId": mdata['data']['member']['clientId'], "online": True}}}
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
"member": {"reconnect_code": mdata['data']['member']['reconnect_code'], "username": "member",
"isUser": False, "isAdmin": False, "id_code": mdata['data']["member"]["id_code"],
"clientId": "", "online": True}}}
member.send_json({"type": "update_groups"})
admin.send_json({"type": "ping_room"})
mdata = member.receive_json()
assert mdata == {"type": "ping", "data": {}}
def test_join_auth_unauthorized(client: TestClient):
token = test_register(client, username="lilian2")['access']
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
assert "waiter_id" in mdata['data']['waiter']
assert mdata == {"type": "waiting", "data": {"waiter": {
"username": "lilian2", "waiter_id": mdata['data']['waiter']['waiter_id']}}}
adata = admin.receive_json()
assert adata == {'type': "waiter", 'data': {
"waiter": {"waiter_id": mdata['data']['waiter']['waiter_id'], "username": "lilian2"}}}
member.send_json({"type": "refuse", "data": {
"waiter_id": mdata['data']['waiter']['waiter_id']}})
mdata = member.receive_json()
assert mdata == {"type": "error", "data": {
"msg": "Vous n'avez pas la permission de faire ca"}}
def test_connect_admin(client: TestClient):
room = test_join_no_auth(client=client)
members = room['members']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": members[0]}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
member.send_json({'type': "login", "data": {
"reconnect_code": members[1]}})
adata = admin.receive_json()
assert adata == {"type": "connect", "data": {"member": {
"username": "member", "reconnect_code": members[1], "isAdmin": False, "isUser": False,
"id_code": adata['data']["member"]["id_code"], "clientId": "", "online": True}}}
def test_connect_member(client: TestClient):
room = test_join_no_auth(client=client)
members = room['members']
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({"type": "login", "data": {
"reconnect_code": members[1]}})
memberws.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({'type': "login", "data": {
"reconnect_code": members[0]}})
mdata = memberws.receive_json()
assert mdata == {"type": "connect", "data": {"member": {
"username": "lilian", "reconnect_code": "", "isAdmin": True, "isUser": False,
"id_code": mdata['data']["member"]["id_code"], "clientId": "", "online": True}}}
def test_disconnect(client: TestClient):
room = test_join_no_auth(client=client)
members = room['members']
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({"type": "login", "data": {
"reconnect_code": members[1]}})
memberws.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({'type': "login", "data": {
"reconnect_code": members[0]}})
memberws.receive_json()
admin.close()
mdata = memberws.receive_json()
assert mdata == {"type": "disconnect", "data": {"member": {
"username": "lilian", "reconnect_code": "", "isAdmin": True, "isUser": False,
"id_code": mdata['data']["member"]["id_code"], "clientId": "", "online": False}}}
def test_disconnect_admin(client: TestClient):
room = test_join_no_auth(client=client)
members = room['members']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": members[0]}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({'type': "login", "data": {
"reconnect_code": members[1]}})
memberws.receive_json()
memberws.close()
admin.receive_json()
adata = admin.receive_json()
assert adata == {"type": "disconnect", "data": {"member": {
"username": "member", "reconnect_code": members[1], "isAdmin": False, "isUser": False,
"id_code": adata['data']["member"]["id_code"], "clientId": "", "online": False}}}
def test_disconnect_waiter(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({'type': "join", "data": {
"username": "test"}})
mdata = memberws.receive_json()
admin.receive_json()
memberws.close()
adata = admin.receive_json()
assert adata == {"type": "disconnect_waiter",
"data": {"waiter": {**mdata['data']['waiter']}}}
def test_leave(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({"type": "join", "data": {
"username": "test"}})
m = memberws.receive_json()
admin.receive_json()
memberws.send_json({"type": "leave"})
data = memberws.receive_json()
assert data == {"type": "successfully_leaved", "data": {}}
adata = admin.receive_json()
assert adata == {"type": "leaved", "data": {"member": {
"username": "test", "reconnect_code": "", "isAdmin": False,
"isUser": False, "id_code": adata['data']["member"]["id_code"], "online": True, "clientId": ""}}}
def test_leave_not_connected(client: TestClient):
room = test_create_room_no_auth(client=client)
members = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({"type": "leave"})
data = memberws.receive_json()
assert data == {"type": "error", "data": {"code": 400,
"msg": "Vous n'êtes connecté à aucune salle"}}
def test_leave_admin(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
admin.send_json({"type": "leave"})
data = admin.receive_json()
assert data == {"type": "error", "data": {"code": 400,
"msg": "Vous ne pouvez pas quitter une salle dont vous êtes l'administrateur"}}
# TODO : Change type for ban and leave
def test_ban_anonymous(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({"type": "join", "data": {
"username": "test"}})
m = memberws.receive_json()
reconnect_code = m['data']['member']['reconnect_code']
id_code = m['data']['member']['id_code']
admin.receive_json()
admin.send_json(
{"type": "ban", "data": {"member_id": m['data']['member']['id_code']}})
mdata = memberws.receive_json()
assert mdata == {"type": "banned", "data": {}}
adata = admin.receive_json()
assert adata == {"type": "leaved", "data": {"member": {
"username": "test", "reconnect_code": "", "isUser": False, "isAdmin": False,
"id_code": id_code, "clientId": "", "online": True}}}
def test_ban_anonymous_unauthorized(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({"type": "join", "data": {
"username": "test"}})
memberws.receive_json()
memberws.send_json({"type": "ban", "data": {"member_id": "OOO"}})
mdata = memberws.receive_json()
assert mdata == {"type": "error", "data": {
"msg": "Vous n'avez pas la permission de faire ca"}}
def test_ban_admin(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
data = admin.receive_json()
admin.send_json(
{"type": "ban", "data": {"member_id": data['data']['member']['id_code']}})
a = admin.receive_json()
assert a == {'type': "error", "data": {"code": 400,
"msg": "Vous ne pouvez pas bannir un administrateur"}}
def test_ban_user(client: TestClient):
token = test_register(client=client, username="lilian2")['access']
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json({"type": "join", "data": {"token": token}})
m = memberws.receive_json()
admin.receive_json()
admin.send_json(
{"type": "ban", "data": {"member_id": m['data']['member']['id_code']}})
mdata = memberws.receive_json()
assert mdata == {"type": "banned", "data": {}}
adata = admin.receive_json()
assert adata == {"type": "leaved", "data": {"member": {
"username": "lilian2", "reconnect_code": "", "isUser": True, "isAdmin": False,
"id_code": m['data']['member']['id_code'], "clientId": "", "online": True}}}
def test_create_parcours_no_auth(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
token = test_register(client, username="lilian")
exo1 = test_create(name="test1", client=client, user={
"token": token['access'], "username": "lilian"})
exo2 = test_create(name="test2", client=client, user={
"token": token['access'], "username": "lilian"})
exo3 = test_create(name="test3", client=client, user={
"token": token['access'], "username": "lilian"})
exo4 = test_create(name="test4", client=client, user={
"token": token['access'], "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
adata = admin.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', params={"clientId": adata['data']['member']['clientId']},
json={
"name": "test_parcours", "time": 10 * 60,
"max_mistakes": 10, "exercices": [
{"exercice_id": "lolilol", "quantity": 12},
{"exercice_id": exo1['id_code'],
"quantity": 10},
{"exercice_id": exo2['id_code'], "quantity": 5},
{"exercice_id": exo3['id_code'],
"quantity": 12}]})
print('resonse', r.json(), exo3)
assert r.json() == {"id_code": r.json()['id_code'], "memberRank": None, "name": "test_parcours", "pb": None,
"rank": None, "ranking": [], "tops": [], "validated": False, "challenges": {},
"name": "test_parcours", "time": 10 * 60,
"max_mistakes": 10, "exercices": [{"examples": exo1['examples'],
"exercice_id": exo1['id_code'], "name": exo1['name'],
"quantity": 10},
{"examples": exo2['examples'],
"exercice_id": exo2['id_code'], "name": exo2['name'],
"quantity": 5},
{"examples": exo3['examples'],
"exercice_id": exo3['id_code'], "name": exo3['name'],
"quantity": 12}]}
adata = admin.receive_json()
assert adata == {"type": "add_parcours", "data": {"parcours": {
"name": "test_parcours", "id_code": r.json()['id_code'], "best_note": None, "validated": False, }}}
return r.json(), member, room
def test_create_parcours_auth(client: TestClient):
token = test_register(client, username="lilian")['access']
room = test_create_room_auth(client=client, token=token)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
exo2 = test_create(name="test2", client=client, user={
"token": token, "username": "lilian"})
exo3 = test_create(name="test3", client=client, user={
"token": token, "username": "lilian"})
exo4 = test_create(name="test4", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token}, json={
"name": "test_parcours", "time": 10 * 60,
"max_mistakes": 10, "exercices": [
{"exercice_id": "lolilol", "quantity": 12},
{"exercice_id": exo1['id_code'],
"quantity": 10},
{"exercice_id": exo2['id_code'], "quantity": 5},
{"exercice_id": exo3['id_code'],
"quantity": 12}]})
assert r.json() == {"id_code": r.json()['id_code'], "memberRank": None, "name": "test_parcours", "pb": None,
"rank": None, "ranking": [], "tops": [], "validated": False, "challenges": {},
"name": "test_parcours", "time": 10 * 60,
"max_mistakes": 10, "exercices": [{"examples": exo1['examples'],
"exercice_id": exo1['id_code'], "name": exo1['name'],
"quantity": 10},
{"examples": exo2['examples'],
"exercice_id": exo2['id_code'], "name": exo2['name'],
"quantity": 5},
{"examples": exo3['examples'],
"exercice_id": exo3['id_code'], "name": exo3['name'],
"quantity": 12}]}
adata = admin.receive_json()
assert adata == {"type": "add_parcours", "data": {"parcours": {
"name": "test_parcours", "id_code": r.json()['id_code'], "best_note": None, "validated": False, }}}
return r.json(), token, room
def test_create_parcours_unauthorized(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
token = test_register(client, username="lilian")['access']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
mdata = memberws.send_json(
{"type": "join", "data": {"token": token}})
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token},
json={"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10,
"exercices": [{"exercice_id": "lolilol", "quantity": 12}]}})
assert r.status_code == 401
assert r.json() == {
"detail": "Vous devez être administrateur pour faire cela"}
def test_create_parcours_not_in_room(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
token = test_register(client, username="lilian")['access']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
adata = admin.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token},
json={"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10,
"exercices": [{"exercice_id": "lolilol", "quantity": 12}]}})
assert r.status_code == 401
assert r.json() == {
"detail": "Vous n'êtes pas dans cette salle"}
def test_create_parcours_no_valid_exo(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
token = test_register(client, username="lilian")['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})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
adata = admin.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', params={"clientId": adata['data']['member']['clientId']},
json={"name": "test_parcours", "time": 10 * 60,
"max_mistakes": 10,
"exercices": [{"exercice_id": "lolilol",
"quantity": 12}, {
"exercice_id":
exoCsv.json()[
'id_code'],
"quantity": 10}]})
assert r.status_code == 400
assert r.json() == {
"detail": "Veuillez entrer au moins un exercice valide"}
def test_create_parcours_not_authenticated(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": member}})
admin.receive_json()
r = client.post(f'/room/{room["room"]}/parcours',
json={"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10,
"exercices": [{"exercice_id": "lolilol", "quantity": 12}]}})
assert r.status_code == 401
assert r.json() == {
"detail": "Not authenticated"}
def test_create_parcours_room_not_found(client: TestClient):
r = client.post(f'/room/test/parcours',
json={"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10,
"exercices": [{"exercice_id": "lolilol", "quantity": 12}]}})
assert r.status_code == 404
assert r.json() == {"detail": "Salle introuvable"}
def test_delete_parcours_auth(client: TestClient):
parcours, token, room = test_create_parcours_auth(client)
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
r = client.delete(f"/room/{room['room']}/parcours/{parcours['id_code']}", headers={
"Authorization": "Bearer " + token})
assert r.json() == "ok"
adata = admin.receive_json()
assert adata == {'type': "del_parcours",
"data": {"parcours_id": parcours['id_code']}}
def test_delete_parcours_no_auth(client: TestClient):
parcours, reconnect, room = test_create_parcours_no_auth(client)
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": reconnect}})
adata = admin.receive_json()
r = client.delete(f"/room/{room['room']}/parcours/{parcours['id_code']}", params={
"clientId": adata['data']['member']['clientId']})
assert r.json() == "ok"
adata = admin.receive_json()
assert adata == {'type': "del_parcours",
"data": {"parcours_id": parcours['id_code']}}
def test_delete_parcours_unauthorized(client: TestClient):
token = test_register(username="lilian2", client=client)['access']
parcours, reconnect, room = test_create_parcours_no_auth(client)
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"reconnect_code": reconnect}})
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
mdata = memberws.send_json(
{"type": "join", "data": {"token": token}})
adata = admin.receive_json()
r = client.delete(f"/room/{room['room']}/parcours/{parcours['id_code']}", headers={
"Authorization": "Bearer " + token})
assert r.status_code == 401
assert r.json() == {
"detail": "Vous devez être administrateur pour faire cela"}
def test_delete_parcours_not_found(client: TestClient):
parcours, token, room = test_create_parcours_auth(client)
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
r = client.delete(
f"/room/{room['room']}/parcours/test", headers={"Authorization": "Bearer " + token})
assert r.status_code == 404
assert r.json() == {
"detail": "Parcours introuvable"}
def validate_entry(out: str):
return out[:-1] == "1 + [0] = " and int(out[-1]) <= 9 and int(out[-1]) >= 1
def fill_challenge(c):
for ex in c:
ex['data'] = [{**e, "inputs": [{**i, "value": str(id)} for i in e['inputs']]} for id,e in enumerate(ex['data'])]
return c
def corrige_challenge(c, corr):
for ex in c:
ex['data'] = [{**e, "inputs": [{**i, "correction": corr, "valid": i['value'] == corr} for i in e['inputs']]} for id,e in enumerate(ex['data'])]
return c
def test_challenge_auth(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
id_code = mdata['data']['member']['id_code']
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token},
json={"name": "test_parcours", "time": 10 * 60, "max_mistakes": 10, "exercices": [
{"exercice_id": exo1['id_code'], "quantity": 3}]})
parcours_id = r.json()['id_code']
# memberws.receive_json()
admin.receive_json() # new parcours
memberws.receive_json()
admin.receive_json()
admin.send_json({"type": "sub_parcours", "data": {
"parcours_id": parcours_id}})
memberws.send_json({"type": "sub_parcours", "data": {
"parcours_id": parcours_id}})
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
assert "id_code" in r.json() and r.json()['id_code'] != ""
assert len(data) == 1 and len(data[0]['data']) == 3 and all([e['inputs'] == [
{'index': 0, "value": ""}] and validate_entry(e['calcul']) for e in data[0]['data']])
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(i)}]} for i, e in enumerate(data[0]['data'])]]
filled_obj = fill_challenge(data)
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162},
headers={"Authorization": "Bearer " + token2})
assert r.json()['id_code'] != ""
assert r.json() == {"id_code": r.json()['id_code'], "mistakes": 2, "time": 162,
"validated": True, "isCorriged": True, "data": corrige_challenge(filled_obj, "1")}
adata = admin.receive_json()
assert adata == {'type': 'challenge', 'data': {
'challenger': {"id_code": mdata['data']['member']['id_code'],
"name": mdata['data']['member']['username']},
'challenges': [
{"canCorrige": True, "id_code": r.json()['id_code'], "mistakes": 2, "time": 162, "isCorriged": True,
"validated": True}]}}
admin.receive_json()
adata = admin.receive_json()
memberws.receive_json()
mdata = memberws.receive_json()
assert mdata == {'type': "newRanks", 'data': {"rank": 1, "avgRank": 1}}
mdata = memberws.receive_json()
assert mdata == adata == {'type': 'newTops', 'data': {
'tops': [{'challenger': {'name': 'lilian2', 'id_code': id_code}, 'mistakes': 2, 'time': 162}],
'avgTops': [{'id_code': id_code, 'avg': 2.0, 'name': 'lilian2'}]}}
def test_corrige_auth(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
id_code = mdata['data']['member']['id_code']
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token},
json={"name": "test_parcours", "time": 10 * 60, "max_mistakes": 10, "exercices": [
{"exercice_id": exo1['id_code'], "quantity": 3}]})
print(r.json())
parcours_id = r.json()['id_code']
memberws.receive_json()
admin.receive_json() # new parcours
# admin.receive_json() # disconnect
admin.receive_json()
memberws.send_json({"type": "sub_parcours", "data": {
"parcours_id": parcours_id}})
admin.send_json({"type": "sub_parcours", "data": {
"parcours_id": parcours_id}})
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
assert "id_code" in r.json()
assert len(data) == 1 and len(data[0]['data']) == 3 and all([e['inputs'] == [
{'index': 0, "value": ""}] and validate_entry(e['calcul']) for e in data[0]['data']])
filled_obj = fill_challenge(data)
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162},
headers={"Authorization": "Bearer " + token2})
assert r.json()['id_code'] != ""
assert r.json() == {"id_code": r.json()['id_code'], "mistakes": 2, "time": 162,
"validated": True, "isCorriged": True, "data": corrige_challenge(filled_obj, "1")}
adata = admin.receive_json()
assert adata == {'type': 'challenge', 'data': {
'challenger': {"id_code": mdata['data']['member']['id_code'],
"name": mdata['data']['member']['username']},
'challenges': [
{"canCorrige": True, "id_code": r.json()['id_code'], "mistakes": 2, "time": 162, "isCorriged": True,
"validated": True}]}}
rr = client.get(
f'/room/{room["room"]}/correction/{r.json()["id_code"]}',
headers={"Authorization": "Bearer " + token2})
assert rr.json() == r.json()
data = r.json()['data']
corriged_obj = [
[{**e, 'inputs': [{**e['inputs'][0], "correction": str(i)}]} for i, e in enumerate(data[0]['data'])]]
corriged_obj = corrige_challenge(filled_obj, "0")
r = client.put(
f'/room/{room["room"]}/challenge/{r.json()["id_code"]}',
json={'correction': corriged_obj},
headers={"Authorization": "Bearer " + token})
assert r.json() == {"id_code": r.json()['id_code'], "mistakes": 3, "time": 162,
"validated": False, "isCorriged": True, "data": corriged_obj}
adata = admin.receive_json()
assert adata == {'type': 'challenge', 'data': {
'challenger': {"id_code": mdata['data']['member']['id_code'],
"name": mdata['data']['member']['username']},
'challenges': [
{"canCorrige": True, "id_code": r.json()['id_code'], "mistakes": 2, "time": 162, "isCorriged": True,
"validated": True}]}}
assert adata == {'type': 'challenge_change',
'data': {'challenger': mdata['data']['member']['id_code'], 'note': {
'value': 3, 'total': 3}, 'time': 162, "validated": True, 'isCorriged': True,
'canCorrige': True, "id_code": r.json()['id_code']}}
rr = client.get(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
headers={"Authorization": "Bearer " + token2})
assert rr.json() == r.json()
def test_get_challenge_auth(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token}, json={
"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10, "exercices": [
{"exercice_id": exo1['id_code'], "quantity": 3}]}})
parcours_id = r.json()['id_code']
memberws.receive_json()
admin.receive_json() # new parcours
admin.receive_json() # disconnect
admin.receive_json()
admin.send_json({"type": "sub_parcours", "data": {
"parcours_id": parcours_id}})
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
assert "id_code" in r.json()
assert len(data) == 1 and len(data[0]) == 3 and all([e['inputs'] == [
{'index': 0, "value": ""}] and validate_entry(e['calcul']) for e in data[0]])
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token2})
assert r.json() == {"id_code": r.json()['id_code'], "note": {"value": 1, "total": 3}, "time": 162,
"validated": False, "isCorriged": True, "data": [
[{**e, 'inputs': [{**e['inputs'][0], "correction": '1'}]} for e in filled_obj[0]]]}
rr = client.get(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
headers={"Authorization": "Bearer " + token2})
assert rr.json() == r.json()
def test_challenge_auth_bad_obj(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token}, json={
"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10, "exercices": [
{"exercice_id": exo1['id_code'], "quantity": 3}]}})
parcours_id = r.json()['id_code']
memberws.receive_json()
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
id_code = r.json()['id_code']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{id_code}',
json={'challenge': [*filled_obj, "test"], "time": 162}, headers={"Authorization": "Bearer " + token2})
assert r.json() == {'detail': {
'challenge_error': 'value is not a valid list'}}
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{id_code}',
json={'challenge': [*filled_obj, ["test"]], "time": 162}, headers={"Authorization": "Bearer " + token2})
assert r.json() == {'detail': {
'challenge_error': 'value is not a valid dict'}}
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{id_code}',
json={'challenge': [*filled_obj, [{"test": "test"}]], "time": 162},
headers={"Authorization": "Bearer " + token2})
assert r.json() == {'detail': {
'calcul_error': 'field required', 'inputs_error': 'field required'}}
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{id_code}',
json={'challenge': [*filled_obj, filled_obj[0]], "time": 162},
headers={"Authorization": "Bearer " + token2})
assert r.json() == {'detail': {
"challenge_error": "Object does not correspond to correction"}}
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{id_code}',
json={'challenge': [*filled_obj, filled_obj[0]], "time": 162},
headers={"Authorization": "Bearer " + token2})
assert r.json() == {'detail': {
"challenge_error": "Object does not correspond to correction"}}
def stripKey(dict, key):
return {k: v for k, v in dict.items() if k != key}
def test_get_parcours_member(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token}, json={
"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10, "exercices": [
{"exercice_id": exo1['id_code'], "quantity": 3}]}})
parcours_id = r.json()['id_code']
memberws.receive_json()
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token2})
challenge1 = {**stripKey(r.json(), "data"),
"challenger": mdata['data']['member']['id_code'], "canCorrige": True}
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token2})
challenge2 = {**stripKey(r.json(), "data"),
"challenger": mdata['data']['member']['id_code'], "canCorrige": True}
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token})
data = r.json()['challenge']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token})
challenge3 = {**stripKey(r.json(), "data"),
"challenger": mdata['data']['member']['id_code'], "canCorrige": True}
r = client.get(url=f'/room/{room["room"]}/parcours/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
assert r.json() == {'name': 'test_parcours', 'time': 600, 'validate_condition': 10, 'id_code': parcours_id,
'exercices': [
{'exercice_id': exo1['id_code'], 'quantity': 3, 'name': 'test1'}],
'challenges': [challenge1, challenge2]}
def test_get_parcours_admin(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token}, json={
"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10, "exercices": [
{"exercice_id": exo1['id_code'], "quantity": 3}]}})
parcours_id = r.json()['id_code']
memberws.receive_json()
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token2})
challenge1 = {**stripKey(r.json(), "data"),
"challenger": mdata['data']['member']['id_code'], "canCorrige": True}
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token2})
challenge2 = {**stripKey(r.json(), "data"),
"challenger": mdata['data']['member']['id_code'], "canCorrige": True}
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token})
data = r.json()['challenge']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token})
challenge3 = {**stripKey(r.json(), "data"),
"challenger": adata['data']['member']['id_code'], "canCorrige": True}
r = client.get(url=f'/room/{room["room"]}/parcours/{parcours_id}',
headers={"Authorization": "Bearer " + token})
assert {**r.json(), "challenges": r.json()['challenges'].sort(key=lambda d: d['id_code'])} == {
'name': 'test_parcours', 'time': 600, 'validate_condition': 10, 'id_code': parcours_id, 'exercices': [
{'exercice_id': exo1['id_code'], 'quantity': 3, 'name': 'test1'}],
'challenges': [challenge1, challenge2, challenge3].sort(key=lambda d: d['id_code'])}
def test_update_parcours_auth(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
exo1 = test_create(name="test1", client=client, user={
"token": token, "username": "lilian"})
exo2 = test_create(name="test2", client=client, user={
"token": token, "username": "lilian"})
exo3 = test_create(name="test3", client=client, user={
"token": token, "username": "lilian"})
exo4 = test_create(name="test4", client=client, user={
"token": token, "username": "lilian"})
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
r = client.post(f'/room/{room["room"]}/parcours', headers={"Authorization": "Bearer " + token}, json={
"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10, "exercices": [
{"exercice_id": exo1['id_code'], "quantity": 3}]}})
parcours_id = r.json()['id_code']
memberws.receive_json()
r = client.get(url=f'/room/{room["room"]}/challenge/{parcours_id}',
headers={"Authorization": "Bearer " + token2})
data = r.json()['challenge']
filled_obj = [
[{**e, 'inputs': [{"index": 0, "value": str(data[0].index(e))}]} for e in data[0]]]
r = client.post(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}',
json={'challenge': filled_obj, "time": 162}, headers={"Authorization": "Bearer " + token2})
challenge1 = {**stripKey(r.json(), "data"),
"challenger": mdata['data']['member']['id_code'], "canCorrige": True}
r = client.get(url=f'/room/{room["room"]}/parcours/{parcours_id}',
headers={"Authorization": "Bearer " + token})
assert {**r.json(), "challenges": r.json()['challenges'].sort(key=lambda d: d['id_code'])} == {
'name': 'test_parcours', 'time': 600, 'validate_condition': 10,
'id_code': parcours_id, 'exercices': [{'exercice_id': exo1['id_code'], 'quantity': 3, 'name': 'test1'}],
'challenges': [challenge1].sort(key=lambda d: d['id_code'])}
r = client.put(url=f'/room/{room["room"]}/parcours/{parcours_id}', json={
"parcours": {"name": "test_parcours", "time": 10 * 60, "validate_condition": 10, "exercices": [
{"exercice_id": "lolilol", "quantity": 12}, {"exercice_id": exo1['id_code'], "quantity": 10},
{"exercice_id": exo2['id_code'], "quantity": 5}, {"exercice_id": exo3['id_code'], "quantity": 12}]}},
headers={'Authorization': "Bearer " + token})
assert r.json() == {"id_code": parcours_id, "challenges": [], "name": "test_parcours", "time": 10 * 60,
"validate_condition": 10, "exercices": [{
"exercice_id": exo1['id_code'], "name": exo1['name'], "quantity": 10},
{"exercice_id": exo2['id_code'], "name": exo2['name'], "quantity": 5},
{"exercice_id": exo3['id_code'], "name": exo3['name'], "quantity": 12}]}
admin.receive_json()
admin.receive_json()
admin.receive_json()
adata = admin.receive_json()
assert adata == {"type": "update_parcours", "data": {"parcours": {
"name": "test_parcours", "id_code": parcours_id, "best_note": None}}}
def test_change_name(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
admin.receive_json()
admin.send_json({"type": "set_name", "data": {"name": "new_name"}})
adata = admin.receive_json()
assert adata == {'type': "new_name", "data": {"name": "new_name"}}
mdata = memberws.receive_json()
assert mdata == {'type': "new_name", "data": {"name": "new_name"}}
def test_change_name_too_long(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
admin.receive_json()
admin.send_json(
{"type": "set_name", "data": {"name": "new_name" * 20}})
adata = admin.receive_json()
assert adata == {'type': "error", "data": {
"msg": "Nom trop long (max 20 character)"}}
def test_change_status(client: TestClient):
token = test_register(client, username="lilian")['access']
token2 = test_register(client, username="lilian2")['access']
room = test_create_room_auth(client=client, token=token, public=True)
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
admin.send_json({"type": "login", "data": {
"token": token}})
adata = admin.receive_json()
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
memberws.send_json(
{"type": "join", "data": {"token": token2}})
mdata = memberws.receive_json()
admin.receive_json()
admin.send_json({"type": "set_visibility",
"data": {"public": False}})
adata = admin.receive_json()
assert adata == {'type': "new_visibility",
"data": {"public": False}}
mdata = memberws.receive_json()
assert mdata == {'type': "new_visibility",
"data": {"public": False}}
with client.websocket_connect(f"/ws/room/" + room['room']) as m2:
m2.send_json(
{"type": "join", "data": {"username": "okok"}})
mdata = m2.receive_json()
assert "waiter_id" in mdata['data']['waiter']
assert mdata == {"type": "waiting", "data": {"waiter": {
"username": "okok", "waiter_id": mdata['data']['waiter']['waiter_id']}}}
adata = admin.receive_json()
assert adata == {'type': "waiter", 'data': {
"waiter": {"waiter_id": mdata['data']['waiter']['waiter_id'], "username": "okok"}}}