-
Notifications
You must be signed in to change notification settings - Fork 3
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 #989 from uktrade/release
Prod Release
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 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,27 @@ | ||
import sys | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management import BaseCommand, call_command | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Purge admin users' | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.UserModel = get_user_model() | ||
|
||
def handle(self, *args, **options): | ||
proceed = input('This operation will purge the users table. Are you sure? [y/N]: ') or 'n' | ||
if proceed[0].lower() == 'y': | ||
self.UserModel.objects.all().delete() | ||
else: | ||
self.stderr.write('Operation cancelled.') | ||
sys.exit(1) | ||
|
||
create_superuser = input('Create superuser? [y/N]: ') or 'n' | ||
|
||
if create_superuser[0].lower() == 'y': | ||
call_command('createsuperuser') | ||
|
||
self.stdout.write(self.style.SUCCESS('All done, bye!')) |
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,29 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from django.contrib.auth import get_user_model | ||
from django.core.management import call_command | ||
|
||
UserModel = get_user_model() | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch('core.management.commands.purge_admin_users.input', return_value='n') | ||
def test_purge_admin_users_cancel_operation(mock_input): | ||
with pytest.raises(SystemExit): | ||
call_command('purge_admin_users') | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch('django.contrib.auth.management.commands.createsuperuser.Command') | ||
@mock.patch('core.management.commands.purge_admin_users.input', return_value='y') | ||
def test_purge_admin_users(mock_input, mock_createsuperuser): | ||
UserModel.objects.create_user('alice', '[email protected]', 'password') | ||
UserModel.objects.create_user('bob', '[email protected]', 'password') | ||
|
||
assert UserModel.objects.all().count() == 2 | ||
|
||
call_command('purge_admin_users') | ||
|
||
assert mock_createsuperuser.call_count == 1 | ||
assert UserModel.objects.all().count() == 0 |