forked from City-of-Helsinki/kerrokantasi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hearing auth method restrictions (#69)
Changes: - added a model and endpoint for authentication methods - added ability to restrict hearing visibility by given authentication methods
- Loading branch information
Showing
15 changed files
with
534 additions
and
12 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
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,43 @@ | ||
# Generated by Django 2.2.28 on 2023-03-10 08:34 | ||
|
||
from django.conf import settings | ||
import django.core.files.storage | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django.utils.timezone | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('democracy', '0059_add_organization_log'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='AuthMethod', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(db_index=True, default=django.utils.timezone.now, editable=False, verbose_name='time of creation')), | ||
('modified_at', models.DateTimeField(default=django.utils.timezone.now, editable=False, verbose_name='time of last modification')), | ||
('published', models.BooleanField(db_index=True, default=True, verbose_name='public')), | ||
('deleted_at', models.DateTimeField(blank=True, default=None, editable=False, null=True, verbose_name='time of deletion')), | ||
('deleted', models.BooleanField(db_index=True, default=False, editable=False, verbose_name='deleted')), | ||
('name', models.CharField(default='', max_length=200, verbose_name='name')), | ||
('amr', models.CharField(help_text='id of the authentication method', max_length=100, unique=True)), | ||
('created_by', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='authmethod_created', to=settings.AUTH_USER_MODEL, verbose_name='created by')), | ||
('deleted_by', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='authmethod_deleted', to=settings.AUTH_USER_MODEL, verbose_name='deleted by')), | ||
('modified_by', models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='authmethod_modified', to=settings.AUTH_USER_MODEL, verbose_name='last modified by')), | ||
], | ||
options={ | ||
'verbose_name': 'Authentication method', | ||
'verbose_name_plural': 'Authentication methods', | ||
}, | ||
), | ||
migrations.AddField( | ||
model_name='hearing', | ||
name='visible_for_auth_methods', | ||
field=models.ManyToManyField(blank=True, help_text='Only users who use given authentication methods are allowed to see this hearing', related_name='hearings', to='democracy.AuthMethod', verbose_name='Visible for authentication methods'), | ||
), | ||
] |
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,21 @@ | ||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from .base import BaseModel | ||
|
||
|
||
class AuthMethod(BaseModel): | ||
'''Model representing a single authentication method in an authentication service''' | ||
name = models.CharField(verbose_name=_('name'), default='', max_length=200) | ||
amr = models.CharField( | ||
help_text=_('id of the authentication method'), | ||
max_length=100, | ||
unique=True | ||
) | ||
|
||
class Meta: | ||
verbose_name = _('Authentication method') | ||
verbose_name_plural = _('Authentication methods') | ||
|
||
def __str__(self): | ||
return f'{self.name} ({self.amr})' |
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,90 @@ | ||
import pytest | ||
|
||
from democracy.tests.utils import get_data_from_response | ||
|
||
|
||
list_url = '/v1/auth_method/' | ||
|
||
def get_detail_url(auth_method): | ||
return list_url + str(auth_method.pk) + '/' | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize('client', [ | ||
'api_client', | ||
'jane_doe_api_client', | ||
'admin_api_client' | ||
]) | ||
def test_get_auth_methods_list(client, request, auth_method_library_card, auth_method_test_auth): | ||
""" | ||
Tests that auth methods can be fetched via list endpoint by anyone | ||
""" | ||
api_client = request.getfixturevalue(client) | ||
response = api_client.get(list_url) | ||
data = get_data_from_response(response) | ||
ids = [auth_method['id'] for auth_method in data['results']] | ||
assert auth_method_library_card.id in ids | ||
assert auth_method_test_auth.id in ids | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize('client, expected', [ | ||
('api_client', 401), | ||
('jane_doe_api_client', 403), | ||
('admin_api_client', 405) | ||
]) | ||
def test_post_auth_methods_list(client, expected, request): | ||
""" | ||
Tests that auth methods cannot be created via list endpoint by anyone | ||
""" | ||
api_client = request.getfixturevalue(client) | ||
data = {'name': 'test', 'amr': 'some_amr'} | ||
response = api_client.post(list_url, data) | ||
assert response.status_code == expected | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize('client', [ | ||
'api_client', | ||
'jane_doe_api_client', | ||
'admin_api_client' | ||
]) | ||
def test_get_auth_method_detail(client, request, auth_method_library_card): | ||
""" | ||
Tests that an auth method can be fetched via detail endpoint by anyone | ||
""" | ||
api_client = request.getfixturevalue(client) | ||
response = api_client.get(get_detail_url(auth_method_library_card)) | ||
data = get_data_from_response(response) | ||
assert auth_method_library_card.id == data.get('id') | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize('client, expected', [ | ||
('api_client', 401), | ||
('jane_doe_api_client', 403), | ||
('admin_api_client', 405) | ||
]) | ||
def test_update_auth_method_detail(client, expected, request, auth_method_library_card): | ||
""" | ||
Tests that auth methods cannot be updated via detail endpoint by anyone | ||
""" | ||
api_client = request.getfixturevalue(client) | ||
data = {'name': 'test', 'amr': 'some_amr'} | ||
response = api_client.put(get_detail_url(auth_method_library_card), data) | ||
assert response.status_code == expected | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize('client, expected', [ | ||
('api_client', 401), | ||
('jane_doe_api_client', 403), | ||
('admin_api_client', 405) | ||
]) | ||
def test_delete_auth_method_detail(client, expected, request, auth_method_library_card): | ||
""" | ||
Tests that auth methods cannot be deleted via detail endpoint by anyone | ||
""" | ||
api_client = request.getfixturevalue(client) | ||
response = api_client.delete(get_detail_url(auth_method_library_card)) | ||
assert response.status_code == expected |
Oops, something went wrong.