|
1 | 1 | import pytest |
2 | 2 | from django.core.exceptions import ValidationError |
3 | | -from django.http import Http404 |
4 | 3 |
|
5 | 4 | from conftest import TestUserTypes |
6 | 5 | from thunderstore.api.cyberstorm.services import team as team_services |
|
13 | 12 | @pytest.mark.django_db |
14 | 13 | def test_disband_team_success(team_owner): |
15 | 14 | team_pk = team_owner.team.pk |
16 | | - team_services.disband_team(team_owner.user, team_owner.team.name) |
| 15 | + team_services.disband_team(agent=team_owner.user, team=team_owner.team) |
17 | 16 | assert not Team.objects.filter(pk=team_pk).exists() |
18 | 17 |
|
19 | 18 |
|
20 | | -@pytest.mark.django_db |
21 | | -def test_disband_team_team_not_found(user): |
22 | | - with pytest.raises(Http404): |
23 | | - team_services.disband_team(user, "NonExistentTeam") |
24 | | - |
25 | | - |
26 | 19 | @pytest.mark.django_db |
27 | 20 | def test_disband_team_user_cannot_access_team(user): |
28 | 21 | team = Team.objects.create(name="TestTeam") |
29 | | - with pytest.raises(ValidationError, match="Must be a member to access team"): |
30 | | - team_services.disband_team(user, team.name) |
| 22 | + error_msg = "Must be a member to access team" |
| 23 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 24 | + team_services.disband_team(agent=user, team=team) |
31 | 25 |
|
32 | 26 |
|
33 | 27 | @pytest.mark.django_db |
34 | 28 | def test_disband_team_user_cannot_disband(team_member): |
35 | | - with pytest.raises(ValidationError, match="Must be an owner to disband team"): |
36 | | - team_services.disband_team(team_member.user, team_member.team.name) |
| 29 | + error_msg = "Must be an owner to disband team" |
| 30 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 31 | + team_services.disband_team(agent=team_member.user, team=team_member.team) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.django_db |
| 35 | +def test_disband_team_with_packages(package, team_owner): |
| 36 | + team = team_owner.team |
| 37 | + package.owner = team |
| 38 | + package.save() |
| 39 | + |
| 40 | + with pytest.raises(ValidationError, match="Unable to disband teams with packages"): |
| 41 | + team_services.disband_team(agent=team_owner.user, team=team) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.django_db |
| 45 | +def test_disband_team_user_is_service_account(service_account, team): |
| 46 | + service_account_user = service_account.user |
| 47 | + error_msg = "Service accounts are unable to perform this action" |
| 48 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 49 | + team_services.disband_team(agent=service_account_user, team=team) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.django_db |
| 53 | +def test_disband_team_user_not_authenticated(team): |
| 54 | + error_msg = "Must be authenticated" |
| 55 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 56 | + team_services.disband_team(agent=None, team=team) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.django_db |
| 60 | +def test_disband_team_user_not_active(user, team): |
| 61 | + user.is_active = False |
| 62 | + user.save() |
| 63 | + |
| 64 | + error_msg = "User has been deactivated" |
| 65 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 66 | + team_services.disband_team(agent=user, team=team) |
37 | 67 |
|
38 | 68 |
|
39 | 69 | @pytest.mark.django_db |
40 | 70 | def test_create_team_name_exists_in_team(user): |
41 | 71 | Team.objects.create(name="existing_team") |
42 | 72 |
|
43 | | - error_msg = "A team with the provided name already exists" |
| 73 | + error_msg = "Team with this name already exists" |
44 | 74 | with pytest.raises(ValidationError, match=error_msg): |
45 | | - team_services.create_team(user, "existing_team") |
| 75 | + team_services.create_team(agent=user, team_name="existing_team") |
46 | 76 |
|
47 | 77 |
|
48 | 78 | @pytest.mark.django_db |
49 | 79 | def test_create_team_name_exists_in_namespace(user): |
50 | 80 | Namespace.objects.create(name="existing_namespace") |
51 | 81 |
|
52 | | - error_msg = "A namespace with the provided name already exists" |
| 82 | + error_msg = "Namespace with this name already exists" |
53 | 83 | with pytest.raises(ValidationError, match=error_msg): |
54 | | - team_services.create_team(user, "existing_namespace") |
| 84 | + team_services.create_team(agent=user, team_name="existing_namespace") |
55 | 85 |
|
56 | 86 |
|
57 | 87 | @pytest.mark.django_db |
58 | 88 | def test_create_team_user_is_service_account(service_account): |
59 | 89 | service_account_user = service_account.user |
60 | 90 |
|
61 | 91 | error_msg = "Service accounts cannot create teams" |
62 | | - with pytest.raises(ValidationError, match=error_msg): |
63 | | - team_services.create_team(service_account_user, "new_team") |
| 92 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 93 | + team_services.create_team(agent=service_account_user, team_name="new_team") |
64 | 94 |
|
65 | 95 |
|
66 | 96 | @pytest.mark.django_db |
67 | 97 | def test_create_team_success(user): |
68 | 98 | team_name = "new_team" |
69 | | - team = team_services.create_team(user, team_name) |
| 99 | + team = team_services.create_team(agent=user, team_name=team_name) |
70 | 100 |
|
71 | 101 | assert Team.objects.filter(name=team_name).exists() |
72 | 102 | assert team.name == team_name |
@@ -330,3 +360,40 @@ def test_update_team_member_cannot_remove_last_owner(team_owner): |
330 | 360 | team_services.update_team_member( |
331 | 361 | team_owner.user, team_owner, TeamMemberRole.member |
332 | 362 | ) |
| 363 | + |
| 364 | + |
| 365 | +@pytest.mark.django_db |
| 366 | +def test_create_team_user_not_authenticated(): |
| 367 | + error_msg = "Must be authenticated to create teams" |
| 368 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 369 | + team_services.create_team(agent=None, team_name="new_team") |
| 370 | + |
| 371 | + |
| 372 | +@pytest.mark.django_db |
| 373 | +def test_create_team_user_not_active(user): |
| 374 | + user.is_active = False |
| 375 | + user.save() |
| 376 | + |
| 377 | + error_msg = "Must be authenticated to create teams" |
| 378 | + with pytest.raises(PermissionValidationError, match=error_msg): |
| 379 | + team_services.create_team(agent=user, team_name="new_team") |
| 380 | + |
| 381 | + |
| 382 | +@pytest.mark.django_db |
| 383 | +@pytest.mark.parametrize( |
| 384 | + ("name1", "name2", "should_fail"), |
| 385 | + ( |
| 386 | + ("Team", "team", True), |
| 387 | + ("Team", "t_eam", False), |
| 388 | + ("team", "teaM", True), |
| 389 | + ("team", "team", True), |
| 390 | + ), |
| 391 | +) |
| 392 | +def test_create_team_name_conflict(user, name1: str, name2: str, should_fail: bool): |
| 393 | + Team.create(name=name1) |
| 394 | + if should_fail: |
| 395 | + with pytest.raises(ValidationError): |
| 396 | + team_services.create_team(agent=user, team_name=name2) |
| 397 | + else: |
| 398 | + team = team_services.create_team(agent=user, team_name=name2) |
| 399 | + assert team.name == name2 |
0 commit comments