generateur_v3/backend/api/tests/test_room.py

1200 lines
61 KiB
Python
Raw Normal View History

2022-10-10 01:34:38 +02:00
import random
2022-09-18 22:43:04 +02:00
from fastapi import HTTPException
from fastapi.testclient import TestClient
from tests.test_auth import test_register
2022-10-10 01:34:38 +02:00
from tests.test_exos import test_create
2022-09-18 22:43:04 +02:00
2022-09-21 22:31:50 +02:00
def test_create_room_no_auth(client: TestClient, public=False):
2022-09-18 22:43:04 +02:00
r = client.post('/room', json={"name": "test_room",
2022-09-25 22:32:19 +02:00
"public": public}, params={'username': "lilian"})
2022-09-18 22:43:04 +02:00
print(r.json())
2022-10-10 01:34:38 +02:00
assert "room" in r.json()
assert "member" in r.json()
assert r.json()['member'] is not None
2022-09-18 22:43:04 +02:00
return r.json()
2022-09-21 22:31:50 +02:00
2022-09-18 22:43:04 +02:00
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'}}
2022-09-21 22:31:50 +02:00
2022-10-10 01:34:38 +02:00
def test_create_room_auth(client: TestClient, token=None, public=False):
2022-09-18 22:43:04 +02:00
if token is None:
token = test_register(client=client)['access']
r = client.post('/room', json={"name": "test_room",
2022-10-10 01:34:38 +02:00
"public": public}, headers={"Authorization": "Bearer " + token})
2022-09-18 22:43:04 +02:00
print(r.json())
2022-10-10 01:34:38 +02:00
assert "room" in r.json()
assert "member" in r.json()
assert r.json()['member'] == None
2022-09-18 22:43:04 +02:00
return r.json()
2022-09-21 22:31:50 +02:00
2022-09-18 22:43:04 +02:00
def test_room_not_found(client: TestClient):
try:
with client.websocket_connect('/ws/room/eee') as r:
pass
2022-09-21 22:31:50 +02:00
except HTTPException as e:
2022-09-18 22:43:04 +02:00
assert True
except Exception:
assert False
2022-09-21 22:31:50 +02:00
2022-09-18 22:43:04 +02:00
def test_login_no_auth(client: TestClient):
room = test_create_room_no_auth(client=client)
2022-09-21 22:31:50 +02:00
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
2022-09-21 22:31:50 +02:00
ws.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-21 22:31:50 +02:00
data = ws.receive_json()
2022-09-25 22:32:19 +02:00
assert "id_code" in data['data']['member']
2022-10-10 01:34:38 +02:00
assert "clientId" in data['data']['member']
assert data == {'type': "loggedIn", "data": {"member": {**data['data']['member'],
"reconnect_code": member, "isAdmin": True, "isUser": False}}}
2022-09-21 22:31:50 +02:00
2022-10-10 01:34:38 +02:00
def test_login_no_auth_different_clientId(client: TestClient):
2022-09-21 22:31:50 +02:00
room = test_create_room_no_auth(client=client)
member = room['member']
2022-10-10 01:34:38 +02:00
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:
2022-09-21 22:31:50 +02:00
ws.send_json({"type": "login", "data": {
"reconnect_code": "lol"}})
2022-09-18 22:43:04 +02:00
data = ws.receive_json()
print(data)
2022-09-21 22:31:50 +02:00
assert data == {'type': "error", "data": {
"msg": "Utilisateur introuvable dans cette salle"}}
2022-09-18 22:43:04 +02:00
def test_login_auth(client: TestClient):
token = test_register(client=client)['access']
room = test_create_room_auth(client=client, token=token)
2022-09-21 22:31:50 +02:00
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
2022-09-21 22:31:50 +02:00
ws.send_json({"type": "login", "data": {"token": token}})
data = ws.receive_json()
print(data)
2022-09-25 22:32:19 +02:00
assert "id_code" in data["data"]['member']
2022-10-10 01:34:38 +02:00
assert data == {'type': "loggedIn", "data": {"member": {**data["data"]['member'],
"username": "lilian", "isAdmin": True, "isUser": True, 'reconnect_code': "", "clientId": ""}}}
2022-09-21 22:31:50 +02:00
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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as ws:
2022-09-18 22:43:04 +02:00
ws.send_json({"type": "login", "data": {"token": token}})
data = ws.receive_json()
print(data)
2022-09-21 22:31:50 +02:00
assert data == {'type': "error", "data": {
"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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-21 22:31:50 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
2022-10-10 01:34:38 +02:00
2022-09-21 22:31:50 +02:00
assert "waiter_id" in mdata['data']['waiter']
assert mdata == {"type": "waiting", "data": {"waiter": {
"username": "lilian2", "waiter_id": mdata['data']['waiter']['waiter_id']}}}
2022-10-10 01:34:38 +02:00
2022-09-25 22:32:19 +02:00
admin.send_json({"type": "ping_room"})
2022-10-10 01:34:38 +02:00
adata = admin.receive_json()
2022-09-21 22:31:50 +02:00
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()
2022-09-25 22:32:19 +02:00
print('MDATA', mdata)
2022-09-21 22:31:50 +02:00
assert mdata == {"type": "accepted", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "lilian2", "isUser": True, "isAdmin": False, "reconnect_code": "", "id_code": mdata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
2022-09-25 22:32:19 +02:00
"member": {"reconnect_code": "", "username": "lilian2", "isUser": True, "isAdmin": False, "id_code": adata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "ping_room"})
mdata = member.receive_json()
assert mdata == {"type": "ping", "data": {}}
2022-10-10 01:34:38 +02:00
2022-09-25 22:32:19 +02:00
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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-25 22:32:19 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
admin.send_json({"type": "accept", "data": {"waiter_id": "OOOO"}})
data = admin.receive_json()
2022-09-26 10:04:02 +02:00
assert data == {"type": "error", "data": {
"msg": "Utilisateur en list d'attente introuvable"}}
2022-09-18 22:43:04 +02:00
2022-10-10 01:34:38 +02:00
2022-09-18 22:43:04 +02:00
def test_join_no_auth(client: TestClient):
room = test_create_room_no_auth(client=client)
2022-09-21 22:31:50 +02:00
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-18 22:43:04 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-21 22:31:50 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-21 22:31:50 +02:00
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"}}}
2022-10-10 01:34:38 +02:00
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "accept", "data": {
"waiter_id": mdata['data']['waiter']['waiter_id']}})
mdata = memberws.receive_json()
new_reconnect = mdata['data']['member']['reconnect_code']
assert 'reconnect_code' in mdata['data']['member']
assert mdata == {"type": "accepted", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "member", "reconnect_code": new_reconnect, "isUser": False, "isAdmin": False, "id_code": mdata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
2022-09-25 22:32:19 +02:00
"member": {"reconnect_code": new_reconnect, "username": "member", "isUser": False, "isAdmin": False, "id_code": adata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "ping_room"})
mdata = memberws.receive_json()
assert mdata == {"type": "ping", "data": {}}
2022-10-10 01:34:38 +02:00
return {"room": room['room'], "members": [member, new_reconnect]}
2022-09-21 22:31:50 +02:00
def test_join_no_auth_username_error(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({"type": "join", "data": {"username": "lilian"}})
mdata = member.receive_json()
assert mdata == {"type": "error", "data": {
"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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({"type": "join", "data": {"username": "lilian"*21}})
mdata = member.receive_json()
2022-10-10 01:34:38 +02:00
assert mdata == {"type": "error", "data": {
"msg": "Nom d'utilisateur invalide ou indisponible"}}
2022-09-21 22:31:50 +02:00
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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-21 22:31:50 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
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}})
2022-09-26 10:04:02 +02:00
adata = admin.receive_json()
2022-10-10 01:34:38 +02:00
assert adata == {"type": "successfullyRefused",
"data": {"waiter_id": waiter_id}}
2022-09-21 22:31:50 +02:00
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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
2022-10-10 01:34:38 +02:00
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': ""}}}
2022-09-21 22:31:50 +02:00
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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({"type": "join", "data": {"token": token}})
mdata = member.receive_json()
assert mdata == {"type": "accepted", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "lilian2", "isUser": True, "isAdmin": False, "reconnect_code": "", "id_code": mdata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
2022-09-25 22:32:19 +02:00
"member": {"reconnect_code": "", "username": "lilian2", "isUser": True, "isAdmin": False, "id_code": mdata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "ping_room"})
mdata = member.receive_json()
2022-09-25 22:32:19 +02:00
assert mdata == {"type": "ping", "data": {}}
2022-09-21 22:31:50 +02:00
def test_join_no_auth_public(client: TestClient):
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({"type": "join", "data": {"username": "member"}})
mdata = member.receive_json()
assert 'reconnect_code' in mdata['data']['member']
assert mdata == {"type": "accepted", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "member", "reconnect_code": mdata['data']['member']['reconnect_code'], "isUser": False, "isAdmin": False, "id_code": mdata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {'type': "joined", 'data': {
2022-09-25 22:32:19 +02:00
"member": {"reconnect_code": mdata['data']['member']['reconnect_code'], "username": "member", "isUser": False, "isAdmin": False, "id_code": mdata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
member.send_json({"type": "update_groups"})
admin.send_json({"type": "ping_room"})
mdata = member.receive_json()
2022-09-25 22:32:19 +02:00
assert mdata == {"type": "ping", "data": {}}
2022-09-21 22:31:50 +02:00
2022-09-25 22:32:19 +02:00
def test_join_auth_unauthorized(client: TestClient):
2022-09-21 22:31:50 +02:00
token = test_register(client, username="lilian2")['access']
room = test_create_room_no_auth(client=client)
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({"type": "join", "data": {"token": token}})
2022-09-18 22:43:04 +02:00
mdata = member.receive_json()
2022-09-25 22:32:19 +02:00
assert "waiter_id" in mdata['data']['waiter']
2022-09-18 22:43:04 +02:00
assert mdata == {"type": "waiting", "data": {"waiter": {
2022-09-25 22:32:19 +02:00
"username": "lilian2", "waiter_id": mdata['data']['waiter']['waiter_id']}}}
2022-09-21 22:31:50 +02:00
2022-09-18 22:43:04 +02:00
adata = admin.receive_json()
assert adata == {'type': "waiter", 'data': {
2022-09-25 22:32:19 +02:00
"waiter": {"waiter_id": mdata['data']['waiter']['waiter_id'], "username": "lilian2"}}}
2022-09-21 22:31:50 +02:00
member.send_json({"type": "refuse", "data": {
2022-09-25 22:32:19 +02:00
"waiter_id": mdata['data']['waiter']['waiter_id']}})
2022-09-21 22:31:50 +02:00
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']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
"reconnect_code": members[0]}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as member:
2022-09-21 22:31:50 +02:00
member.send_json({'type': "login", "data": {
2022-09-25 22:32:19 +02:00
"reconnect_code": members[1]}})
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {"type": "connect", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "member", "reconnect_code": members[1], "isAdmin": False, "isUser": False, "id_code": adata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
def test_connect_member(client: TestClient):
room = test_join_no_auth(client=client)
members = room['members']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-21 22:31:50 +02:00
memberws.send_json({"type": "login", "data": {
"reconnect_code": members[1]}})
2022-09-25 22:32:19 +02:00
memberws.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({'type': "login", "data": {
"reconnect_code": members[0]}})
mdata = memberws.receive_json()
assert mdata == {"type": "connect", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "lilian", "reconnect_code": "", "isAdmin": True, "isUser": False, "id_code": mdata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
2022-10-10 01:34:38 +02:00
2022-09-21 22:31:50 +02:00
def test_disconnect(client: TestClient):
room = test_join_no_auth(client=client)
members = room['members']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-21 22:31:50 +02:00
memberws.send_json({"type": "login", "data": {
"reconnect_code": members[1]}})
2022-09-25 22:32:19 +02:00
memberws.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({'type': "login", "data": {
"reconnect_code": members[0]}})
2022-09-25 22:32:19 +02:00
memberws.receive_json()
2022-09-21 22:31:50 +02:00
admin.close()
mdata = memberws.receive_json()
assert mdata == {"type": "disconnect", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "lilian", "reconnect_code": "", "isAdmin": True, "isUser": False, "id_code": mdata['data']["member"]["id_code"]}}}
2022-10-10 01:34:38 +02:00
2022-09-25 22:32:19 +02:00
def test_disconnect_waiter(client: TestClient):
room = test_create_room_no_auth(client=client)
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-25 22:32:19 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-25 22:32:19 +02:00
memberws.send_json({'type': "join", "data": {
2022-10-10 01:34:38 +02:00
"username": "test"}})
2022-09-25 22:32:19 +02:00
mdata = memberws.receive_json()
admin.receive_json()
memberws.close()
adata = admin.receive_json()
2022-10-10 01:34:38 +02:00
assert adata == {"type": "disconnect_waiter",
"data": {"waiter": {**mdata['data']['waiter']}}}
2022-09-21 22:31:50 +02:00
def test_leave(client: TestClient):
2022-09-25 22:32:19 +02:00
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-25 22:32:19 +02:00
memberws.send_json({"type": "join", "data": {
2022-10-10 01:34:38 +02:00
"username": "test"}})
2022-09-25 22:32:19 +02:00
m = memberws.receive_json()
admin.receive_json()
2022-09-21 22:31:50 +02:00
memberws.send_json({"type": "leave"})
data = memberws.receive_json()
2022-09-25 22:32:19 +02:00
assert data == {"type": "successfully_leaved", "data": {}}
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {"type": "leaved", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "test", "reconnect_code": m["data"]['member']["reconnect_code"], "isAdmin": False, "isUser": False, "id_code": adata['data']["member"]["id_code"]}}}
2022-09-21 22:31:50 +02:00
def test_leave_not_connected(client: TestClient):
room = test_create_room_no_auth(client=client)
2022-09-25 22:32:19 +02:00
members = room['member']
2022-09-21 22:31:50 +02:00
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-21 22:31:50 +02:00
memberws.send_json({"type": "leave"})
data = memberws.receive_json()
assert data == {"type": "error", "data": {
"msg": "Vous n'êtes connecté à aucune salle"}}
def test_leave_admin(client: TestClient):
2022-09-25 22:32:19 +02:00
room = test_create_room_no_auth(client=client)
member = room['member']
2022-09-21 22:31:50 +02:00
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "leave"})
data = admin.receive_json()
assert data == {"type": "error", "data": {
"msg": "Vous ne pouvez pas quitter une salle dont vous êtes l'administrateur"}}
def test_ban_anonymous(client: TestClient):
2022-09-25 22:32:19 +02:00
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-25 22:32:19 +02:00
memberws.send_json({"type": "join", "data": {
"username": "test"}})
m = memberws.receive_json()
2022-10-10 01:34:38 +02:00
reconnect_code = m['data']['member']['reconnect_code']
id_code = m['data']['member']['id_code']
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
admin.send_json(
{"type": "ban", "data": {"member_id": m['data']['member']['id_code']}})
2022-09-25 22:32:19 +02:00
mdata = memberws.receive_json()
assert mdata == {"type": "banned", "data": {}}
2022-10-10 01:34:38 +02:00
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {"type": "leaved", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "test", "reconnect_code": reconnect_code, "isUser": False, "isAdmin": False, "id_code": id_code}}}
2022-09-21 22:31:50 +02:00
def test_ban_anonymous_unauthorized(client: TestClient):
2022-09-25 22:32:19 +02:00
room = test_create_room_no_auth(client=client, public=True)
2022-10-10 01:34:38 +02:00
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-25 22:32:19 +02:00
memberws.send_json({"type": "join", "data": {
"username": "test"}})
memberws.receive_json()
memberws.send_json({"type": "ban", "data": {"member_id": "OOO"}})
2022-09-21 22:31:50 +02:00
mdata = memberws.receive_json()
assert mdata == {"type": "error", "data": {
2022-09-25 22:32:19 +02:00
"msg": "Vous n'avez pas la permission de faire ca"}}
2022-10-10 01:34:38 +02:00
2022-09-25 22:32:19 +02:00
def test_ban_admin(client: TestClient):
room = test_create_room_no_auth(client=client)
2022-10-10 01:34:38 +02:00
member = room['member']
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-25 22:32:19 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
data = admin.receive_json()
2022-10-10 01:34:38 +02:00
admin.send_json(
{"type": "ban", "data": {"member_id": data['data']['member']['id_code']}})
2022-09-25 22:32:19 +02:00
a = admin.receive_json()
assert a == {'type': "error", "data": {
"msg": "Vous ne pouvez pas bannir un administrateur"}}
2022-09-21 22:31:50 +02:00
def test_ban_user(client: TestClient):
2022-09-25 22:32:19 +02:00
token = test_register(client=client, username="lilian2")['access']
room = test_create_room_no_auth(client=client, public=True)
member = room['member']
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as admin:
2022-09-21 22:31:50 +02:00
admin.send_json({"type": "login", "data": {
2022-10-10 01:34:38 +02:00
"reconnect_code": member}})
2022-09-25 22:32:19 +02:00
admin.receive_json()
2022-10-10 01:34:38 +02:00
with client.websocket_connect(f"/ws/room/" + room['room']) as memberws:
2022-09-21 22:31:50 +02:00
memberws.send_json({"type": "join", "data": {"token": token}})
2022-09-25 22:32:19 +02:00
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": {}}
2022-09-21 22:31:50 +02:00
adata = admin.receive_json()
assert adata == {"type": "leaved", "data": {"member": {
2022-09-25 22:32:19 +02:00
"username": "lilian2", "reconnect_code": "", "isUser": True, "isAdmin": False, "id_code": m['data']['member']['id_code']}}}
2022-10-10 01:34:38 +02:00
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', json={"clientId": adata['data']['member']['clientId'], "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}]}})
print('resonse', r.json())
assert r.json() == {"id_code": r.json()['id_code'], "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}]}
adata = admin.receive_json()
assert adata == {"type": "add_parcours", "data": {"parcours": {
"name": "test_parcours", "id_code": r.json()['id_code'], "best_note": None}}}
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={"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}]}})
assert r.json() == {"id_code": r.json()['id_code'], "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}]}
adata = admin.receive_json()
assert adata == {"type": "add_parcours", "data": {"parcours": {
"name": "test_parcours", "id_code": r.json()['id_code'], "best_note": None}}}
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', json={"clientId": adata['data']['member']['clientId'], "parcours": {"name": "test_parcours", "time": 10*60, "validate_condition": 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']}", json={
"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 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()
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']
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]]]}
adata = admin.receive_json()
assert adata == {'type': 'challenge', 'data': {'challenger': mdata['data']['member']['id_code'], 'note': {
'value': 1, 'total': 3}, 'time': 162,"validated": False, 'isCorriged': True, 'canCorrige': True, "id_code": r.json()['id_code']}}
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()
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']
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]]]}
adata = admin.receive_json()
assert adata == {'type': 'challenge', 'data': {'challenger': mdata['data']['member']['id_code'], 'note': {
'value': 1, 'total': 3}, 'time': 162, 'isCorriged': True,"validated": False, '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()
data = r.json()['data']
corriged_obj = [
[{**e, 'inputs': [{**e['inputs'][0], "correction": str(data[0].index(e))}]} for e in data[0]]]
r = client.put(
f'/room/{room["room"]}/challenge/{parcours_id}/{r.json()["id_code"]}', json={'correction': corriged_obj}, headers={"Authorization": "Bearer " + token})
assert r.json() == {"id_code": r.json()['id_code'], "note": {"value": 3, "total": 3}, "time": 162, "validated": True, "isCorriged": True, "data": corriged_obj}
adata = admin.receive_json()
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"}}}