arbret/backend/tests/test_counter.py

208 lines
6.6 KiB
Python
Raw Normal View History

2025-12-20 11:12:11 +01:00
"""Tests for counter endpoints.
Note: Registration now requires an invite code.
"""
2025-12-18 21:48:41 +01:00
import pytest
2025-12-18 22:24:46 +01:00
from auth import COOKIE_NAME
2025-12-20 11:43:32 +01:00
from models import ROLE_REGULAR
from tests.conftest import create_user_with_roles
from tests.helpers import create_invite_for_godfather, unique_email
2025-12-18 21:48:41 +01:00
2025-12-18 22:08:31 +01:00
# Protected endpoint tests - without auth
@pytest.mark.asyncio
async def test_get_counter_requires_auth(client):
response = await client.get("/api/counter")
2025-12-18 22:24:46 +01:00
assert response.status_code == 401
2025-12-18 21:48:41 +01:00
2025-12-18 22:08:31 +01:00
@pytest.mark.asyncio
async def test_increment_counter_requires_auth(client):
response = await client.post("/api/counter/increment")
2025-12-18 22:24:46 +01:00
assert response.status_code == 401
2025-12-18 22:08:31 +01:00
2025-12-18 21:48:41 +01:00
2025-12-18 22:08:31 +01:00
@pytest.mark.asyncio
2025-12-18 22:24:46 +01:00
async def test_get_counter_invalid_cookie(client_factory):
async with client_factory.create(cookies={COOKIE_NAME: "invalidtoken"}) as authed:
response = await authed.get("/api/counter")
2025-12-18 22:08:31 +01:00
assert response.status_code == 401
2025-12-18 21:48:41 +01:00
@pytest.mark.asyncio
2025-12-18 22:24:46 +01:00
async def test_increment_counter_invalid_cookie(client_factory):
async with client_factory.create(cookies={COOKIE_NAME: "invalidtoken"}) as authed:
response = await authed.post("/api/counter/increment")
2025-12-18 22:08:31 +01:00
assert response.status_code == 401
# Authenticated counter tests
@pytest.mark.asyncio
2025-12-18 22:24:46 +01:00
async def test_get_counter_authenticated(client_factory):
2025-12-20 11:12:11 +01:00
async with client_factory.get_db_session() as db:
godfather = await create_user_with_roles(
db, unique_email("gf"), "pass123", [ROLE_REGULAR]
)
2025-12-20 11:43:32 +01:00
invite_code = await create_invite_for_godfather(db, godfather.id)
2025-12-18 22:24:46 +01:00
reg = await client_factory.post(
"/api/auth/register",
2025-12-20 11:12:11 +01:00
json={
"email": unique_email(),
"password": "testpass123",
"invite_identifier": invite_code,
},
2025-12-18 22:24:46 +01:00
)
cookies = dict(reg.cookies)
2025-12-18 22:24:46 +01:00
async with client_factory.create(cookies=cookies) as authed:
response = await authed.get("/api/counter")
2025-12-18 21:48:41 +01:00
assert response.status_code == 200
2025-12-18 22:08:31 +01:00
assert "value" in response.json()
2025-12-18 21:48:41 +01:00
@pytest.mark.asyncio
2025-12-18 22:24:46 +01:00
async def test_increment_counter(client_factory):
2025-12-20 11:12:11 +01:00
async with client_factory.get_db_session() as db:
godfather = await create_user_with_roles(
db, unique_email("gf"), "pass123", [ROLE_REGULAR]
)
2025-12-20 11:43:32 +01:00
invite_code = await create_invite_for_godfather(db, godfather.id)
2025-12-18 22:24:46 +01:00
reg = await client_factory.post(
"/api/auth/register",
2025-12-20 11:12:11 +01:00
json={
"email": unique_email(),
"password": "testpass123",
"invite_identifier": invite_code,
},
2025-12-18 22:24:46 +01:00
)
cookies = dict(reg.cookies)
2025-12-18 22:24:46 +01:00
async with client_factory.create(cookies=cookies) as authed:
# Get current value
before = await authed.get("/api/counter")
before_value = before.json()["value"]
2025-12-18 22:24:46 +01:00
# Increment
response = await authed.post("/api/counter/increment")
assert response.status_code == 200
assert response.json()["value"] == before_value + 1
2025-12-18 21:48:41 +01:00
@pytest.mark.asyncio
2025-12-18 22:24:46 +01:00
async def test_increment_counter_multiple(client_factory):
2025-12-20 11:12:11 +01:00
async with client_factory.get_db_session() as db:
godfather = await create_user_with_roles(
db, unique_email("gf"), "pass123", [ROLE_REGULAR]
)
2025-12-20 11:43:32 +01:00
invite_code = await create_invite_for_godfather(db, godfather.id)
2025-12-18 22:24:46 +01:00
reg = await client_factory.post(
"/api/auth/register",
2025-12-20 11:12:11 +01:00
json={
"email": unique_email(),
"password": "testpass123",
"invite_identifier": invite_code,
},
2025-12-18 22:24:46 +01:00
)
cookies = dict(reg.cookies)
2025-12-18 22:24:46 +01:00
async with client_factory.create(cookies=cookies) as authed:
# Get starting value
before = await authed.get("/api/counter")
start = before.json()["value"]
2025-12-18 22:24:46 +01:00
# Increment 3 times
await authed.post("/api/counter/increment")
await authed.post("/api/counter/increment")
response = await authed.post("/api/counter/increment")
2025-12-18 22:24:46 +01:00
assert response.json()["value"] == start + 3
2025-12-18 21:48:41 +01:00
@pytest.mark.asyncio
2025-12-18 22:24:46 +01:00
async def test_get_counter_after_increment(client_factory):
2025-12-20 11:12:11 +01:00
async with client_factory.get_db_session() as db:
godfather = await create_user_with_roles(
db, unique_email("gf"), "pass123", [ROLE_REGULAR]
)
2025-12-20 11:43:32 +01:00
invite_code = await create_invite_for_godfather(db, godfather.id)
2025-12-18 22:24:46 +01:00
reg = await client_factory.post(
"/api/auth/register",
2025-12-20 11:12:11 +01:00
json={
"email": unique_email(),
"password": "testpass123",
"invite_identifier": invite_code,
},
2025-12-18 22:24:46 +01:00
)
cookies = dict(reg.cookies)
2025-12-18 22:24:46 +01:00
async with client_factory.create(cookies=cookies) as authed:
before = await authed.get("/api/counter")
start = before.json()["value"]
2025-12-18 22:24:46 +01:00
await authed.post("/api/counter/increment")
await authed.post("/api/counter/increment")
2025-12-18 22:24:46 +01:00
response = await authed.get("/api/counter")
assert response.json()["value"] == start + 2
2025-12-18 22:08:31 +01:00
# Counter is shared between users
@pytest.mark.asyncio
2025-12-18 22:24:46 +01:00
async def test_counter_shared_between_users(client_factory):
2025-12-20 11:43:32 +01:00
# Create godfather and invites for two users
2025-12-20 11:12:11 +01:00
async with client_factory.get_db_session() as db:
godfather = await create_user_with_roles(
db, unique_email("gf"), "pass123", [ROLE_REGULAR]
)
2025-12-20 11:43:32 +01:00
invite1 = await create_invite_for_godfather(db, godfather.id)
invite2 = await create_invite_for_godfather(db, godfather.id)
2025-12-18 22:24:46 +01:00
# Create first user
reg1 = await client_factory.post(
"/api/auth/register",
2025-12-20 11:12:11 +01:00
json={
"email": unique_email("share1"),
"password": "testpass123",
"invite_identifier": invite1,
},
2025-12-18 22:24:46 +01:00
)
cookies1 = dict(reg1.cookies)
2025-12-18 22:24:46 +01:00
async with client_factory.create(cookies=cookies1) as user1:
# Get starting value
before = await user1.get("/api/counter")
start = before.json()["value"]
2025-12-18 22:24:46 +01:00
await user1.post("/api/counter/increment")
await user1.post("/api/counter/increment")
2025-12-18 22:24:46 +01:00
# Create second user - should see the increments
reg2 = await client_factory.post(
"/api/auth/register",
2025-12-20 11:12:11 +01:00
json={
"email": unique_email("share2"),
"password": "testpass123",
"invite_identifier": invite2,
},
2025-12-18 22:24:46 +01:00
)
cookies2 = dict(reg2.cookies)
2025-12-18 22:24:46 +01:00
async with client_factory.create(cookies=cookies2) as user2:
response = await user2.get("/api/counter")
assert response.json()["value"] == start + 2
2025-12-18 22:24:46 +01:00
# Second user increments
await user2.post("/api/counter/increment")
2025-12-18 22:08:31 +01:00
# First user sees the increment
2025-12-18 22:24:46 +01:00
async with client_factory.create(cookies=cookies1) as user1:
response = await user1.get("/api/counter")
assert response.json()["value"] == start + 3