-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow disabling and deleting users via admin UI. API only disables users. Cognito sync job also considers disabled/enabled users.
- Loading branch information
Showing
7 changed files
with
87 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
from django.contrib import admin | ||
from django.db import models | ||
from django.http import HttpRequest | ||
|
||
from .models import User | ||
|
||
|
||
@admin.register(User) | ||
class UserAdmin(admin.ModelAdmin): # type:ignore[type-arg] | ||
'''Admin View for User''' | ||
list_display = ('provider', 'username', 'deleted_at') | ||
actions = ["make_disabled"] | ||
|
||
@admin.action(description="Disable selected users") | ||
def make_disabled(self, request: HttpRequest, queryset: models.QuerySet[User]) -> None: | ||
for u in queryset: | ||
u.disable() |
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 |
---|---|---|
|
@@ -22,13 +22,13 @@ def setUp(self): | |
"email": "[email protected]", | ||
"provider": provider, | ||
} | ||
User.objects.create(**model_fields) | ||
User.objects_api().create(**model_fields) | ||
|
||
self.client = TestClient(router) | ||
|
||
def test_user_to_response_maps_fields_correctly(self): | ||
|
||
model = User.objects.last() | ||
model = User.objects_api().last() | ||
|
||
actual = user_to_response(model) | ||
|
||
|
@@ -86,7 +86,7 @@ def test_get_users_returns_users_ordered_by_id(self): | |
"email": "[email protected]", | ||
"provider": Provider.objects.last(), | ||
} | ||
User.objects.create(**model_fields) | ||
User.objects_api().create(**model_fields) | ||
|
||
response = self.client.get("users") | ||
|
||
|
@@ -228,7 +228,7 @@ def test_post_users_returns_500_if_cognito_inconsistent(self, create_cognito_use | |
|
||
assert response.status_code == 500 | ||
assert response.data == {'code': 500, 'description': 'Internal Server Error'} | ||
assert User.objects.count() == 1 | ||
assert User.objects_api().count() == 1 | ||
assert create_cognito_user.called | ||
|
||
@patch('access.api.create_cognito_user') | ||
|
@@ -247,7 +247,7 @@ def test_post_users_returns_503_if_cognito_down(self, create_cognito_user): | |
|
||
assert response.status_code == 503 | ||
assert response.data == {'code': 503, 'description': 'Service Unavailable'} | ||
assert User.objects.count() == 1 | ||
assert User.objects_api().count() == 1 | ||
assert create_cognito_user.called | ||
|
||
@patch('access.api.disable_cognito_user') | ||
|
@@ -258,7 +258,7 @@ def test_delete_user_deletes_user(self, disable_cognito_user): | |
|
||
assert response.status_code == 204 | ||
assert response.content == b'' | ||
assert User.objects.count() == 0 | ||
assert User.objects_api().count() == 0 | ||
assert disable_cognito_user.called | ||
|
||
@patch('access.api.disable_cognito_user') | ||
|
@@ -269,7 +269,7 @@ def test_delete_user_returns_404_if_nonexisting(self, disable_cognito_user): | |
|
||
assert response.status_code == 404 | ||
assert response.data == {"code": 404, "description": "Resource not found"} | ||
assert User.objects.count() == 1 | ||
assert User.objects_api().count() == 1 | ||
assert not disable_cognito_user.called | ||
|
||
@patch('access.api.disable_cognito_user') | ||
|
@@ -280,7 +280,7 @@ def test_delete_user_returns_500_if_cognito_inconsistent(self, disable_cognito_u | |
|
||
assert response.status_code == 500 | ||
assert response.data == {"code": 500, "description": "Internal Server Error"} | ||
assert User.objects.count() == 1 | ||
assert User.objects_api().count() == 1 | ||
assert disable_cognito_user.called | ||
|
||
@patch('access.api.disable_cognito_user') | ||
|
@@ -291,5 +291,5 @@ def test_delete_user_returns_503_if_cognito_down(self, disable_cognito_user): | |
|
||
assert response.status_code == 503 | ||
assert response.data == {"code": 503, "description": "Service Unavailable"} | ||
assert User.objects.count() == 1 | ||
assert User.objects_api().count() == 1 | ||
assert disable_cognito_user.called |
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 |
---|---|---|
|
@@ -7,10 +7,15 @@ | |
|
||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
|
||
def cognito_user(username, email): | ||
return {'Username': username, 'Attributes': [{'Name': 'email', 'Value': email}]} | ||
def cognito_user(username, email, enabled=True): | ||
return { | ||
'Username': username, 'Attributes': [{ | ||
'Name': 'email', 'Value': email | ||
}], 'Enabled': enabled | ||
} | ||
|
||
|
||
class CognitoSyncCommandTest(TestCase): | ||
|
@@ -29,13 +34,14 @@ def setUp(self): | |
name_rm="Uffizi federal per l'ambient", | ||
) | ||
|
||
def add_user(self, username, email): | ||
def add_user(self, username, email, deleted_at=None): | ||
User.objects.create( | ||
username=username, | ||
first_name=username, | ||
last_name=username, | ||
email=email, | ||
provider=self.provider | ||
provider=self.provider, | ||
deleted_at=deleted_at | ||
) | ||
|
||
@patch('cognito.management.commands.cognito_sync.Client') | ||
|
@@ -73,6 +79,30 @@ def test_command_updates(self, client): | |
self.assertIn('1 user(s) updated', out.getvalue()) | ||
self.assertIn(call().update_user('1', '[email protected]'), client.mock_calls) | ||
|
||
@patch('cognito.management.commands.cognito_sync.Client') | ||
def test_command_updates_disabled(self, client): | ||
self.add_user('1', '[email protected]', timezone.now()) | ||
client.return_value.list_users.return_value = [cognito_user('1', '[email protected]')] | ||
|
||
out = StringIO() | ||
call_command('cognito_sync', verbosity=2, stdout=out) | ||
|
||
self.assertIn('disabling user 1', out.getvalue()) | ||
self.assertIn('1 user(s) disabled', out.getvalue()) | ||
self.assertIn(call().disable_user('1'), client.mock_calls) | ||
|
||
@patch('cognito.management.commands.cognito_sync.Client') | ||
def test_command_updates_enabled(self, client): | ||
self.add_user('1', '[email protected]') | ||
client.return_value.list_users.return_value = [cognito_user('1', '[email protected]', False)] | ||
|
||
out = StringIO() | ||
call_command('cognito_sync', verbosity=2, stdout=out) | ||
|
||
self.assertIn('enabling user 1', out.getvalue()) | ||
self.assertIn('1 user(s) enabled', out.getvalue()) | ||
self.assertIn(call().enable_user('1'), client.mock_calls) | ||
|
||
@patch('cognito.management.commands.cognito_sync.Client') | ||
def test_command_does_not_updates_if_unchanged(self, client): | ||
self.add_user('1', '[email protected]') | ||
|
This file was deleted.
Oops, something went wrong.