Merge lp:~wgrant/launchpad/suspend-bot-account into lp:launchpad

Proposed by William Grant
Status: Merged
Merged at revision: 18728
Proposed branch: lp:~wgrant/launchpad/suspend-bot-account
Merge into: lp:launchpad
Diff against target: 103 lines (+89/-0)
3 files modified
lib/lp/registry/scripts/suspendbotaccount.py (+43/-0)
lib/lp/registry/scripts/tests/test_suspendbotaccount.py (+33/-0)
scripts/suspend-bot-account.py (+13/-0)
To merge this branch: bzr merge lp:~wgrant/launchpad/suspend-bot-account
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+349392@code.launchpad.net

Commit message

Add a suspend-bot-account.py script to suspend an account by email address.

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'lib/lp/registry/scripts/suspendbotaccount.py'
2--- lib/lp/registry/scripts/suspendbotaccount.py 1970-01-01 00:00:00 +0000
3+++ lib/lp/registry/scripts/suspendbotaccount.py 2018-07-13 03:44:33 +0000
4@@ -0,0 +1,43 @@
5+# Copyright 2018 Canonical Ltd. This software is licensed under the
6+# GNU Affero General Public License version 3 (see the file LICENSE).
7+
8+"""Suspend a bot account."""
9+
10+from zope.component import getUtility
11+
12+from lp.registry.interfaces.person import IPersonSet
13+from lp.services.identity.interfaces.account import AccountStatus
14+from lp.services.scripts.base import (
15+ LaunchpadScript,
16+ LaunchpadScriptFailure,
17+ )
18+from lp.services.webapp import canonical_url
19+
20+
21+class SuspendBotAccountScript(LaunchpadScript):
22+
23+ description = "Suspend a bot account."
24+ output = None
25+
26+ def add_my_options(self):
27+ self.parser.add_option(
28+ '-e', '--email', metavar='ADDR', action='store',
29+ type='string', dest='email', default='', help='Email address')
30+
31+ def main(self):
32+ emailaddress = unicode(self.options.email)
33+ if not emailaddress:
34+ raise LaunchpadScriptFailure('--email is required')
35+
36+ person = getUtility(IPersonSet).getByEmail(emailaddress)
37+ if person is None:
38+ raise LaunchpadScriptFailure(
39+ 'Account with email address {} does not exist'.format(
40+ emailaddress))
41+
42+ person.account.setStatus(
43+ AccountStatus.SUSPENDED, None,
44+ 'Suspended by suspend-bot-account.py')
45+
46+ self.logger.info('Suspended {}'.format(canonical_url(person)))
47+ self.txn.commit()
48
49=== added file 'lib/lp/registry/scripts/tests/test_suspendbotaccount.py'
50--- lib/lp/registry/scripts/tests/test_suspendbotaccount.py 1970-01-01 00:00:00 +0000
51+++ lib/lp/registry/scripts/tests/test_suspendbotaccount.py 2018-07-13 03:44:33 +0000
52@@ -0,0 +1,33 @@
53+# Copyright 2018 Canonical Ltd. This software is licensed under the
54+# GNU Affero General Public License version 3 (see the file LICENSE).
55+
56+"""Test the suspend-bot-account script."""
57+
58+__metaclass__ = type
59+
60+from lp.registry.scripts.suspendbotaccount import SuspendBotAccountScript
61+from lp.services.identity.interfaces.account import AccountStatus
62+from lp.services.log.logger import DevNullLogger
63+from lp.testing import TestCaseWithFactory
64+from lp.testing.faketransaction import FakeTransaction
65+from lp.testing.layers import ZopelessDatabaseLayer
66+
67+
68+class TestSuspendBotAccount(TestCaseWithFactory):
69+ """Test `suspend-bot-account`."""
70+
71+ layer = ZopelessDatabaseLayer
72+
73+ def makeScript(self, test_args):
74+ script = SuspendBotAccountScript(test_args=test_args)
75+ script.logger = DevNullLogger()
76+ script.txn = FakeTransaction()
77+ return script
78+
79+ def test_suspendbotaccount(self):
80+ bot = self.factory.makePerson(email='webops+bot@canonical.com')
81+ script = self.makeScript(['--email', 'webops+bot@canonical.com'])
82+ script.main()
83+ self.assertEqual(AccountStatus.SUSPENDED, bot.account_status)
84+
85+ self.assertEqual(1, script.txn.commit_count)
86
87=== added file 'scripts/suspend-bot-account.py'
88--- scripts/suspend-bot-account.py 1970-01-01 00:00:00 +0000
89+++ scripts/suspend-bot-account.py 2018-07-13 03:44:33 +0000
90@@ -0,0 +1,13 @@
91+#!/usr/bin/python -S
92+#
93+# Copyright 2018 Canonical Ltd. This software is licensed under the
94+# GNU Affero General Public License version 3 (see the file LICENSE).
95+
96+import _pythonpath
97+
98+from lp.registry.scripts.createbotaccount import SuspendBotAccountScript
99+
100+
101+if __name__ == '__main__':
102+ script = SuspendBotAccountScript('suspend-bot-account', dbuser='launchpad')
103+ script.run()