update
This commit is contained in:
185
backend/tests/test_login.py
Normal file
185
backend/tests/test_login.py
Normal file
@@ -0,0 +1,185 @@
|
||||
"""Anmelden per Film.
|
||||
|
||||
Wer einen Anmeldefilm hinterlegt hat, muss ihn beim Namenswechsel in der
|
||||
Suche finden. Ohne Film bleibt der Name offen für alle.
|
||||
"""
|
||||
|
||||
|
||||
def namen(client, name="Anna"):
|
||||
"""Legt den Namen an und gibt seine Kennung zurück."""
|
||||
antwort = client.post("/api/users", json={"name": name})
|
||||
if antwort.status_code == 200:
|
||||
return antwort.json()["id"]
|
||||
runde = client.get("/api/users").json()["items"]
|
||||
return next(u["id"] for u in runde if u["name"] == name)
|
||||
|
||||
|
||||
def test_ohne_film_bleibt_der_name_offen(client2, film):
|
||||
user_id = namen(client2, "Offen")
|
||||
antwort = client2.post("/api/users/waehlen", json={"user_id": user_id})
|
||||
assert antwort.status_code == 200
|
||||
assert antwort.json()["mine"]["geschuetzt"] is False
|
||||
|
||||
|
||||
def test_falscher_film_kommt_nicht_durch(client, client2, film):
|
||||
user_id = namen(client, "Anna")
|
||||
client.post(f"/api/users/{user_id}/schutz", json={"movie_id": film(42)})
|
||||
|
||||
# Ben kennt den Film nicht
|
||||
assert (
|
||||
client2.post(
|
||||
"/api/users/waehlen", json={"user_id": user_id, "movie_id": film(7)}
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
# Ganz ohne Film auch nicht
|
||||
assert (
|
||||
client2.post("/api/users/waehlen", json={"user_id": user_id}).status_code == 403
|
||||
)
|
||||
assert client2.get("/api/users").json()["mine"]["name"] == "Ben"
|
||||
|
||||
|
||||
def test_richtiger_film_laesst_durch(client, client2, film):
|
||||
user_id = namen(client, "Anna")
|
||||
client.post(f"/api/users/{user_id}/schutz", json={"movie_id": film(42)})
|
||||
|
||||
antwort = client2.post(
|
||||
"/api/users/waehlen", json={"user_id": user_id, "movie_id": 42}
|
||||
)
|
||||
assert antwort.status_code == 200
|
||||
assert antwort.json()["mine"]["name"] == "Anna"
|
||||
assert client2.get("/api/users").json()["mine"]["name"] == "Anna"
|
||||
|
||||
|
||||
def test_der_film_verlaesst_den_server_nicht(client, film):
|
||||
user_id = namen(client, "Anna")
|
||||
client.post(f"/api/users/{user_id}/schutz", json={"movie_id": film(42)})
|
||||
|
||||
runde = client.get("/api/users").json()
|
||||
anna = next(u for u in runde["items"] if u["id"] == user_id)
|
||||
assert anna["geschuetzt"] is True
|
||||
assert "auth_movie_id" not in anna
|
||||
assert "auth_movie_id" not in runde["mine"]
|
||||
|
||||
|
||||
def test_aufheben_macht_den_namen_wieder_frei(client, client2, film):
|
||||
user_id = namen(client, "Anna")
|
||||
client.post(f"/api/users/{user_id}/schutz", json={"movie_id": film(42)})
|
||||
client.post(f"/api/users/{user_id}/schutz", json={"movie_id": None})
|
||||
|
||||
assert client.get("/api/users").json()["items"][0]["geschuetzt"] is False
|
||||
assert (
|
||||
client2.post("/api/users/waehlen", json={"user_id": user_id}).status_code == 200
|
||||
)
|
||||
|
||||
|
||||
def test_host_braucht_den_film_nicht(client, zuschauer, film, hostmodus):
|
||||
user_id = namen(client, "Anna")
|
||||
client.post(f"/api/users/{user_id}/schutz", json={"movie_id": film(42)})
|
||||
|
||||
hostmodus(zuschauer)
|
||||
assert zuschauer.get("/api/users").json()["host"] is True
|
||||
assert (
|
||||
zuschauer.post("/api/users/waehlen", json={"user_id": user_id}).status_code
|
||||
== 200
|
||||
)
|
||||
|
||||
# Hostmodus aus: die Hürde steht wieder
|
||||
zuschauer.post("/api/users/waehlen", json={"user_id": None})
|
||||
zuschauer.post("/api/host", json={"an": False})
|
||||
assert (
|
||||
zuschauer.post("/api/users/waehlen", json={"user_id": user_id}).status_code
|
||||
== 403
|
||||
)
|
||||
|
||||
|
||||
def test_wer_schon_angemeldet_ist_muss_nicht_nochmal(client, film):
|
||||
user_id = namen(client, "Anna")
|
||||
client.post("/api/users/waehlen", json={"user_id": user_id})
|
||||
client.post(f"/api/users/{user_id}/schutz", json={"movie_id": film(42)})
|
||||
# Derselbe Name in derselben Sitzung, etwa nach einem Neuladen
|
||||
assert (
|
||||
client.post("/api/users/waehlen", json={"user_id": user_id}).status_code == 200
|
||||
)
|
||||
|
||||
|
||||
def test_fremden_schutz_darf_man_nicht_setzen(client, client2, film):
|
||||
anna = namen(client, "Anna")
|
||||
assert (
|
||||
client2.post(f"/api/users/{anna}/schutz", json={"movie_id": film(42)}).status_code
|
||||
== 403
|
||||
)
|
||||
assert client2.get("/api/users").json()["items"][0]["geschuetzt"] is False
|
||||
|
||||
|
||||
def test_eigenen_schutz_darf_man_setzen(client, film):
|
||||
"""client ist als Anna angemeldet und ändert ihren eigenen Film."""
|
||||
anna = client.get("/api/users").json()["mine"]["id"]
|
||||
assert (
|
||||
client.post(f"/api/users/{anna}/schutz", json={"movie_id": film(42)}).status_code
|
||||
== 200
|
||||
)
|
||||
|
||||
|
||||
def test_hinterlegten_film_darf_man_nachschlagen(client, film):
|
||||
"""Wer ihn ändern darf, darf ihn auch sehen — sonst rät man beim
|
||||
Überschreiben."""
|
||||
anna = client.get("/api/users").json()["mine"]["id"]
|
||||
client.post(f"/api/users/{anna}/schutz", json={"movie_id": film(42, "Alien")})
|
||||
|
||||
antwort = client.get(f"/api/users/{anna}/schutz")
|
||||
assert antwort.status_code == 200
|
||||
assert antwort.json()["film"]["title"] == "Alien"
|
||||
|
||||
|
||||
def test_ohne_film_kommt_nichts_zurueck(client):
|
||||
anna = client.get("/api/users").json()["mine"]["id"]
|
||||
assert client.get(f"/api/users/{anna}/schutz").json()["film"] is None
|
||||
|
||||
|
||||
def test_fremden_film_darf_man_nicht_nachschlagen(client, client2, film):
|
||||
anna = client.get("/api/users").json()["mine"]["id"]
|
||||
client.post(f"/api/users/{anna}/schutz", json={"movie_id": film(42)})
|
||||
assert client2.get(f"/api/users/{anna}/schutz").status_code == 403
|
||||
|
||||
|
||||
def test_host_darf_jeden_film_nachschlagen(client, zuschauer, film, hostmodus):
|
||||
anna = client.get("/api/users").json()["mine"]["id"]
|
||||
client.post(f"/api/users/{anna}/schutz", json={"movie_id": film(42, "Alien")})
|
||||
|
||||
hostmodus(zuschauer)
|
||||
assert zuschauer.get(f"/api/users/{anna}/schutz").json()["film"]["title"] == "Alien"
|
||||
|
||||
|
||||
def test_ohne_schluessel_kein_hostmodus(zuschauer):
|
||||
"""Die Tastenkombination allein reicht nicht mehr."""
|
||||
assert zuschauer.post("/api/host", json={"an": True}).status_code == 403
|
||||
assert zuschauer.get("/api/host").json()["host"] is False
|
||||
|
||||
|
||||
def test_falscher_schluessel_kein_hostmodus(zuschauer):
|
||||
antwort = zuschauer.post(
|
||||
"/api/host", json={"an": True, "schluessel": "geraten"}
|
||||
)
|
||||
assert antwort.status_code == 403
|
||||
assert zuschauer.get("/api/host").json()["host"] is False
|
||||
|
||||
|
||||
def test_schluessel_schaltet_frei_und_bleibt(zuschauer, hostmodus):
|
||||
assert hostmodus(zuschauer).json()["host"] is True
|
||||
assert zuschauer.get("/api/host").json()["host"] is True
|
||||
# Die Freischaltung hält, ein zweiter Aufruf braucht den Schlüssel nicht
|
||||
assert zuschauer.post("/api/host", json={"an": True}).status_code == 200
|
||||
assert zuschauer.get("/api/host").json()["host"] is True
|
||||
|
||||
|
||||
def test_ausschalten_geht_ohne_schluessel(zuschauer, hostmodus):
|
||||
hostmodus(zuschauer)
|
||||
assert zuschauer.post("/api/host", json={"an": False}).json()["host"] is False
|
||||
# Jetzt ist die Sitzung wieder gewöhnlich
|
||||
assert zuschauer.post("/api/host", json={"an": True}).status_code == 403
|
||||
|
||||
|
||||
def test_der_schluessel_gilt_nur_fuer_die_eigene_sitzung(zuschauer, client2, hostmodus):
|
||||
hostmodus(zuschauer)
|
||||
assert client2.get("/api/host").json()["host"] is False
|
||||
Reference in New Issue
Block a user