Merge lp:~bloodearnest/u1-test-utils/new-email-parsing into lp:u1-test-utils

Proposed by Simon Davy
Status: Merged
Approved by: Leo Arias
Approved revision: 28
Merged at revision: 29
Proposed branch: lp:~bloodearnest/u1-test-utils/new-email-parsing
Merge into: lp:u1-test-utils
Diff against target: 86 lines (+38/-10)
2 files modified
u1testutils/sst/sso/selftests/unit/test_sso_mail.py (+24/-2)
u1testutils/sst/sso/utils/mail.py (+14/-8)
To merge this branch: bzr merge lp:~bloodearnest/u1-test-utils/new-email-parsing
Reviewer Review Type Date Requested Status
Canonical ISD hackers Pending
Review via email: mp+146414@code.launchpad.net

Commit message

Change SSO email parsing logic to cope with both old and new emails

Description of the change

Change SSO email parsing logic to cope with both old and new emails

To post a comment you must log in.
28. By Simon Davy

clean up

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'u1testutils/sst/sso/selftests/unit/test_sso_mail.py'
--- u1testutils/sst/sso/selftests/unit/test_sso_mail.py 2013-01-30 22:09:48 +0000
+++ u1testutils/sst/sso/selftests/unit/test_sso_mail.py 2013-02-04 15:16:19 +0000
@@ -74,8 +74,8 @@
74 def setUp(self):74 def setUp(self):
75 super(SSOMailTestCase, self).setUp()75 super(SSOMailTestCase, self).setUp()
76 patcher = mock.patch('u1testutils.mail')76 patcher = mock.patch('u1testutils.mail')
77 mock_mail = patcher.start()77 self.mock_mail = patcher.start()
78 mock_mail.get_latest_email_sent_to.return_value = \78 self.mock_mail.get_latest_email_sent_to.return_value = \
79 self.verification_email79 self.verification_email
80 self.addCleanup(patcher.stop)80 self.addCleanup(patcher.stop)
8181
@@ -97,3 +97,25 @@
97 self.assertEquals(97 self.assertEquals(
98 invalidation_link,98 invalidation_link,
99 'https://login.ubuntu.com/invalidate-email/42FcJG/to@example.com')99 'https://login.ubuntu.com/invalidate-email/42FcJG/to@example.com')
100
101 def test_no_code(self):
102 mock_email = mock.MagicMock()
103 mock_email.get_payload.return_value = 'XXXXXX'
104 self.mock_mail.get_latest_email_sent_to.return_value = mock_email
105 with self.assertRaises(AssertionError):
106 mail.get_verification_code_for_address('to@example.com')
107
108 def test_token_regex(self):
109 urls = [
110 r'/%s/+new_account',
111 r'/invalidate-email/%s/a@b.com',
112 r'/confirm-account/%s/a@b.com',
113 r'/token/%s/+resetpassword/a@b.com',
114 r'/token/%s/+newemail/a@b.com',
115 ]
116 token = 'T0Kenz'
117 for pattern in urls:
118 for host in ('http://localhost:8000', 'https://login.ubuntu.com'):
119 url = host + pattern % token
120 m = mail.TOKEN_REGEX.search(url)
121 self.assertEqual(m.group(1), token)
100122
=== modified file 'u1testutils/sst/sso/utils/mail.py'
--- u1testutils/sst/sso/utils/mail.py 2013-01-30 19:00:26 +0000
+++ u1testutils/sst/sso/utils/mail.py 2013-02-04 15:16:19 +0000
@@ -19,6 +19,8 @@
1919
20import u1testutils.mail20import u1testutils.mail
2121
22TOKEN_REGEX = re.compile(r'/([A-Za-z0-9]{6})/', re.S)
23
2224
23def _get_verification_data_for_address(email_address):25def _get_verification_data_for_address(email_address):
24 """A private helper for public helpers below.26 """A private helper for public helpers below.
@@ -35,20 +37,24 @@
35 # line longer than a certain length. Decode now to not have37 # line longer than a certain length. Decode now to not have
36 # to worry about it in the regexen.38 # to worry about it in the regexen.
37 body = quopri.decodestring(email_msg.get_payload())39 body = quopri.decodestring(email_msg.get_payload())
38 match = re.search(40
39 'Here is your confirmation code:(.*)(Enter|If you made)',
40 body, re.S)
41 if match:
42 vcode = match.group(1).strip()
43 else:
44 raise AssertionError("No verification code found in email.")
45 links = map(str.strip, re.findall('^(http.*)$', body, re.MULTILINE))41 links = map(str.strip, re.findall('^(http.*)$', body, re.MULTILINE))
46 if len(links) > 0:42 if len(links) > 0:
47 validation_link = links[0]43 validation_link = links[0]
48 if len(links) > 1:44 if len(links) > 1:
49 invalidation_link = links[1]45 invalidation_link = links[1]
46 match = TOKEN_REGEX.search(links[0])
47 if match:
48 vcode = match.group(1).strip()
50 else:49 else:
51 msg = "No verification link found in email. Email is:\n%r."50 match = re.search(
51 'Here is your confirmation code:(.*)(Enter|If you made)',
52 body, re.S)
53 if match:
54 vcode = match.group(1).strip()
55
56 if not vcode:
57 msg = "No verification code found in email. Email is:\n%r."
52 raise AssertionError(msg % body)58 raise AssertionError(msg % body)
5359
54 return vcode, validation_link, invalidation_link60 return vcode, validation_link, invalidation_link

Subscribers

People subscribed via source and target branches

to all changes: