|
1 | 1 | import re |
2 | 2 | from collections.abc import Callable |
3 | 3 |
|
4 | | -from allauth.socialaccount.models import SocialAccount |
5 | 4 | from django.contrib.auth.models import User |
6 | | -from django.test import Client, TestCase |
7 | 5 | from django.urls import reverse |
8 | 6 | from playwright.sync_api import Page, expect |
9 | 7 | from pytest_django.live_server_helper import LiveServer |
10 | 8 |
|
11 | 9 | from shared.listeners.notify_users import create_package_subscription_notifications |
12 | 10 | from shared.models.linkage import CVEDerivationClusterProposal, ProvenanceFlags |
13 | 11 | from shared.models.nix_evaluation import ( |
14 | | - NixChannel, |
15 | 12 | NixDerivation, |
16 | | - NixDerivationMeta, |
17 | | - NixEvaluation, |
18 | 13 | NixMaintainer, |
19 | 14 | ) |
20 | 15 | from webview.models import Notification |
@@ -204,128 +199,21 @@ def test_package_subscription( |
204 | 199 | subscribe.click() |
205 | 200 |
|
206 | 201 |
|
207 | | -class SubscriptionTests(TestCase): |
208 | | - def setUp(self) -> None: |
209 | | - # Create test user with social account |
210 | | - self.user = User.objects.create_user(username="testuser", password="testpass") |
211 | | - self.user.is_staff = True |
212 | | - self.user.save() |
213 | | - |
214 | | - SocialAccount.objects.get_or_create( |
215 | | - user=self.user, |
216 | | - provider="github", |
217 | | - uid="123456", |
218 | | - extra_data={"login": "testuser"}, |
219 | | - ) |
220 | | - |
221 | | - self.client = Client() |
222 | | - self.client.login(username="testuser", password="testpass") |
223 | | - |
224 | | - # Create test NixDerivation data for package validation |
225 | | - self.maintainer = NixMaintainer.objects.create( |
226 | | - github_id=123, |
227 | | - github="testmaintainer", |
228 | | - name="Test Maintainer", |
229 | | - email="test@example.com", |
230 | | - ) |
231 | | - self.meta = NixDerivationMeta.objects.create( |
232 | | - description="Test package", |
233 | | - insecure=False, |
234 | | - available=True, |
235 | | - broken=False, |
236 | | - unfree=False, |
237 | | - unsupported=False, |
238 | | - ) |
239 | | - self.meta.maintainers.add(self.maintainer) |
240 | | - |
241 | | - self.evaluation = NixEvaluation.objects.create( |
242 | | - channel=NixChannel.objects.create( |
243 | | - staging_branch="release-24.05", |
244 | | - channel_branch="nixos-24.05", |
245 | | - head_sha1_commit="deadbeef", |
246 | | - state=NixChannel.ChannelState.STABLE, |
247 | | - release_version="24.05", |
248 | | - repository="https://github.com/NixOS/nixpkgs", |
249 | | - ), |
250 | | - commit_sha1="deadbeef", |
251 | | - state=NixEvaluation.EvaluationState.COMPLETED, |
252 | | - ) |
253 | | - |
254 | | - # Create valid packages that can be subscribed to |
255 | | - self.valid_package1 = NixDerivation.objects.create( |
256 | | - attribute="firefox", |
257 | | - derivation_path="/nix/store/firefox.drv", |
258 | | - name="firefox-120.0", |
259 | | - metadata=self.meta, |
260 | | - system="x86_64-linux", |
261 | | - parent_evaluation=self.evaluation, |
262 | | - ) |
263 | | - |
264 | | - # Create separate metadata for chromium |
265 | | - self.meta2 = NixDerivationMeta.objects.create( |
266 | | - description="Test chromium package", |
267 | | - insecure=False, |
268 | | - available=True, |
269 | | - broken=False, |
270 | | - unfree=False, |
271 | | - unsupported=False, |
272 | | - ) |
273 | | - self.meta2.maintainers.add(self.maintainer) |
274 | | - |
275 | | - self.valid_package2 = NixDerivation.objects.create( |
276 | | - attribute="chromium", |
277 | | - derivation_path="/nix/store/chromium.drv", |
278 | | - name="chromium-119.0", |
279 | | - metadata=self.meta2, |
280 | | - system="x86_64-linux", |
281 | | - parent_evaluation=self.evaluation, |
282 | | - ) |
283 | | - |
284 | | - # Create a maintainer for the test user |
285 | | - self.user_maintainer = NixMaintainer.objects.create( |
286 | | - github_id=123456, # Same as the user's social account uid |
287 | | - github="testuser", # Same as the user's username |
288 | | - name="Test User", |
289 | | - email="testuser@example.com", |
290 | | - ) |
291 | | - |
292 | | - # Create metadata for a package where test user is maintainer |
293 | | - self.meta3 = NixDerivationMeta.objects.create( |
294 | | - description="Test package maintained by test user", |
295 | | - insecure=False, |
296 | | - available=True, |
297 | | - broken=False, |
298 | | - unfree=False, |
299 | | - unsupported=False, |
300 | | - ) |
301 | | - self.meta3.maintainers.add(self.user_maintainer) |
302 | | - |
303 | | - # Create a package where the test user is a maintainer |
304 | | - self.user_maintained_package = NixDerivation.objects.create( |
305 | | - attribute="neovim", |
306 | | - derivation_path="/nix/store/neovim.drv", |
307 | | - name="neovim-0.9.5", |
308 | | - metadata=self.meta3, |
309 | | - system="x86_64-linux", |
310 | | - parent_evaluation=self.evaluation, |
311 | | - ) |
312 | | - |
313 | | - def test_package_subscription_page_shows_invalid_package(self) -> None: |
314 | | - """Test that the package subscription page shows error for invalid packages""" |
315 | | - url = reverse( |
316 | | - "webview:subscriptions:package", kwargs={"package_name": "nonexistent"} |
317 | | - ) |
318 | | - response = self.client.get(url) |
319 | | - |
320 | | - self.assertEqual(response.status_code, 200) |
321 | | - self.assertTemplateUsed(response, "subscriptions/package_subscription.html") |
322 | | - |
323 | | - # Check context |
324 | | - self.assertEqual(response.context["package_name"], "nonexistent") |
325 | | - self.assertFalse(response.context["package_exists"]) |
326 | | - self.assertFalse(response.context["is_subscribed"]) |
327 | | - self.assertIsNotNone(response.context["error_message"]) |
328 | | - self.assertIn("does not exist", response.context["error_message"]) |
| 202 | +def test_package_subscription_invalid_name( |
| 203 | + live_server: LiveServer, |
| 204 | + as_staff: Page, |
| 205 | +) -> None: |
| 206 | + """Test that the package subscription page shows error for invalid packages""" |
| 207 | + url = reverse( |
| 208 | + "webview:subscriptions:package", kwargs={"package_name": "nonexistent"} |
| 209 | + ) |
| 210 | + as_staff.goto(live_server.url + url) |
| 211 | + main = as_staff.locator("main") |
| 212 | + error = main.locator( |
| 213 | + ".error-block", |
| 214 | + has_text="could not be found", |
| 215 | + ) |
| 216 | + expect(error).to_be_visible() |
329 | 217 |
|
330 | 218 |
|
331 | 219 | def test_maintainer_notification_many_packages_in_suggestion( |
|
0 commit comments