Skip to content

Commit edc0568

Browse files
committed
WIP - 404 error on details endpoint
1 parent 2224ece commit edc0568

3 files changed

Lines changed: 107 additions & 3 deletions

File tree

tests/store/tests_details.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ def create_app(self):
6060

6161
return app
6262

63+
def assert_not_in_context(self, name):
64+
try:
65+
self.get_context_variable(name)
66+
except Exception:
67+
# flask-testing throws exception if context doesn't have "name"
68+
# that's what we expect so we just return and let the test pass
69+
return
70+
# If we reach this point it means the variable IS in context
71+
self.fail(f"Context variable exists: {name}")
72+
6373
@responses.activate
6474
def test_api_404(self):
6575
payload = {"error-list": [{"code": "resource-not-found"}]}
@@ -77,6 +87,86 @@ def test_api_404(self):
7787

7888
assert response.status_code == 404
7989

90+
@responses.activate
91+
def test_extra_details_404(self):
92+
payload = {
93+
"snap-id": "id",
94+
"name": "toto",
95+
"default-track": None,
96+
"snap": {
97+
"title": "Snap Title",
98+
"summary": "This is a summary",
99+
"description": "this is a description",
100+
"media": [],
101+
"license": "license",
102+
"publisher": {
103+
"display-name": "Toto",
104+
"username": "toto",
105+
"validation": True,
106+
},
107+
"categories": [{"name": "test"}],
108+
"trending": False,
109+
"unlisted": False,
110+
"links": {},
111+
},
112+
"channel-map": [
113+
{
114+
"channel": {
115+
"architecture": "amd64",
116+
"name": "stable",
117+
"risk": "stable",
118+
"track": "latest",
119+
"released-at": "2018-09-18T14:45:28.064633+00:00",
120+
},
121+
"created-at": "2018-09-18T14:45:28.064633+00:00",
122+
"version": "1.0",
123+
"confinement": "conf",
124+
"download": {"size": 100000},
125+
}
126+
],
127+
}
128+
extra_details_payload = {
129+
"error_list": [
130+
{
131+
"code": "resource-not-found",
132+
"message": "No snap named 'toto' found in series '16'.",
133+
}
134+
],
135+
"errors": ["No snap named 'toto' found in series '16'."],
136+
"result": "error",
137+
}
138+
139+
responses.add(
140+
responses.Response(
141+
method="GET", url=self.api_url, json=payload, status=200
142+
)
143+
)
144+
responses.add(
145+
responses.Response(
146+
method="GET",
147+
url=self.api_url_details,
148+
json=extra_details_payload,
149+
status=404,
150+
)
151+
)
152+
metrics_url = "https://api.snapcraft.io/api/v1/snaps/metrics"
153+
responses.add(
154+
responses.Response(
155+
method="POST", url=metrics_url, json={}, status=200
156+
)
157+
)
158+
159+
response = self.client.get(self.endpoint_url)
160+
161+
assert len(responses.calls) == 3
162+
assert responses.calls[0].request.url == self.api_url
163+
assert responses.calls[1].request.url == self.api_url_details
164+
assert responses.calls[2].request.url == metrics_url
165+
166+
self.assert200(response)
167+
self.assert_not_in_context("aliases")
168+
self.assert_context("snap-id", "id")
169+
80170
@responses.activate
81171
def test_api_500(self):
82172
payload = {"error-list": []}

webapp/api/requests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def __init__(self, *args, **kwargs):
2929

3030
def request(self, method, url, timeout=12, **kwargs):
3131
try:
32+
print("Sending request to Store API")
33+
print(url)
34+
print(self.headers)
35+
print(self.cookies)
3236
return super().request(
3337
method=method, url=url, timeout=timeout, **kwargs
3438
)

webapp/store/snap_details_views.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import flask
22
from flask import Response
3+
import requests
34

45
import humanize
56
import os
@@ -212,9 +213,18 @@ def snap_details(snap_name):
212213
status_code = 200
213214

214215
context = _get_context_snap_details(snap_name)
215-
extra_details = device_gateway.get_snap_details(
216-
snap_name, fields=FIELDS_EXTRA_DETAILS
217-
)
216+
print(f"DEBUGGING: {snap_name}")
217+
try:
218+
extra_details = device_gateway.get_snap_details(
219+
snap_name, fields=FIELDS_EXTRA_DETAILS
220+
)
221+
except Exception as e:
222+
print(e)
223+
response = requests.get(
224+
f"https://api.snapcraft.io/api/v1/snaps/details/{snap_name}?fields=aliases",
225+
headers={"X-Ubuntu-Series": "16"})
226+
print(response.status_code)
227+
print(response.text)
218228

219229
if extra_details and extra_details["aliases"]:
220230
context["aliases"] = [

0 commit comments

Comments
 (0)