-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
server/accounts/management/commands/delete_zentral_user.py
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 @@ | ||
import sys | ||
from django.core.management.base import BaseCommand | ||
from accounts.models import User | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Delete a Zentral user or service account.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('username') | ||
|
||
def handle(self, *args, **kwargs): | ||
username = kwargs["username"] | ||
deleted_user_count, _ = User.objects.filter(username=username).delete() | ||
if deleted_user_count == 0: | ||
self.stderr.write("0 users deleted") | ||
sys.exit(11) | ||
else: | ||
self.stdout.write(f"{deleted_user_count} user(s) deleted") |
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,33 @@ | ||
from io import StringIO | ||
from unittest.mock import patch | ||
from django.core.management import call_command | ||
from django.test import TestCase | ||
from accounts.models import User | ||
|
||
|
||
class DeleteZentralUserTestCase(TestCase): | ||
def call_command(self, *args, **kwargs): | ||
stdout = StringIO() | ||
stderr = StringIO() | ||
call_command( | ||
"delete_zentral_user", | ||
*args, | ||
stdout=stdout, | ||
stderr=stderr, | ||
**kwargs, | ||
) | ||
return stdout.getvalue(), stderr.getvalue() | ||
|
||
@patch("accounts.management.commands.delete_zentral_user.sys.exit") | ||
def test_delete_user_zero_users(self, sys_exit): | ||
stdout, stderr = self.call_command("yolo") | ||
sys_exit.assert_called_once_with(11) | ||
self.assertEqual(stdout, "") | ||
self.assertEqual(stderr, "0 users deleted\n") | ||
|
||
def test_delete_one_user(self): | ||
User.objects.create_user("yolo", "[email protected]") | ||
stdout, stderr = self.call_command("yolo") | ||
self.assertEqual(stdout, "1 user(s) deleted\n") | ||
self.assertEqual(stderr, "") | ||
self.assertFalse(User.objects.filter(username="yolo").exists()) |