Skip to content

Commit

Permalink
Add delete_zentral_user command
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Jul 1, 2024
1 parent 3183870 commit 9acee8e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions server/accounts/management/commands/delete_zentral_user.py
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")
33 changes: 33 additions & 0 deletions tests/server_accounts/test_delete_zentral_user_cmd.py
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())

0 comments on commit 9acee8e

Please sign in to comment.