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

83 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-22 17:19 +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\nPage: page\nUser: <not logged in>" 

45 ), 

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

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 page="page", 

70 ) 

71 ) 

72 

73 assert res.bug_id == "#11" 

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

75 

76 

77def test_bugs_with_user(db): 

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

79 

80 with bugs_session(token) as bugs: 

81 

82 def dud_post(url, auth, json): 

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

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

85 assert json == { 

86 "title": "subject", 

87 "body": ( 

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

89 + config["VERSION"] 

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

91 ), 

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

93 } 

94 

95 class _PostReturn: 

96 status_code = 201 

97 

98 def json(self): 

99 return {"number": 11} 

100 

101 return _PostReturn() 

102 

103 new_config = config.copy() 

104 new_config["BUG_TOOL_ENABLED"] = True 

105 

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

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

108 res = bugs.ReportBug( 

109 bugs_pb2.ReportBugReq( 

110 subject="subject", 

111 description="description", 

112 results="results", 

113 frontend_version="frontend_version", 

114 user_agent="user_agent", 

115 page="page", 

116 ) 

117 ) 

118 

119 assert res.bug_id == "#11" 

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

121 

122 

123def test_bugs_fails_on_network_error(db): 

124 with bugs_session() as bugs: 

125 

126 def dud_post(url, auth, json): 

127 class _PostReturn: 

128 status_code = 400 

129 

130 return _PostReturn() 

131 

132 new_config = config.copy() 

133 new_config["BUG_TOOL_ENABLED"] = True 

134 

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

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

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

138 res = bugs.ReportBug( 

139 bugs_pb2.ReportBugReq( 

140 subject="subject", 

141 description="description", 

142 results="results", 

143 frontend_version="frontend_version", 

144 user_agent="user_agent", 

145 page="page", 

146 ) 

147 ) 

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

149 

150 

151def test_version(): 

152 with bugs_session() as bugs: 

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

154 assert res.version == "testing_version" 

155 

156 

157def test_status(db): 

158 for _ in range(5): 

159 generate_user() 

160 

161 with bugs_session() as bugs: 

162 nonce = random_hex() 

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

164 assert res.nonce == nonce 

165 assert res.version == "testing_version" 

166 assert res.coucher_count == 5 

167 

168 

169def test_GetDescriptors(): 

170 with bugs_session() as bugs: 

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

172 # test we got something roughly binary back 

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

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