-
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.
Merge pull request #38 from geoadmin/feat-PB-1074-user-deactivate
PB-1074: deactivate user
- Loading branch information
Showing
11 changed files
with
244 additions
and
34 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,20 @@ | ||
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 = ["disable"] | ||
|
||
@admin.action(description="Disable selected users") | ||
def disable(self, request: HttpRequest, queryset: models.QuerySet[User]) -> None: | ||
for u in queryset: | ||
u.disable() | ||
|
||
def get_queryset(self, request: HttpRequest) -> models.QuerySet[User]: | ||
return User.all_objects.get_queryset() |
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,19 @@ | ||
# Generated by Django 5.0.9 on 2024-11-11 16:07 | ||
|
||
from django.db import migrations | ||
from django.db import models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('access', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='user', | ||
name='deleted_at', | ||
field=models.DateTimeField(blank=True, null=True, verbose_name='deleted at'), | ||
), | ||
] |
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
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]') | ||
|
Oops, something went wrong.