|
1 | 1 | import pytest
|
2 | 2 | from jwt_auth.models import LSAPIToken, LSTokenBackend
|
3 | 3 | from organizations.models import OrganizationMember
|
| 4 | +from organizations.tests.factories import OrganizationFactory |
4 | 5 | from rest_framework_simplejwt.exceptions import TokenError
|
5 | 6 | from rest_framework_simplejwt.settings import api_settings as simple_jwt_settings
|
6 | 7 | from rest_framework_simplejwt.token_blacklist.models import BlacklistedToken, OutstandingToken
|
| 8 | +from users.models import User |
7 | 9 |
|
8 | 10 | from ..utils import mock_feature_flag
|
9 | 11 | from .utils import create_user_with_token_settings
|
|
12 | 14 | @mock_feature_flag(flag_name='fflag__feature_develop__prompts__dia_1829_jwt_token_auth', value=True)
|
13 | 15 | @pytest.mark.django_db
|
14 | 16 | def test_jwt_settings_permissions():
|
15 |
| - user = create_user_with_token_settings(api_tokens_enabled=True, legacy_api_tokens_enabled=False) |
16 |
| - org = user.active_organization |
17 |
| - OrganizationMember.objects.create( |
18 |
| - user=user, |
19 |
| - organization=org, |
20 |
| - ) |
| 17 | + org = OrganizationFactory() |
| 18 | + user = org.created_by |
21 | 19 |
|
22 | 20 | # Any member should be able to view
|
23 | 21 | assert org.jwt.has_view_permission(user)
|
24 | 22 |
|
25 |
| - # Only owners and administrators can modify |
26 |
| - user.is_owner = True |
27 |
| - user.save() |
| 23 | + # Any LSO member should be able to modify |
| 24 | + # (tests for enterprise handled in enterprise test suite) |
28 | 25 | assert org.jwt.has_modify_permission(user)
|
29 | 26 | assert org.jwt.has_permission(user)
|
30 | 27 |
|
31 |
| - user.is_owner = False |
32 |
| - user.save() |
33 |
| - assert not org.jwt.has_modify_permission(user) |
34 |
| - assert not org.jwt.has_permission(user) |
| 28 | + |
| 29 | +@mock_feature_flag(flag_name='fflag__feature_develop__prompts__dia_1829_jwt_token_auth', value=True) |
| 30 | +@pytest.mark.django_db |
| 31 | +def test_non_owner_user_can_modify_jwt_settings(): |
| 32 | + """Test that a regular non-owner user who is added to an organization can modify JWT settings""" |
| 33 | + org = OrganizationFactory() |
| 34 | + non_owner = User. objects. create( email='[email protected]') |
| 35 | + |
| 36 | + OrganizationMember.objects.create( |
| 37 | + user=non_owner, |
| 38 | + organization=org, |
| 39 | + ) |
| 40 | + non_owner.active_organization = org |
| 41 | + non_owner.save() |
| 42 | + |
| 43 | + assert org.jwt.has_view_permission(non_owner) |
| 44 | + assert org.jwt.has_modify_permission(non_owner) |
| 45 | + assert org.jwt.has_permission(non_owner) |
| 46 | + |
| 47 | + |
| 48 | +@mock_feature_flag(flag_name='fflag__feature_develop__prompts__dia_1829_jwt_token_auth', value=True) |
| 49 | +@pytest.mark.django_db |
| 50 | +def test_user_from_other_org_cannot_access_jwt_settings(): |
| 51 | + """Test that users from other organizations cannot view or modify JWT settings""" |
| 52 | + org1 = OrganizationFactory() |
| 53 | + org1_owner = org1.created_by |
| 54 | + |
| 55 | + org2 = OrganizationFactory() |
| 56 | + org2_owner = org2.created_by |
| 57 | + |
| 58 | + # Verify org1 owner cannot view or modify JWT settings of org2 |
| 59 | + assert not org2.jwt.has_view_permission(org1_owner) |
| 60 | + assert not org2.jwt.has_modify_permission(org1_owner) |
| 61 | + assert not org2.jwt.has_permission(org1_owner) |
| 62 | + |
| 63 | + # Verify org2 owner cannot view or modify JWT settings of org1 |
| 64 | + assert not org1.jwt.has_view_permission(org2_owner) |
| 65 | + assert not org1.jwt.has_modify_permission(org2_owner) |
| 66 | + assert not org1.jwt.has_permission(org2_owner) |
35 | 67 |
|
36 | 68 |
|
37 | 69 | @mock_feature_flag(flag_name='fflag__feature_develop__prompts__dia_1829_jwt_token_auth', value=True)
|
|
0 commit comments