75
76
REQUEST_PASSWORD_TOKEN_TECH_ERROR,
77
78
VERIFY_EMAIL_CONTENT,
80
82
#ignore the comon mocker issues with lint
81
83
# pylint: disable=W0212,W0104,W0106,E1103
86
class FakeButton(FakePageUiStyle):
91
"""Initialize object."""
92
super(FakeButton, self).__init__()
96
def connect(self, function):
97
"""Receive the function that should be connected to this button."""
98
self.function = function
101
class FakeField(object):
103
"""Fake Field for text input."""
105
def __init__(self, text):
108
# pylint: disable=C0103
110
"""Fake toString method for input fields."""
112
# pylint: enable=C0103
115
class FakeWizard(object):
119
current_user_page_id = 1
120
setup_account_page_id = 2
121
forgotten_password_page_id = 3
122
reset_password_page_id = 4
124
def __init__(self, view):
125
"""Initialize object."""
127
self.app_name = 'app_name'
128
self.help_text = 'help'
131
# pylint: disable=C0103
132
self.loginSuccess = self
133
self.registrationSuccess = self
134
# pylint: enable=C0103
135
self.forgotten = self
137
self.email_line_edit = QLineEdit()
139
# pylint: disable=C0103
140
def registerField(self, key, text):
141
"""Fake registerField from wizard."""
142
self.properties['field_%s' % key] = FakeField(text)
143
# pylint: enable=C0103
145
def field(self, key):
146
"""Fake field from wizard"""
147
return self.properties['field_%s' % key]
150
"""Fake next method to move to to next page in the wizard."""
151
self.view.properties['wizard_next'] = True
153
def emit(self, name, email):
155
self.properties['emit'] = (name, email)
158
class FakeGenericView(object):
160
"""Fake Generic page."""
163
"""Initialize object."""
167
self.properties['currentId'] = -1
169
self.fake_wizard = FakeWizard(self)
171
def set_title(self, title):
172
"""Fake set_title for the page."""
173
self.properties['title'] = title
175
def set_subtitle(self, subtitle):
176
"""Fake set_subtitle for the page"""
177
self.properties['subtitle'] = subtitle
179
# pylint: disable=C0103
180
def setTitle(self, title):
182
self.set_title(title)
184
def setSubTitle(self, subtitle):
186
self.set_subtitle(subtitle)
187
# pylint: enable=C0103
190
"""Fake wizard is returned."""
191
return self.fake_wizard
194
class FakeChooseSignInView(FakeGenericView):
196
"""Fake ChooseSignIn page."""
199
"""Initialize object."""
200
super(FakeChooseSignInView, self).__init__()
201
self.existing_account_button = FakeButton()
202
self.setup_account_button = FakeButton()
205
class FakeCurrentUserView(FakeGenericView):
207
"""Fake Current User view."""
210
super(FakeCurrentUserView, self).__init__()
211
self.email_label = QLabel()
212
self.password_label = QLabel()
213
self.forgot_password_label = QLabel()
214
self.sign_in_button = FakeButton()
215
self.email_edit = QLineEdit()
216
self.password_edit = QLineEdit()
217
self.on_logged_in_cb = None
218
self.on_login_error_cb = None
220
def fake_backend(self):
221
"""Fake get_backend."""
224
def login(self, appname, email, password):
225
"""Fake backend login."""
226
self.properties['backend_login'] = (appname, email, password)
229
# pylint: disable=C0103
230
def addErrback(self, function):
231
"""Fake defer addErrback."""
232
self.properties['login-addErrback'] = function
233
# pylint: enable=C0103
236
class FakeLineEdit(object):
238
"""A fake QLineEdit."""
240
def __init__(self, *args):
248
# setText is inherited
249
# pylint: disable=C0103
250
def setText(self, text):
251
"""Return saved text."""
255
class FakePage(object):
257
"""A fake wizard page."""
266
class FakeCurrentUserPage(FakePageUiStyle):
268
"""Fake CurrentuserPage."""
270
def __init__(self, *args):
272
super(FakeCurrentUserPage, self).__init__(*args)
273
self.ui.email_edit = FakeLineEdit()
274
self.ui.password_edit = FakeLineEdit()
275
self.sign_in_button = self
278
class FakeSetupAccountPageView(FakeGenericView):
280
"""Fake SetupAccount view."""
283
super(FakeSetupAccountPageView, self).__init__()
286
self.name_label = QLabel()
287
self.email_label = QLabel()
288
self.confirm_email_label = QLabel()
289
self.password_label = QLabel()
290
self.confirm_password_label = QLabel()
291
self.password_info_label = QLabel()
292
self.refresh_label = QLabel()
293
self.name_assistance = QLabel()
294
self.email_assistance = QLabel()
295
self.confirm_email_assistance = QLabel()
297
self.captcha_solution_edit = QLineEdit()
298
self.name_edit = QLineEdit()
299
self.email_edit = QLineEdit()
300
self.confirm_email_edit = QLineEdit()
301
self.password_edit = QLineEdit()
302
self.confirm_password_edit = QLineEdit()
303
self.captcha_solution_edit = QLineEdit()
305
self.terms_checkbox = QCheckBox()
307
self.set_up_button = FakeButton()
309
self.on_captcha_generated_cb = None
310
self.on_captcha_generation_error_cb = None
311
self.on_user_registered_cb = None
312
self.on_user_registration_error_cb = None
314
def fake_backend(self):
315
"""Fake get_backend."""
318
def set_line_edit_validation_rule(self, edit, function):
319
"""Fake set_line_edit_validation_rule from view."""
320
elements = self.properties.get('validation_rule', None)
323
elements.append((edit, function))
324
self.properties['validation_rule'] = elements
326
# pylint: disable=C0103
327
def registerField(self, key, edit):
328
"""Fake registerField from page."""
329
self.properties[key] = edit
330
# pylint: enable=C0103
332
def set_error_message(self, label, message):
333
"""Fake set_error_message."""
334
label.setText(message)
337
class FakeMessageBox(object):
339
"""A fake message box."""
344
def __init__(self, *args, **kwargs):
345
self.args = (args, kwargs)
347
def critical(self, *args, **kwargs):
348
"""Fake critical popup."""
349
self.critical_args = (args, kwargs)
352
class FakeSetupAccountUi(object):
353
"""Fake ui variable for FakeView."""
355
email_assistance = None
358
class FakeView(object):
363
ui = FakeSetupAccountUi()
366
"""Use itself as a fake wizard, too."""
369
def set_error_message(self, label, msg):
370
"""Simulate to add the message to the label as SetupAccount do."""
373
class FakeSetupAccountView(FakeView):
379
captcha_refresh_executed = False
380
captcha_refreshing_value = False
382
def on_captcha_refresh_complete(self):
383
"""Fake the call to refresh finished."""
384
self.captcha_refresh_executed = True
386
def on_captcha_refreshing(self):
387
"""Fake the call to refreshing."""
388
self.captcha_refreshing_value = True
390
def fake_open(self, filename):
391
"""Fake open for Image."""
394
# pylint: disable=W0622
395
def save(self, io, format):
396
"""Fake save for Image."""
397
# pylint: enable=W0622
400
class FakeControllerForCaptcha(object):
402
"""Fake controller to test refresh_captcha method."""
405
"""Initialize FakeControllerForCaptcha."""
406
self.callback_error = False
408
def generate_captcha(self, *args):
409
"""Fake generate deferred for captcha."""
412
# pylint: disable=C0103
413
def addErrback(self, call):
414
"""Fake addErrback."""
415
self.callback_error = call is not None
416
# pylint: enable=C0103
419
class FakeEmailVerificationView(FakePageUiStyle):
420
"""Fake EmailVerification Page."""
422
def __init__(self, *args):
424
super(FakeEmailVerificationView, self).__init__(*args)
425
self.verification_code = ''
426
self.next_button = self
429
class EmailVerificationView(FakeGenericView):
431
"""Fake Email Verification view."""
434
super(EmailVerificationView, self).__init__()
437
self.verification_code_edit = QLineEdit()
438
self.verification_code = ''
440
self.next_button = FakeButton()
442
self.on_email_validated_cb = None
443
self.on_email_validation_error_cb = None
445
def fake_backend(self):
446
"""Fake get_backend."""
449
def validate_email(self, *args, **kwargs):
450
"""Fake validate_email method from backend"""
451
self.properties['validate_email'] = (args, kwargs)
454
class ErrorPageView(FakeGenericView):
456
"""Fake Error Page view."""
459
super(ErrorPageView, self).__init__()
461
self.error_message_label = QLabel()
464
class SuccessPageView(FakeGenericView):
466
"""Fake Success Page view."""
469
super(SuccessPageView, self).__init__()
471
self.success_message_label = QLabel()
474
class UbuntuSSOView(object):
476
"""Fake view for UbuntuSSO."""
484
self.app_name = 'app_name'
485
self.success_message_label = QLabel()
487
# pylint: disable=C0103
488
self.loginSuccess = FakeButton()
489
self.registrationSuccess = FakeButton()
490
# pylint: enable=C0103
491
self.page = FakeGenericView()
493
def button(self, *args):
495
self.properties['button_method'] = args
496
self.properties['button'] = FakeButton()
497
return self.properties['button']
499
def callback(self, *args, **kwargs):
501
self.properties['callback'] = (args, kwargs)
506
self.properties['close'] = True
508
# pylint: disable=C0103
509
def setButtonLayout(self, layout):
510
"""Fake setButtonLayout."""
511
self.properties['buttons_layout'] = layout
513
def currentPage(self):
514
"""Fake currentPage."""
517
def setWizardStyle(self, style):
518
"""Fake setWizardStyle."""
519
self.properties['wizard_style'] = style
520
# pylint: enable=C0103
523
"""Fake next method to move to to next page in the wizard."""
524
self.properties['wizard_next'] = True
527
class FakeLineEditForForgottenPage(object):
532
"""Initilize object."""
533
self.email_text = QString()
536
"""Fake text for QLineEdit."""
537
return self.email_text
539
# setText is inherited
540
# pylint: disable=C0103
541
def setText(self, text):
542
"""Fake setText for QLineEdit."""
543
self.email_text = QString(text)
544
# pylint: enable=C0103
547
class FakeForgottenPasswordPage(FakePageUiStyle):
551
super(FakeForgottenPasswordPage, self).__init__()
552
self.email_address_line_edit = self
553
self.send_button = self
554
self.email_widget = FakePageUiStyle()
555
self.forgotted_password_intro_label = FakePageUiStyle()
556
self.try_again_widget = FakePageUiStyle()
557
self.email_line_edit = FakeLineEditForForgottenPage()
559
def fake_backend(self):
560
"""Fake get_backend."""
564
class FakeForgottenPasswordPageView(FakeGenericView):
568
super(FakeForgottenPasswordPageView, self).__init__()
569
self.email_address_line_edit = QLineEdit()
570
self.send_button = FakeButton()
571
self.try_again_button = FakeButton()
572
self.email_widget = FakePageUiStyle()
573
self.forgotted_password_intro_label = QLabel()
574
self.email_address_label = QLabel()
575
self.try_again_widget = FakePageUiStyle()
576
self.email_line_edit = QLineEdit()
578
self.on_password_reset_error_cb = None
579
self.on_password_reset_token_sent_cb = None
581
def fake_backend(self):
582
"""Fake get_backend."""
585
# pylint: disable=C0103
586
def registerField(self, key, item):
587
"""Fake registerField from wizard."""
588
self.properties['field_%s' % key] = item
589
# pylint: enable=C0103
591
def field(self, key):
592
"""Fake field from wizard"""
593
return self.properties['field_%s' % key]
595
def set_line_edit_validation_rule(self, edit, function):
596
"""Fake set_line_edit_validation_rule from view."""
597
elements = self.properties.get('validation_rule', None)
600
elements.append((edit, function))
601
self.properties['validation_rule'] = elements
604
class ResetPasswordPageView(FakeGenericView):
608
super(ResetPasswordPageView, self).__init__()
609
self.reset_password_button = FakeButton()
610
self.reset_code_line_edit = QLineEdit()
611
self.password_line_edit = QLineEdit()
612
self.confirm_password_line_edit = QLineEdit()
614
self.on_password_changed_cb = None
615
self.on_password_change_error_cb = None
617
def fake_backend(self):
618
"""Fake get_backend."""
621
# pylint: disable=C0103
622
def registerField(self, key, item):
623
"""Fake registerField from wizard."""
624
self.properties['field_%s' % key] = item
625
# pylint: enable=C0103
627
def field(self, key):
628
"""Fake field from wizard"""
629
return self.properties['field_%s' % key]
631
def set_line_edit_validation_rule(self, edit, function):
632
"""Fake set_line_edit_validation_rule from view."""
633
elements = self.properties.get('validation_rule', None)
636
elements.append((edit, function))
637
self.properties['validation_rule'] = elements
639
def set_new_password(self, *args, **kwargs):
640
"""Fake set_new_password from backend."""
641
self.properties['backend_new_password'] = (args, kwargs)
644
class FakeResetPasswordPage(FakePageUiStyle):
646
"""Fake ResetPasswordPage."""
648
def __init__(self, *args):
650
super(FakeResetPasswordPage, self).__init__()
651
self.ui.reset_code_line_edit = FakeLineEdit()
652
self.ui.password_line_edit = FakeLineEdit()
653
self.ui.confirm_password_line_edit = FakeLineEdit()
654
self.reset_password_button = self
655
self._enabled = 9 # Intentionally wrong.
658
class FakeWizardForResetPassword(object):
660
"""Fake Wizard for ResetPasswordController."""
663
self.current_user = self
665
self.email_edit = self
666
self.email_line_edit = self
667
self.forgotten = self
670
self.hide_value = False
671
self.text_value = 'mail@mail.com'
674
"""Fake wizard function for view."""
678
"""Fake back for wizard."""
682
"""Fake hide for overlay."""
683
self.hide_value = True
686
"""Fake text for QLineEdit."""
687
return self.text_value
689
# pylint: disable=C0103
690
def setText(self, text):
691
"""Fake setText for QLineEdit."""
692
self.text_value = text
693
# pylint: enable=C0103
84
696
class BackendControllerTestCase(BaseTestCase):
85
697
"""The test case for the BackendController."""
96
708
self.assertTrue(self.sso_login_backend is result)
99
class ChooseSignInControllerTestCase(MockerTestCase):
711
class ChooseSignInControllerTestCase(BaseTestCase):
100
712
"""Test the choose sign in controller."""
104
716
super(ChooseSignInControllerTestCase, self).setUp()
105
self.view = self.mocker.mock()
106
self.backend = self.mocker.mock()
107
self.controller = ChooseSignInController()
718
self.subtitle = 'subtitle'
719
self.controller = ChooseSignInController(self.title, self.subtitle)
720
self.view = FakeChooseSignInView()
108
721
self.controller.view = self.view
109
self.controller.backend = self.backend
111
self.subtitle = 'sub'
113
723
def test_setup_ui(self):
114
724
"""Test the set up of the ui."""
115
self.controller._title = self.title
116
self.controller._subtitle = self.subtitle
117
self.controller._set_up_translated_strings()
118
self.view.header.set_title(self.title)
119
self.view.header.set_subtitle(self.subtitle)
120
self.controller._connect_buttons()
122
725
self.controller.setupUi(self.view)
726
self.assertEqual(self.view.properties['title'], self.title)
727
self.assertEqual(self.view.properties['subtitle'], self.subtitle)
728
self.assertEqual(self.view.existing_account_button.text(),
729
EXISTING_ACCOUNT_CHOICE_BUTTON)
730
self.assertEqual(self.view.setup_account_button.text(),
731
SET_UP_ACCOUNT_CHOICE_BUTTON)
732
self.assertIsInstance(self.view.existing_account_button.function,
733
collections.Callable)
734
self.assertIsInstance(self.view.setup_account_button.function,
735
collections.Callable)
124
737
def test_set_up_translated_strings(self):
125
738
"""Ensure that the translations are used."""
126
self.view.ui.existing_account_button.setText(
127
EXISTING_ACCOUNT_CHOICE_BUTTON)
128
self.view.ui.setup_account_button.setText(SET_UP_ACCOUNT_CHOICE_BUTTON)
130
739
self.controller._set_up_translated_strings()
740
self.assertEqual(self.view.existing_account_button.text(),
741
EXISTING_ACCOUNT_CHOICE_BUTTON)
742
self.assertEqual(self.view.setup_account_button.text(),
743
SET_UP_ACCOUNT_CHOICE_BUTTON)
132
745
def test_connect_buttons(self):
133
746
"""Ensure that all the buttons are correcly connected."""
134
self.view.ui.existing_account_button.clicked.connect(
135
self.controller._set_next_existing)
136
self.view.ui.setup_account_button.clicked.connect(
137
self.controller._set_next_new)
139
747
self.controller._connect_buttons()
748
self.assertIsInstance(self.view.existing_account_button.function,
749
collections.Callable)
750
self.assertIsInstance(self.view.setup_account_button.function,
751
collections.Callable)
141
753
def test_set_next_existing(self):
142
754
"""Test the execution of the callback."""
144
self.view.wizard().current_user_page_id
145
self.mocker.result(next_id)
146
self.view.next = next_id
147
self.view.wizard().next()
149
755
self.controller._set_next_existing()
756
self.assertTrue(self.view.properties['wizard_next'])
757
self.assertEqual(self.view.next,
758
self.view.fake_wizard.current_user_page_id)
151
760
def test_set_next_new(self):
152
761
"""Test the execution of the callback."""
154
self.view.wizard().setup_account_page_id
155
self.mocker.result(next_id)
156
self.view.next = next_id
157
self.view.wizard().next()
159
762
self.controller._set_next_new()
162
class CurrentUserControllerTestCase(MockerTestCase):
763
self.assertTrue(self.view.properties['wizard_next'])
764
self.assertEqual(self.view.next,
765
self.view.fake_wizard.setup_account_page_id)
768
class CurrentUserControllerTestCase(BaseTestCase):
163
769
"""Test the current user controller."""
166
772
"""Setup tests."""
167
773
super(CurrentUserControllerTestCase, self).setUp()
168
self.view = self.mocker.mock()
169
self.backend = self.mocker.mock()
170
self.connect = self.mocker.mock()
171
self.signal = self.mocker.replace('PyQt4.QtCore.SIGNAL')
172
774
self.controller = CurrentUserController(title='the title',
173
775
subtitle='the subtitle')
776
self.view = FakeCurrentUserView()
174
777
self.controller.view = self.view
175
self.controller.backend = self.backend
778
self.patch(self.controller, "get_backend", self.view.fake_backend)
779
self.controller.setupUi(self.view)
177
781
def test_translated_strings(self):
178
782
"""test that the ui is correctly set up."""
179
self.view.ui.email_label.setText(EMAIL_LABEL)
180
self.view.ui.password_label.setText(LOGIN_PASSWORD_LABEL)
181
self.view.ui.forgot_password_label.setText(
182
FAKE_URL % FORGOTTEN_PASSWORD_BUTTON)
183
self.view.ui.sign_in_button.setText(SIGN_IN_BUTTON)
185
783
self.controller._set_translated_strings()
784
self.assertEqual(self.view.email_label.text(), EMAIL_LABEL)
785
self.assertEqual(self.view.password_label.text(), LOGIN_PASSWORD_LABEL)
786
self.assertEqual(self.view.forgot_password_label.text(),
787
FAKE_URL % FORGOTTEN_PASSWORD_BUTTON)
788
self.assertEqual(self.view.sign_in_button.text(), SIGN_IN_BUTTON)
187
790
def test_connect_ui(self):
188
791
"""test that the ui is correctly set up."""
189
self.view.ui.sign_in_button.clicked.connect(self.controller.login)
190
self.backend.on_login_error_cb = MATCH(callable)
191
self.backend.on_logged_in_cb = MATCH(callable)
192
self.view.ui.forgot_password_label.linkActivated.connect(
194
self.view.ui.email_edit.textChanged.connect(MATCH(callable))
195
self.view.ui.password_edit.textChanged.connect(MATCH(callable))
197
792
self.controller._connect_ui()
793
self.assertIsInstance(self.view.sign_in_button.function,
794
collections.Callable)
795
# Check if receivers is 2 because _connect_ui was called on setUp
796
self.assertEqual(self.view.forgot_password_label.receivers(
797
SIGNAL('linkActivated(QString)')), 2)
798
self.assertEqual(self.view.email_edit.receivers(
799
SIGNAL('textChanged(QString)')), 2)
800
self.assertEqual(self.view.password_edit.receivers(
801
SIGNAL('textChanged(QString)')), 2)
802
self.assertFalse(self.view.on_login_error_cb is None)
803
self.assertFalse(self.view.on_logged_in_cb is None)
199
805
def test_title_subtitle(self):
200
806
"""Ensure we are storing the title and subtitle correctly."""
202
self.assertEqual(self.controller._title, 'the title')
203
self.assertEqual(self.controller._subtitle, 'the subtitle')
206
class FakeLineEdit(object):
208
"""A fake QLineEdit."""
210
def __init__(self, *args):
218
# setText is inherited
219
# pylint: disable=C0103
220
def setText(self, text):
221
"""Return saved text."""
225
class FakePage(object):
227
"""A fake wizard page."""
236
class FakeCurrentUserPage(FakePageUiStyle):
238
"""Fake CurrentuserPage."""
240
def __init__(self, *args):
242
super(FakeCurrentUserPage, self).__init__(*args)
243
self.ui.email_edit = FakeLineEdit()
244
self.ui.password_edit = FakeLineEdit()
245
self.sign_in_button = self
248
class CurrentUserControllerValidationMockTest(MockerTestCase):
249
"""Test the choose sign in controller."""
253
super(CurrentUserControllerValidationMockTest, self).setUp()
254
self.view = self.mocker.mock()
255
self.backend = self.mocker.mock()
256
self.controller = CurrentUserController()
257
self.controller.view = self.view
258
self.controller.backend = self.backend
260
self.subtitle = 'sub'
807
self.assertEqual(self.view.properties['title'], 'the title')
808
self.assertEqual(self.view.properties['subtitle'], 'the subtitle')
810
def test_on_logged_in(self):
811
"""Test on_logged_in method."""
812
self.view.email_edit.setText('email@email.com')
813
self.controller.on_logged_in('app-name', {})
814
self.assertEqual(self.view.fake_wizard.properties['emit'],
815
('app-name', 'email@email.com'))
817
def test_login(self):
818
"""Test login method."""
819
email = 'email@email.com'
820
password = 'password'
821
self.view.email_edit.setText(email)
822
self.view.password_edit.setText(password)
823
self.controller.login()
824
self.assertEqual(self.view.properties['backend_login'],
825
(self.view.fake_wizard.app_name, email, password))
826
self.assertIsInstance(self.view.properties['login-addErrback'],
827
collections.Callable)
829
def test_on_forgotten_password(self):
830
"""Test on_forgotten_password flow."""
831
email = 'email@email.com'
832
self.view.email_edit.setText(email)
833
self.controller.on_forgotten_password()
834
self.assertEqual(self.view.next,
835
self.view.fake_wizard.forgotten_password_page_id)
836
self.assertTrue(self.view.properties['wizard_next'])
262
838
def test_setup_ui(self):
263
839
"""Test the set up of the ui."""
264
self.controller._title = self.title
265
self.controller._subtitle = self.subtitle
266
self.backend = yield self.controller.get_backend()
267
self.view.header.set_title(self.title)
268
self.view.header.set_subtitle(self.subtitle)
269
self.controller._set_translated_strings()
840
self.controller._title = 'title_setup'
841
self.controller._subtitle = 'subtitle_setup'
271
842
self.controller.setupUi(self.view)
843
self.assertEqual(self.view.properties['title'], 'title_setup')
844
self.assertEqual(self.view.properties['subtitle'], 'subtitle_setup')
845
self.assertIsInstance(self.view.sign_in_button.function,
846
collections.Callable)
847
# Check if receivers is 2 because _connect_ui was called on setUp
848
self.assertEqual(self.view.forgot_password_label.receivers(
849
SIGNAL('linkActivated(QString)')), 2)
850
self.assertEqual(self.view.email_edit.receivers(
851
SIGNAL('textChanged(QString)')), 2)
852
self.assertEqual(self.view.password_edit.receivers(
853
SIGNAL('textChanged(QString)')), 2)
854
self.assertFalse(self.view.on_login_error_cb is None)
855
self.assertFalse(self.view.on_logged_in_cb is None)
856
self.assertEqual(self.view.email_label.text(), EMAIL_LABEL)
857
self.assertEqual(self.view.password_label.text(), LOGIN_PASSWORD_LABEL)
858
self.assertEqual(self.view.forgot_password_label.text(),
859
FAKE_URL % FORGOTTEN_PASSWORD_BUTTON)
860
self.assertEqual(self.view.sign_in_button.text(), SIGN_IN_BUTTON)
274
863
class CurrentUserControllerErrorTestCase(BaseTestCase):
431
1020
not self.controller.view.sign_in_button.enabled())
434
class SetUpAccountControllerTestCase(MockerTestCase):
1023
class SetUpAccountControllerTestCase(BaseTestCase):
435
1024
"""test the controller used to setup a new account."""
437
1026
def setUp(self):
438
1027
"""Set the different tests."""
439
1028
super(SetUpAccountControllerTestCase, self).setUp()
440
self.view = self.mocker.mock()
441
self.backend = self.mocker.mock()
442
self.get_password_strength = self.mocker.replace(
443
'ubuntu_sso.utils.ui.get_password_strength')
444
self.app_name = 'test'
446
self.message_box = self.mocker.mock()
1029
self.view = FakeSetupAccountPageView()
1030
self.message_box = FakeMessageBox()
447
1031
self.controller = SetUpAccountController(message_box=self.message_box)
448
self.controller.backend = self.backend
1032
self.patch(self.controller, "get_backend", self.view.fake_backend)
449
1033
self.controller.view = self.view
1034
self.controller.backend = self.view
451
1036
def test_set_translated_strings(self):
452
1037
"""Ensure all the strings are set."""
453
self.view.ui.name_label.setText(NAME_ENTRY)
454
self.view.ui.email_label.setText(EMAIL1_ENTRY)
455
self.view.ui.confirm_email_label.setText(EMAIL2_ENTRY)
456
self.view.ui.password_label.setText(PASSWORD1_ENTRY)
457
self.view.ui.confirm_password_label.setText(PASSWORD2_ENTRY)
458
self.view.ui.password_info_label.setText(PASSWORD_HELP)
459
self.view.ui.captcha_solution_edit.setPlaceholderText(
460
CAPTCHA_SOLUTION_ENTRY)
462
1038
self.controller._set_translated_strings()
1039
self.assertEqual(self.view.name_label.text(), NAME_ENTRY)
1040
self.assertEqual(self.view.email_label.text(), EMAIL1_ENTRY)
1041
self.assertEqual(self.view.confirm_email_label.text(), EMAIL2_ENTRY)
1042
self.assertEqual(self.view.password_label.text(), PASSWORD1_ENTRY)
1043
self.assertEqual(self.view.confirm_password_label.text(),
1045
self.assertEqual(self.view.password_info_label.text(), PASSWORD_HELP)
1046
self.assertEqual(self.view.captcha_solution_edit.placeholderText(),
1047
CAPTCHA_SOLUTION_ENTRY)
464
1049
def test_set_titles(self):
465
1050
"""Test how the different titles are set."""
466
self.view.wizard().app_name
467
self.mocker.result(self.app_name)
468
self.view.wizard().help_text
469
self.mocker.result(self.help)
470
self.view.header.set_title(
471
JOIN_HEADER_LABEL % {'app_name': self.app_name})
472
self.view.header.set_subtitle(self.help)
474
1051
self.controller._set_titles()
1052
self.assertEqual(self.view.properties['title'],
1053
JOIN_HEADER_LABEL % {'app_name': self.view.fake_wizard.app_name})
1054
self.assertEqual(self.view.properties['subtitle'],
1055
self.view.fake_wizard.help_text)
476
1057
def test_connect_ui_elements(self):
477
1058
"""Test that the ui elements are correctly connect."""
478
self.controller._enable_setup_button
479
self.mocker.result(lambda: None)
480
self.view.ui.name_edit.textEdited.connect(MATCH(callable))
481
self.view.ui.email_edit.textEdited.connect(MATCH(callable))
482
self.view.ui.confirm_email_edit.textEdited.connect(MATCH(callable))
483
self.view.ui.password_edit.textEdited.connect(MATCH(callable))
484
self.view.ui.confirm_password_edit.textEdited.connect(MATCH(callable))
485
self.view.ui.captcha_solution_edit.textEdited.connect(MATCH(callable))
486
self.view.ui.refresh_label.linkActivated.connect(MATCH(callable))
487
self.view.terms_checkbox.stateChanged.connect(MATCH(callable))
1059
self.controller._connect_ui_elements()
1060
self.assertEqual(self.view.name_edit.receivers(
1061
SIGNAL('textEdited(QString)')), 1)
1062
self.assertEqual(self.view.email_edit.receivers(
1063
SIGNAL('textEdited(QString)')), 1)
1064
self.assertEqual(self.view.confirm_email_edit.receivers(
1065
SIGNAL('textEdited(QString)')), 1)
1066
self.assertEqual(self.view.password_edit.receivers(
1067
SIGNAL('textEdited(QString)')), 1)
1068
self.assertEqual(self.view.confirm_password_edit.receivers(
1069
SIGNAL('textEdited(QString)')), 1)
1070
self.assertEqual(self.view.captcha_solution_edit.receivers(
1071
SIGNAL('textEdited(QString)')), 1)
1072
self.assertEqual(self.view.refresh_label.receivers(
1073
SIGNAL('linkActivated(QString)')), 1)
1074
self.assertEqual(self.view.terms_checkbox.receivers(
1075
SIGNAL('stateChanged(int)')), 1)
488
1076
# set the callbacks for the captcha generation
489
self.backend.on_captcha_generated_cb = MATCH(callable)
490
self.backend.on_captcha_generation_error_cb = MATCH(callable)
491
self.backend.on_user_registration_error_cb = MATCH(callable)
492
self.backend.on_user_registered_cb = MATCH(callable)
494
self.controller._connect_ui_elements()
496
def test_is_correct_password_confirmation_false(self):
497
"""Test when the password is not correct."""
499
self.view.ui.password_edit.text()
500
self.mocker.result('other')
503
self.controller.is_correct_password_confirmation(password))
505
def test_is_correct_password_confirmation_true(self):
506
"""Test when the password is correct."""
508
self.view.ui.password_edit.text()
509
self.mocker.result(password)
512
self.controller.is_correct_password_confirmation(password))
514
def test_is_correct_email_confirmation_false(self):
515
"""Test when the emails are not the same."""
516
email_address = 'address'
517
self.view.ui.email_edit.text()
518
self.mocker.result('other')
521
self.controller.is_correct_email_confirmation(email_address))
523
def test_is_correct_email_confirmation_true(self):
524
"""Test when the emails are the same."""
525
email_address = 'address'
526
self.view.ui.email_edit.text()
527
self.mocker.result(email_address)
530
self.controller.is_correct_email_confirmation(email_address))
532
def test_set_next_validation(self):
533
"""Test the callback."""
535
email = 'email@example.com'
536
password = 'Qwerty9923'
537
captcha_id = 'captcha_id'
538
captcha_solution = 'captcha_solution'
539
self.view.wizard().app_name
540
self.mocker.result(self.app_name)
541
self.view.ui.name_edit.text()
542
self.mocker.result(name)
543
self.view.ui.name_edit.text()
544
self.mocker.result(name)
545
self.view.ui.email_edit.text()
546
self.mocker.result(email)
547
self.view.ui.email_edit.text()
548
self.mocker.result(email)
549
self.view.ui.confirm_email_edit.text()
550
self.mocker.result(email)
551
self.view.ui.password_edit.text()
552
self.mocker.result(password)
553
self.view.ui.password_edit.text()
554
self.mocker.result(password)
555
self.view.ui.confirm_password_edit.text()
556
self.mocker.result(password)
558
self.mocker.result(captcha_id)
559
self.view.ui.captcha_solution_edit.text()
560
self.mocker.result(captcha_solution)
561
self.backend.register_user(self.app_name, email, password, name,
562
captcha_id, captcha_solution)
563
self.view.wizard().email_verification_page_id
564
self.view.wizard().page(None).controller.set_titles()
565
self.view.ui.captcha_solution_edit.text()
567
self.controller.set_next_validation()
569
def test_set_next_validation_wrong_email(self):
570
"""Test the callback when there is a wrong email."""
572
email = 'email@example.com'
573
password = 'Qwerty9923'
574
confirm_email_assistance = 'Confirm email'
575
captcha_solution = 'captcha'
576
self.view.ui.email_edit.text()
577
self.mocker.result(email)
578
self.view.ui.confirm_email_edit.text()
579
self.mocker.result('email2')
580
self.view.ui.password_edit.text()
581
self.mocker.result(password)
582
self.view.ui.name_edit.text()
583
self.mocker.result(name)
584
self.view.ui.confirm_password_edit.text()
585
self.mocker.result(password)
586
self.view.ui.captcha_solution_edit.text()
587
self.mocker.result(captcha_solution)
588
self.view.ui.confirm_email_assistance
589
self.mocker.result(confirm_email_assistance)
590
self.view.set_error_message(confirm_email_assistance,
593
self.assertFalse(self.controller.validate_form())
595
def test_set_next_validation_not_min_password(self):
596
"""Test the callback with a weak password."""
598
weak_password = 'weak'
599
email = 'email@example.com'
600
captcha_solution = 'captcha'
601
self.view.ui.email_edit.text()
602
self.mocker.result(email)
603
self.view.ui.confirm_email_edit.text()
604
self.mocker.result(email)
605
self.view.ui.password_edit.text()
606
self.mocker.result(weak_password)
607
self.view.ui.name_edit.text()
608
self.mocker.result(name)
609
self.view.ui.captcha_solution_edit.text()
610
self.mocker.result(captcha_solution)
611
self.view.ui.confirm_password_edit.text()
612
self.mocker.result(weak_password)
613
self.message_box.critical(PASSWORD_TOO_WEAK, self.controller.view)
615
self.assertFalse(self.controller.validate_form())
617
def test_set_next_validation_wrong_password(self):
618
"""Test the callback where the password is wrong."""
620
password = 'Qwerty9923'
621
email = 'email@example.com'
622
captcha_solution = 'captcha'
623
self.view.ui.email_edit.text()
624
self.mocker.result(email)
625
self.view.ui.confirm_email_edit.text()
626
self.mocker.result(email)
627
self.view.ui.password_edit.text()
628
self.mocker.result(password)
629
self.view.ui.name_edit.text()
630
self.mocker.result(name)
631
self.view.ui.confirm_password_edit.text()
632
self.mocker.result('other_password')
633
self.view.ui.captcha_solution_edit.text()
634
self.mocker.result(captcha_solution)
635
self.message_box.critical(PASSWORD_MISMATCH, self.controller.view)
637
self.assertFalse(self.controller.validate_form())
639
def test_set_next_validation_empty_captcha(self):
640
"""Test the callback where the password is wrong."""
642
password = 'Qwerty9923'
643
email = 'email@example.com'
644
captcha_solution = ''
645
self.view.ui.email_edit.text()
646
self.mocker.result(email)
647
self.view.ui.confirm_email_edit.text()
648
self.mocker.result(email)
649
self.view.ui.password_edit.text()
650
self.mocker.result(password)
651
self.view.ui.name_edit.text()
652
self.mocker.result(name)
653
self.view.ui.confirm_password_edit.text()
654
self.mocker.result(password)
655
self.view.ui.captcha_solution_edit.text()
656
self.mocker.result(captcha_solution)
657
self.message_box.critical(CAPTCHA_REQUIRED_ERROR, self.controller.view)
659
self.assertFalse(self.controller.validate_form())
1077
self.assertIsInstance(self.view.on_captcha_generated_cb,
1078
collections.Callable)
1079
self.assertIsInstance(self.view.on_captcha_generation_error_cb,
1080
collections.Callable)
1081
self.assertIsInstance(self.view.on_user_registration_error_cb,
1082
collections.Callable)
1083
self.assertIsInstance(self.view.on_user_registered_cb,
1084
collections.Callable)
661
1086
def test_set_line_edits_validations(self):
662
"""Set the validations to be performed on the edits."""
663
self.view.ui.email_edit
664
self.mocker.result(self.view)
665
self.view.set_line_edit_validation_rule(self.view, is_correct_email)
666
self.view.ui.confirm_email_edit
667
self.mocker.result(self.view)
668
self.view.set_line_edit_validation_rule(self.view, MATCH(callable))
669
self.view.ui.confirm_email_edit.textChanged.emit
670
self.mocker.result(lambda: None)
671
self.view.ui.email_edit.textChanged.connect(MATCH(callable))
672
self.view.ui.password_edit
673
self.mocker.result(self.view)
674
self.view.set_line_edit_validation_rule(self.view,
675
is_min_required_password)
676
self.view.ui.confirm_password_edit
677
self.mocker.result(self.view)
678
self.view.set_line_edit_validation_rule(self.view, MATCH(callable))
679
self.view.ui.confirm_password_edit.textChanged.emit
680
self.mocker.result(lambda: None)
681
self.view.ui.password_edit.textChanged.connect(MATCH(callable))
1087
"""Test _set_line_validations from controller."""
683
1088
self.controller._set_line_edits_validations()
686
class FakeMessageBox(object):
688
"""A fake message box."""
693
def __init__(self, *args, **kwargs):
694
self.args = (args, kwargs)
696
def critical(self, *args, **kwargs):
697
"""Fake critical popup."""
698
self.critical_args = (args, kwargs)
701
class FakeSetupAccountUi(object):
702
"""Fake ui variable for FakeView."""
704
email_assistance = None
707
class FakeView(object):
712
ui = FakeSetupAccountUi()
715
"""Use itself as a fake wizard, too."""
718
def set_error_message(self, label, msg):
719
"""Simulate to add the message to the label as SetupAccount do."""
722
class FakeSetupAccountView(FakeView):
728
captcha_refresh_executed = False
729
captcha_refreshing_value = False
731
def on_captcha_refresh_complete(self):
732
"""Fake the call to refresh finished."""
733
self.captcha_refresh_executed = True
735
def on_captcha_refreshing(self):
736
"""Fake the call to refreshing."""
737
self.captcha_refreshing_value = True
739
def fake_open(self, filename):
740
"""Fake open for Image."""
743
# pylint: disable=W0622
744
def save(self, io, format):
745
"""Fake save for Image."""
746
# pylint: enable=W0622
749
class FakeControllerForCaptcha(object):
751
"""Fake controller to test refresh_captcha method."""
754
"""Initialize FakeControllerForCaptcha."""
755
self.callback_error = False
757
def generate_captcha(self, *args):
758
"""Fake generate deferred for captcha."""
761
# pylint: disable=C0103
762
def addErrback(self, call):
763
"""Fake addErrback."""
764
self.callback_error = call is not None
765
# pylint: enable=C0103
768
class FakeEmailVerificationView(FakePageUiStyle):
769
"""Fake EmailVerification Page."""
771
def __init__(self, *args):
773
super(FakeEmailVerificationView, self).__init__(*args)
774
self.verification_code = ''
775
self.next_button = self
1089
elements = self.view.properties['validation_rule']
1090
self.assertFalse(elements is None)
1092
self.assertTrue(type(e[0]) is QLineEdit)
1093
self.assertIsInstance(e[1], collections.Callable)
1095
def all_valid(self):
1096
"""Set all the widgets to a valid state."""
1097
self.view.name_edit.setText('Name')
1098
self.view.email_edit.setText('email@email.com')
1099
self.view.confirm_email_edit.setText('email@email.com')
1100
self.view.password_edit.setText('T3st3rqw')
1101
self.view.confirm_password_edit.setText('T3st3rqw')
1102
self.view.captcha_solution_edit.setText('captcha solution')
1103
self.view.terms_checkbox.setChecked(True)
1105
def all_invalid(self):
1106
"""Set all the widgets to an invalid state."""
1107
self.view.name_edit.setText('')
1108
self.view.email_edit.setText('')
1109
self.view.confirm_email_edit.setText('email')
1110
self.view.password_edit.setText('')
1111
self.view.confirm_password_edit.setText('t')
1112
self.view.captcha_solution_edit.setText('')
1113
self.view.terms_checkbox.setChecked(False)
1115
def test_enable_setup_button_only_checkbox_ok(self):
1116
"""Test enable button only name valid."""
1118
self.view.terms_checkbox.setChecked(True)
1119
self.controller._enable_setup_button()
1121
self.assertFalse(self.view.set_up_button.enabled())
1122
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1123
self.assertTrue(self.view.set_up_button.properties['polish'])
1124
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1126
def test_enable_setup_button_only_name_ok(self):
1127
"""Test enable button only name valid."""
1129
self.view.name_edit.setText('Name')
1130
self.controller._enable_setup_button()
1132
self.assertFalse(self.view.set_up_button.enabled())
1133
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1134
self.assertTrue(self.view.set_up_button.properties['polish'])
1135
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1137
def test_enable_setup_button_only_email_ok(self):
1138
"""Test enable button only name valid."""
1140
self.view.email_edit.setText('email@email.com')
1141
self.controller._enable_setup_button()
1143
self.assertFalse(self.view.set_up_button.enabled())
1144
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1145
self.assertTrue(self.view.set_up_button.properties['polish'])
1146
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1148
def test_enable_setup_button_only_confirm_email_ok(self):
1149
"""Test enable button only name valid."""
1151
self.view.confirm_email_edit.setText('email@email.com')
1152
self.controller._enable_setup_button()
1154
self.assertFalse(self.view.set_up_button.enabled())
1155
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1156
self.assertTrue(self.view.set_up_button.properties['polish'])
1157
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1159
def test_enable_setup_button_only_email_and_confirm_ok(self):
1160
"""Test enable button only name valid."""
1162
self.view.email_edit.setText('email@email.com')
1163
self.view.confirm_email_edit.setText('email@email.com')
1164
self.controller._enable_setup_button()
1166
self.assertFalse(self.view.set_up_button.enabled())
1167
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1168
self.assertTrue(self.view.set_up_button.properties['polish'])
1169
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1171
def test_enable_setup_button_only_password_ok(self):
1172
"""Test enable button only name valid."""
1174
self.view.password_edit.setText('T3st3rqw')
1175
self.controller._enable_setup_button()
1177
self.assertFalse(self.view.set_up_button.enabled())
1178
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1179
self.assertTrue(self.view.set_up_button.properties['polish'])
1180
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1182
def test_enable_setup_button_only_password_confirm_ok(self):
1183
"""Test enable button only name valid."""
1185
self.view.confirm_password_edit.setText('T3st3rqw')
1186
self.controller._enable_setup_button()
1188
self.assertFalse(self.view.set_up_button.enabled())
1189
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1190
self.assertTrue(self.view.set_up_button.properties['polish'])
1191
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1193
def test_enable_setup_button_only_password_and_confirm_ok(self):
1194
"""Test enable button only name valid."""
1196
self.view.password_edit.setText('T3st3rqw')
1197
self.view.confirm_password_edit.setText('T3st3rqw')
1198
self.controller._enable_setup_button()
1200
self.assertFalse(self.view.set_up_button.enabled())
1201
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1202
self.assertTrue(self.view.set_up_button.properties['polish'])
1203
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1205
def test_enable_setup_button_only_captcha_ok(self):
1206
"""Test enable button only name valid."""
1208
self.view.captcha_solution_edit.setText('captcha solution')
1209
self.controller._enable_setup_button()
1211
self.assertFalse(self.view.set_up_button.enabled())
1212
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1213
self.assertTrue(self.view.set_up_button.properties['polish'])
1214
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1216
def test_enable_setup_button_only_all_ok(self):
1217
"""Test enable button only name valid."""
1219
self.controller._enable_setup_button()
1221
self.assertTrue(self.view.set_up_button.enabled())
1222
self.assertFalse(self.view.set_up_button.property('DisabledState'))
1223
self.assertTrue(self.view.set_up_button.properties['polish'])
1224
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1226
def test_enable_setup_button_all_wrong(self):
1227
"""Test enable button only name valid."""
1229
self.controller._enable_setup_button()
1231
self.assertFalse(self.view.set_up_button.enabled())
1232
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1233
self.assertTrue(self.view.set_up_button.properties['polish'])
1234
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1236
def test_enable_setup_button_name_wrong(self):
1237
"""Test enable button only name valid."""
1239
self.view.name_edit.setText('')
1240
self.controller._enable_setup_button()
1242
self.assertFalse(self.view.set_up_button.enabled())
1243
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1244
self.assertTrue(self.view.set_up_button.properties['polish'])
1245
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1247
def test_enable_setup_button_email_wrong(self):
1248
"""Test enable button only name valid."""
1250
self.view.email_edit.setText('')
1251
self.controller._enable_setup_button()
1253
self.assertFalse(self.view.set_up_button.enabled())
1254
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1255
self.assertTrue(self.view.set_up_button.properties['polish'])
1256
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1258
def test_enable_setup_button_confirm_email_wrong(self):
1259
"""Test enable button only name valid."""
1261
self.view.confirm_email_edit.setText('')
1262
self.controller._enable_setup_button()
1264
self.assertFalse(self.view.set_up_button.enabled())
1265
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1266
self.assertTrue(self.view.set_up_button.properties['polish'])
1267
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1269
def test_enable_setup_button_password_wrong(self):
1270
"""Test enable button only name valid."""
1272
self.view.password_edit.setText('')
1273
self.controller._enable_setup_button()
1275
self.assertFalse(self.view.set_up_button.enabled())
1276
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1277
self.assertTrue(self.view.set_up_button.properties['polish'])
1278
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1280
def test_enable_setup_button_confirm_password_wrong(self):
1281
"""Test enable button only name valid."""
1283
self.view.confirm_password_edit.setText('')
1284
self.controller._enable_setup_button()
1286
self.assertFalse(self.view.set_up_button.enabled())
1287
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1288
self.assertTrue(self.view.set_up_button.properties['polish'])
1289
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1291
def test_enable_setup_button_captcha_wrong(self):
1292
"""Test enable button only name valid."""
1294
self.view.captcha_solution_edit.setText('')
1295
self.controller._enable_setup_button()
1297
self.assertFalse(self.view.set_up_button.enabled())
1298
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1299
self.assertTrue(self.view.set_up_button.properties['polish'])
1300
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1302
def test_enable_setup_button_checkbox_wrong(self):
1303
"""Test enable button only name valid."""
1305
self.view.terms_checkbox.setChecked(False)
1306
self.controller._enable_setup_button()
1308
self.assertFalse(self.view.set_up_button.enabled())
1309
self.assertTrue(self.view.set_up_button.property('DisabledState'))
1310
self.assertTrue(self.view.set_up_button.properties['polish'])
1311
self.assertTrue(self.view.set_up_button.properties['unpolish'])
1313
def test_register_fields(self):
1314
"""Test _register_fields."""
1315
self.controller._register_fields()
1316
self.assertTrue('email_address' in self.view.properties)
1317
self.assertTrue('password' in self.view.properties)
1318
self.assertTrue(type(self.view.properties['email_address']) is \
1320
self.assertTrue(type(self.view.properties['password']) is \
1323
def test_validate_form_all_ok(self):
1324
"""Test the result of validate_form."""
1326
self.assertTrue(self.controller.validate_form())
1327
self.assertNotEqual(self.view.name_assistance.text(), NAME_INVALID)
1328
self.assertNotEqual(self.view.email_assistance.text(), EMAIL_INVALID)
1329
self.assertNotEqual(self.view.confirm_email_assistance.text(),
1331
messages = '\n'.join([PASSWORD_TOO_WEAK, PASSWORD_MISMATCH,
1332
CAPTCHA_REQUIRED_ERROR])
1333
self.assertNotEqual(self.message_box.critical_args,
1334
((messages, self.view), {}))
1336
def test_validate_form_all_wrong(self):
1337
"""Test the result of validate_form."""
1339
self.assertFalse(self.controller.validate_form())
1340
self.assertEqual(self.view.name_assistance.text(), NAME_INVALID)
1341
self.assertEqual(self.view.email_assistance.text(), EMAIL_INVALID)
1342
self.assertEqual(self.view.confirm_email_assistance.text(),
1344
messages = '\n'.join([PASSWORD_TOO_WEAK, PASSWORD_MISMATCH,
1345
CAPTCHA_REQUIRED_ERROR])
1346
self.assertEqual(self.message_box.critical_args,
1347
((messages, self.view), {}))
1349
def test_validate_form_name_ok(self):
1350
"""Test the result of validate_form."""
1352
self.view.name_edit.setText('Name')
1353
self.assertFalse(self.controller.validate_form())
1354
self.assertEqual(self.view.email_assistance.text(), EMAIL_INVALID)
1355
self.assertEqual(self.view.confirm_email_assistance.text(),
1357
messages = '\n'.join([PASSWORD_TOO_WEAK, PASSWORD_MISMATCH,
1358
CAPTCHA_REQUIRED_ERROR])
1359
self.assertEqual(self.message_box.critical_args,
1360
((messages, self.view), {}))
1362
def test_validate_form_email_ok(self):
1363
"""Test the result of validate_form."""
1365
self.view.email_edit.setText('email@email.com')
1366
self.assertFalse(self.controller.validate_form())
1367
self.assertEqual(self.view.name_assistance.text(), NAME_INVALID)
1368
self.assertEqual(self.view.confirm_email_assistance.text(),
1370
messages = '\n'.join([PASSWORD_TOO_WEAK, PASSWORD_MISMATCH,
1371
CAPTCHA_REQUIRED_ERROR])
1372
self.assertEqual(self.message_box.critical_args,
1373
((messages, self.view), {}))
1375
def test_validate_form_confirm_email_ok(self):
1376
"""Test the result of validate_form."""
1378
self.view.email_edit.setText('email@email.com')
1379
self.view.confirm_email_edit.setText('email@email.com')
1380
self.assertFalse(self.controller.validate_form())
1381
self.assertEqual(self.view.name_assistance.text(), NAME_INVALID)
1382
messages = '\n'.join([PASSWORD_TOO_WEAK, PASSWORD_MISMATCH,
1383
CAPTCHA_REQUIRED_ERROR])
1384
self.assertEqual(self.message_box.critical_args,
1385
((messages, self.view), {}))
1387
def test_validate_form_password_weak(self):
1388
"""Test the result of validate_form."""
1390
self.view.password_edit.setText('Test')
1391
self.view.confirm_password_edit.setText('Test')
1392
self.assertFalse(self.controller.validate_form())
1393
messages = '\n'.join([PASSWORD_TOO_WEAK])
1394
self.assertEqual(self.message_box.critical_args,
1395
((messages, self.view), {}))
1397
def test_validate_form_password_mismatch(self):
1398
"""Test the result of validate_form."""
1400
self.view.password_edit.setText('T3st3rqw')
1401
self.view.confirm_password_edit.setText('test')
1402
self.assertFalse(self.controller.validate_form())
1403
messages = '\n'.join([PASSWORD_MISMATCH])
1404
self.assertEqual(self.message_box.critical_args,
1405
((messages, self.view), {}))
1407
def test_validate_form_captcha_required(self):
1408
"""Test the result of validate_form."""
1410
self.view.captcha_solution_edit.setText('')
1411
self.assertFalse(self.controller.validate_form())
1412
messages = '\n'.join([CAPTCHA_REQUIRED_ERROR])
1413
self.assertEqual(self.message_box.critical_args,
1414
((messages, self.view), {}))
1416
def test_is_correct_email(self):
1417
"""Test if the email is correct."""
1418
self.assertTrue(self.controller.is_correct_email('email@'))
1419
self.assertFalse(self.controller.is_correct_email('email'))
1421
def test_is_correct_email_confirmation(self):
1422
"""Test if the email confirmation is correct."""
1423
self.view.email_edit.setText('email@email.com')
1424
self.controller.is_correct_email_confirmation('email.@email.com')
1426
def test_is_correct_password_confirmation(self):
1427
"""Test is_correct_password_confirmation method."""
1428
self.view.password_edit.setText('T3st3rqw')
1429
self.controller.is_correct_password_confirmation('T3st3rqw')
778
1432
class SetupAccountControllerCaptchaTest(BaseTestCase):
1006
1678
"""Test the process that sets the ui."""
1007
1679
self.controller._title = SUCCESS
1008
1680
self.controller._subtitle = SUCCESS
1010
self.view.ui.success_message_label.setText(SUCCESS)
1011
self.view.header.set_title(SUCCESS)
1012
self.view.header.set_subtitle(SUCCESS)
1013
self.mocker.replay()
1014
self.controller.setupUi(self.view)
1016
def test_hide_titles(self):
1017
"""Test how the different titles are set."""
1019
self.view.ui.success_message_label.setText(SUCCESS)
1020
self.view.header.set_title('')
1021
self.view.header.set_subtitle('')
1022
self.mocker.replay()
1023
self.controller.setupUi(self.view)
1026
class UbuntuSSOWizardControllerTestCase(MockerTestCase):
1681
self.controller.setupUi(self.view)
1682
self.assertEqual(self.view.success_message_label.text(), SUCCESS)
1683
self.assertEqual(self.view.properties['title'], SUCCESS)
1684
self.assertEqual(self.view.properties['subtitle'], SUCCESS)
1685
self.assertEqual(self.view.next, -1)
1688
class UbuntuSSOWizardControllerTestCase(BaseTestCase):
1027
1689
"""Test the wizard controller."""
1029
1691
def setUp(self):
1030
1692
"""Set tests."""
1031
1693
super(UbuntuSSOWizardControllerTestCase, self).setUp()
1032
self.app_name = 'test'
1033
self.view = self.mocker.mock()
1034
self.backend = self.mocker.mock()
1035
self.callback = self.mocker.mock()
1694
self.view = UbuntuSSOView()
1695
self.backend = self.view
1696
self.callback = self.view.callback
1036
1697
self.controller = UbuntuSSOWizardController()
1037
1698
self.controller.view = self.view
1038
1699
self.controller.backend = self.backend
1040
1701
def test_on_user_cancelation(self):
1041
1702
"""Test that the callback is indeed called."""
1043
self.mocker.result(self.app_name)
1044
1703
self.controller.user_cancellation_callback = self.callback
1045
self.callback(self.app_name)
1047
self.mocker.replay()
1048
1704
self.controller.on_user_cancelation()
1705
self.assertTrue(self.view.properties.get('close', False))
1706
self.assertEqual(self.view.properties['callback'],
1707
((self.view.app_name, ), {}))
1050
1709
def test_on_login_success(self):
1051
1710
"""Test that the callback is indeed called."""
1052
1711
app_name = 'app'
1053
1712
email = 'email'
1054
1713
self.controller.login_success_callback = self.callback
1055
self.callback(app_name, email)
1056
self.mocker.replay()
1057
1714
self.controller.login_success_callback(app_name, email)
1715
self.assertEqual(self.view.properties['callback'],
1716
((app_name, email), {}))
1059
1718
def test_on_registration_success(self):
1060
1719
"""Test that the callback is indeed called."""
1061
1720
app_name = 'app'
1062
1721
email = 'email'
1063
1722
self.controller.registration_success_callback = self.callback
1064
self.callback(app_name, email)
1065
self.mocker.replay()
1066
1723
self.controller.registration_success_callback(app_name, email)
1724
self.assertEqual(self.view.properties['callback'],
1725
((app_name, email), {}))
1068
1727
def test_show_success_message(self):
1069
1728
"""Test that the correct page will be shown."""
1071
# the buttons layout we expect to have
1073
buttons_layout.append(QWizard.Stretch)
1074
buttons_layout.append(QWizard.FinishButton)
1075
self.view.success_page_id
1076
self.mocker.result(success_page_id)
1077
self.view.currentPage()
1078
self.mocker.result(self.view)
1079
self.view.next = success_page_id
1081
self.view.setButtonLayout(buttons_layout)
1082
self.mocker.replay()
1083
1729
self.controller.show_success_message()
1730
# the buttons layout we expect to have
1732
layout.append(QWizard.Stretch)
1733
layout.append(QWizard.FinishButton)
1735
self.assertEqual(self.view.page.next, self.view.success_page_id)
1736
self.assertTrue(self.view.properties['wizard_next'])
1737
self.assertEqual(self.view.properties['buttons_layout'], layout)
1085
1739
def test_show_error_message(self):
1086
1740
"""Test that the correct page will be shown."""
1088
# the buttons layout we expect to have
1090
buttons_layout.append(QWizard.Stretch)
1091
buttons_layout.append(QWizard.FinishButton)
1092
self.view.error_page_id
1093
self.mocker.result(error_page_id)
1094
self.view.currentPage()
1095
self.mocker.result(self.view)
1096
self.view.next = error_page_id
1098
self.view.setButtonLayout(buttons_layout)
1099
self.mocker.replay()
1100
1741
self.controller.show_error_message()
1742
# the buttons layout we expect to have
1744
layout.append(QWizard.Stretch)
1745
layout.append(QWizard.FinishButton)
1747
self.assertEqual(self.view.page.next, self.view.error_page_id)
1748
self.assertTrue(self.view.properties['wizard_next'])
1749
self.assertEqual(self.view.properties['buttons_layout'], layout)
1102
1751
def test_setup_ui(self):
1103
1752
"""Test that the ui is connect."""
1104
self.view.setWizardStyle(QWizard.ModernStyle)
1105
self.view.button(QWizard.CancelButton)
1106
self.mocker.result(self.view)
1107
self.view.clicked.connect(self.controller.on_user_cancelation)
1108
self.view.loginSuccess.connect(self.controller.on_login_success)
1109
self.view.registrationSuccess.connect(
1110
self.controller.on_registration_success)
1111
self.mocker.replay()
1112
1753
self.controller.setupUi(self.view)
1115
class FakeLineEditForForgottenPage(object):
1117
"""Fake Line Edit"""
1120
"""Initilize object."""
1121
self.email_text = QString()
1124
"""Fake text for QLineEdit."""
1125
return self.email_text
1127
# setText is inherited
1128
# pylint: disable=C0103
1129
def setText(self, text):
1130
"""Fake setText for QLineEdit."""
1131
self.email_text = QString(text)
1132
# pylint: enable=C0103
1135
class FakeForgottenPasswordPage(FakePageUiStyle):
1136
"""Fake the page."""
1139
super(FakeForgottenPasswordPage, self).__init__()
1140
self.email_address_line_edit = self
1141
self.send_button = self
1142
self.email_widget = FakePageUiStyle()
1143
self.forgotted_password_intro_label = FakePageUiStyle()
1144
self.try_again_widget = FakePageUiStyle()
1145
self.email_line_edit = FakeLineEditForForgottenPage()
1754
self.assertEqual(self.view.properties['wizard_style'],
1755
QWizard.ModernStyle)
1756
self.assertIsInstance(self.view.properties['button'].function,
1757
collections.Callable)
1758
self.assertIsInstance(self.view.loginSuccess.function,
1759
collections.Callable)
1760
self.assertIsInstance(self.view.registrationSuccess.function,
1761
collections.Callable)
1148
1764
class ForgottenPasswordControllerValidationTest(BaseTestCase):
1236
1854
((msg, self.controller.view), {}))
1239
class ForgottenPasswordControllerTestCase(MockerTestCase):
1857
class ForgottenPasswordControllerTestCase(BaseTestCase):
1240
1858
"""Test the controller of the fogotten password page."""
1242
1860
def setUp(self):
1243
1861
"""Setup the tests."""
1244
1862
super(ForgottenPasswordControllerTestCase, self).setUp()
1245
self.view = self.mocker.mock()
1246
self.backend = self.mocker.mock()
1247
self.controller = ForgottenPasswordController(message_box=self.view)
1863
self.view = FakeForgottenPasswordPageView()
1864
self.backend = self.view
1865
self.controller = ForgottenPasswordController(
1866
message_box=FakeMessageBox())
1248
1867
self.controller.view = self.view
1249
1868
self.controller.backend = self.backend
1250
self.app_name = 'app_name'
1252
1870
def test_register_fields(self):
1253
1871
"""Ensure that all the diff fields are registered."""
1254
line_edit = 'line_edit'
1255
self.view.email_address_line_edit
1256
self.mocker.result(line_edit)
1257
self.view.registerField('email_address', line_edit)
1258
self.mocker.replay()
1259
1872
self.controller._register_fields()
1873
self.assertEqual(self.view.field('email_address'),
1874
self.view.email_address_line_edit)
1261
1876
def test_set_translated_strings(self):
1262
1877
"""Ensure that the correct strings are translated."""
1263
self.view.wizard().app_name
1264
self.mocker.result(self.app_name)
1265
self.view.forgotted_password_intro_label.setText(
1266
REQUEST_PASSWORD_TOKEN_LABEL % {'app_name':
1268
self.view.email_address_label.setText(EMAIL_LABEL)
1269
self.view.send_button.setText(RESET_PASSWORD)
1270
self.view.try_again_button.setText(TRY_AGAIN_BUTTON)
1271
self.mocker.replay()
1272
1878
self.controller._set_translated_strings()
1879
self.assertEqual(self.view.forgotted_password_intro_label.text(),
1880
REQUEST_PASSWORD_TOKEN_LABEL % {'app_name':
1881
self.view.fake_wizard.app_name})
1882
self.assertEqual(self.view.email_address_label.text(),
1884
self.assertEqual(self.view.send_button.text(),
1886
self.assertEqual(self.view.try_again_button.text(),
1274
1889
def test_set_enhanced_line_edit(self):
1275
1890
"""Test that the correct line enhancements have been added."""
1276
line_edit = 'line_edit'
1277
self.view.email_address_line_edit
1278
self.mocker.result(line_edit)
1279
self.view.set_line_edit_validation_rule(line_edit, is_correct_email)
1280
self.mocker.replay()
1281
1891
self.controller._set_enhanced_line_edit()
1892
elements = self.view.properties['validation_rule']
1893
self.assertFalse(elements is None)
1895
self.assertTrue(e[0] is self.view.email_address_line_edit)
1896
self.assertIsInstance(e[1], collections.Callable)
1283
1898
def test_connect_ui(self):
1284
1899
"""Test that the correct ui signals are connected."""
1285
self.view.email_address_line_edit.textChanged.connect(MATCH(callable))
1286
self.view.send_button.clicked.connect(MATCH(callable))
1287
self.view.try_again_button.clicked.connect(
1288
self.controller.on_try_again)
1289
self.backend.on_password_reset_token_sent_cb = MATCH(callable)
1290
self.backend.on_password_reset_error_cb = \
1291
self.controller.on_password_reset_error
1292
self.mocker.replay()
1293
1900
self.controller._connect_ui()
1901
self.assertEqual(self.view.email_address_line_edit.receivers(
1902
SIGNAL('textChanged(QString)')), 1)
1903
self.assertIsInstance(self.view.send_button.function,
1904
collections.Callable)
1905
self.assertIsInstance(self.view.try_again_button.function,
1906
collections.Callable)
1907
self.assertIsInstance(self.view.on_password_reset_token_sent_cb,
1908
collections.Callable)
1909
self.assertIsInstance(self.view.on_password_reset_error_cb,
1910
collections.Callable)
1295
1912
def test_on_try_again(self):
1296
1913
"""Test that the on_try_again callback does work as expected."""
1297
self.view.try_again_widget.setVisible(False)
1298
self.view.email_widget.setVisible(True)
1299
self.mocker.replay()
1300
1914
self.controller.on_try_again()
1915
self.assertFalse(self.view.try_again_widget.isVisible())
1916
self.assertTrue(self.view.email_widget.isVisible())
1302
1918
def test_on_password_reset_token_sent(self):
1303
1919
"""Test that the on_password_token_sent callback works as expected."""
1305
self.view.wizard().reset_password_page_id
1306
self.mocker.result(page_id)
1307
self.view.next = page_id
1308
self.view.wizard().next()
1309
self.mocker.replay()
1310
1920
self.controller.on_password_reset_token_sent()
1313
class ResetPasswordControllerTestCase(MockerTestCase):
1921
self.assertTrue(self.view.properties['wizard_next'])
1922
self.assertEqual(self.view.next,
1923
self.view.fake_wizard.reset_password_page_id)
1926
class ResetPasswordControllerTestCase(BaseTestCase):
1314
1927
"""Ensure that the reset password works as expected."""
1316
1929
def setUp(self):
1317
1930
"""Setup the tests."""
1318
1931
super(ResetPasswordControllerTestCase, self).setUp()
1319
self.view = self.mocker.mock()
1320
self.backend = self.mocker.mock()
1932
self.view = ResetPasswordPageView()
1933
self.backend = self.view
1321
1934
self.controller = ResetPasswordController()
1322
1935
self.controller.view = self.view
1323
1936
self.controller.backend = self.backend
1325
1938
def test_set_translated_strings(self):
1326
1939
"""Ensure that the correct strings are set."""
1327
self.controller._subtitle = PASSWORD_HELP
1328
self.view.ui.reset_password_button.setText(RESET_PASSWORD)
1329
self.view.setSubTitle(PASSWORD_HELP)
1330
self.mocker.replay()
1331
1940
self.controller._set_translated_strings()
1941
self.assertEqual(self.view.reset_password_button.text(),
1943
self.assertEqual(self.view.properties['subtitle'],
1333
1946
def test_connect_ui(self):
1334
1947
"""Ensure that the diffent signals from the ui are connected."""
1335
self.view.ui.reset_password_button.clicked.connect(
1336
self.controller.set_new_password)
1337
self.backend.on_password_changed_cb = \
1338
self.controller.on_password_changed
1339
self.backend.on_password_change_error_cb = \
1340
self.controller.on_password_change_error
1341
self.view.ui.reset_code_line_edit.textChanged.connect(
1343
self.view.ui.password_line_edit.textChanged.connect(
1345
self.view.ui.confirm_password_line_edit.textChanged.connect(
1348
self.mocker.replay()
1349
1948
self.controller._connect_ui()
1949
self.assertIsInstance(self.view.reset_password_button.function,
1950
collections.Callable)
1951
self.assertIsInstance(self.view.on_password_changed_cb,
1952
collections.Callable)
1953
self.assertIsInstance(self.view.on_password_change_error_cb,
1954
collections.Callable)
1955
self.assertEqual(self.view.reset_code_line_edit.receivers(
1956
SIGNAL('textChanged(QString)')), 1)
1957
self.assertEqual(self.view.password_line_edit.receivers(
1958
SIGNAL('textChanged(QString)')), 1)
1959
self.assertEqual(self.view.confirm_password_line_edit.receivers(
1960
SIGNAL('textChanged(QString)')), 1)
1351
1962
def test_add_line_edits_validations(self):
1352
1963
"""Ensure that the line validation have been added."""
1353
line_edit = 'line_edit'
1355
self.view.ui.password_line_edit
1356
self.mocker.result('line_edit')
1357
self.view.set_line_edit_validation_rule(line_edit,
1358
is_min_required_password)
1359
self.view.ui.confirm_password_line_edit
1360
self.mocker.result('line_edit')
1361
self.view.set_line_edit_validation_rule(line_edit, MATCH(callable))
1362
self.view.ui.confirm_password_line_edit.textChanged.emit
1363
self.mocker.result(emit)
1364
self.view.ui.password_line_edit.textChanged.connect(emit)
1365
self.mocker.replay()
1366
1964
self.controller._add_line_edits_validations()
1965
self.assertEqual(self.view.password_line_edit.receivers(
1966
SIGNAL('textChanged(QString)')), 1)
1967
elements = self.view.properties['validation_rule']
1968
self.assertFalse(elements is None)
1970
self.assertTrue(type(e[0]) is QLineEdit)
1971
self.assertIsInstance(e[1], collections.Callable)
1368
1973
def test_set_new_password(self):
1369
1974
"""Test that the correct action is performed."""
1370
app_name = 'app_name'
1373
password = 'password'
1374
self.view.wizard().app_name
1375
self.mocker.result(app_name)
1376
unicode(self.view.wizard().forgotten.ui.email_line_edit.text())
1377
self.mocker.result(email)
1378
self.view.ui.reset_code_line_edit.text()
1379
self.mocker.result(code)
1380
self.view.ui.password_line_edit.text()
1381
self.mocker.result(password)
1382
self.backend.set_new_password(app_name, email, code, password)
1383
self.mocker.replay()
1975
email = 'email@email.com'
1977
password = 'T3st3rqw'
1978
self.view.wizard().forgotten.ui.email_line_edit.setText(email)
1979
self.view.reset_code_line_edit.setText(code)
1980
self.view.password_line_edit.setText(password)
1384
1981
self.controller.set_new_password()
1982
self.assertEqual(self.view.properties['backend_new_password'],
1983
((self.view.fake_wizard.app_name, email, code, password), {}))
1386
1985
def test_is_correct_password_confirmation_true(self):
1387
1986
"""Test that the correct password confirmation is used."""
1388
1987
password = 'password'
1389
self.view.ui.password_line_edit.text()
1390
self.mocker.result(password)
1391
self.mocker.replay()
1988
self.view.password_line_edit.setText(password)
1392
1989
self.assertTrue(self.controller.is_correct_password_confirmation(
1395
1992
def test_is_correct_password_confirmation_false(self):
1396
1993
"""Test that the correct password confirmation is used."""
1397
1994
password = 'password'
1398
self.view.ui.password_line_edit.text()
1399
self.mocker.result(password + password)
1400
self.mocker.replay()
1995
self.view.password_line_edit.setText(password + password)
1401
1996
self.assertFalse(self.controller.is_correct_password_confirmation(
1405
class FakeResetPasswordPage(FakePageUiStyle):
1407
"""Fake ResetPasswordPage."""
1409
def __init__(self, *args):
1411
super(FakeResetPasswordPage, self).__init__()
1412
self.ui.reset_code_line_edit = FakeLineEdit()
1413
self.ui.password_line_edit = FakeLineEdit()
1414
self.ui.confirm_password_line_edit = FakeLineEdit()
1415
self.reset_password_button = self
1416
self._enabled = 9 # Intentionally wrong.
1419
2000
class ResetPasswordControllerValidationTest(BaseTestCase):
1421
2002
"""Tests for ResetPasswordController, but without Mocker."""