← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~cjwatson/launchpad/fix-create-bot-account-tests into lp:launchpad

 

Colin Watson has proposed merging lp:~cjwatson/launchpad/fix-create-bot-account-tests into lp:launchpad.

Commit message:
Rearrange create-bot-account tests to avoid raising SystemExit due to bad option handling.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/fix-create-bot-account-tests/+merge/323742
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~cjwatson/launchpad/fix-create-bot-account-tests into lp:launchpad.
=== modified file 'lib/lp/registry/scripts/tests/test_createbotaccount.py'
--- lib/lp/registry/scripts/tests/test_createbotaccount.py	2017-03-21 07:35:08 +0000
+++ lib/lp/registry/scripts/tests/test_createbotaccount.py	2017-05-08 11:36:04 +0000
@@ -1,65 +1,69 @@
 # Copyright 2017 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
-"""Tests for PersonSet."""
+"""Test the create-bot-account script."""
 
 __metaclass__ = type
 
-
-from mock import MagicMock
 from zope.component import getUtility
+
 from lp.registry.interfaces.person import IPersonSet
 from lp.registry.interfaces.ssh import ISSHKeySet
 from lp.registry.scripts.createbotaccount import CreateBotAccountScript
 from lp.services.identity.interfaces.emailaddress import EmailAddressStatus
+from lp.services.log.logger import DevNullLogger
 from lp.testing import TestCase
+from lp.testing.faketransaction import FakeTransaction
 from lp.testing.layers import ZopelessDatabaseLayer
 
 
-class CreateBotAccountTests(TestCase):
-    """Test `IPersonSet`."""
+class TestCreateBotAccount(TestCase):
+    """Test `create-bot-account`."""
+
     layer = ZopelessDatabaseLayer
 
+    def makeScript(self, test_args):
+        script = CreateBotAccountScript(test_args=test_args)
+        script.logger = DevNullLogger()
+        script.txn = FakeTransaction()
+        return script
+
     def test_createbotaccount(self):
-        script = CreateBotAccountScript()
-
-        class _opt:
-            username = 'botty'
-            openid = 'bottyid'
-            email = ''
-            teams = 'rosetta-admins,simple-team'
-            sshkey = ('ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA9BC4zfVrGsve6zh'
-                      'jOiEftyNMjqV8YMv1lLMpbWqa7Eqr0ZL+oAoJQMq2w8Dk/1hrgJ'
-                      '1pxdwxwQWogDHZTer8YDa89OSBWGenl++s6bk28h/ysZettSS82'
-                      'BrfpoSUc8Cfz2K1SbI9kz5OhmE4nBVsJgsdiHp9WwwQiyRrjfAu'
-                      'NhE= whatever@here.local.')
-
-        script.options = _opt
-        script.logger = MagicMock()
-        script.txn = MagicMock()
+        sshkey_text = (
+            'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA9BC4zfVrGsve6zh'
+            'jOiEftyNMjqV8YMv1lLMpbWqa7Eqr0ZL+oAoJQMq2w8Dk/1hrgJ'
+            '1pxdwxwQWogDHZTer8YDa89OSBWGenl++s6bk28h/ysZettSS82'
+            'BrfpoSUc8Cfz2K1SbI9kz5OhmE4nBVsJgsdiHp9WwwQiyRrjfAu'
+            'NhE= whatever@here.local.')
+        script = self.makeScript([
+            '--username', 'botty',
+            '--openid', 'bottyid',
+            '--teams', 'rosetta-admins,simple-team',
+            '--sshkey', sshkey_text,
+            ])
         script.main()
 
         person_set = getUtility(IPersonSet)
 
         person = person_set.getByName(u'botty')
-        self.assertEqual(person.name, u'botty')
+        self.assertEqual(u'botty', person.name)
         self.assertTrue(person.hide_email_addresses)
         # Bots tend to flood email, so filtering is important.
         self.assertTrue(person.expanded_notification_footers)
 
         account = person.account
         openid = account.openid_identifiers.one()
-        self.assertEqual(openid.identifier, u'bottyid')
+        self.assertEqual(u'bottyid', openid.identifier)
 
         sshkey_set = getUtility(ISSHKeySet)
         self.assertIsNotNone(
-            sshkey_set.getByPersonAndKeyText(person, _opt.sshkey))
+            sshkey_set.getByPersonAndKeyText(person, sshkey_text))
 
         email = person.preferredemail
-        self.assertEqual(email.email, 'webops+botty@xxxxxxxxxxxxx')
-        self.assertEqual(email.status, EmailAddressStatus.PREFERRED)
+        self.assertEqual('webops+botty@xxxxxxxxxxxxx', email.email)
+        self.assertEqual(EmailAddressStatus.PREFERRED, email.status)
 
         self.assertTrue(person.inTeam(person_set.getByName('rosetta-admins')))
         self.assertTrue(person.inTeam(person_set.getByName('simple-team')))
 
-        self.assertTrue(script.txn.commit.called)
+        self.assertEqual(1, script.txn.commit_count)


Follow ups