Coverage for app/backend/src/tests/test_reporting.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-19 15:47 +0000

1import grpc 

2import pytest 

3from sqlalchemy import select 

4 

5from couchers.db import session_scope 

6from couchers.models import ContentReport 

7from couchers.proto import reporting_pb2 

8from tests.fixtures.db import generate_user 

9from tests.fixtures.sessions import reporting_session 

10 

11 

12def test_reporting(db): 

13 user1, token1 = generate_user() 

14 user2, token2 = generate_user() 

15 

16 with reporting_session(token1) as api: 

17 res = api.Report( 

18 reporting_pb2.ReportReq( 

19 reason="spam", 

20 description="I think this is spam and does not belong on couchers", 

21 content_ref="comment/123", 

22 author_user=user2.username, 

23 user_agent="n/a", 

24 page="https://couchers.org/comment/123", 

25 ) 

26 ) 

27 

28 with session_scope() as session: 

29 entries = session.execute(select(ContentReport)).scalars().all() 

30 

31 assert len(entries) == 1 

32 

33 assert entries[0].reporting_user_id == user1.id 

34 assert entries[0].reason == "spam" 

35 assert entries[0].description == "I think this is spam and does not belong on couchers" 

36 assert entries[0].content_ref == "comment/123" 

37 assert entries[0].author_user_id == user2.id 

38 assert entries[0].user_agent == "n/a" 

39 assert entries[0].page == "https://couchers.org/comment/123" 

40 

41 # Test that reporting nonexisting user fails 

42 

43 with reporting_session(token1) as api: 

44 with pytest.raises(grpc.RpcError) as e: 

45 api.Report( 

46 reporting_pb2.ReportReq( 

47 reason="spam", 

48 description="I think this is spam and does not belong on couchers", 

49 content_ref="comment/123", 

50 author_user="impossible username", 

51 user_agent="n/a", 

52 page="https://couchers.org/comment/123", 

53 ) 

54 ) 

55 assert e.value.code() == grpc.StatusCode.NOT_FOUND 

56 assert e.value.details() == "Couldn't find that user."