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
=== modified file 'requirements.txt'
--- requirements.txt 2013-07-03 07:23:07 +0000
+++ requirements.txt 2013-07-06 07:12:27 +0000
@@ -4,6 +4,7 @@
4mock4mock
5pep85pep8
6pyflakes6pyflakes
7testscenarios==0.4
7Twisted8Twisted
8bzr+ssh://bazaar.launchpad.net/~bloodearnest/localmail/trunk@369bzr+ssh://bazaar.launchpad.net/~bloodearnest/localmail/trunk@36
9bzr+http://bazaar.launchpad.net/~ubuntuone-hackers/payclient/trunk@410bzr+http://bazaar.launchpad.net/~ubuntuone-hackers/payclient/trunk@4
1011
=== modified file 'u1testutils/sso/selftests/acceptance/test_sst_helpers.py'
--- u1testutils/sso/selftests/acceptance/test_sst_helpers.py 2013-07-03 07:44:12 +0000
+++ u1testutils/sso/selftests/acceptance/test_sst_helpers.py 2013-07-06 07:12:27 +0000
@@ -15,7 +15,9 @@
15import os15import os
16import shutil16import shutil
17import tempfile17import tempfile
18import uuid
1819
20import testscenarios
19from sst import cases21from sst import cases
2022
21import u1testutils.sst23import u1testutils.sst
@@ -26,17 +28,27 @@
26)28)
2729
2830
31_WRONG_PAGE_SOURCE = (
32 """
33 <html data-qa-id="wrong">
34 </html>
35 """
36)
37
29_LOGIN_FROM_REDIRECT_PAGE_SOURCE = (38_LOGIN_FROM_REDIRECT_PAGE_SOURCE = (
30 """39 """
31 <html data-qa-id="login">40 <html data-qa-id="login">
32 <head>41 <head>
33 <title>Log in</title>42 <title>Log in</title>
34 </head>43 </head>
35 <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" >
36 <input id="id_email" type="email" />47 <input id="id_email" type="email" />
37 <input id="id_password" type="password" />48 <input id="id_password" type="password" />
38 <button data-qa-id="login_button" type="submit" />49 <button data-qa-id="login_button" type="submit" />
39 </form>50 </form>
51 <a href="{create_account_link}" data-qa-id="create_account_link" />
40 </html>52 </html>
41 """53 """
42)54)
@@ -52,100 +64,284 @@
52 """64 """
53)65)
5466
55_WRONG_PAGE_SOURCE = (67_CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE = (
56 """68 """
57 <html data-qa-id="wrong">69 <html data-qa-id="new_account">
58 </html>70 <form data-qa-id="create_account_form"
59 """71 action="{create_account_form_submit_link}" method="get" >
60)72 <input id="id_email" type="email" />
6173 <input id="id_displayname" type="text" />
6274 <input id="id_password" type="password" />
63class SignInTestCase(cases.SSTTestCase, logging.LogHandlerTestCase):75 <input id="id_passwordconfirm" type="password" />
76 <input id="recaptcha_response_field" type="text" />
77 <input id="id_accept_tos" type="checkbox" />
78 <button data-qa-id="register_button" type="submit" />
79 </form>
80 </html>
81 """
82)
83
84_UNIFIED_LOGIN_FROM_REDIRECT_PAGE_SOURCE = (
85 """
86 <html data-qa-id="login">
87 <head>
88 <title>Log in</title>
89 </head>
90 <span data-qa-id="brand_ubuntuone" />
91 <form data-qa-id="login_form" action="{login_form_submit_link}"
92 method="get" >
93 <input id="id_email" type="email" />
94 <input id="id_password" type="password" />
95 <button data-qa-id="login_button" type="submit" />
96 </form>
97 <a href="{create_account_link}" data-qa-id="user_intention_create" />
98 </html>
99 """
100)
101
102
103_UNIFIED_CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE = (
104 """
105 <html data-qa-id="login">
106 <head>
107 <title>Log in</title>
108 </head>
109 <span data-qa-id="brand_ubuntuone" />
110 <form data-qa-id="create_account_form"
111 action="{create_account_form_submit_link}" method="get" >
112 <input id="id_email" type="email" />
113 <input id="id_displayname" type="text" />
114 <input id="id_password" type="password" />
115 <input id="id_passwordconfirm" type="password" />
116 <input id="recaptcha_response_field" type="text" />
117 <input id="id_accept_tos" type="checkbox" />
118 <button data-qa-id="register_button" type="submit" />
119 </form>
120 </html>
121 """
122)
123
124
125class FakeSSOWebsite(object):
126
127 def __init__(self, is_user_logged_in, is_site_recognized, brand):
128 super(FakeSSOWebsite, self).__init__()
129 self.is_user_logged_in = is_user_logged_in
130 self.is_site_recognized = is_site_recognized
131 self.brand = brand
132
133 def __enter__(self):
134 self.temp_directory = tempfile.mkdtemp()
135 self._write_fake_pages()
136
137 def _write_fake_pages(self):
138 if self.is_site_recognized:
139 not_recognized_path = ''
140 else:
141 not_recognized_path = self._write_fake_site_not_recognized_page()
142 if not self.is_user_logged_in:
143 self._write_login_and_create_account_pages(not_recognized_path)
144
145 def _write_fake_site_not_recognized_page(self):
146 source = _SITE_NOT_RECOGNIZED_PAGE_SOURCE
147 path = '+decide'
148 qa_anchor = 'decide'
149 return self._write_fake_page_with_token(path, source, qa_anchor)
150
151 def _write_fake_page_with_token(self, path, source, qa_anchor, **links):
152 # We do not check the tokens, so the easiest thing is to have the
153 # every page in separate paths, to write them to HTML files and be able
154 # to open all of them on the same test. In real life, the tokens are
155 # the same. To check the token can be a future improvement.
156 # -- elopio - 2013-07-03
157 token = str(uuid.uuid1())
158 token_directory = os.path.join(self.temp_directory, token)
159 os.mkdir(token_directory)
160 return self._write_fake_page(
161 token_directory, path, source, qa_anchor, **links)
162
163 def _write_fake_page(self, directory, path, source, qa_anchor, **links):
164 if links:
165 source = source.format(**links)
166 page_path = os.path.join(directory, path)
167 u1testutils.sst.StringHTMLPage(
168 source, page_path, qa_anchor, open_page=True)
169 return page_path
170
171 def _write_login_and_create_account_pages(self, next_page_path):
172 if self.brand == 'ubuntuone':
173 create_account_path = self._write_fake_unified_create_account_page(
174 next_page_path)
175 self._write_fake_unified_login_page(
176 next_page_path, create_account_path)
177 else:
178 create_account_path = self._write_fake_create_account_page(
179 next_page_path)
180 self._write_fake_login_page(
181 next_page_path, create_account_path)
182
183 def _write_fake_unified_create_account_page(self, next_page_path):
184 source = _UNIFIED_CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE
185 path = '+decide'
186 qa_anchor = 'login'
187 return self._write_fake_page_with_token(
188 path, source, qa_anchor,
189 create_account_form_submit_link=next_page_path)
190
191 def _write_fake_unified_login_page(
192 self, next_page_path, create_account_path):
193 source = _UNIFIED_LOGIN_FROM_REDIRECT_PAGE_SOURCE
194 path = '+decide'
195 qa_anchor = 'login'
196 return self._write_fake_page_with_token(
197 path, source, qa_anchor,
198 login_form_submit_link=next_page_path,
199 create_account_link=create_account_path)
200
201 def _write_fake_create_account_page(self, next_page_path):
202 source = _CREATE_ACCOUNT_FROM_REDIRECT_PAGE_SOURCE
203 path = '+new_account'
204 qa_anchor = 'new_account'
205 return self._write_fake_page_with_token(
206 path, source, qa_anchor,
207 create_account_form_submit_link=next_page_path)
208
209 def _write_fake_login_page(self, next_page_path, create_account_path):
210 source = _LOGIN_FROM_REDIRECT_PAGE_SOURCE
211 path = '+decide'
212 qa_anchor = 'login'
213 return self._write_fake_page_with_token(
214 path, source, qa_anchor, login_form_submit_link=next_page_path,
215 create_account_link=create_account_path)
216
217 def __exit__(self, exc_type, exc_val, exc_tb):
218 shutil.rmtree(self.temp_directory)
219
220
221class WrongSSOSiteTestCase(cases.SSTTestCase):
64222
65 xserver_headless = True223 xserver_headless = True
66224
67 def setUp(self):225 def setUp(self):
68 self.temp_directory = tempfile.mkdtemp()226 self.base_url = 'file://'
69 self.addCleanup(shutil.rmtree, self.temp_directory)227 super(WrongSSOSiteTestCase, self).setUp()
70 self.base_url = 'file://' + self.temp_directory228 self._write_wrong_page()
71 super(SignInTestCase, self).setUp()
72229
73 def test_sign_in_to_wrong_page(self):230 def _write_wrong_page(self):
74 page_path = os.path.join(self.temp_directory, 'wrong')231 temp_directory = tempfile.mkdtemp()
232 self.addCleanup(shutil.rmtree, temp_directory)
233 page_path = os.path.join(temp_directory, 'wrong')
75 u1testutils.sst.StringHTMLPage(234 u1testutils.sst.StringHTMLPage(
76 _WRONG_PAGE_SOURCE, page_path, qa_anchor='wrong',235 _WRONG_PAGE_SOURCE, page_path, qa_anchor='wrong',
77 open_page=True)236 open_page=True)
78237
79 self.assertRaises(238 def test_sign_in(self):
80 AssertionError, sso_sst.sign_in, user=None,239 self.assertRaises(
81 is_site_recognized=False)240 AssertionError, sso_sst.sign_in, user='not important',
241 is_site_recognized=False)
242
243 def test_create_account(self):
244 self.assertRaises(
245 AssertionError, sso_sst.sign_in, user='not important',
246 is_site_recognized=False)
247
248
249class SignInTestCase(
250 testscenarios.TestWithScenarios, cases.SSTTestCase,
251 logging.LogHandlerTestCase):
252
253 scenarios = [
254 ('ubuntuone brand', {'brand': 'ubuntuone'}),
255 ('other brand', {'brand': 'other'}),
256 ]
257
258 xserver_headless = True
259
260 def setUp(self):
261 self.base_url = 'file://'
262 super(SignInTestCase, self).setUp()
82263
83 def test_sign_in_with_already_signed_user_to_site_recognized(self):264 def test_sign_in_with_already_signed_user_to_site_recognized(self):
84 # Sign in will do nothing.265 # Sign in will do nothing.
85 sso_sst.sign_in(user=None, is_site_recognized=True)266 sso_sst.sign_in(user=None, is_site_recognized=True)
86267
87 def test_sign_in_with_already_signed_user_to_site_not_recognized(self):268 def test_sign_in_with_already_signed_user_to_site_not_recognized(self):
88 self._write_fake_site_not_recognized_page(open_page=True)269 with FakeSSOWebsite(
89 sso_sst.sign_in(user=None, is_site_recognized=False)270 is_user_logged_in=True, is_site_recognized=False,
90271 brand=self.brand):
91 def _write_fake_site_not_recognized_page(self, open_page, token='token'):272 sso_sst.sign_in(user=None, is_site_recognized=False)
92 token_directory = os.path.join(self.temp_directory, token)
93 os.mkdir(token_directory)
94 site_not_recognized_page_path = os.path.join(
95 token_directory, '+decide')
96 u1testutils.sst.StringHTMLPage(
97 _SITE_NOT_RECOGNIZED_PAGE_SOURCE, site_not_recognized_page_path,
98 qa_anchor='decide', open_page=open_page)
99 return site_not_recognized_page_path
100273
101 def test_sign_in_to_site_recognized(self):274 def test_sign_in_to_site_recognized(self):
102 self._write_fake_log_in_page(open_page=True)
103 user = data.User.make_unique()275 user = data.User.make_unique()
104276 with FakeSSOWebsite(
105 sso_sst.sign_in(user, is_site_recognized=True)277 is_user_logged_in=False, is_site_recognized=True,
106278 brand=self.brand):
107 def _write_fake_log_in_page(279 sso_sst.sign_in(user, is_site_recognized=True)
108 self, open_page, token='token',
109 site_not_recognized_page_path=None):
110 log_in_page_source = _LOGIN_FROM_REDIRECT_PAGE_SOURCE
111 if site_not_recognized_page_path is not None:
112 log_in_page_source = log_in_page_source.format(
113 site_not_recognized_page_path)
114 token_directory = os.path.join(self.temp_directory, token)
115 os.mkdir(token_directory)
116 log_in_page_path = os.path.join(token_directory, '+decide')
117 u1testutils.sst.StringHTMLPage(
118 log_in_page_source, log_in_page_path, qa_anchor='login',
119 open_page=open_page)
120280
121 def test_sign_in_to_site_not_recognized(self):281 def test_sign_in_to_site_not_recognized(self):
122 self._write_fake_log_in_to_site_not_recognized_pages(open_page=True)
123 user = data.User.make_unique()282 user = data.User.make_unique()
124283 with FakeSSOWebsite(
125 sso_sst.sign_in(user, is_site_recognized=False)284 is_user_logged_in=False, is_site_recognized=False,
126285 brand=self.brand):
127 def _write_fake_log_in_to_site_not_recognized_pages(self, open_page):286 sso_sst.sign_in(user, is_site_recognized=False)
128 # We do not check the tokens, so the easiest thing is to have the
129 # two pages in separate paths, to write both to HTML files and be able
130 # to open the second from the first. In real life, the tokens are the
131 # same. To check the token can be a future improvement.
132 # -- elopio - 2013-07-03
133 site_not_recognized_page_path = (
134 self._write_fake_site_not_recognized_page(
135 open_page=False, token='not_recognized'))
136 self._write_fake_log_in_page(
137 open_page=open_page, token='login',
138 site_not_recognized_page_path=site_not_recognized_page_path)
139287
140 def test_sign_in_with_unexpected_site_not_recognized(self):288 def test_sign_in_with_unexpected_site_not_recognized(self):
141 self._write_fake_log_in_to_site_not_recognized_pages(open_page=True)289 user = data.User.make_unique()
142 user = data.User.make_unique()290 with FakeSSOWebsite(
143291 is_user_logged_in=False, is_site_recognized=False,
144 self.assertRaises(292 brand=self.brand):
145 AssertionError, sso_sst.sign_in, user, is_site_recognized=True)293 self.assertRaises(
146 self.assertLogLevelContains(294 AssertionError, sso_sst.sign_in, user, is_site_recognized=True)
147 'ERROR',295 self.assertLogLevelContains(
148 'Please check that you are logging in from a server that is '296 'ERROR',
149 'trusted by SSO. Otherwise, the unexpected Site Not Recognized '297 'Please check that you are accessing SSO from a server that is '
150 'page will be opened.'298 'trusted. Otherwise, the unexpected Site Not Recognized page will '
299 'be opened.'
300 )
301
302
303class CreateNewAccountTestCase(
304 testscenarios.TestWithScenarios, cases.SSTTestCase,
305 logging.LogHandlerTestCase):
306
307 scenarios = [
308 ('ubuntuone brand', {'brand': 'ubuntuone'}),
309 ('other brand', {'brand': 'other'}),
310 ]
311
312 xserver_headless = True
313
314 def setUp(self):
315 self.temp_directory = tempfile.mkdtemp()
316 self.addCleanup(shutil.rmtree, self.temp_directory)
317 self.base_url = 'file://' + self.temp_directory
318 super(CreateNewAccountTestCase, self).setUp()
319
320 def test_create_account_to_site_recognized(self):
321 user = data.User.make_unique()
322 with FakeSSOWebsite(
323 is_user_logged_in=False, is_site_recognized=True,
324 brand=self.brand):
325 sso_sst.create_new_account(user, is_site_recognized=True)
326
327 def test_create_account_to_site_not_recognized(self):
328 user = data.User.make_unique()
329 with FakeSSOWebsite(
330 is_user_logged_in=False, is_site_recognized=False,
331 brand=self.brand):
332 sso_sst.create_new_account(user, is_site_recognized=False)
333
334 def test_create_account_with_unexpected_site_not_recognized(self):
335 user = data.User.make_unique()
336 with FakeSSOWebsite(
337 is_user_logged_in=False, is_site_recognized=False,
338 brand=self.brand):
339 self.assertRaises(
340 AssertionError, sso_sst.create_new_account, user,
341 is_site_recognized=True)
342 self.assertLogLevelContains(
343 'ERROR',
344 'Please check that you are accessing SSO from a server that is '
345 'trusted. Otherwise, the unexpected Site Not Recognized page will '
346 'be opened.'
151 )347 )
152348
=== modified file 'u1testutils/sso/sst/__init__.py'
--- u1testutils/sso/sst/__init__.py 2013-07-03 07:50:35 +0000
+++ u1testutils/sso/sst/__init__.py 2013-07-06 07:12:27 +0000
@@ -49,6 +49,20 @@
49 site_not_recognized = pages.SiteNotRecognized()49 site_not_recognized = pages.SiteNotRecognized()
50 site_not_recognized.make_all_information_available_to_website()50 site_not_recognized.make_all_information_available_to_website()
51 site_not_recognized.yes_sign_me_in()51 site_not_recognized.yes_sign_me_in()
52 else:
53 _assert_site_not_recognized_page_not_opened()
54
55
56def _assert_site_not_recognized_page_not_opened():
57 try:
58 sst.actions.fails(pages.SiteNotRecognized)
59 except AssertionError:
60 suggestion = (
61 'Please check that you are accessing SSO from a server that is '
62 'trusted. Otherwise, the unexpected Site Not Recognized page will '
63 'be opened.')
64 logger.error(suggestion)
65 raise
5266
5367
54def validate_user_email(user):68def validate_user_email(user):
@@ -109,18 +123,6 @@
109 site_not_recognized.yes_sign_me_in()123 site_not_recognized.yes_sign_me_in()
110124
111125
112def _assert_site_not_recognized_page_not_opened():
113 try:
114 sst.actions.fails(pages.SiteNotRecognized)
115 except AssertionError:
116 suggestion = (
117 'Please check that you are logging in from a server that is '
118 'trusted by SSO. Otherwise, the unexpected Site Not Recognized '
119 'page will be opened.')
120 logger.error(suggestion)
121 raise
122
123
124def log_out():126def log_out():
125 """Log out from the Ubuntu Single Sign On site.127 """Log out from the Ubuntu Single Sign On site.
126128

Subscribers

People subscribed via source and target branches

to all changes: