Merge lp:~elopio/u1-test-utils/update_sso_pages1-tests2 into lp:u1-test-utils

Proposed by Leo Arias
Status: Merged
Approved by: Leo Arias
Approved revision: 93
Merged at revision: 87
Proposed branch: lp:~elopio/u1-test-utils/update_sso_pages1-tests2
Merge into: lp:u1-test-utils
Diff against target: 455 lines (+285/-86)
3 files modified
requirements.txt (+1/-0)
u1testutils/sso/selftests/acceptance/test_sst_helpers.py (+270/-74)
u1testutils/sso/sst/__init__.py (+14/-12)
To merge this branch: bzr merge lp:~elopio/u1-test-utils/update_sso_pages1-tests2
Reviewer Review Type Date Requested Status
Vincent Ladeuil (community) Approve
Review via email: mp+173300@code.launchpad.net

Commit message

Added acceptance tests for the create account helper.
Run the sign in and create account tests for the ubuntuone brand and for the other brands.

Description of the change

I added the tests for the create account helper, and afterwards refactored all the tests to use a FakeSSOWebsite that works as a context manager deleting all its files on exit.

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

218 + def __exit__(self, exc_type, exc_val, exc_tb):
219 + shutil.rmtree(self.temp_directory)

That would be easier to find (and to think about) if it was closer to __enter__.

222 +class WrongSSOSiteTestCase(cases.SSTTestCase):

Nice simplifications in these tests !

review: Approve
Revision history for this message
Leo Arias (elopio) wrote :

> 218 + def __exit__(self, exc_type, exc_val, exc_tb):
> 219 + shutil.rmtree(self.temp_directory)
>
> That would be easier to find (and to think about) if it was closer to
> __enter__.

I'll take a look at contextlib. I think this might look nicer with the decorator. But that tomorrow, I'll merge it now.

