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

83 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-06-23 04:49 +0000

1from unittest.mock import patch 

2 

3import grpc 

4import pytest 

5from google.protobuf import empty_pb2 

6 

7from couchers.config import config 

8from couchers.crypto import random_hex 

9from proto import bugs_pb2 

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

11 

12 

13@pytest.fixture(autouse=True) 

14def _(testconfig): 

15 pass 

16 

17 

18def test_bugs_disabled(): 

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

20 bugs.ReportBug( 

21 bugs_pb2.ReportBugReq( 

22 subject="subject", 

23 description="description", 

24 results="results", 

25 frontend_version="frontend_version", 

26 user_agent="user_agent", 

27 page="page", 

28 ) 

29 ) 

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

31 

32 

33def test_bugs(db): 

34 with bugs_session() as bugs: 

35 

36 def dud_post(url, auth, json): 

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

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

39 assert json == { 

40 "title": "subject", 

41 "body": ( 

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

43 + config["VERSION"] 

44 + "\nFrontend version: frontend_version\nUser Agent: user_agent\nScreen resolution: 1920x1080\nPage: page\nUser: <not logged in>" 

45 ), 

46 "labels": ["bug tool", "bug: triage needed"], 

47 } 

48 

49 class _PostReturn: 

50 status_code = 201 

51 

52 def json(self): 

53 return {"number": 11} 

54 

55 return _PostReturn() 

56 

57 new_config = config.copy() 

58 new_config["BUG_TOOL_ENABLED"] = True 

59 

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

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

62 res = bugs.ReportBug( 

63 bugs_pb2.ReportBugReq( 

64 subject="subject", 

65 description="description", 

66 results="results", 

67 frontend_version="frontend_version", 

68 user_agent="user_agent", 

69 screen_resolution=bugs_pb2.ScreenResolution(width=1920, height=1080), 

70 page="page", 

71 ) 

72 ) 

73 

74 assert res.bug_id == "#11" 

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

76 

77 

78def test_bugs_with_user(db): 

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

80 

81 with bugs_session(token) as bugs: 

82 

83 def dud_post(url, auth, json): 

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

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

86 assert json == { 

87 "title": "subject", 

88 "body": ( 

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

90 + config["VERSION"] 

91 + "\nFrontend version: frontend_version\nUser Agent: user_agent\nScreen resolution: 390x844\nPage: page\nUser: [@testing_user](http://localhost:3000/user/testing_user) (1)" 

92 ), 

93 "labels": ["bug tool", "bug: triage needed"], 

94 } 

95 

96 class _PostReturn: 

97 status_code = 201 

98 

99 def json(self): 

100 return {"number": 11} 

101 

102 return _PostReturn() 

103 

104 new_config = config.copy() 

105 new_config["BUG_TOOL_ENABLED"] = True 

106 

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

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

109 res = bugs.ReportBug( 

110 bugs_pb2.ReportBugReq( 

111 subject="subject", 

112 description="description", 

113 results="results", 

114 frontend_version="frontend_version", 

115 user_agent="user_agent", 

116 screen_resolution=bugs_pb2.ScreenResolution(width=390, height=844), 

117 page="page", 

118 ) 

119 ) 

120 

121 assert res.bug_id == "#11" 

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

123 

124 

125def test_bugs_fails_on_network_error(db): 

126 with bugs_session() as bugs: 

127 

128 def dud_post(url, auth, json): 

129 class _PostReturn: 

130 status_code = 400 

131 

132 return _PostReturn() 

133 

134 new_config = config.copy() 

135 new_config["BUG_TOOL_ENABLED"] = True 

136 

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

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

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

140 res = bugs.ReportBug( 

141 bugs_pb2.ReportBugReq( 

142 subject="subject", 

143 description="description", 

144 results="results", 

145 frontend_version="frontend_version", 

146 user_agent="user_agent", 

147 page="page", 

148 ) 

149 ) 

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

151 

152 

153def test_version(): 

154 with bugs_session() as bugs: 

155 res = bugs.Version(empty_pb2.Empty()) 

156 assert res.version == "testing_version" 

157 

158 

159def test_status(db): 

160 for _ in range(5): 

161 generate_user() 

162 

163 with bugs_session() as bugs: 

164 nonce = random_hex() 

165 res = bugs.Status(bugs_pb2.StatusReq(nonce=nonce)) 

166 assert res.nonce == nonce 

167 assert res.version == "testing_version" 

168 assert res.coucher_count == 5 

169 

170 

171def test_GetDescriptors(): 

172 with bugs_session() as bugs: 

173 res = bugs.GetDescriptors(empty_pb2.Empty()) 

174 # test we got something roughly binary back 

175 assert res.content_type == "application/octet-stream" 

176 assert len(res.data) > 2**12