Skip to content

Commit

Permalink
Merge pull request #989 from uktrade/release
Browse files Browse the repository at this point in the history
Prod Release
  • Loading branch information
webbyfox authored Nov 2, 2021
2 parents 18bd922 + bb91ff5 commit e779f5a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Pre-release
### Implemented enhancements
- GP2-3387 - Management command to purge admin users
- NOTICKET: Added GreatMedia to d-cms
- GP2-3740: Python 3.9 upgrade

Expand Down
27 changes: 27 additions & 0 deletions core/management/commands/purge_admin_users.py
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!'))
29 changes: 29 additions & 0 deletions tests/core/test_purge_admin_users.py
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

0 comments on commit e779f5a

Please sign in to comment.