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

84 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-02 20:20 +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 couchers.proto import bugs_pb2 

10from tests.fixtures.db import generate_user 

11from tests.fixtures.sessions import bugs_session 

12 

13 

14@pytest.fixture(autouse=True) 

15def _(testconfig): 

16 pass 

17 

18 

19def test_bugs_disabled(): 

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

21 bugs.ReportBug( 

22 bugs_pb2.ReportBugReq( 

23 subject="subject", 

24 description="description", 

25 results="results", 

26 frontend_version="frontend_version", 

27 user_agent="user_agent", 

28 page="page", 

29 ) 

30 ) 

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

32 

33 

34def test_bugs(db): 

35 with bugs_session() as bugs: 

36 

37 def dud_post(url, auth, json): 

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

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

40 assert json == { 

41 "title": "subject", 

42 "body": ( 

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

44 + config["VERSION"] 

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

46 ), 

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

48 } 

49 

50 class _PostReturn: 

51 status_code = 201 

52 

53 def json(self): 

54 return {"number": 11} 

55 

56 return _PostReturn() 

57 

58 new_config = config.copy() 

59 new_config["BUG_TOOL_ENABLED"] = True 

60 

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

62 with patch("couchers.servicers.bugs.requests.post", dud_post): 62 ↛ anywhereline 62 didn't jump anywhere: it always raised an exception.

63 res = bugs.ReportBug( 

64 bugs_pb2.ReportBugReq( 

65 subject="subject", 

66 description="description", 

67 results="results", 

68 frontend_version="frontend_version", 

69 user_agent="user_agent", 

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

71 page="page", 

72 ) 

73 ) 

74 

75 assert res.bug_id == "#11" 

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

77 

78 

79def test_bugs_with_user(db): 

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

81 

82 with bugs_session(token) as bugs: 

83 

84 def dud_post(url, auth, json): 

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

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

87 assert json == { 

88 "title": "subject", 

89 "body": ( 

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

91 + config["VERSION"] 

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

93 ), 

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

95 } 

96 

97 class _PostReturn: 

98 status_code = 201 

99 

100 def json(self): 

101 return {"number": 11} 

102 

103 return _PostReturn() 

104 

105 new_config = config.copy() 

106 new_config["BUG_TOOL_ENABLED"] = True 

107 

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

109 with patch("couchers.servicers.bugs.requests.post", dud_post): 109 ↛ anywhereline 109 didn't jump anywhere: it always raised an exception.

110 res = bugs.ReportBug( 

111 bugs_pb2.ReportBugReq( 

112 subject="subject", 

113 description="description", 

114 results="results", 

115 frontend_version="frontend_version", 

116 user_agent="user_agent", 

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

118 page="page", 

119 ) 

120 ) 

121 

122 assert res.bug_id == "#11" 

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

124 

125 

126def test_bugs_fails_on_network_error(db): 

127 with bugs_session() as bugs: 

128 

129 def dud_post(url, auth, json): 

130 class _PostReturn: 

131 status_code = 400 

132 

133 return _PostReturn() 

134 

135 new_config = config.copy() 

136 new_config["BUG_TOOL_ENABLED"] = True 

137 

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

139 with patch("couchers.servicers.bugs.requests.post", dud_post): 139 ↛ anywhereline 139 didn't jump anywhere: it always raised an exception.

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

141 res = bugs.ReportBug( 

142 bugs_pb2.ReportBugReq( 

143 subject="subject", 

144 description="description", 

145 results="results", 

146 frontend_version="frontend_version", 

147 user_agent="user_agent", 

148 page="page", 

149 ) 

150 ) 

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

152 

153 

154def test_version(): 

155 with bugs_session() as bugs: 

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

157 assert res.version == "testing_version" 

158 

159 

160def test_status(db): 

161 for _ in range(5): 

162 generate_user() 

163 

164 with bugs_session() as bugs: 

165 nonce = random_hex() 

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

167 assert res.nonce == nonce 

168 assert res.version == "testing_version" 

169 assert res.coucher_count == 5 

170 

171 

172def test_GetDescriptors(): 

173 with bugs_session() as bugs: 

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

175 # test we got something roughly binary back 

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

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