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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

63 statements  

1from unittest.mock import patch 

2 

3import grpc 

4import pytest 

5 

6from couchers.config import config 

7from proto import bugs_pb2 

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

9 

10 

11@pytest.fixture(autouse=True) 

12def _(testconfig): 

13 pass 

14 

15 

16def test_bugs_disabled(): 

17 with bugs_session() as bugs, pytest.raises(grpc.RpcError) as e: 

18 bugs.ReportBug( 

19 bugs_pb2.ReportBugReq( 

20 subject="subject", 

21 description="description", 

22 results="results", 

23 frontend_version="frontend_version", 

24 user_agent="user_agent", 

25 page="page", 

26 ) 

27 ) 

28 assert e.value.code() == grpc.StatusCode.UNAVAILABLE 

29 

30 

31def test_bugs(db): 

32 with bugs_session() as bugs: 

33 

34 def dud_post(url, auth, json): 

35 assert url == "https://api.github.com/repos/org/repo/issues" 

36 assert auth == ("user", "token") 

37 assert json == { 

38 "title": "subject", 

39 "body": ( 

40 "Subject: subject\nDescription:\ndescription\n\nResults:\nresults\n\nBackend version: " 

41 + config["VERSION"] 

42 + "\nFrontend version: frontend_version\nUser Agent: user_agent\nPage: page\nUser: <not logged in>" 

43 ), 

44 "labels": ["bug tool"], 

45 } 

46 

47 class _PostReturn: 

48 status_code = 201 

49 

50 def json(self): 

51 return {"number": 11} 

52 

53 return _PostReturn() 

54 

55 new_config = config.copy() 

56 new_config["BUG_TOOL_ENABLED"] = True 

57 

58 with patch("couchers.servicers.bugs.config", new_config): 

59 with patch("couchers.servicers.bugs.requests.post", dud_post): 

60 res = bugs.ReportBug( 

61 bugs_pb2.ReportBugReq( 

62 subject="subject", 

63 description="description", 

64 results="results", 

65 frontend_version="frontend_version", 

66 user_agent="user_agent", 

67 page="page", 

68 ) 

69 ) 

70 

71 assert res.bug_id == "#11" 

72 assert res.bug_url == "https://github.com/org/repo/issues/11" 

73 

74 

75def test_bugs_with_user(db): 

76 user, token = generate_user(username="testing_user") 

77 

78 with bugs_session(token) as bugs: 

79 

80 def dud_post(url, auth, json): 

81 assert url == "https://api.github.com/repos/org/repo/issues" 

82 assert auth == ("user", "token") 

83 assert json == { 

84 "title": "subject", 

85 "body": ( 

86 "Subject: subject\nDescription:\ndescription\n\nResults:\nresults\n\nBackend version: " 

87 + config["VERSION"] 

88 + "\nFrontend version: frontend_version\nUser Agent: user_agent\nPage: page\nUser: testing_user (1)" 

89 ), 

90 "labels": ["bug tool"], 

91 } 

92 

93 class _PostReturn: 

94 status_code = 201 

95 

96 def json(self): 

97 return {"number": 11} 

98 

99 return _PostReturn() 

100 

101 new_config = config.copy() 

102 new_config["BUG_TOOL_ENABLED"] = True 

103 

104 with patch("couchers.servicers.bugs.config", new_config): 

105 with patch("couchers.servicers.bugs.requests.post", dud_post): 

106 res = bugs.ReportBug( 

107 bugs_pb2.ReportBugReq( 

108 subject="subject", 

109 description="description", 

110 results="results", 

111 frontend_version="frontend_version", 

112 user_agent="user_agent", 

113 page="page", 

114 ) 

115 ) 

116 

117 assert res.bug_id == "#11" 

118 assert res.bug_url == "https://github.com/org/repo/issues/11" 

119 

120 

121def test_bugs_fails_on_network_error(db): 

122 with bugs_session() as bugs: 

123 

124 def dud_post(url, auth, json): 

125 class _PostReturn: 

126 status_code = 400 

127 

128 return _PostReturn() 

129 

130 new_config = config.copy() 

131 new_config["BUG_TOOL_ENABLED"] = True 

132 

133 with patch("couchers.servicers.bugs.config", new_config): 

134 with patch("couchers.servicers.bugs.requests.post", dud_post): 

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

136 res = bugs.ReportBug( 

137 bugs_pb2.ReportBugReq( 

138 subject="subject", 

139 description="description", 

140 results="results", 

141 frontend_version="frontend_version", 

142 user_agent="user_agent", 

143 page="page", 

144 ) 

145 ) 

146 assert e.value.code() == grpc.StatusCode.INTERNAL