Coverage for tests / publisher / tests_publisher.py: 100%
97 statements
« prev ^ index » next coverage.py v7.13.1, created at 2025-12-29 22:06 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2025-12-29 22:06 +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 "email": "test@example.com",
81 }
82 s["macaroon_root"] = root.serialize()
83 s["macaroon_discharge"] = discharge.serialize()
85 return get_authorization_header(
86 root.serialize(), discharge.serialize()
87 )
89 def test_username_not_logged_in(self):
90 response = self.client.get("/account/username")
91 self.assertEqual(302, response.status_code)
92 self.assertEqual("/login?next=/account/username", response.location)
94 def test_account_not_logged_in(self):
95 response = self.client.get("/account")
96 self.assertEqual(302, response.status_code)
97 self.assertEqual("/login?next=/account", response.location)
99 # /account endpoint
100 # ===
101 @responses.activate
102 def test_account_redirect(self):
103 self._log_in(self.client)
104 response = self.client.get("/account")
105 self.assertEqual(302, response.status_code)
106 self.assertEqual("/snaps", response.location)
108 # /account/username endpoint
109 # ===
110 @responses.activate
111 def test_username_logged_in(self):
112 self._log_in(self.client)
113 response = self.client.get("/account/username")
115 assert response.status_code == 200
116 self.assert_template_used("publisher/username.html")
118 @responses.activate
119 def test_post_username_logged_in(self):
120 responses.add(
121 responses.PATCH,
122 "https://dashboard.snapcraft.io/dev/api/account",
123 json={},
124 status=200,
125 )
127 authorization = self._log_in(self.client)
128 response = self.client.post(
129 "/account/username", data={"username": "toto"}
130 )
132 self.assertEqual(1, len(responses.calls))
133 called = responses.calls[0]
134 self.assertEqual(
135 "https://dashboard.snapcraft.io/dev/api/account",
136 called.request.url,
137 )
138 self.assertEqual(
139 authorization, called.request.headers.get("Authorization")
140 )
141 self.assertEqual(called.response.json(), {})
142 self.assertEqual(b'{"short_namespace": "toto"}', called.request.body)
144 self.assertEqual(302, response.status_code)
145 self.assertEqual("/account/", response.location)
147 @responses.activate
148 def test_post_no_username_logged_in(self):
149 self._log_in(self.client)
150 response = self.client.post("/account/username")
152 self.assertEqual(0, len(responses.calls))
154 self.assertEqual(302, response.status_code)
155 self.assertEqual("/account/username", response.location)
157 @responses.activate
158 def test_post_bad_username_logged_in(self):
159 payload = {
160 "error_list": [
161 {"code": "custom-error", "message": "great message"}
162 ]
163 }
164 responses.add(
165 responses.PATCH,
166 "https://dashboard.snapcraft.io/dev/api/account",
167 json=payload,
168 status=400,
169 )
171 authorization = self._log_in(self.client)
172 response = self.client.post(
173 "/account/username", data={"username": "toto"}
174 )
176 self.assertEqual(1, len(responses.calls))
177 called = responses.calls[0]
178 self.assertEqual(
179 "https://dashboard.snapcraft.io/dev/api/account",
180 called.request.url,
181 )
182 self.assertEqual(
183 authorization, called.request.headers.get("Authorization")
184 )
185 self.assertEqual(b'{"short_namespace": "toto"}', called.request.body)
187 assert response.status_code == 200
188 self.assert_template_used("publisher/username.html")
189 self.assert_context("username", "toto")
190 self.assert_context("error_list", payload["error_list"])