Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete accounts that are not staff #1615

Merged
merged 10 commits into from
Jun 27, 2018
Merged
122 changes: 65 additions & 57 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions network-api/networkapi/management/commands/delete_non_staff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.contrib.auth.models import User, Group
from django.db.models import Q
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = 'Check for accounts created by non-staff people and delete them'

def handle(self, *args, **options):

print("Deleting non staff users")
group_q = Group.objects.all()
non_staff = User.objects.exclude(
Q(email__endswith='@mozillafoundation.org') |
Q(is_staff=True) |
Q(groups__in=group_q)
)

if non_staff:
print('Deleting:', ', '.join([e.username for e in non_staff]))
non_staff.delete()
else:
print('Nothing to delete')
print("Done!")
62 changes: 62 additions & 0 deletions network-api/networkapi/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth.models import User, Group
from django.core.management import call_command
from django.test import TestCase
from unittest.mock import MagicMock
Expand Down Expand Up @@ -28,3 +29,64 @@ def test_no_migrations_missing(self):
Ensure we didn't forget a migration
"""
call_command('makemigrations', interactive=False, dry_run=True, check_changes=True)


class DeleteNonStaffTest(TestCase):

def setUp(self):
User.objects.create(username='Alex'),

def test_non_staff_is_deleted(self):
"""
Simple users are deleted
"""

call_command('delete_non_staff')

self.assertEqual(User.objects.count(), 0)


class IsStaffNotDeletedTest(TestCase):

def setUp(self):
User.objects.create(username='Alex', is_staff=True)

def test_is_staff_not_deleted(self):
"""
Users with 'is_staff' flag at True are not deleted
"""

call_command('delete_non_staff')

self.assertEqual(User.objects.count(), 1)


class InGroupNotDeletedTest(TestCase):

def setUp(self):
group = Group.objects.create(name='TestGroup')
group.user_set.create(username='Alex')

def test_in_group_not_deleted(self):
"""
Users in a group are not deleted
"""

call_command('delete_non_staff')

self.assertEqual(User.objects.count(), 1)


class MozillaFoundationUsersNotDeletedTest(TestCase):

def setUp(self):
User.objects.create(username='Alex', email='alex@mozillafoundation.org')

def test_mozilla_Foundation_users_not_deleted(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you fix the capitalization of this test name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

"""
Mozilla Foundation Users are not deleted
"""

call_command('delete_non_staff')

self.assertEqual(User.objects.count(), 1)