Merge lp:~elopio/u1-test-utils/rename_mail into lp:u1-test-utils

Proposed by Leo Arias
Status: Merged
Merged at revision: 19
Proposed branch: lp:~elopio/u1-test-utils/rename_mail
Merge into: lp:u1-test-utils
Diff against target: 317 lines (+206/-18)
8 files modified
fabtasks/development.py (+4/-0)
requirements.txt (+2/-0)
u1testutils/mail.py (+17/-1)
u1testutils/selftests/django_project/settings.py (+24/-0)
u1testutils/selftests/unit/test_mail.py (+59/-0)
u1testutils/sst/sso/selftests/unit/test_data.py (+6/-15)
u1testutils/sst/sso/selftests/unit/test_sso_mail.py (+92/-0)
u1testutils/sst/sso/utils/mail.py (+2/-2)
To merge this branch: bzr merge lp:~elopio/u1-test-utils/rename_mail
Reviewer Review Type Date Requested Status
Simon Davy (community) Approve
Review via email: mp+144047@code.launchpad.net

Commit message

Added tests for the mail utilities.

Description of the change

Added tests for the mail utilities.

To post a comment you must log in.
lp:~elopio/u1-test-utils/rename_mail updated
23. By Leo Arias

Added the subject in the verification.

24. By Leo Arias

Moved the localmay to class methods and wait for it to start.

