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

30 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-12-24 15:28 +0000

1import grpc 

2import pytest 

3 

4from couchers.db import session_scope 

5from couchers.models import ContentReport 

6from couchers.proto import reporting_pb2 

7from couchers.sql import couchers_select as select 

8from tests.test_fixtures import db, generate_user, reporting_session, testconfig # noqa 

9 

10 

11@pytest.fixture(autouse=True) 

12def _(testconfig): 

13 pass 

14 

15 

16def test_reporting(db): 

17 user1, token1 = generate_user() 

18 user2, token2 = generate_user() 

19 

20 with reporting_session(token1) as api: 

21 res = api.Report( 

22 reporting_pb2.ReportReq( 

23 reason="spam", 

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

25 content_ref="comment/123", 

26 author_user=user2.username, 

27 user_agent="n/a", 

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

29 ) 

30 ) 

31 

32 with session_scope() as session: 

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

34 

35 assert len(entries) == 1 

36 

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

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

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

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

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

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

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

44 

45 # Test that reporting nonexisting user fails 

46 

47 with reporting_session(token1) as api: 

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

49 api.Report( 

50 reporting_pb2.ReportReq( 

51 reason="spam", 

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

53 content_ref="comment/123", 

54 author_user="impossible username", 

55 user_agent="n/a", 

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

57 ) 

58 ) 

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

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