-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proxy permission and role model (#749)
* new proxy Permission model * Role Model
- Loading branch information
1 parent
d530db1
commit f677506
Showing
6 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Generated by Django 5.0.6 on 2024-07-11 19:06 | ||
|
||
import django.contrib.auth.models | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('auth', '0012_alter_user_first_name_max_length'), | ||
('core', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Permission', | ||
fields=[ | ||
], | ||
options={ | ||
'verbose_name': 'Permission', | ||
'verbose_name_plural': 'Permissions', | ||
'ordering': ['name'], | ||
'proxy': True, | ||
'indexes': [], | ||
'constraints': [], | ||
}, | ||
bases=('auth.permission',), | ||
managers=[ | ||
('objects', django.contrib.auth.models.PermissionManager()), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 5.0.6 on 2024-07-11 19:39 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0002_permission'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Role', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=150, unique=True, verbose_name='name')), | ||
('permissions', models.ManyToManyField(to='core.permission', verbose_name='permissions')), | ||
], | ||
options={ | ||
'verbose_name': 'Role', | ||
'verbose_name_plural': 'Roles', | ||
'ordering': ['name'], | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from apps.core.models import Permission, Role | ||
|
||
|
||
class TestPermissionModel: | ||
mock_app_label = "test_app" | ||
mock_model = "test_model" | ||
|
||
@pytest.fixture | ||
def mock_content_type(self): | ||
return ContentType.objects.create(app_label=self.mock_app_label, model=self.mock_model) | ||
|
||
@pytest.mark.django_db | ||
def test_saves_new_permissions(self, mock_content_type): | ||
permission_name = "test_permission" | ||
permission = Permission.objects.create( | ||
content_type=mock_content_type, content_type_id=1, name=permission_name | ||
) | ||
|
||
saved_permission = Permission.objects.get(name=permission_name) | ||
|
||
assert saved_permission.name == permission_name | ||
assert saved_permission.id == permission.id | ||
|
||
|
||
class TestRoleModel: | ||
@pytest.mark.django_db | ||
def test_role_with_multiple_permissions(self, permission_factory): | ||
permission1 = permission_factory(content_type_id=1) | ||
permission2 = permission_factory(content_type_id=2) | ||
role = Role.objects.create(name="test_role") | ||
role.permissions.add(permission1, permission2) | ||
assert role.permissions.count() == 2 | ||
assert permission1 in role.permissions.all() | ||
assert permission2 in role.permissions.all() | ||
|
||
@pytest.mark.django_db | ||
def test_role_str_representation(self): | ||
role = Role.objects.create(name="test_role") | ||
assert str(role) == "test_role" |