Thanks!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'requirements.txt'
2--- requirements.txt 2013-07-03 07:23:07 +0000
3+++ requirements.txt 2013-07-06 07:12:27 +0000
4@@ -4,6 +4,7 @@
5 mock
6 pep8
7 pyflakes
8+testscenarios==0.4
9 Twisted
10 bzr+ssh://bazaar.launchpad.net/~bloodearnest/localmail/trunk@36
11 bzr+http://bazaar.launchpad.net/~ubuntuone-hackers/payclient/trunk@4
12
13=== modified file 'u1testutils/sso/selftests/acceptance/test_sst_helpers.py'
14--- u1testutils/sso/selftests/acceptance/test_sst_helpers.py 2013-07-03 07:44:12 +0000
15+++ u1testutils/sso/selftests/acceptance/test_sst_helpers.py 2013-07-06 07:12:27 +0000
16@@ -15,7 +15,9 @@
17 import os
18 import shutil
19 import tempfile
20+import uuid
21
22+import testscenarios
23 from sst import cases
24
25 import u1testutils.sst
26@@ -26,17 +28,27 @@
27 )
28
29
30+_WRONG_PAGE_SOURCE = (
31+ """
32+ <html data-qa-id="wrong">
33+ </html>
34+ """
35+)
36+
37 _LOGIN_FROM_REDIRECT_PAGE_SOURCE = (
38 """
39 <html data-qa-id="login">
40 <head>
41 <title>Log in</title>
42 </head>
43- <form data-qa-id="login_form" action="{0}" method="get" >
44+ <span data-qa-id="brand_ubuntu" />
45+ <form data-qa-id="login_form" action="{login_form_submit_link}"
46+ method="get" >
47 <input id="id_email" type="email" />
48 <input id="id_password" type="password" />
49 <button data-qa-id="login_button" type="submit" />
50 </form>
51+ <a href="{create_account_link}" data-qa-id="create_account_link" />
52 </html>
53 """
54 )
55@@ -52,100 +64,284 @@
56 """
57 )
58
59-_WRONG_PAGE_SOURCE = (
60- """
61- <html data-qa-id="wrong">
62- </html>
63- """
64-)
65-
66-
67-class SignInTestCase(cases.SSTTestCase, logging.LogHandlerTestCase):
68+_CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE = (
69+ """
70+ <html data-qa-id="new_account">
71+ <form data-qa-id="create_account_form"
72+ action="{create_account_form_submit_link}" method="get" >
73+ <input id="id_email" type="email" />
74+ <input id="id_displayname" type="text" />
75+ <input id="id_password" type="password" />
76+ <input id="id_passwordconfirm" type="password" />
77+ <input id="recaptcha_response_field" type="text" />
78+ <input id="id_accept_tos" type="checkbox" />
79+ <button data-qa-id="register_button" type="submit" />
80+ </form>
81+ </html>
82+ """
83+)
84+
85+_UNIFIED_LOGIN_FROM_REDIRECT_PAGE_SOURCE = (
86+ """
87+ <html data-qa-id="login">
88+ <head>
89+ <title>Log in</title>
90+ </head>
91+ <span data-qa-id="brand_ubuntuone" />
92+ <form data-qa-id="login_form" action="{login_form_submit_link}"
93+ method="get" >
94+ <input id="id_email" type="email" />
95+ <input id="id_password" type="password" />
96+ <button data-qa-id="login_button" type="submit" />
97+ </form>
98+ <a href="{create_account_link}" data-qa-id="user_intention_create" />
99+ </html>
100+ """
101+)
102+
103+
104+_UNIFIED_CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE = (
105+ """
106+ <html data-qa-id="login">
107+ <head>
108+ <title>Log in</title>
109+ </head>
110+ <span data-qa-id="brand_ubuntuone" />
111+ <form data-qa-id="create_account_form"
112+ action="{create_account_form_submit_link}" method="get" >
113+ <input id="id_email" type="email" />
114+ <input id="id_displayname" type="text" />
115+ <input id="id_password" type="password" />
116+ <input id="id_passwordconfirm" type="password" />
117+ <input id="recaptcha_response_field" type="text" />
118+ <input id="id_accept_tos" type="checkbox" />
119+ <button data-qa-id="register_button" type="submit" />
120+ </form>
121+ </html>
122+ """
123+)
124+
125+
126+class FakeSSOWebsite(object):
127+
128+ def __init__(self, is_user_logged_in, is_site_recognized, brand):
129+ super(FakeSSOWebsite, self).__init__()
130+ self.is_user_logged_in = is_user_logged_in
131+ self.is_site_recognized = is_site_recognized
132+ self.brand = brand
133+
134+ def __enter__(self):
135+ self.temp_directory = tempfile.mkdtemp()
136+ self._write_fake_pages()
137+
138+ def _write_fake_pages(self):
139+ if self.is_site_recognized:
140+ not_recognized_path = ''
141+ else:
142+ not_recognized_path = self._write_fake_site_not_recognized_page()
143+ if not self.is_user_logged_in:
144+ self._write_login_and_create_account_pages(not_recognized_path)
145+
146+ def _write_fake_site_not_recognized_page(self):
147+ source = _SITE_NOT_RECOGNIZED_PAGE_SOURCE
148+ path = '+decide'
149+ qa_anchor = 'decide'
150+ return self._write_fake_page_with_token(path, source, qa_anchor)
151+
152+ def _write_fake_page_with_token(self, path, source, qa_anchor, **links):
153+ # We do not check the tokens, so the easiest thing is to have the
154+ # every page in separate paths, to write them to HTML files and be able
155+ # to open all of them on the same test. In real life, the tokens are
156+ # the same. To check the token can be a future improvement.
157+ # -- elopio - 2013-07-03
158+ token = str(uuid.uuid1())
159+ token_directory = os.path.join(self.temp_directory, token)
160+ os.mkdir(token_directory)
161+ return self._write_fake_page(
162+ token_directory, path, source, qa_anchor, **links)
163+
164+ def _write_fake_page(self, directory, path, source, qa_anchor, **links):
165+ if links:
166+ source = source.format(**links)
167+ page_path = os.path.join(directory, path)
168+ u1testutils.sst.StringHTMLPage(
169+ source, page_path, qa_anchor, open_page=True)
170+ return page_path
171+
172+ def _write_login_and_create_account_pages(self, next_page_path):
173+ if self.brand == 'ubuntuone':
174+ create_account_path = self._write_fake_unified_create_account_page(
175+ next_page_path)
176+ self._write_fake_unified_login_page(
177+ next_page_path, create_account_path)
178+ else:
179+ create_account_path = self._write_fake_create_account_page(
180+ next_page_path)
181+ self._write_fake_login_page(
182+ next_page_path, create_account_path)
183+
184+ def _write_fake_unified_create_account_page(self, next_page_path):
185+ source = _UNIFIED_CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE
186+ path = '+decide'
187+ qa_anchor = 'login'
188+ return self._write_fake_page_with_token(
189+ path, source, qa_anchor,
190+ create_account_form_submit_link=next_page_path)
191+
192+ def _write_fake_unified_login_page(
193+ self, next_page_path, create_account_path):
194+ source = _UNIFIED_LOGIN_FROM_REDIRECT_PAGE_SOURCE
195+ path = '+decide'
196+ qa_anchor = 'login'
197+ return self._write_fake_page_with_token(
198+ path, source, qa_anchor,
199+ login_form_submit_link=next_page_path,
200+ create_account_link=create_account_path)
201+
202+ def _write_fake_create_account_page(self, next_page_path):
203+ source = _CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE
204+ path = '+new_account'
205+ qa_anchor = 'new_account'
206+ return self._write_fake_page_with_token(
207+ path, source, qa_anchor,
208+ create_account_form_submit_link=next_page_path)
209+
210+ def _write_fake_login_page(self, next_page_path, create_account_path):
211+ source = _LOGIN_FROM_REDIRECT_PAGE_SOURCE
212+ path = '+decide'
213+ qa_anchor = 'login'
214+ return self._write_fake_page_with_token(
215+ path, source, qa_anchor, login_form_submit_link=next_page_path,
216+ create_account_link=create_account_path)
217+
218+ def __exit__(self, exc_type, exc_val, exc_tb):
219+ shutil.rmtree(self.temp_directory)
220+
221+
222+class WrongSSOSiteTestCase(cases.SSTTestCase):
223
224 xserver_headless = True
225
226 def setUp(self):
227- self.temp_directory = tempfile.mkdtemp()
228- self.addCleanup(shutil.rmtree, self.temp_directory)
229- self.base_url = 'file://' + self.temp_directory
230- super(SignInTestCase, self).setUp()
231+ self.base_url = 'file://'
232+ super(WrongSSOSiteTestCase, self).setUp()
233+ self._write_wrong_page()
234
235- def test_sign_in_to_wrong_page(self):
236- page_path = os.path.join(self.temp_directory, 'wrong')
237+ def _write_wrong_page(self):
238+ temp_directory = tempfile.mkdtemp()
239+ self.addCleanup(shutil.rmtree, temp_directory)
240+ page_path = os.path.join(temp_directory, 'wrong')
241 u1testutils.sst.StringHTMLPage(
242 _WRONG_PAGE_SOURCE, page_path, qa_anchor='wrong',
243 open_page=True)
244
245- self.assertRaises(
246- AssertionError, sso_sst.sign_in, user=None,
247- is_site_recognized=False)
248+ def test_sign_in(self):
249+ self.assertRaises(
250+ AssertionError, sso_sst.sign_in, user='not important',
251+ is_site_recognized=False)
252+
253+ def test_create_account(self):
254+ self.assertRaises(
255+ AssertionError, sso_sst.sign_in, user='not important',
256+ is_site_recognized=False)
257+
258+
259+class SignInTestCase(
260+ testscenarios.TestWithScenarios, cases.SSTTestCase,
261+ logging.LogHandlerTestCase):
262+
263+ scenarios = [
264+ ('ubuntuone brand', {'brand': 'ubuntuone'}),
265+ ('other brand', {'brand': 'other'}),
266+ ]
267+
268+ xserver_headless = True
269+
270+ def setUp(self):
271+ self.base_url = 'file://'
272+ super(SignInTestCase, self).setUp()
273
274 def test_sign_in_with_already_signed_user_to_site_recognized(self):
275 # Sign in will do nothing.
276 sso_sst.sign_in(user=None, is_site_recognized=True)
277
278 def test_sign_in_with_already_signed_user_to_site_not_recognized(self):
279- self._write_fake_site_not_recognized_page(open_page=True)
280- sso_sst.sign_in(user=None, is_site_recognized=False)
281-
282- def _write_fake_site_not_recognized_page(self, open_page, token='token'):
283- token_directory = os.path.join(self.temp_directory, token)
284- os.mkdir(token_directory)
285- site_not_recognized_page_path = os.path.join(
286- token_directory, '+decide')
287- u1testutils.sst.StringHTMLPage(
288- _SITE_NOT_RECOGNIZED_PAGE_SOURCE, site_not_recognized_page_path,
289- qa_anchor='decide', open_page=open_page)
290- return site_not_recognized_page_path
291+ with FakeSSOWebsite(
292+ is_user_logged_in=True, is_site_recognized=False,
293+ brand=self.brand):
294+ sso_sst.sign_in(user=None, is_site_recognized=False)
295
296 def test_sign_in_to_site_recognized(self):
297- self._write_fake_log_in_page(open_page=True)
298 user = data.User.make_unique()
299-
300- sso_sst.sign_in(user, is_site_recognized=True)
301-
302- def _write_fake_log_in_page(
303- self, open_page, token='token',
304- site_not_recognized_page_path=None):
305- log_in_page_source = _LOGIN_FROM_REDIRECT_PAGE_SOURCE
306- if site_not_recognized_page_path is not None:
307- log_in_page_source = log_in_page_source.format(
308- site_not_recognized_page_path)
309- token_directory = os.path.join(self.temp_directory, token)
310- os.mkdir(token_directory)
311- log_in_page_path = os.path.join(token_directory, '+decide')
312- u1testutils.sst.StringHTMLPage(
313- log_in_page_source, log_in_page_path, qa_anchor='login',
314- open_page=open_page)
315+ with FakeSSOWebsite(
316+ is_user_logged_in=False, is_site_recognized=True,
317+ brand=self.brand):
318+ sso_sst.sign_in(user, is_site_recognized=True)
319
320 def test_sign_in_to_site_not_recognized(self):
321- self._write_fake_log_in_to_site_not_recognized_pages(open_page=True)
322 user = data.User.make_unique()
323-
324- sso_sst.sign_in(user, is_site_recognized=False)
325-
326- def _write_fake_log_in_to_site_not_recognized_pages(self, open_page):
327- # We do not check the tokens, so the easiest thing is to have the
328- # two pages in separate paths, to write both to HTML files and be able
329- # to open the second from the first. In real life, the tokens are the
330- # same. To check the token can be a future improvement.
331- # -- elopio - 2013-07-03
332- site_not_recognized_page_path = (
333- self._write_fake_site_not_recognized_page(
334- open_page=False, token='not_recognized'))
335- self._write_fake_log_in_page(
336- open_page=open_page, token='login',
337- site_not_recognized_page_path=site_not_recognized_page_path)
338+ with FakeSSOWebsite(
339+ is_user_logged_in=False, is_site_recognized=False,
340+ brand=self.brand):
341+ sso_sst.sign_in(user, is_site_recognized=False)
342
343 def test_sign_in_with_unexpected_site_not_recognized(self):
344- self._write_fake_log_in_to_site_not_recognized_pages(open_page=True)
345- user = data.User.make_unique()
346-
347- self.assertRaises(
348- AssertionError, sso_sst.sign_in, user, is_site_recognized=True)
349- self.assertLogLevelContains(
350- 'ERROR',
351- 'Please check that you are logging in from a server that is '
352- 'trusted by SSO. Otherwise, the unexpected Site Not Recognized '
353- 'page will be opened.'
354+ user = data.User.make_unique()
355+ with FakeSSOWebsite(
356+ is_user_logged_in=False, is_site_recognized=False,
357+ brand=self.brand):
358+ self.assertRaises(
359+ AssertionError, sso_sst.sign_in, user, is_site_recognized=True)
360+ self.assertLogLevelContains(
361+ 'ERROR',
362+ 'Please check that you are accessing SSO from a server that is '
363+ 'trusted. Otherwise, the unexpected Site Not Recognized page will '
364+ 'be opened.'
365+ )
366+
367+
368+class CreateNewAccountTestCase(
369+ testscenarios.TestWithScenarios, cases.SSTTestCase,
370+ logging.LogHandlerTestCase):
371+
372+ scenarios = [
373+ ('ubuntuone brand', {'brand': 'ubuntuone'}),
374+ ('other brand', {'brand': 'other'}),
375+ ]
376+
377+ xserver_headless = True
378+
379+ def setUp(self):
380+ self.temp_directory = tempfile.mkdtemp()
381+ self.addCleanup(shutil.rmtree, self.temp_directory)
382+ self.base_url = 'file://' + self.temp_directory
383+ super(CreateNewAccountTestCase, self).setUp()
384+
385+ def test_create_account_to_site_recognized(self):
386+ user = data.User.make_unique()
387+ with FakeSSOWebsite(
388+ is_user_logged_in=False, is_site_recognized=True,
389+ brand=self.brand):
390+ sso_sst.create_new_account(user, is_site_recognized=True)
391+
392+ def test_create_account_to_site_not_recognized(self):
393+ user = data.User.make_unique()
394+ with FakeSSOWebsite(
395+ is_user_logged_in=False, is_site_recognized=False,
396+ brand=self.brand):
397+ sso_sst.create_new_account(user, is_site_recognized=False)
398+
399+ def test_create_account_with_unexpected_site_not_recognized(self):
400+ user = data.User.make_unique()
401+ with FakeSSOWebsite(
402+ is_user_logged_in=False, is_site_recognized=False,
403+ brand=self.brand):
404+ self.assertRaises(
405+ AssertionError, sso_sst.create_new_account, user,
406+ is_site_recognized=True)
407+ self.assertLogLevelContains(
408+ 'ERROR',
409+ 'Please check that you are accessing SSO from a server that is '
410+ 'trusted. Otherwise, the unexpected Site Not Recognized page will '
411+ 'be opened.'
412 )
413
414=== modified file 'u1testutils/sso/sst/__init__.py'
415--- u1testutils/sso/sst/__init__.py 2013-07-03 07:50:35 +0000
416+++ u1testutils/sso/sst/__init__.py 2013-07-06 07:12:27 +0000
417@@ -49,6 +49,20 @@
418 site_not_recognized = pages.SiteNotRecognized()
419 site_not_recognized.make_all_information_available_to_website()
420 site_not_recognized.yes_sign_me_in()
421+ else:
422+ _assert_site_not_recognized_page_not_opened()
423+
424+
425+def _assert_site_not_recognized_page_not_opened():
426+ try:
427+ sst.actions.fails(pages.SiteNotRecognized)
428+ except AssertionError:
429+ suggestion = (
430+ 'Please check that you are accessing SSO from a server that is '
431+ 'trusted. Otherwise, the unexpected Site Not Recognized page will '
432+ 'be opened.')
433+ logger.error(suggestion)
434+ raise
435
436
437 def validate_user_email(user):
438@@ -109,18 +123,6 @@
439 site_not_recognized.yes_sign_me_in()
440
441
442-def _assert_site_not_recognized_page_not_opened():
443- try:
444- sst.actions.fails(pages.SiteNotRecognized)
445- except AssertionError:
446- suggestion = (
447- 'Please check that you are logging in from a server that is '
448- 'trusted by SSO. Otherwise, the unexpected Site Not Recognized '
449- 'page will be opened.')
450- logger.error(suggestion)
451- raise
452-
453-
454 def log_out():
455 """Log out from the Ubuntu Single Sign On site.
456

Subscribers

People subscribed via source and target branches

to all changes: