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
1=== modified file 'u1testutils/sst/sso/selftests/unit/test_sso_mail.py'
2--- u1testutils/sst/sso/selftests/unit/test_sso_mail.py 2013-01-30 22:09:48 +0000
3+++ u1testutils/sst/sso/selftests/unit/test_sso_mail.py 2013-02-04 15:16:19 +0000
4@@ -74,8 +74,8 @@
5 def setUp(self):
6 super(SSOMailTestCase, self).setUp()
7 patcher = mock.patch('u1testutils.mail')
8- mock_mail = patcher.start()
9- mock_mail.get_latest_email_sent_to.return_value = \
10+ self.mock_mail = patcher.start()
11+ self.mock_mail.get_latest_email_sent_to.return_value = \
12 self.verification_email
13 self.addCleanup(patcher.stop)
14
15@@ -97,3 +97,25 @@
16 self.assertEquals(
17 invalidation_link,
18 'https://login.ubuntu.com/invalidate-email/42FcJG/to@example.com')
19+
20+ def test_no_code(self):
21+ mock_email = mock.MagicMock()
22+ mock_email.get_payload.return_value = 'XXXXXX'
23+ self.mock_mail.get_latest_email_sent_to.return_value = mock_email
24+ with self.assertRaises(AssertionError):
25+ mail.get_verification_code_for_address('to@example.com')
26+
27+ def test_token_regex(self):
28+ urls = [
29+ r'/%s/+new_account',
30+ r'/invalidate-email/%s/a@b.com',
31+ r'/confirm-account/%s/a@b.com',
32+ r'/token/%s/+resetpassword/a@b.com',
33+ r'/token/%s/+newemail/a@b.com',
34+ ]
35+ token = 'T0Kenz'
36+ for pattern in urls:
37+ for host in ('http://localhost:8000', 'https://login.ubuntu.com'):
38+ url = host + pattern % token
39+ m = mail.TOKEN_REGEX.search(url)
40+ self.assertEqual(m.group(1), token)
41
42=== modified file 'u1testutils/sst/sso/utils/mail.py'
43--- u1testutils/sst/sso/utils/mail.py 2013-01-30 19:00:26 +0000
44+++ u1testutils/sst/sso/utils/mail.py 2013-02-04 15:16:19 +0000
45@@ -19,6 +19,8 @@
46
47 import u1testutils.mail
48
49+TOKEN_REGEX = re.compile(r'/([A-Za-z0-9]{6})/', re.S)
50+
51
52 def _get_verification_data_for_address(email_address):
53 """A private helper for public helpers below.
54@@ -35,20 +37,24 @@
55 # line longer than a certain length. Decode now to not have
56 # to worry about it in the regexen.
57 body = quopri.decodestring(email_msg.get_payload())
58- match = re.search(
59- 'Here is your confirmation code:(.*)(Enter|If you made)',
60- body, re.S)
61- if match:
62- vcode = match.group(1).strip()
63- else:
64- raise AssertionError("No verification code found in email.")
65+
66 links = map(str.strip, re.findall('^(http.*)$', body, re.MULTILINE))
67 if len(links) > 0:
68 validation_link = links[0]
69 if len(links) > 1:
70 invalidation_link = links[1]
71+ match = TOKEN_REGEX.search(links[0])
72+ if match:
73+ vcode = match.group(1).strip()
74 else:
75- msg = "No verification link found in email. Email is:\n%r."
76+ match = re.search(
77+ 'Here is your confirmation code:(.*)(Enter|If you made)',
78+ body, re.S)
79+ if match:
80+ vcode = match.group(1).strip()
81+
82+ if not vcode:
83+ msg = "No verification code found in email. Email is:\n%r."
84 raise AssertionError(msg % body)
85
86 return vcode, validation_link, invalidation_link

Subscribers

People subscribed via source and target branches

to all changes: