Coverage for tests/publisher/tests_publisher.py: 100%
97 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 22:05 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 22:05 +0000
1from requests.exceptions import ConnectionError
3import pymacaroons
4import responses
5from flask_testing import TestCase
6from tests.publisher.endpoint_testing import BaseTestCases
7from webapp.app import create_app
8from webapp.authentication import get_authorization_header
10# Make sure tests fail on stray responses.
11responses.mock.assert_all_requests_are_fired = True
14class TestCache(BaseTestCases.EndpointLoggedInErrorHandling):
15 def setUp(self):
16 api_url = "https://dashboard.snapcraft.io/dev/api/account"
17 endpoint_url = "/account/publisher"
19 super().setUp(
20 snap_name=None,
21 endpoint_url=endpoint_url,
22 method_endpoint="GET",
23 api_url=api_url,
24 method_api="GET",
25 )
27 @responses.activate
28 def test_cache_disabled(self):
29 responses.add(
30 responses.GET,
31 url="https://066-eov-335.mktorest.com/identity/oauth/token"
32 "?grant_type=client_credentials&client_id=fake_id&"
33 "client_secret=fake_secret",
34 json={},
35 status=200,
36 )
37 responses.add(responses.GET, self.api_url, json={}, status=200)
39 response = self.client.get(self.endpoint_url)
40 self.assertEqual(200, response.status_code)
42 responses.replace(responses.GET, self.api_url, body=ConnectionError())
43 response = self.client.get(self.endpoint_url)
44 self.assertEqual(response.status_code, 502)
47class PublisherPage(TestCase):
48 def create_app(self):
49 app = create_app(testing=True)
50 app.secret_key = "secret_key"
51 app.config["WTF_CSRF_METHODS"] = []
53 return app
55 def test_account(self):
56 local_redirect = self.client.get("/account")
57 redirect_url = "/login?next=/account"
58 assert local_redirect.status_code == 302
59 assert local_redirect.headers.get("Location") == redirect_url
61 def _log_in(self, client):
62 """Emulates test client login in the store.
64 Fill current session with `openid`, `macaroon_root` and
65 `macaroon_discharge`.
67 Return the expected `Authorization` header for further verification in
68 API requests.
69 """
70 # Basic root/discharge macaroons pair.
71 root = pymacaroons.Macaroon("test", "testing", "a_key")
72 root.add_third_party_caveat("3rd", "a_caveat-key", "a_ident")
73 discharge = pymacaroons.Macaroon("3rd", "a_ident", "a_caveat_key")
75 with client.session_transaction() as s:
76 s["publisher"] = {
77 "image": None,
78 "nickname": "Toto",
79 "fullname": "El Toto",
80 }
81 s["macaroon_root"] = root.serialize()
82 s["macaroon_discharge"] = discharge.serialize()
84 return get_authorization_header(
85 root.serialize(), discharge.serialize()
86 )
88 def test_username_not_logged_in(self):
89 response = self.client.get("/account/username")
90 self.assertEqual(302, response.status_code)
91 self.assertEqual("/login?next=/account/username", response.location)
93 def test_account_not_logged_in(self):
94 response = self.client.get("/account")
95 self.assertEqual(302, response.status_code)
96 self.assertEqual("/login?next=/account", response.location)
98 # /account endpoint
99 # ===
100 @responses.activate
101 def test_account_redirect(self):
102 self._log_in(self.client)
103 response = self.client.get("/account")
104 self.assertEqual(302, response.status_code)
105 self.assertEqual("/snaps", response.location)
107 # /account/username endpoint
108 # ===
109 @responses.activate
110 def test_username_logged_in(self):
111 self._log_in(self.client)
112 response = self.client.get("/account/username")
114 assert response.status_code == 200
115 self.assert_template_used("publisher/username.html")
117 @responses.activate
118 def test_post_username_logged_in(self):
119 responses.add(
120 responses.PATCH,
121 "https://dashboard.snapcraft.io/dev/api/account",
122 json={},
123 status=200,
124 )
126 authorization = self._log_in(self.client)
127 response = self.client.post(
128 "/account/username", data={"username": "toto"}
129 )
131 self.assertEqual(1, len(responses.calls))
132 called = responses.calls[0]
133 self.assertEqual(
134 "https://dashboard.snapcraft.io/dev/api/account",
135 called.request.url,
136 )
137 self.assertEqual(
138 authorization, called.request.headers.get("Authorization")
139 )
140 self.assertEqual(called.response.json(), {})
141 self.assertEqual(b'{"short_namespace": "toto"}', called.request.body)
143 self.assertEqual(302, response.status_code)
144 self.assertEqual("/account/", response.location)
146 @responses.activate
147 def test_post_no_username_logged_in(self):
148 self._log_in(self.client)
149 response = self.client.post("/account/username")
151 self.assertEqual(0, len(responses.calls))
153 self.assertEqual(302, response.status_code)
154 self.assertEqual("/account/username", response.location)
156 @responses.activate
157 def test_post_bad_username_logged_in(self):
158 payload = {
159 "error_list": [
160 {"code": "custom-error", "message": "great message"}
161 ]
162 }
163 responses.add(
164 responses.PATCH,
165 "https://dashboard.snapcraft.io/dev/api/account",
166 json=payload,
167 status=400,
168 )
170 authorization = self._log_in(self.client)
171 response = self.client.post(
172 "/account/username", data={"username": "toto"}
173 )
175 self.assertEqual(1, len(responses.calls))
176 called = responses.calls[0]
177 self.assertEqual(
178 "https://dashboard.snapcraft.io/dev/api/account",
179 called.request.url,
180 )
181 self.assertEqual(
182 authorization, called.request.headers.get("Authorization")
183 )
184 self.assertEqual(b'{"short_namespace": "toto"}', called.request.body)
186 assert response.status_code == 200
187 self.assert_template_used("publisher/username.html")
188 self.assert_context("username", "toto")
189 self.assert_context("error_list", payload["error_list"])