Coverage for tests/store/tests_details.py: 100%

80 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-28 22:05 +0000

1import responses 

2from urllib.parse import urlencode 

3from flask_testing import TestCase 

4from webapp.app import create_app 

5 

6 

7class GetDetailsPageTest(TestCase): 

8 def setUp(self): 

9 self.snap_name = "toto" 

10 self.api_url = "".join( 

11 [ 

12 "https://api.snapcraft.io/v2/", 

13 "snaps/info/", 

14 self.snap_name, 

15 "?", 

16 urlencode( 

17 { 

18 "fields": ",".join( 

19 [ 

20 "title", 

21 "summary", 

22 "description", 

23 "license", 

24 "contact", 

25 "website", 

26 "publisher", 

27 "media", 

28 "download", 

29 "version", 

30 "created-at", 

31 "confinement", 

32 "categories", 

33 "trending", 

34 "unlisted", 

35 "links", 

36 ] 

37 ) 

38 } 

39 ), 

40 ] 

41 ) 

42 self.endpoint_url = "/" + self.snap_name 

43 

44 def create_app(self): 

45 app = create_app(testing=True) 

46 app.secret_key = "secret_key" 

47 app.config["WTF_CSRF_METHODS"] = [] 

48 

49 return app 

50 

51 @responses.activate 

52 def test_api_404(self): 

53 payload = {"error-list": [{"code": "resource-not-found"}]} 

54 responses.add( 

55 responses.Response( 

56 method="GET", url=self.api_url, json=payload, status=404 

57 ) 

58 ) 

59 

60 response = self.client.get(self.endpoint_url) 

61 

62 assert len(responses.calls) == 1 

63 called = responses.calls[0] 

64 assert called.request.url == self.api_url 

65 

66 assert response.status_code == 404 

67 

68 @responses.activate 

69 def test_api_500(self): 

70 payload = {"error-list": []} 

71 responses.add( 

72 responses.Response( 

73 method="GET", url=self.api_url, json=payload, status=500 

74 ) 

75 ) 

76 

77 response = self.client.get(self.endpoint_url) 

78 

79 assert len(responses.calls) == 1 

80 called = responses.calls[0] 

81 assert called.request.url == self.api_url 

82 

83 assert response.status_code == 502 

84 

85 @responses.activate 

86 def test_api_500_no_answer(self): 

87 responses.add( 

88 responses.Response(method="GET", url=self.api_url, status=500) 

89 ) 

90 

91 response = self.client.get(self.endpoint_url) 

92 

93 assert len(responses.calls) == 1 

94 called = responses.calls[0] 

95 assert called.request.url == self.api_url 

96 

97 assert response.status_code == 502 

98 

99 @responses.activate 

100 def test_no_channel_map(self): 

101 payload = { 

102 "snap-id": "id", 

103 "name": "snapName", 

104 "default-track": None, 

105 "snap": { 

106 "title": "Snap Title", 

107 "summary": "This is a summary", 

108 "description": "this is a description", 

109 "media": [], 

110 "license": "license", 

111 "publisher": { 

112 "display-name": "Toto", 

113 "username": "toto", 

114 "validation": True, 

115 }, 

116 "categories": [{"name": "test"}], 

117 "trending": False, 

118 "unlisted": False, 

119 "links": {}, 

120 }, 

121 } 

122 

123 responses.add( 

124 responses.Response( 

125 method="GET", url=self.api_url, json=payload, status=200 

126 ) 

127 ) 

128 

129 response = self.client.get(self.endpoint_url) 

130 

131 assert response.status_code == 404 

132 

133 @responses.activate 

134 def test_user_connected(self): 

135 payload = { 

136 "snap-id": "id", 

137 "name": "toto", 

138 "default-track": None, 

139 "snap": { 

140 "title": "Snap Title", 

141 "summary": "This is a summary", 

142 "description": "this is a description", 

143 "media": [], 

144 "license": "license", 

145 "publisher": { 

146 "display-name": "Toto", 

147 "username": "toto", 

148 "validation": True, 

149 }, 

150 "categories": [{"name": "test"}], 

151 "trending": False, 

152 "unlisted": False, 

153 "links": {}, 

154 }, 

155 "channel-map": [ 

156 { 

157 "channel": { 

158 "architecture": "amd64", 

159 "name": "stable", 

160 "risk": "stable", 

161 "track": "latest", 

162 "released-at": "2018-09-18T14:45:28.064633+00:00", 

163 }, 

164 "created-at": "2018-09-18T14:45:28.064633+00:00", 

165 "version": "1.0", 

166 "confinement": "conf", 

167 "download": {"size": 100000}, 

168 } 

169 ], 

170 } 

171 

172 responses.add( 

173 responses.Response( 

174 method="GET", url=self.api_url, json=payload, status=200 

175 ) 

176 ) 

177 

178 metrics_url = "https://api.snapcraft.io/api/v1/snaps/metrics" 

179 responses.add( 

180 responses.Response( 

181 method="POST", url=metrics_url, json={}, status=200 

182 ) 

183 ) 

184 

185 with self.client.session_transaction() as s: 

186 # make test session 'authenticated' 

187 s["publisher"] = {"nickname": "toto", "fullname": "Totinio"} 

188 s["macaroon_root"] = "test" 

189 s["macaroon_discharge"] = "test" 

190 # mock test user snaps list 

191 s["user_snaps"] = {"toto": {"snap-id": "test"}} 

192 

193 response = self.client.get(self.endpoint_url) 

194 

195 self.assert200(response) 

196 self.assert_context("is_users_snap", True) 

197 

198 @responses.activate 

199 def test_user_not_connected(self): 

200 payload = { 

201 "snap-id": "id", 

202 "name": "snapName", 

203 "default-track": None, 

204 "snap": { 

205 "title": "Snap Title", 

206 "summary": "This is a summary", 

207 "description": "this is a description", 

208 "media": [], 

209 "license": "license", 

210 "publisher": { 

211 "display-name": "Toto", 

212 "username": "toto", 

213 "validation": True, 

214 }, 

215 "categories": [{"name": "test"}], 

216 "trending": False, 

217 "unlisted": False, 

218 "links": {}, 

219 }, 

220 "channel-map": [ 

221 { 

222 "channel": { 

223 "architecture": "amd64", 

224 "name": "stable", 

225 "risk": "stable", 

226 "track": "latest", 

227 "released-at": "2018-09-18T14:45:28.064633+00:00", 

228 }, 

229 "created-at": "2018-09-18T14:45:28.064633+00:00", 

230 "version": "1.0", 

231 "confinement": "conf", 

232 "download": {"size": 100000}, 

233 } 

234 ], 

235 } 

236 

237 responses.add( 

238 responses.Response( 

239 method="GET", url=self.api_url, json=payload, status=200 

240 ) 

241 ) 

242 

243 metrics_url = "https://api.snapcraft.io/api/v1/snaps/metrics" 

244 responses.add( 

245 responses.Response( 

246 method="POST", url=metrics_url, json={}, status=200 

247 ) 

248 ) 

249 

250 response = self.client.get(self.endpoint_url) 

251 

252 assert response.status_code == 200 

253 self.assert_context("is_users_snap", False) 

254 

255 @responses.activate 

256 def test_user_connected_on_not_own_snap(self): 

257 payload = { 

258 "snap-id": "id", 

259 "name": "snapName", 

260 "default-track": None, 

261 "snap": { 

262 "title": "Snap Title", 

263 "summary": "This is a summary", 

264 "description": "this is a description", 

265 "media": [], 

266 "license": "license", 

267 "publisher": { 

268 "display-name": "Toto", 

269 "username": "toto", 

270 "validation": True, 

271 }, 

272 "categories": [{"name": "test"}], 

273 "trending": False, 

274 "unlisted": False, 

275 "links": {}, 

276 }, 

277 "channel-map": [ 

278 { 

279 "channel": { 

280 "architecture": "amd64", 

281 "name": "stable", 

282 "risk": "stable", 

283 "track": "latest", 

284 "released-at": "2018-09-18T14:45:28.064633+00:00", 

285 }, 

286 "created-at": "2018-09-18T14:45:28.064633+00:00", 

287 "version": "1.0", 

288 "confinement": "conf", 

289 "download": {"size": 100000}, 

290 } 

291 ], 

292 } 

293 

294 responses.add( 

295 responses.Response( 

296 method="GET", url=self.api_url, json=payload, status=200 

297 ) 

298 ) 

299 

300 metrics_url = "https://api.snapcraft.io/api/v1/snaps/metrics" 

301 responses.add( 

302 responses.Response( 

303 method="POST", url=metrics_url, json={}, status=200 

304 ) 

305 ) 

306 

307 with self.client.session_transaction() as s: 

308 s["publisher"] = {"nickname": "greg"} 

309 

310 response = self.client.get(self.endpoint_url) 

311 

312 assert response.status_code == 200 

313 self.assert_context("is_users_snap", False)