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

31 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-29 01:26 +0000

1import grpc 

2import pytest 

3 

4from couchers import errors 

5from couchers.db import session_scope 

6from couchers.models import ContentReport 

7from couchers.sql import couchers_select as select 

8from proto import reporting_pb2 

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

10 

11 

12@pytest.fixture(autouse=True) 

13def _(testconfig): 

14 pass 

15 

16 

17def test_reporting(db): 

18 user1, token1 = generate_user() 

19 user2, token2 = generate_user() 

20 

21 with reporting_session(token1) as api: 

22 res = api.Report( 

23 reporting_pb2.ReportReq( 

24 reason="spam", 

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

26 content_ref="comment/123", 

27 author_user=user2.username, 

28 user_agent="n/a", 

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

30 ) 

31 ) 

32 

33 with session_scope() as session: 

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

35 

36 assert len(entries) == 1 

37 

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

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

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

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

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

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

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

45 

46 # Test that reporting nonexisting user fails 

47 

48 with reporting_session(token1) as api: 

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

50 api.Report( 

51 reporting_pb2.ReportReq( 

52 reason="spam", 

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

54 content_ref="comment/123", 

55 author_user="impossible username", 

56 user_agent="n/a", 

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

58 ) 

59 ) 

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

61 assert e.value.details() == errors.USER_NOT_FOUND