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

71 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-12-20 18:03 +0000

1import grpc 

2import pytest 

3 

4from couchers import errors 

5from couchers.db import session_scope 

6from couchers.utils import now, to_aware_datetime 

7from proto import discussions_pb2 

8from tests.test_communities import create_community, create_group 

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

10 

11 

12@pytest.fixture(autouse=True) 

13def _(testconfig): 

14 pass 

15 

16 

17def test_create_discussion_errors(db): 

18 user, token = generate_user() 

19 with discussions_session(token) as api: 

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

21 api.CreateDiscussion( 

22 discussions_pb2.CreateDiscussionReq( 

23 title=None, 

24 content="dummy content", 

25 ) 

26 ) 

27 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

28 assert e.value.details() == errors.MISSING_DISCUSSION_TITLE 

29 

30 with discussions_session(token) as api: 

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

32 api.CreateDiscussion( 

33 discussions_pb2.CreateDiscussionReq( 

34 title="dummy title", 

35 content=None, 

36 ) 

37 ) 

38 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT 

39 assert e.value.details() == errors.MISSING_DISCUSSION_CONTENT 

40 

41 

42def test_create_and_get_discussion(db): 

43 generate_user() 

44 user, token = generate_user() 

45 user2, token2 = generate_user() 

46 generate_user() 

47 generate_user() 

48 

49 with session_scope() as session: 

50 community = create_community(session, 0, 1, "Testing Community", [user2], [], None) 

51 group_id = create_group(session, "Testing Group", [user2], [], community).id 

52 community_id = community.id 

53 

54 with discussions_session(token) as api: 

55 time_before_create = now() 

56 res = api.CreateDiscussion( 

57 discussions_pb2.CreateDiscussionReq( 

58 title="dummy title", 

59 content="dummy content", 

60 owner_community_id=community_id, 

61 ) 

62 ) 

63 time_after_create = now() 

64 

65 assert res.title == "dummy title" 

66 assert res.content == "dummy content" 

67 assert res.slug == "dummy-title" 

68 assert time_before_create <= to_aware_datetime(res.created) <= time_after_create 

69 assert res.creator_user_id == user.id 

70 assert res.owner_community_id == community_id 

71 

72 discussion_id = res.discussion_id 

73 

74 with discussions_session(token) as api: 

75 res = api.GetDiscussion( 

76 discussions_pb2.GetDiscussionReq( 

77 discussion_id=discussion_id, 

78 ) 

79 ) 

80 

81 assert res.title == "dummy title" 

82 assert res.content == "dummy content" 

83 assert res.slug == "dummy-title" 

84 assert time_before_create <= to_aware_datetime(res.created) <= time_after_create 

85 assert res.creator_user_id == user.id 

86 assert res.owner_community_id == community_id 

87 

88 with discussions_session(token) as api: 

89 time_before_create = now() 

90 res = api.CreateDiscussion( 

91 discussions_pb2.CreateDiscussionReq( 

92 title="dummy title", 

93 content="dummy content", 

94 owner_group_id=group_id, 

95 ) 

96 ) 

97 time_after_create = now() 

98 

99 assert res.title == "dummy title" 

100 assert res.content == "dummy content" 

101 assert res.slug == "dummy-title" 

102 assert time_before_create <= to_aware_datetime(res.created) <= time_after_create 

103 assert res.creator_user_id == user.id 

104 assert res.owner_group_id == group_id 

105 

106 discussion_id = res.discussion_id 

107 

108 with discussions_session(token) as api: 

109 res = api.GetDiscussion( 

110 discussions_pb2.GetDiscussionReq( 

111 discussion_id=discussion_id, 

112 ) 

113 ) 

114 

115 assert res.title == "dummy title" 

116 assert res.content == "dummy content" 

117 assert res.slug == "dummy-title" 

118 assert time_before_create <= to_aware_datetime(res.created) <= time_after_create 

119 assert res.creator_user_id == user.id 

120 assert res.owner_group_id == group_id