Revision history for this message
Simon Davy (bloodearnest) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'fabtasks/development.py'
2--- fabtasks/development.py 2013-01-16 15:16:50 +0000
3+++ fabtasks/development.py 2013-01-21 16:21:20 +0000
4@@ -14,6 +14,7 @@
5 # You should have received a copy of the GNU General Public License along
6 # with this program. If not, see <http://www.gnu.org/licenses/>.
7
8+import os
9 import unittest
10
11
12@@ -25,6 +26,8 @@
13 'static' and 'unit'. If none are supplied, all the known ones are run.
14
15 """
16+ os.environ['DJANGO_SETTINGS_MODULE'] = \
17+ 'u1testutils.selftests.django_project.settings'
18 if suites is None:
19 suites = ['static', 'unit']
20 suite = unittest.TestSuite()
21@@ -37,6 +40,7 @@
22 if 'static' in suites:
23 module_paths.append('u1testutils/selftests/static')
24 if 'unit' in suites:
25+ module_paths.append('u1testutils/selftests/unit')
26 module_paths.append('u1testutils/sst/sso/selftests/unit')
27 for mp in module_paths:
28 suite.addTest(unittest.TestLoader().discover(mp))
29
30=== modified file 'requirements.txt'
31--- requirements.txt 2013-01-16 02:55:27 +0000
32+++ requirements.txt 2013-01-21 16:21:20 +0000
33@@ -3,3 +3,5 @@
34 pep8
35 pyflakes
36 sst
37+Twisted
38+bzr+ssh://bazaar.launchpad.net/~bloodearnest/+junk/test-email-server/
39
40=== renamed file 'u1testutils/sst/mail.py' => 'u1testutils/mail.py'
41--- u1testutils/sst/mail.py 2012-12-04 19:47:27 +0000
42+++ u1testutils/mail.py 2013-01-21 16:21:20 +0000
43@@ -1,4 +1,20 @@
44-#!/usr/bin/env python
45+# -*- coding: utf-8 -*-
46+
47+# Copyright 2013 Canonical Ltd.
48+#
49+# This program is free software: you can redistribute it and/or modify it
50+# under the terms of the GNU Lesser General Public License version 3, as
51+# published by the Free Software Foundation.
52+#
53+# This program is distributed in the hope that it will be useful, but
54+# WITHOUT ANY WARRANTY; without even the implied warranties of
55+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
56+# PURPOSE. See the GNU Lesser General Public License for more details.
57+#
58+# You should have received a copy of the GNU General Public License along
59+# with this program. If not, see <http://www.gnu.org/licenses/
60+
61+from __future__ import absolute_import
62
63 import imaplib
64 import time
65
66=== added directory 'u1testutils/selftests/django_project'
67=== added file 'u1testutils/selftests/django_project/__init__.py'
68=== added file 'u1testutils/selftests/django_project/settings.py'
69--- u1testutils/selftests/django_project/settings.py 1970-01-01 00:00:00 +0000
70+++ u1testutils/selftests/django_project/settings.py 2013-01-21 16:21:20 +0000
71@@ -0,0 +1,24 @@
72+# -*- coding: utf-8 -*-
73+
74+# Copyright 2013 Canonical Ltd.
75+#
76+# This program is free software: you can redistribute it and/or modify it
77+# under the terms of the GNU Lesser General Public License version 3, as
78+# published by the Free Software Foundation.
79+#
80+# This program is distributed in the hope that it will be useful, but
81+# WITHOUT ANY WARRANTY; without even the implied warranties of
82+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
83+# PURPOSE. See the GNU Lesser General Public License for more details.
84+#
85+# You should have received a copy of the GNU General Public License along
86+# with this program. If not, see <http://www.gnu.org/licenses/
87+
88+IMAP_USERNAME = 'test'
89+IMAP_PASSWORD = 'test'
90+IMAP_USE_SSL = False
91+IMAP_SERVER = 'localhost'
92+IMAP_PORT = 2143
93+SSO_TEST_ACCOUNT_FULL_NAME = 'Test full name'
94+SSO_TEST_ACCOUNT_EMAIL = 'email@test.com'
95+SSO_TEST_ACCOUNT_PASSWORD = '7357 ¶@55\^/0rD'
96
97=== added directory 'u1testutils/selftests/unit'
98=== added file 'u1testutils/selftests/unit/__init__.py'
99=== added file 'u1testutils/selftests/unit/test_mail.py'
100--- u1testutils/selftests/unit/test_mail.py 1970-01-01 00:00:00 +0000
101+++ u1testutils/selftests/unit/test_mail.py 2013-01-21 16:21:20 +0000
102@@ -0,0 +1,59 @@
103+# -*- coding: utf-8 -*-
104+
105+# Copyright 2013 Canonical Ltd.
106+#
107+# This program is free software: you can redistribute it and/or modify it
108+# under the terms of the GNU Lesser General Public License version 3, as
109+# published by the Free Software Foundation.
110+#
111+# This program is distributed in the hope that it will be useful, but
112+# WITHOUT ANY WARRANTY; without even the implied warranties of
113+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
114+# PURPOSE. See the GNU Lesser General Public License for more details.
115+#
116+# You should have received a copy of the GNU General Public License along
117+# with this program. If not, see <http://www.gnu.org/licenses/
118+
119+import socket
120+import threading
121+import unittest
122+
123+import localmail
124+import localmail.tests.helpers
125+from django.conf import settings
126+
127+from u1testutils import mail
128+
129+
130+class MailTestCase(unittest.TestCase):
131+
132+ SMTP_PORT = 2025
133+
134+ @classmethod
135+ def setUpClass(class_):
136+ class_.localmail_thread = threading.Thread(
137+ target=localmail.run, args=(class_.SMTP_PORT, settings.IMAP_PORT))
138+ class_.localmail_thread.start()
139+ # Try the port to make sure the server started.
140+ socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
141+ socket_.settimeout(10)
142+ socket_.connect(('localhost', class_.SMTP_PORT))
143+ socket_.close()
144+
145+ @classmethod
146+ def tearDownClass(class_):
147+ localmail.shutdown_thread(class_.localmail_thread)
148+
149+ def test_get_latest_email(self):
150+ from_ = 'from@example.com'
151+ to = 'to@example.com'
152+ subject = 'Test Subject'
153+ body = 'Test body'
154+ with localmail.tests.helpers.SMTPHelper(port=self.SMTP_PORT) as smtp:
155+ smtp.login()
156+ smtp.send(from_, to, subject, body)
157+ latest_email = mail.get_latest_email_sent_to('to@example.com')
158+ self.assertEquals(latest_email['From'], from_)
159+ self.assertEquals(latest_email['To'], to)
160+ self.assertEquals(latest_email['Subject'], subject)
161+ self.assertEquals(latest_email.get_payload(), body + '\n')
162
163=== added file 'u1testutils/sst/sso/selftests/unit/__init__.py'
164=== modified file 'u1testutils/sst/sso/selftests/unit/test_data.py'
165--- u1testutils/sst/sso/selftests/unit/test_data.py 2013-01-16 02:55:27 +0000
166+++ u1testutils/sst/sso/selftests/unit/test_data.py 2013-01-21 16:21:20 +0000
167@@ -16,7 +16,7 @@
168
169 import unittest
170
171-import mock
172+from django.conf import settings
173
174 from u1testutils.sst.sso import data
175
176@@ -24,17 +24,8 @@
177 class DataTestCase(unittest.TestCase):
178
179 def test_make_exising_user(self):
180- test_full_name = 'Test full name'
181- test_email = 'email@test.com'
182- test_password = '7357 ¶@55\^/0rD'
183- with mock.patch('u1testutils.sst.sso.data.settings') as mock_settings:
184- type(mock_settings).SSO_TEST_ACCOUNT_FULL_NAME = \
185- mock.PropertyMock(return_value=test_full_name)
186- type(mock_settings).SSO_TEST_ACCOUNT_EMAIL = \
187- mock.PropertyMock(return_value=test_email)
188- type(mock_settings).SSO_TEST_ACCOUNT_PASSWORD = \
189- mock.PropertyMock(return_value=test_password)
190- user = data.User.make_from_configuration()
191- self.assertEqual(user.full_name, test_full_name)
192- self.assertEqual(user.email, test_email)
193- self.assertEqual(user.password, test_password)
194+ # The DJANGO_SETTINGS_MODULE is set by the test fab task.
195+ user = data.User.make_from_configuration()
196+ self.assertEqual(user.full_name, settings.SSO_TEST_ACCOUNT_FULL_NAME)
197+ self.assertEqual(user.email, settings.SSO_TEST_ACCOUNT_EMAIL)
198+ self.assertEqual(user.password, settings.SSO_TEST_ACCOUNT_PASSWORD)
199
200=== added file 'u1testutils/sst/sso/selftests/unit/test_sso_mail.py'
201--- u1testutils/sst/sso/selftests/unit/test_sso_mail.py 1970-01-01 00:00:00 +0000
202+++ u1testutils/sst/sso/selftests/unit/test_sso_mail.py 2013-01-21 16:21:20 +0000
203@@ -0,0 +1,92 @@
204+# -*- coding: utf-8 -*-
205+
206+# Copyright 2013 Canonical Ltd.
207+#
208+# This program is free software: you can redistribute it and/or modify it
209+# under the terms of the GNU Lesser General Public License version 3, as
210+# published by the Free Software Foundation.
211+#
212+# This program is distributed in the hope that it will be useful, but
213+# WITHOUT ANY WARRANTY; without even the implied warranties of
214+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
215+# PURPOSE. See the GNU Lesser General Public License for more details.
216+#
217+# You should have received a copy of the GNU General Public License along
218+# with this program. If not, see <http://www.gnu.org/licenses/
219+
220+import email.parser
221+import unittest
222+
223+import mock
224+
225+from u1testutils.sst.sso.utils import mail
226+
227+
228+class SSOMailTestCase(unittest.TestCase):
229+
230+ verification_email = email.parser.Parser().parsestr(
231+ 'Content-Type: text/plain; charset="utf-8"\n'
232+ 'MIME-Version: 1.0\n'
233+ 'Content-Transfer-Encoding: quoted-printable\n'
234+ 'Subject: Ubuntu Single Sign On: Finish your registration\n'
235+ 'From: Ubuntu Single Sign On <noreply@ubuntu.com>\n'
236+ 'To: to@example.com\n'
237+ '\n'
238+ 'Hello\n'
239+ '\n'
240+ 'As a final step of the Ubuntu Single Sign On (SSO) account '
241+ 'creation process=\n'
242+ ', please validate the email address to@example.com. Ubuntu SSO '
243+ 'enables conv=\n'
244+ 'enient access to a variety of Ubuntu-related services like Ubuntu '
245+ 'One with =\n'
246+ 'the same username and password.\n'
247+ '\n'
248+ 'Here is your confirmation code:\n'
249+ '\n'
250+ 'QM9CSR\n'
251+ '\n'
252+ 'Enter this code into the registration form, or click the following '
253+ 'link to =\n'
254+ 'automatically confirm your account:\n'
255+ '\n'
256+ 'https://login.ubuntu.com/confirm-account/QM9CSR/to@example.com\n'
257+ '\n'
258+ 'If you don\'t know what this is about, then someone has probably '
259+ 'entered you=\n'
260+ 'r email address by mistake. Sorry about that.\n'
261+ 'If you wish to report this email being incorrectly used, please '
262+ 'click the f=\n'
263+ 'ollowing link:\n'
264+ '\n'
265+ 'https://login.ubuntu.com/invalidate-email/42FcJG/to@example.com\n'
266+ '\n'
267+ 'You can also seek further assistance on:\n'
268+ '\n'
269+ 'https://forms.canonical.com/sso-support/\n'
270+ '\n'
271+ 'Thank you,\n'
272+ '\n'
273+ 'The Ubuntu Single Sign On team\n'
274+ 'https://login.ubuntu.com/\n'
275+ )
276+
277+ def setUp(self):
278+ super(SSOMailTestCase, self).setUp()
279+ patcher = mock.patch('u1testutils.mail')
280+ mock_mail = patcher.start()
281+ mock_mail.get_latest_email_sent_to.return_value = \
282+ self.verification_email
283+ self.addCleanup(patcher.stop)
284+
285+ def test_get_verification_code(self):
286+ verification_code = mail.get_verification_code_for_address(
287+ 'to@example.com')
288+ self.assertEquals(verification_code, 'QM9CSR')
289+
290+ def test_get_verification_link(self):
291+ verification_link = mail.get_verification_link_for_address(
292+ 'to@example.com')
293+ self.assertEquals(
294+ verification_link,
295+ 'https://login.ubuntu.com/confirm-account/QM9CSR/to@example.com')
296
297=== modified file 'u1testutils/sst/sso/utils/mail.py'
298--- u1testutils/sst/sso/utils/mail.py 2013-01-17 21:34:26 +0000
299+++ u1testutils/sst/sso/utils/mail.py 2013-01-21 16:21:20 +0000
300@@ -17,7 +17,7 @@
301 import quopri
302 import re
303
304-from u1testutils.sst import mail
305+import u1testutils.mail
306
307
308 def _get_verification_data_for_address(email_address):
309@@ -28,7 +28,7 @@
310 idioms like:
311 vcode, ignored = get_verification_for_address(email_address).
312 """
313- email_msg = mail.get_latest_email_sent_to(email_address)
314+ email_msg = u1testutils.mail.get_latest_email_sent_to(email_address)
315 vcode = link = None
316 if email_msg:
317 # The body is encoded as quoted-printable. This affects any

Subscribers

People subscribed via source and target branches

to all changes: