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

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import responses
2from urllib.parse import urlencode
3from flask_testing import TestCase
4from webapp.app import create_app
7class GetDistroPageTest(TestCase):
8 snap_payload = {
9 "snap-id": "id",
10 "name": "snapName",
11 "default-track": None,
12 "snap": {
13 "title": "Snap Title",
14 "summary": "This is a summary",
15 "description": "this is a description",
16 "media": [],
17 "license": "license",
18 "publisher": {
19 "display-name": "Toto",
20 "username": "toto",
21 "validation": True,
22 },
23 "categories": [{"name": "test"}],
24 "trending": False,
25 "unlisted": False,
26 "links": {},
27 },
28 "channel-map": [
29 {
30 "channel": {
31 "architecture": "amd64",
32 "name": "stable",
33 "risk": "stable",
34 "track": "latest",
35 "released-at": "2018-09-18T14:45:28.064633+00:00",
36 },
37 "created-at": "2018-09-18T14:45:28.064633+00:00",
38 "version": "1.0",
39 "confinement": "conf",
40 "download": {"size": 100000},
41 }
42 ],
43 }
45 def setUp(self):
46 self.snap_name = "toto"
47 self.api_url = "".join(
48 [
49 "https://api.snapcraft.io/v2/",
50 "snaps/info/",
51 self.snap_name,
52 "?",
53 urlencode(
54 {
55 "fields": ",".join(
56 [
57 "title",
58 "summary",
59 "description",
60 "license",
61 "contact",
62 "website",
63 "publisher",
64 "media",
65 "download",
66 "version",
67 "created-at",
68 "confinement",
69 "categories",
70 "trending",
71 "unlisted",
72 "links",
73 ]
74 )
75 }
76 ),
77 ]
78 )
79 self.featured_snaps_api_url = "".join(
80 [
81 "https://api.snapcraft.io/api/v1/",
82 "snaps/search",
83 "?",
84 urlencode(
85 {
86 "q": "",
87 "size": "13",
88 "page": "1",
89 "scope": "wide",
90 "confinement": "strict,classic",
91 "fields": "package_name,title,summary,"
92 "architecture,media,developer_name,developer_id,"
93 "developer_validation,origin,apps,sections",
94 "arch": "wide",
95 "section": "featured",
96 }
97 ),
98 ]
99 )
100 self.endpoint_url = "/install/" + self.snap_name + "/debian"
102 def create_app(self):
103 app = create_app(testing=True)
104 app.secret_key = "secret_key"
105 app.config["WTF_CSRF_METHODS"] = []
107 return app
109 @responses.activate
110 def test_api_404(self):
111 payload = {"error-list": []}
112 responses.add(
113 responses.Response(
114 method="GET", url=self.api_url, json=payload, status=404
115 )
116 )
118 response = self.client.get(self.endpoint_url)
120 self.assertEqual(len(responses.calls), 1)
121 called = responses.calls[0]
122 self.assertEqual(called.request.url, self.api_url)
124 self.assert404(response)
126 @responses.activate
127 def test_api_500(self):
128 payload = {"error-list": []}
129 responses.add(
130 responses.Response(
131 method="GET", url=self.api_url, json=payload, status=500
132 )
133 )
135 response = self.client.get(self.endpoint_url)
137 self.assertEqual(len(responses.calls), 1)
138 called = responses.calls[0]
139 self.assertEqual(called.request.url, self.api_url)
141 self.assertStatus(response, 502)
143 def test_no_distro_data(self):
144 response = self.client.get("/install/" + self.snap_name + "/noname")
146 self.assert404(response)
148 @responses.activate
149 def test_get_page(self):
150 payload = self.snap_payload
152 responses.add(
153 responses.Response(
154 method="GET", url=self.api_url, json=payload, status=200
155 )
156 )
158 responses.add(
159 responses.Response(
160 method="GET",
161 url=self.featured_snaps_api_url,
162 json={},
163 status=200,
164 )
165 )
167 response = self.client.get(self.endpoint_url)
169 self.assert200(response)
170 self.assert_context("snap_title", "Snap Title")