Skip to content

Commit

Permalink
Merge 89a3a01 into 0b3ae85
Browse files Browse the repository at this point in the history
  • Loading branch information
patjouk committed Jun 27, 2018
2 parents 0b3ae85 + 89a3a01 commit 7c2a9ae
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 57 deletions.
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):
"""
Mozilla Foundation Users are not deleted
"""

call_command('delete_non_staff')

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

0 comments on commit 7c2a9ae

Please sign in to comment.