Phase 2: Job enqueueing from counter
- Add backend/jobs.py with enqueue_random_number_job function - Modify counter increment endpoint to enqueue job after incrementing - Add mock_enqueue_job fixture to conftest.py for all tests - Add test_increment_enqueues_job_with_user_id to verify correct user_id - Job is enqueued synchronously; failure causes request to fail
This commit is contained in:
parent
10c0316603
commit
6ca0ae88dd
4 changed files with 98 additions and 2 deletions
|
|
@ -11,6 +11,38 @@ from tests.conftest import create_user_with_roles
|
|||
from tests.helpers import create_invite_for_godfather, unique_email
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_increment_enqueues_job_with_user_id(client_factory, mock_enqueue_job):
|
||||
"""Verify that incrementing the counter enqueues a job with the user's ID."""
|
||||
async with client_factory.get_db_session() as db:
|
||||
godfather = await create_user_with_roles(
|
||||
db, unique_email("gf"), "pass123", [ROLE_REGULAR]
|
||||
)
|
||||
invite_code = await create_invite_for_godfather(db, godfather.id)
|
||||
|
||||
reg = await client_factory.post(
|
||||
"/api/auth/register",
|
||||
json={
|
||||
"email": unique_email(),
|
||||
"password": "testpass123",
|
||||
"invite_identifier": invite_code,
|
||||
},
|
||||
)
|
||||
cookies = dict(reg.cookies)
|
||||
|
||||
# Get user ID from the me endpoint
|
||||
async with client_factory.create(cookies=cookies) as authed:
|
||||
me_response = await authed.get("/api/auth/me")
|
||||
user_id = me_response.json()["id"]
|
||||
|
||||
# Increment counter
|
||||
response = await authed.post("/api/counter/increment")
|
||||
assert response.status_code == 200
|
||||
|
||||
# Verify enqueue was called with the correct user_id
|
||||
mock_enqueue_job.assert_called_once_with(user_id)
|
||||
|
||||
|
||||
# Protected endpoint tests - without auth
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_counter_requires_auth(client):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue