86 lines
3.8 KiB
Python
86 lines
3.8 KiB
Python
"""A10 — the whole path through the site, as a visitor and a player would walk it: home, sign up, deposit, bet, draw,
|
|
public result (WEB-05, WEB-06, WEB-10, WEB-12, WEB-13, WEB-22, WEB-26, WEB-27)."""
|
|
|
|
import re
|
|
from datetime import timedelta
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import Client
|
|
from django.utils import timezone
|
|
|
|
from apps.lottery import lifecycle, simulation
|
|
from apps.lottery.ledger import SimulatedLedger
|
|
from apps.lottery.models import Round
|
|
from apps.lottery.simulation import SimulatedChain
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
PASSWORD = "correct-horse-battery"
|
|
|
|
|
|
def form_post(client, url, data, *, page=None):
|
|
"""Read the page first (it carries the CSRF cookie and the idempotency key), then send what a browser would."""
|
|
page_html = client.get(page or url).content.decode()
|
|
token = client.cookies["csrftoken"].value
|
|
extra = dict(re.findall(r'<input type="hidden" name="(round_id|request_id)" value="([^"]+)"', page_html))
|
|
return client.post(url, {**extra, **data, "csrfmiddlewaretoken": token})
|
|
|
|
|
|
def test_from_the_home_page_to_the_public_result(settings):
|
|
settings.SIMULATED_BLOCK_SECONDS, settings.SIMULATED_CONFIRM_SECONDS = 10, 2
|
|
client = Client(enforce_csrf_checks=True)
|
|
chain = SimulatedChain()
|
|
lifecycle.tick(chain=chain) # the worker opens the round
|
|
|
|
# a visitor sees an open round and is invited to take part
|
|
home = client.get("/").content.decode()
|
|
assert "Puntate aperte" in home and "Registrati" in home
|
|
# somebody else is already in, so the draw has two players
|
|
other = get_user_model().objects.create_user("avversario", password=PASSWORD)
|
|
round_ = Round.objects.get()
|
|
ledger = SimulatedLedger()
|
|
ledger.deposit(other, round_.bet_amount_sats)
|
|
ledger.confirm_bet(ledger.place_bet(other, round_.pk, request_id=uuid4()).obj)
|
|
|
|
# sign up: the player lands signed in on the play page
|
|
response = form_post(client, "/registrati/", {"username": "giulia", "password": PASSWORD, "password_confirm": PASSWORD})
|
|
assert response.status_code == 302 and response["Location"] == "/app/"
|
|
assert client.get("/app/").status_code == 200
|
|
|
|
# deposit on the simulated ledger: first «in conferma», then spendable once the worker settles it
|
|
response = form_post(client, "/app/portafoglio/deposito-prova/", {"amount": "10"}, page="/app/portafoglio/")
|
|
assert response.status_code == 302
|
|
assert "in conferma" in client.get("/app/portafoglio/").content.decode().lower()
|
|
now = timezone.now() + timedelta(seconds=5)
|
|
simulation.settle(now)
|
|
|
|
# bet: the confirmation page first, then the explicit POST
|
|
assert "Conferma" in client.get("/app/punta/").content.decode()
|
|
response = form_post(client, "/app/punta/", {})
|
|
assert response.status_code == 302
|
|
assert client.get("/app/stato/").json()["me"]["participation"]["status"] == "pending"
|
|
simulation.settle(now + timedelta(seconds=5))
|
|
assert client.get("/app/stato/").json()["me"]["participation"]["status"] == "confirmed"
|
|
|
|
# the worker closes the round, draws and pays
|
|
now += timedelta(seconds=round_.duration_seconds + 10)
|
|
for _ in range(400):
|
|
now += timedelta(seconds=5)
|
|
simulation.settle(now)
|
|
lifecycle.tick(now=now, chain=chain)
|
|
round_.refresh_from_db()
|
|
if round_.status == "closed":
|
|
break
|
|
assert round_.status == "closed" and round_.winner_id is not None
|
|
|
|
# the public result recomputes the winner and shows no account name
|
|
page = Client().get(f"/risultati/{round_.pk}/").content.decode()
|
|
assert "Vincitore" in page and 'data-ok="0"' not in page
|
|
assert "giulia" not in page and "avversario" not in page
|
|
assert Client().get("/risultati/").status_code == 200
|
|
|
|
# the player is told how it ended
|
|
me = client.get("/app/stato/").json()["me"]
|
|
assert me["last_result"] is not None
|