Merge lp:~roadmr/canonical-identity-provider/suspend-user-mgmt-command into lp:canonical-identity-provider/release

Proposed by Daniel Manrique
Status: Merged
Approved by: Daniel Manrique
Approved revision: no longer in the source branch.
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: lp:~roadmr/canonical-identity-provider/suspend-user-mgmt-command
Merge into: lp:canonical-identity-provider/release
Diff against target: 79 lines (+70/-0)
2 files modified
src/identityprovider/management/commands/suspend_account.py (+24/-0)
src/identityprovider/tests/test_command_suspend_account.py (+46/-0)
To merge this branch: bzr merge lp:~roadmr/canonical-identity-provider/suspend-user-mgmt-command
Reviewer Review Type Date Requested Status
Daniel Manrique (community) Approve
Maximiliano Bertacchini Approve
Review via email: mp+349375@code.launchpad.net

Commit message

Implement a suspend_account management command.

Description of the change

Implement a suspend_account management command.

To post a comment you must log in.
Revision history for this message
Maximiliano Bertacchini (maxiberta) wrote :

Looks good to me! (with a question)

review: Approve
Revision history for this message
Daniel Manrique (roadmr) wrote :

yay

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'src/identityprovider/management/commands/suspend_account.py'
--- src/identityprovider/management/commands/suspend_account.py 1970-01-01 00:00:00 +0000
+++ src/identityprovider/management/commands/suspend_account.py 2018-07-11 21:00:51 +0000
@@ -0,0 +1,24 @@
1from django.core.management.base import BaseCommand, CommandError
2
3from identityprovider.models import Account
4from identityprovider.models.const import AccountStatus
5
6
7class Command(BaseCommand):
8
9 help = ("Suspend an account identified by displayname.")
10
11 def add_arguments(self, parser):
12 parser.add_argument('username')
13
14 def handle(self, *args, **options):
15 name = options['username']
16
17 try:
18 account = Account.objects.get(displayname=name)
19 except Account.DoesNotExist:
20 raise CommandError("Account {} not found".format(name))
21 account.status = AccountStatus.SUSPENDED
22 account.save()
23
24 print("Suspended account {}".format(account))
025
=== added file 'src/identityprovider/tests/test_command_suspend_account.py'
--- src/identityprovider/tests/test_command_suspend_account.py 1970-01-01 00:00:00 +0000
+++ src/identityprovider/tests/test_command_suspend_account.py 2018-07-11 21:00:51 +0000
@@ -0,0 +1,46 @@
1from django.core.management import CommandError, call_command
2
3from identityprovider.models import Account
4from identityprovider.models.const import AccountStatus
5from identityprovider.tests.utils import SSOBaseTestCase
6
7
8class SuspendAccountCommandTestCase(SSOBaseTestCase):
9
10 def setUp(self):
11 super(SuspendAccountCommandTestCase, self).setUp()
12 self.test_name = 'foo-user'
13
14 self.account = self.factory.make_account(
15 status=AccountStatus.ACTIVE,
16 email="foo-user@example.com",
17 displayname=self.test_name,
18 email_validated=True)
19
20 def test_missing_arg(self):
21 with self.assertRaises(CommandError) as err:
22 call_command('suspend_account')
23 self.assertEqual("Error: too few arguments", str(err.exception))
24
25 def test_extra_arg(self):
26 with self.assertRaises(CommandError) as err:
27 call_command(
28 'suspend_account', self.test_name, "somethingelse")
29 self.assertEqual("Error: unrecognized arguments: somethingelse",
30 str(err.exception))
31
32 def test_suspend_account(self):
33 call_command('suspend_account', self.test_name)
34
35 self.assertEqual(
36 AccountStatus.SUSPENDED,
37 Account.objects.get(displayname=self.test_name).status)
38
39 def test_suspend_account_not_found(self):
40 bogusname = self.test_name + "bogus"
41
42 with self.assertRaises(CommandError) as err:
43 call_command('suspend_account', bogusname)
44 self.assertEqual(
45 "Account {} not found".format(bogusname),
46 str(err.exception))