~nataliabidart/ubuntu-sso-client/no-hardcode-appname

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/tests/test_controllers.py

Tests migrated to no mocker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
"""Test the ui controllers."""
21
21
 
 
22
import collections
 
23
 
22
24
from twisted.internet import defer
23
25
 
24
 
from mocker import MATCH, MockerTestCase
25
 
from PyQt4.QtGui import QWizard
26
 
from PyQt4.QtCore import QString
 
26
from PyQt4.QtGui import QCheckBox, QLabel, QLineEdit, QWizard
 
27
from PyQt4.QtCore import QString, SIGNAL
27
28
 
28
29
# pylint: disable=F0401
29
30
try:
51
52
    CAPTCHA_SOLUTION_ENTRY,
52
53
    EMAIL1_ENTRY,
53
54
    EMAIL2_ENTRY,
 
55
    EMAIL_INVALID,
54
56
    EMAIL_MISMATCH,
55
57
    ERROR,
56
58
    EMAIL_LABEL,
57
59
    EXISTING_ACCOUNT_CHOICE_BUTTON,
58
60
    FORGOTTEN_PASSWORD_BUTTON,
59
 
    is_min_required_password,
60
 
    is_correct_email,
61
61
    JOIN_HEADER_LABEL,
62
62
    LOGIN_PASSWORD_LABEL,
63
63
    NAME_ENTRY,
 
64
    NAME_INVALID,
64
65
    PASSWORD1_ENTRY,
65
66
    PASSWORD2_ENTRY,
66
67
    PASSWORD_HELP,
75
76
    REQUEST_PASSWORD_TOKEN_TECH_ERROR,
76
77
    TRY_AGAIN_BUTTON,
77
78
    VERIFY_EMAIL_CONTENT,
 
79
    VERIFY_EMAIL_TITLE,
78
80
)
79
81
 
80
82
#ignore the comon mocker issues with lint
81
83
# pylint: disable=W0212,W0104,W0106,E1103
82
84
 
83
85
 
 
86
class FakeButton(FakePageUiStyle):
 
87
 
 
88
    """Fake Button."""
 
89
 
 
90
    def __init__(self):
 
91
        """Initialize object."""
 
92
        super(FakeButton, self).__init__()
 
93
        self.function = None
 
94
        self.clicked = self
 
95
 
 
96
    def connect(self, function):
 
97
        """Receive the function that should be connected to this button."""
 
98
        self.function = function
 
99
 
 
100
 
 
101
class FakeField(object):
 
102
 
 
103
    """Fake Field for text input."""
 
104
 
 
105
    def __init__(self, text):
 
106
        self.text = text
 
107
 
 
108
    # pylint: disable=C0103
 
109
    def toString(self):
 
110
        """Fake toString method for input fields."""
 
111
        return self.text
 
112
    # pylint: enable=C0103
 
113
 
 
114
 
 
115
class FakeWizard(object):
 
116
 
 
117
    """Fake Wizard."""
 
118
 
 
119
    current_user_page_id = 1
 
120
    setup_account_page_id = 2
 
121
    forgotten_password_page_id = 3
 
122
    reset_password_page_id = 4
 
123
 
 
124
    def __init__(self, view):
 
125
        """Initialize object."""
 
126
        self.properties = {}
 
127
        self.app_name = 'app_name'
 
128
        self.help_text = 'help'
 
129
        self.view = view
 
130
        self.next_id = 0
 
131
        # pylint: disable=C0103
 
132
        self.loginSuccess = self
 
133
        self.registrationSuccess = self
 
134
        # pylint: enable=C0103
 
135
        self.forgotten = self
 
136
        self.ui = self
 
137
        self.email_line_edit = QLineEdit()
 
138
 
 
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
 
144
 
 
145
    def field(self, key):
 
146
        """Fake field from wizard"""
 
147
        return self.properties['field_%s' % key]
 
148
 
 
149
    def next(self):
 
150
        """Fake next method to move to to next page in the wizard."""
 
151
        self.view.properties['wizard_next'] = True
 
152
 
 
153
    def emit(self, name, email):
 
154
        """Fake emit."""
 
155
        self.properties['emit'] = (name, email)
 
156
 
 
157
 
 
158
class FakeGenericView(object):
 
159
 
 
160
    """Fake Generic page."""
 
161
 
 
162
    def __init__(self):
 
163
        """Initialize object."""
 
164
        self.properties = {}
 
165
        self.ui = self
 
166
        self.header = self
 
167
        self.properties['currentId'] = -1
 
168
        self.next = 0
 
169
        self.fake_wizard = FakeWizard(self)
 
170
 
 
171
    def set_title(self, title):
 
172
        """Fake set_title for the page."""
 
173
        self.properties['title'] = title
 
174
 
 
175
    def set_subtitle(self, subtitle):
 
176
        """Fake set_subtitle for the page"""
 
177
        self.properties['subtitle'] = subtitle
 
178
 
 
179
    # pylint: disable=C0103
 
180
    def setTitle(self, title):
 
181
        """Fake setTitle."""
 
182
        self.set_title(title)
 
183
 
 
184
    def setSubTitle(self, subtitle):
 
185
        """Fake setTitle."""
 
186
        self.set_subtitle(subtitle)
 
187
    # pylint: enable=C0103
 
188
 
 
189
    def wizard(self):
 
190
        """Fake wizard is returned."""
 
191
        return self.fake_wizard
 
192
 
 
193
 
 
194
class FakeChooseSignInView(FakeGenericView):
 
195
 
 
196
    """Fake ChooseSignIn page."""
 
197
 
 
198
    def __init__(self):
 
199
        """Initialize object."""
 
200
        super(FakeChooseSignInView, self).__init__()
 
201
        self.existing_account_button = FakeButton()
 
202
        self.setup_account_button = FakeButton()
 
203
 
 
204
 
 
205
class FakeCurrentUserView(FakeGenericView):
 
206
 
 
207
    """Fake Current User view."""
 
208
 
 
209
    def __init__(self):
 
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
 
219
 
 
220
    def fake_backend(self):
 
221
        """Fake get_backend."""
 
222
        return self
 
223
 
 
224
    def login(self, appname, email, password):
 
225
        """Fake backend login."""
 
226
        self.properties['backend_login'] = (appname, email, password)
 
227
        return self
 
228
 
 
229
    # pylint: disable=C0103
 
230
    def addErrback(self, function):
 
231
        """Fake defer addErrback."""
 
232
        self.properties['login-addErrback'] = function
 
233
    # pylint: enable=C0103
 
234
 
 
235
 
 
236
class FakeLineEdit(object):
 
237
 
 
238
    """A fake QLineEdit."""
 
239
 
 
240
    def __init__(self, *args):
 
241
        """Initialize."""
 
242
        self._text = u""
 
243
 
 
244
    def text(self):
 
245
        """Save text."""
 
246
        return self._text
 
247
 
 
248
    # setText is inherited
 
249
    # pylint: disable=C0103
 
250
    def setText(self, text):
 
251
        """Return saved text."""
 
252
        self._text = text
 
253
 
 
254
 
 
255
class FakePage(object):
 
256
 
 
257
    """A fake wizard page."""
 
258
 
 
259
    app_name = "APP"
 
260
 
 
261
    def wizard(self):
 
262
        """Fake wizard."""
 
263
        return self
 
264
 
 
265
 
 
266
class FakeCurrentUserPage(FakePageUiStyle):
 
267
 
 
268
    """Fake CurrentuserPage."""
 
269
 
 
270
    def __init__(self, *args):
 
271
        """Initialize."""
 
272
        super(FakeCurrentUserPage, self).__init__(*args)
 
273
        self.ui.email_edit = FakeLineEdit()
 
274
        self.ui.password_edit = FakeLineEdit()
 
275
        self.sign_in_button = self
 
276
 
 
277
 
 
278
class FakeSetupAccountPageView(FakeGenericView):
 
279
 
 
280
    """Fake SetupAccount view."""
 
281
 
 
282
    def __init__(self):
 
283
        super(FakeSetupAccountPageView, self).__init__()
 
284
        self.ui = self
 
285
        # Labels
 
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()
 
296
        # Line Edits
 
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()
 
304
        # Check Box
 
305
        self.terms_checkbox = QCheckBox()
 
306
        # Setup Button
 
307
        self.set_up_button = FakeButton()
 
308
        # Backend
 
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
 
313
 
 
314
    def fake_backend(self):
 
315
        """Fake get_backend."""
 
316
        return self
 
317
 
 
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)
 
321
        if elements is None:
 
322
            elements = []
 
323
        elements.append((edit, function))
 
324
        self.properties['validation_rule'] = elements
 
325
 
 
326
    # pylint: disable=C0103
 
327
    def registerField(self, key, edit):
 
328
        """Fake registerField from page."""
 
329
        self.properties[key] = edit
 
330
    # pylint: enable=C0103
 
331
 
 
332
    def set_error_message(self, label, message):
 
333
        """Fake set_error_message."""
 
334
        label.setText(message)
 
335
 
 
336
 
 
337
class FakeMessageBox(object):
 
338
 
 
339
    """A fake message box."""
 
340
 
 
341
    args = None
 
342
    critical_args = None
 
343
 
 
344
    def __init__(self, *args, **kwargs):
 
345
        self.args = (args, kwargs)
 
346
 
 
347
    def critical(self, *args, **kwargs):
 
348
        """Fake critical popup."""
 
349
        self.critical_args = (args, kwargs)
 
350
 
 
351
 
 
352
class FakeSetupAccountUi(object):
 
353
    """Fake ui variable for FakeView."""
 
354
 
 
355
    email_assistance = None
 
356
 
 
357
 
 
358
class FakeView(object):
 
359
 
 
360
    """A fake view."""
 
361
 
 
362
    app_name = "TestApp"
 
363
    ui = FakeSetupAccountUi()
 
364
 
 
365
    def wizard(self):
 
366
        """Use itself as a fake wizard, too."""
 
367
        return self
 
368
 
 
369
    def set_error_message(self, label, msg):
 
370
        """Simulate to add the message to the label as SetupAccount do."""
 
371
 
 
372
 
 
373
class FakeSetupAccountView(FakeView):
 
374
 
 
375
    """A Fake view."""
 
376
 
 
377
    captcha_file = None
 
378
    captcha_image = None
 
379
    captcha_refresh_executed = False
 
380
    captcha_refreshing_value = False
 
381
 
 
382
    def on_captcha_refresh_complete(self):
 
383
        """Fake the call to refresh finished."""
 
384
        self.captcha_refresh_executed = True
 
385
 
 
386
    def on_captcha_refreshing(self):
 
387
        """Fake the call to refreshing."""
 
388
        self.captcha_refreshing_value = True
 
389
 
 
390
    def fake_open(self, filename):
 
391
        """Fake open for Image."""
 
392
        return self
 
393
 
 
394
    # pylint: disable=W0622
 
395
    def save(self, io, format):
 
396
        """Fake save for Image."""
 
397
    # pylint: enable=W0622
 
398
 
 
399
 
 
400
class FakeControllerForCaptcha(object):
 
401
 
 
402
    """Fake controller to test refresh_captcha method."""
 
403
 
 
404
    def __init__(self):
 
405
        """Initialize FakeControllerForCaptcha."""
 
406
        self.callback_error = False
 
407
 
 
408
    def generate_captcha(self, *args):
 
409
        """Fake generate deferred for captcha."""
 
410
        return self
 
411
 
 
412
    # pylint: disable=C0103
 
413
    def addErrback(self, call):
 
414
        """Fake addErrback."""
 
415
        self.callback_error = call is not None
 
416
    # pylint: enable=C0103
 
417
 
 
418
 
 
419
class FakeEmailVerificationView(FakePageUiStyle):
 
420
    """Fake EmailVerification Page."""
 
421
 
 
422
    def __init__(self, *args):
 
423
        """Initialize."""
 
424
        super(FakeEmailVerificationView, self).__init__(*args)
 
425
        self.verification_code = ''
 
426
        self.next_button = self
 
427
 
 
428
 
 
429
class EmailVerificationView(FakeGenericView):
 
430
 
 
431
    """Fake Email Verification view."""
 
432
 
 
433
    def __init__(self):
 
434
        super(EmailVerificationView, self).__init__()
 
435
        self.ui = self
 
436
        # Line Edits
 
437
        self.verification_code_edit = QLineEdit()
 
438
        self.verification_code = ''
 
439
        # Setup Button
 
440
        self.next_button = FakeButton()
 
441
        # Backend
 
442
        self.on_email_validated_cb = None
 
443
        self.on_email_validation_error_cb = None
 
444
 
 
445
    def fake_backend(self):
 
446
        """Fake get_backend."""
 
447
        return self
 
448
 
 
449
    def validate_email(self, *args, **kwargs):
 
450
        """Fake validate_email method from backend"""
 
451
        self.properties['validate_email'] = (args, kwargs)
 
452
 
 
453
 
 
454
class ErrorPageView(FakeGenericView):
 
455
 
 
456
    """Fake Error Page view."""
 
457
 
 
458
    def __init__(self):
 
459
        super(ErrorPageView, self).__init__()
 
460
        self.ui = self
 
461
        self.error_message_label = QLabel()
 
462
 
 
463
 
 
464
class SuccessPageView(FakeGenericView):
 
465
 
 
466
    """Fake Success Page view."""
 
467
 
 
468
    def __init__(self):
 
469
        super(SuccessPageView, self).__init__()
 
470
        self.ui = self
 
471
        self.success_message_label = QLabel()
 
472
 
 
473
 
 
474
class UbuntuSSOView(object):
 
475
 
 
476
    """Fake view for UbuntuSSO."""
 
477
 
 
478
    success_page_id = 5
 
479
    error_page_id = 6
 
480
 
 
481
    def __init__(self):
 
482
        self.properties = {}
 
483
        self.ui = self
 
484
        self.app_name = 'app_name'
 
485
        self.success_message_label = QLabel()
 
486
        self.result = 0
 
487
        # pylint: disable=C0103
 
488
        self.loginSuccess = FakeButton()
 
489
        self.registrationSuccess = FakeButton()
 
490
        # pylint: enable=C0103
 
491
        self.page = FakeGenericView()
 
492
 
 
493
    def button(self, *args):
 
494
        """Fake button."""
 
495
        self.properties['button_method'] = args
 
496
        self.properties['button'] = FakeButton()
 
497
        return self.properties['button']
 
498
 
 
499
    def callback(self, *args, **kwargs):
 
500
        """Fake callback."""
 
501
        self.properties['callback'] = (args, kwargs)
 
502
        return self.result
 
503
 
 
504
    def close(self):
 
505
        """Fake close."""
 
506
        self.properties['close'] = True
 
507
 
 
508
    # pylint: disable=C0103
 
509
    def setButtonLayout(self, layout):
 
510
        """Fake setButtonLayout."""
 
511
        self.properties['buttons_layout'] = layout
 
512
 
 
513
    def currentPage(self):
 
514
        """Fake currentPage."""
 
515
        return self.page
 
516
 
 
517
    def setWizardStyle(self, style):
 
518
        """Fake setWizardStyle."""
 
519
        self.properties['wizard_style'] = style
 
520
    # pylint: enable=C0103
 
521
 
 
522
    def next(self):
 
523
        """Fake next method to move to to next page in the wizard."""
 
524
        self.properties['wizard_next'] = True
 
525
 
 
526
 
 
527
class FakeLineEditForForgottenPage(object):
 
528
 
 
529
    """Fake Line Edit"""
 
530
 
 
531
    def __init__(self):
 
532
        """Initilize object."""
 
533
        self.email_text = QString()
 
534
 
 
535
    def text(self):
 
536
        """Fake text for QLineEdit."""
 
537
        return self.email_text
 
538
 
 
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
 
545
 
 
546
 
 
547
class FakeForgottenPasswordPage(FakePageUiStyle):
 
548
    """Fake the page."""
 
549
 
 
550
    def __init__(self):
 
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()
 
558
 
 
559
    def fake_backend(self):
 
560
        """Fake get_backend."""
 
561
        return self
 
562
 
 
563
 
 
564
class FakeForgottenPasswordPageView(FakeGenericView):
 
565
    """Fake the page."""
 
566
 
 
567
    def __init__(self):
 
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()
 
577
        # Backend
 
578
        self.on_password_reset_error_cb = None
 
579
        self.on_password_reset_token_sent_cb = None
 
580
 
 
581
    def fake_backend(self):
 
582
        """Fake get_backend."""
 
583
        return self
 
584
 
 
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
 
590
 
 
591
    def field(self, key):
 
592
        """Fake field from wizard"""
 
593
        return self.properties['field_%s' % key]
 
594
 
 
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)
 
598
        if elements is None:
 
599
            elements = []
 
600
        elements.append((edit, function))
 
601
        self.properties['validation_rule'] = elements
 
602
 
 
603
 
 
604
class ResetPasswordPageView(FakeGenericView):
 
605
    """Fake the page."""
 
606
 
 
607
    def __init__(self):
 
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()
 
613
        # Backend
 
614
        self.on_password_changed_cb = None
 
615
        self.on_password_change_error_cb = None
 
616
 
 
617
    def fake_backend(self):
 
618
        """Fake get_backend."""
 
619
        return self
 
620
 
 
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
 
626
 
 
627
    def field(self, key):
 
628
        """Fake field from wizard"""
 
629
        return self.properties['field_%s' % key]
 
630
 
 
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)
 
634
        if elements is None:
 
635
            elements = []
 
636
        elements.append((edit, function))
 
637
        self.properties['validation_rule'] = elements
 
638
 
 
639
    def set_new_password(self, *args, **kwargs):
 
640
        """Fake set_new_password from backend."""
 
641
        self.properties['backend_new_password'] = (args, kwargs)
 
642
 
 
643
 
 
644
class FakeResetPasswordPage(FakePageUiStyle):
 
645
 
 
646
    """Fake ResetPasswordPage."""
 
647
 
 
648
    def __init__(self, *args):
 
649
        """Initialize."""
 
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.
 
656
 
 
657
 
 
658
class FakeWizardForResetPassword(object):
 
659
 
 
660
    """Fake Wizard for ResetPasswordController."""
 
661
 
 
662
    def __init__(self):
 
663
        self.current_user = self
 
664
        self.ui = self
 
665
        self.email_edit = self
 
666
        self.email_line_edit = self
 
667
        self.forgotten = self
 
668
        self.overlay = self
 
669
        self.count_back = 0
 
670
        self.hide_value = False
 
671
        self.text_value = 'mail@mail.com'
 
672
 
 
673
    def wizard(self):
 
674
        """Fake wizard function for view."""
 
675
        return self
 
676
 
 
677
    def back(self):
 
678
        """Fake back for wizard."""
 
679
        self.count_back += 1
 
680
 
 
681
    def hide(self):
 
682
        """Fake hide for overlay."""
 
683
        self.hide_value = True
 
684
 
 
685
    def text(self):
 
686
        """Fake text for QLineEdit."""
 
687
        return self.text_value
 
688
 
 
689
    # pylint: disable=C0103
 
690
    def setText(self, text):
 
691
        """Fake setText for QLineEdit."""
 
692
        self.text_value = text
 
693
    # pylint: enable=C0103
 
694
 
 
695
 
84
696
class BackendControllerTestCase(BaseTestCase):
85
697
    """The test case for the BackendController."""
86
698
 
96
708
        self.assertTrue(self.sso_login_backend is result)
97
709
 
98
710
 
99
 
class ChooseSignInControllerTestCase(MockerTestCase):
 
711
class ChooseSignInControllerTestCase(BaseTestCase):
100
712
    """Test the choose sign in controller."""
101
713
 
102
714
    def setUp(self):
103
715
        """Set tests."""
104
716
        super(ChooseSignInControllerTestCase, self).setUp()
105
 
        self.view = self.mocker.mock()
106
 
        self.backend = self.mocker.mock()
107
 
        self.controller = ChooseSignInController()
 
717
        self.title = 'title'
 
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
110
 
        self.title = 'title'
111
 
        self.subtitle = 'sub'
112
722
 
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()
121
 
        self.mocker.replay()
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)
123
736
 
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)
129
 
        self.mocker.replay()
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)
131
744
 
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)
138
 
        self.mocker.replay()
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)
140
752
 
141
753
    def test_set_next_existing(self):
142
754
        """Test the execution of the callback."""
143
 
        next_id = 4
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()
148
 
        self.mocker.replay()
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)
150
759
 
151
760
    def test_set_next_new(self):
152
761
        """Test the execution of the callback."""
153
 
        next_id = 5
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()
158
 
        self.mocker.replay()
159
762
        self.controller._set_next_new()
160
 
 
161
 
 
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)
 
766
 
 
767
 
 
768
class CurrentUserControllerTestCase(BaseTestCase):
163
769
    """Test the current user controller."""
164
770
 
165
771
    def setUp(self):
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)
176
780
 
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)
184
 
        self.mocker.replay()
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)
186
789
 
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(
193
 
                                                        MATCH(callable))
194
 
        self.view.ui.email_edit.textChanged.connect(MATCH(callable))
195
 
        self.view.ui.password_edit.textChanged.connect(MATCH(callable))
196
 
        self.mocker.replay()
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)
198
804
 
199
805
    def test_title_subtitle(self):
200
806
        """Ensure we are storing the title and subtitle correctly."""
201
 
        self.mocker.replay()
202
 
        self.assertEqual(self.controller._title, 'the title')
203
 
        self.assertEqual(self.controller._subtitle, 'the subtitle')
204
 
 
205
 
 
206
 
class FakeLineEdit(object):
207
 
 
208
 
    """A fake QLineEdit."""
209
 
 
210
 
    def __init__(self, *args):
211
 
        """Initialize."""
212
 
        self._text = u""
213
 
 
214
 
    def text(self):
215
 
        """Save text."""
216
 
        return self._text
217
 
 
218
 
    # setText is inherited
219
 
    # pylint: disable=C0103
220
 
    def setText(self, text):
221
 
        """Return saved text."""
222
 
        self._text = text
223
 
 
224
 
 
225
 
class FakePage(object):
226
 
 
227
 
    """A fake wizard page."""
228
 
 
229
 
    app_name = "APP"
230
 
 
231
 
    def wizard(self):
232
 
        """Fake wizard."""
233
 
        return self
234
 
 
235
 
 
236
 
class FakeCurrentUserPage(FakePageUiStyle):
237
 
 
238
 
    """Fake CurrentuserPage."""
239
 
 
240
 
    def __init__(self, *args):
241
 
        """Initialize."""
242
 
        super(FakeCurrentUserPage, self).__init__(*args)
243
 
        self.ui.email_edit = FakeLineEdit()
244
 
        self.ui.password_edit = FakeLineEdit()
245
 
        self.sign_in_button = self
246
 
 
247
 
 
248
 
class CurrentUserControllerValidationMockTest(MockerTestCase):
249
 
    """Test the choose sign in controller."""
250
 
 
251
 
    def setUp(self):
252
 
        """Set tests."""
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
259
 
        self.title = 'title'
260
 
        self.subtitle = 'sub'
 
807
        self.assertEqual(self.view.properties['title'], 'the title')
 
808
        self.assertEqual(self.view.properties['subtitle'], 'the subtitle')
 
809
 
 
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'))
 
816
 
 
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)
 
828
 
 
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'])
261
837
 
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()
270
 
        self.mocker.replay()
 
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)
272
861
 
273
862
 
274
863
class CurrentUserControllerErrorTestCase(BaseTestCase):
431
1020
            not self.controller.view.sign_in_button.enabled())
432
1021
 
433
1022
 
434
 
class SetUpAccountControllerTestCase(MockerTestCase):
 
1023
class SetUpAccountControllerTestCase(BaseTestCase):
435
1024
    """test the controller used to setup a new account."""
436
1025
 
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'
445
 
        self.help = 'help'
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
450
1035
 
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)
461
 
        self.mocker.replay()
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(),
 
1044
            PASSWORD2_ENTRY)
 
1045
        self.assertEqual(self.view.password_info_label.text(), PASSWORD_HELP)
 
1046
        self.assertEqual(self.view.captcha_solution_edit.placeholderText(),
 
1047
            CAPTCHA_SOLUTION_ENTRY)
463
1048
 
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)
473
 
        self.mocker.replay()
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)
475
1056
 
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)
493
 
        self.mocker.replay()
494
 
        self.controller._connect_ui_elements()
495
 
 
496
 
    def test_is_correct_password_confirmation_false(self):
497
 
        """Test when the password is not correct."""
498
 
        password = 'test'
499
 
        self.view.ui.password_edit.text()
500
 
        self.mocker.result('other')
501
 
        self.mocker.replay()
502
 
        self.assertFalse(
503
 
                    self.controller.is_correct_password_confirmation(password))
504
 
 
505
 
    def test_is_correct_password_confirmation_true(self):
506
 
        """Test when the password is correct."""
507
 
        password = 'test'
508
 
        self.view.ui.password_edit.text()
509
 
        self.mocker.result(password)
510
 
        self.mocker.replay()
511
 
        self.assertTrue(
512
 
                    self.controller.is_correct_password_confirmation(password))
513
 
 
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')
519
 
        self.mocker.replay()
520
 
        self.assertFalse(
521
 
                self.controller.is_correct_email_confirmation(email_address))
522
 
 
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)
528
 
        self.mocker.replay()
529
 
        self.assertTrue(
530
 
                self.controller.is_correct_email_confirmation(email_address))
531
 
 
532
 
    def test_set_next_validation(self):
533
 
        """Test the callback."""
534
 
        name = 'name'
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)
557
 
        self.view.captcha_id
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()
566
 
        self.mocker.replay()
567
 
        self.controller.set_next_validation()
568
 
 
569
 
    def test_set_next_validation_wrong_email(self):
570
 
        """Test the callback when there is a wrong email."""
571
 
        name = 'name'
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,
591
 
            EMAIL_MISMATCH)
592
 
        self.mocker.replay()
593
 
        self.assertFalse(self.controller.validate_form())
594
 
 
595
 
    def test_set_next_validation_not_min_password(self):
596
 
        """Test the callback with a weak password."""
597
 
        name = 'name'
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)
614
 
        self.mocker.replay()
615
 
        self.assertFalse(self.controller.validate_form())
616
 
 
617
 
    def test_set_next_validation_wrong_password(self):
618
 
        """Test the callback where the password is wrong."""
619
 
        name = 'name'
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)
636
 
        self.mocker.replay()
637
 
        self.assertFalse(self.controller.validate_form())
638
 
 
639
 
    def test_set_next_validation_empty_captcha(self):
640
 
        """Test the callback where the password is wrong."""
641
 
        name = 'name'
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)
658
 
        self.mocker.replay()
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)
660
1085
 
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))
682
 
        self.mocker.replay()
 
1087
        """Test _set_line_validations from controller."""
683
1088
        self.controller._set_line_edits_validations()
684
 
 
685
 
 
686
 
class FakeMessageBox(object):
687
 
 
688
 
    """A fake message box."""
689
 
 
690
 
    args = None
691
 
    critical_args = None
692
 
 
693
 
    def __init__(self, *args, **kwargs):
694
 
        self.args = (args, kwargs)
695
 
 
696
 
    def critical(self, *args, **kwargs):
697
 
        """Fake critical popup."""
698
 
        self.critical_args = (args, kwargs)
699
 
 
700
 
 
701
 
class FakeSetupAccountUi(object):
702
 
    """Fake ui variable for FakeView."""
703
 
 
704
 
    email_assistance = None
705
 
 
706
 
 
707
 
class FakeView(object):
708
 
 
709
 
    """A fake view."""
710
 
 
711
 
    app_name = "TestApp"
712
 
    ui = FakeSetupAccountUi()
713
 
 
714
 
    def wizard(self):
715
 
        """Use itself as a fake wizard, too."""
716
 
        return self
717
 
 
718
 
    def set_error_message(self, label, msg):
719
 
        """Simulate to add the message to the label as SetupAccount do."""
720
 
 
721
 
 
722
 
class FakeSetupAccountView(FakeView):
723
 
 
724
 
    """A Fake view."""
725
 
 
726
 
    captcha_file = None
727
 
    captcha_image = None
728
 
    captcha_refresh_executed = False
729
 
    captcha_refreshing_value = False
730
 
 
731
 
    def on_captcha_refresh_complete(self):
732
 
        """Fake the call to refresh finished."""
733
 
        self.captcha_refresh_executed = True
734
 
 
735
 
    def on_captcha_refreshing(self):
736
 
        """Fake the call to refreshing."""
737
 
        self.captcha_refreshing_value = True
738
 
 
739
 
    def fake_open(self, filename):
740
 
        """Fake open for Image."""
741
 
        return self
742
 
 
743
 
    # pylint: disable=W0622
744
 
    def save(self, io, format):
745
 
        """Fake save for Image."""
746
 
    # pylint: enable=W0622
747
 
 
748
 
 
749
 
class FakeControllerForCaptcha(object):
750
 
 
751
 
    """Fake controller to test refresh_captcha method."""
752
 
 
753
 
    def __init__(self):
754
 
        """Initialize FakeControllerForCaptcha."""
755
 
        self.callback_error = False
756
 
 
757
 
    def generate_captcha(self, *args):
758
 
        """Fake generate deferred for captcha."""
759
 
        return self
760
 
 
761
 
    # pylint: disable=C0103
762
 
    def addErrback(self, call):
763
 
        """Fake addErrback."""
764
 
        self.callback_error = call is not None
765
 
    # pylint: enable=C0103
766
 
 
767
 
 
768
 
class FakeEmailVerificationView(FakePageUiStyle):
769
 
    """Fake EmailVerification Page."""
770
 
 
771
 
    def __init__(self, *args):
772
 
        """Initialize."""
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)
 
1091
        for e in elements:
 
1092
            self.assertTrue(type(e[0]) is QLineEdit)
 
1093
            self.assertIsInstance(e[1], collections.Callable)
 
1094
 
 
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)
 
1104
 
 
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)
 
1114
 
 
1115
    def test_enable_setup_button_only_checkbox_ok(self):
 
1116
        """Test enable button only name valid."""
 
1117
        self.all_invalid()
 
1118
        self.view.terms_checkbox.setChecked(True)
 
1119
        self.controller._enable_setup_button()
 
1120
 
 
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'])
 
1125
 
 
1126
    def test_enable_setup_button_only_name_ok(self):
 
1127
        """Test enable button only name valid."""
 
1128
        self.all_invalid()
 
1129
        self.view.name_edit.setText('Name')
 
1130
        self.controller._enable_setup_button()
 
1131
 
 
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'])
 
1136
 
 
1137
    def test_enable_setup_button_only_email_ok(self):
 
1138
        """Test enable button only name valid."""
 
1139
        self.all_invalid()
 
1140
        self.view.email_edit.setText('email@email.com')
 
1141
        self.controller._enable_setup_button()
 
1142
 
 
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'])
 
1147
 
 
1148
    def test_enable_setup_button_only_confirm_email_ok(self):
 
1149
        """Test enable button only name valid."""
 
1150
        self.all_invalid()
 
1151
        self.view.confirm_email_edit.setText('email@email.com')
 
1152
        self.controller._enable_setup_button()
 
1153
 
 
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'])
 
1158
 
 
1159
    def test_enable_setup_button_only_email_and_confirm_ok(self):
 
1160
        """Test enable button only name valid."""
 
1161
        self.all_invalid()
 
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()
 
1165
 
 
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'])
 
1170
 
 
1171
    def test_enable_setup_button_only_password_ok(self):
 
1172
        """Test enable button only name valid."""
 
1173
        self.all_invalid()
 
1174
        self.view.password_edit.setText('T3st3rqw')
 
1175
        self.controller._enable_setup_button()
 
1176
 
 
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'])
 
1181
 
 
1182
    def test_enable_setup_button_only_password_confirm_ok(self):
 
1183
        """Test enable button only name valid."""
 
1184
        self.all_invalid()
 
1185
        self.view.confirm_password_edit.setText('T3st3rqw')
 
1186
        self.controller._enable_setup_button()
 
1187
 
 
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'])
 
1192
 
 
1193
    def test_enable_setup_button_only_password_and_confirm_ok(self):
 
1194
        """Test enable button only name valid."""
 
1195
        self.all_invalid()
 
1196
        self.view.password_edit.setText('T3st3rqw')
 
1197
        self.view.confirm_password_edit.setText('T3st3rqw')
 
1198
        self.controller._enable_setup_button()
 
1199
 
 
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'])
 
1204
 
 
1205
    def test_enable_setup_button_only_captcha_ok(self):
 
1206
        """Test enable button only name valid."""
 
1207
        self.all_invalid()
 
1208
        self.view.captcha_solution_edit.setText('captcha solution')
 
1209
        self.controller._enable_setup_button()
 
1210
 
 
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'])
 
1215
 
 
1216
    def test_enable_setup_button_only_all_ok(self):
 
1217
        """Test enable button only name valid."""
 
1218
        self.all_valid()
 
1219
        self.controller._enable_setup_button()
 
1220
 
 
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'])
 
1225
 
 
1226
    def test_enable_setup_button_all_wrong(self):
 
1227
        """Test enable button only name valid."""
 
1228
        self.all_invalid()
 
1229
        self.controller._enable_setup_button()
 
1230
 
 
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'])
 
1235
 
 
1236
    def test_enable_setup_button_name_wrong(self):
 
1237
        """Test enable button only name valid."""
 
1238
        self.all_valid()
 
1239
        self.view.name_edit.setText('')
 
1240
        self.controller._enable_setup_button()
 
1241
 
 
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'])
 
1246
 
 
1247
    def test_enable_setup_button_email_wrong(self):
 
1248
        """Test enable button only name valid."""
 
1249
        self.all_valid()
 
1250
        self.view.email_edit.setText('')
 
1251
        self.controller._enable_setup_button()
 
1252
 
 
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'])
 
1257
 
 
1258
    def test_enable_setup_button_confirm_email_wrong(self):
 
1259
        """Test enable button only name valid."""
 
1260
        self.all_valid()
 
1261
        self.view.confirm_email_edit.setText('')
 
1262
        self.controller._enable_setup_button()
 
1263
 
 
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'])
 
1268
 
 
1269
    def test_enable_setup_button_password_wrong(self):
 
1270
        """Test enable button only name valid."""
 
1271
        self.all_valid()
 
1272
        self.view.password_edit.setText('')
 
1273
        self.controller._enable_setup_button()
 
1274
 
 
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'])
 
1279
 
 
1280
    def test_enable_setup_button_confirm_password_wrong(self):
 
1281
        """Test enable button only name valid."""
 
1282
        self.all_valid()
 
1283
        self.view.confirm_password_edit.setText('')
 
1284
        self.controller._enable_setup_button()
 
1285
 
 
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'])
 
1290
 
 
1291
    def test_enable_setup_button_captcha_wrong(self):
 
1292
        """Test enable button only name valid."""
 
1293
        self.all_valid()
 
1294
        self.view.captcha_solution_edit.setText('')
 
1295
        self.controller._enable_setup_button()
 
1296
 
 
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'])
 
1301
 
 
1302
    def test_enable_setup_button_checkbox_wrong(self):
 
1303
        """Test enable button only name valid."""
 
1304
        self.all_valid()
 
1305
        self.view.terms_checkbox.setChecked(False)
 
1306
        self.controller._enable_setup_button()
 
1307
 
 
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'])
 
1312
 
 
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 \
 
1319
            QLineEdit)
 
1320
        self.assertTrue(type(self.view.properties['password']) is \
 
1321
            QLineEdit)
 
1322
 
 
1323
    def test_validate_form_all_ok(self):
 
1324
        """Test the result of validate_form."""
 
1325
        self.all_valid()
 
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(),
 
1330
            EMAIL_MISMATCH)
 
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), {}))
 
1335
 
 
1336
    def test_validate_form_all_wrong(self):
 
1337
        """Test the result of validate_form."""
 
1338
        self.all_invalid()
 
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(),
 
1343
            EMAIL_MISMATCH)
 
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), {}))
 
1348
 
 
1349
    def test_validate_form_name_ok(self):
 
1350
        """Test the result of validate_form."""
 
1351
        self.all_invalid()
 
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(),
 
1356
            EMAIL_MISMATCH)
 
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), {}))
 
1361
 
 
1362
    def test_validate_form_email_ok(self):
 
1363
        """Test the result of validate_form."""
 
1364
        self.all_invalid()
 
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(),
 
1369
            EMAIL_MISMATCH)
 
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), {}))
 
1374
 
 
1375
    def test_validate_form_confirm_email_ok(self):
 
1376
        """Test the result of validate_form."""
 
1377
        self.all_invalid()
 
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), {}))
 
1386
 
 
1387
    def test_validate_form_password_weak(self):
 
1388
        """Test the result of validate_form."""
 
1389
        self.all_valid()
 
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), {}))
 
1396
 
 
1397
    def test_validate_form_password_mismatch(self):
 
1398
        """Test the result of validate_form."""
 
1399
        self.all_valid()
 
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), {}))
 
1406
 
 
1407
    def test_validate_form_captcha_required(self):
 
1408
        """Test the result of validate_form."""
 
1409
        self.all_valid()
 
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), {}))
 
1415
 
 
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'))
 
1420
 
 
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')
 
1425
 
 
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')
776
1430
 
777
1431
 
778
1432
class SetupAccountControllerCaptchaTest(BaseTestCase):
852
1506
        self.assertTrue(self.controller.view.captcha_refresh_executed)
853
1507
 
854
1508
 
855
 
class EmailVerificationControllerTestCase(MockerTestCase):
 
1509
class EmailVerificationControllerTestCase(BaseTestCase):
856
1510
    """Test the controller."""
857
1511
 
858
1512
    def setUp(self):
859
1513
        """Set tests."""
860
1514
        super(EmailVerificationControllerTestCase, self).setUp()
861
 
        self.view = self.mocker.mock()
862
 
        self.backend = self.mocker.mock()
 
1515
        self.view = EmailVerificationView()
 
1516
        self.backend = self.view
863
1517
        self.controller = EmailVerificationController(
864
 
            message_box=self.mocker.mock())
 
1518
            message_box=FakeMessageBox())
865
1519
        self.controller.view = self.view
866
1520
        self.controller.backend = self.backend
867
 
 
868
 
    def test_set_translated_strings(self):
869
 
        """Test that strings are translated."""
870
 
        self.mocker.replay()
871
 
        self.controller._set_translated_strings()
 
1521
        self.patch(self.controller, "get_backend", self.view.fake_backend)
 
1522
        self.email = 'email@email.com'
 
1523
        self.password = 'T3st3rqw'
 
1524
        self.view.fake_wizard.registerField('email_address', self.email)
 
1525
        self.view.fake_wizard.registerField('password', self.password)
872
1526
 
873
1527
    def test_connect_ui_elements(self):
874
1528
        """Set the ui connections."""
875
 
        self.view.ui.verification_code_edit.textChanged.connect(
876
 
            self.controller.validate_form)
877
 
        self.view.next_button.clicked.connect(self.controller.validate_email)
878
 
        self.backend.on_email_validated_cb = MATCH(callable)
879
 
        self.backend.on_email_validation_error_cb = \
880
 
                                    self.controller.on_email_validation_error
881
 
        self.mocker.replay()
882
1529
        self.controller._connect_ui_elements()
 
1530
        self.assertEqual(self.view.verification_code_edit.receivers(
 
1531
            SIGNAL('textChanged(QString)')), 1)
 
1532
        self.assertIsInstance(self.view.next_button.function,
 
1533
            collections.Callable)
 
1534
        self.assertIsInstance(self.view.on_email_validated_cb,
 
1535
            collections.Callable)
 
1536
        self.assertIsInstance(self.view.on_email_validation_error_cb,
 
1537
            collections.Callable)
883
1538
 
884
1539
    def test_set_titles(self):
885
1540
        """Test that the titles are set."""
886
 
        self.view.header.set_title(VERIFY_EMAIL_CONTENT)
887
 
        self.view.header.set_subtitle(VERIFY_EMAIL_CONTENT % {
888
 
            "app_name": None,
889
 
            "email": None,
890
 
            })
891
 
        self.mocker.replay()
892
 
        self.view.header.set_title(VERIFY_EMAIL_CONTENT)
893
 
        self.view.header.set_subtitle(VERIFY_EMAIL_CONTENT % {
894
 
            "app_name": None,
895
 
            "email": None,
 
1541
        self.controller.set_titles()
 
1542
        self.assertEqual(self.view.properties['title'], VERIFY_EMAIL_TITLE)
 
1543
        self.assertEqual(self.view.properties['subtitle'],
 
1544
            VERIFY_EMAIL_CONTENT % {
 
1545
            "app_name": self.view.fake_wizard.app_name,
 
1546
            "email": self.email,
896
1547
            })
897
1548
 
898
1549
    def test_validate_email(self):
899
1550
        """Test the callback."""
900
 
        email_address = 'email@test.com'
901
 
        password = 'password'
902
 
        app_name = 'app_name'
903
 
        code = 'code'
904
 
        self.view.wizard().field('email_address').toString()
905
 
        self.mocker.result(email_address)
906
 
        self.view.wizard().field('password').toString()
907
 
        self.mocker.result(password)
908
 
        self.view.wizard().app_name
909
 
        self.mocker.result(app_name)
910
 
        self.view.ui.verification_code_edit.text()
911
 
        self.mocker.result(code)
912
 
        self.backend.validate_email(app_name, email_address, password, code)
913
 
        self.mocker.replay()
 
1551
        code = 'qwe123'
 
1552
        self.view.verification_code_edit.setText(code)
914
1553
        self.controller.validate_email()
 
1554
        self.assertEqual(self.view.properties['validate_email'],
 
1555
            ((self.view.fake_wizard.app_name, self.email, self.password,
 
1556
            code), {}))
 
1557
 
 
1558
    def test_validate_form(self):
 
1559
        """Test validate_form."""
 
1560
        self.view.verification_code = 'qwe123'
 
1561
        self.controller.validate_form()
 
1562
        self.assertTrue(self.view.next_button.enabled())
 
1563
        self.assertFalse(self.view.next_button.property('DisabledState'))
 
1564
        self.assertTrue(self.view.next_button.properties['polish'])
 
1565
        self.assertTrue(self.view.next_button.properties['unpolish'])
 
1566
 
 
1567
        self.view.verification_code = ''
 
1568
        self.controller.validate_form()
 
1569
        self.assertFalse(self.view.next_button.enabled())
 
1570
        self.assertTrue(self.view.next_button.property('DisabledState'))
 
1571
        self.assertTrue(self.view.next_button.properties['polish'])
 
1572
        self.assertTrue(self.view.next_button.properties['unpolish'])
 
1573
 
 
1574
    def test_page_initialized(self):
 
1575
        """test pageInitialized to check the initial state of the page."""
 
1576
        self.controller.pageInitialized()
 
1577
        self.assertTrue(self.view.next_button.properties['default'])
 
1578
        self.assertFalse(self.view.next_button.enabled())
 
1579
        self.assertTrue(self.view.next_button.property('DisabledState'))
 
1580
        self.assertTrue(self.view.next_button.properties['polish'])
 
1581
        self.assertTrue(self.view.next_button.properties['unpolish'])
 
1582
 
 
1583
    def test_on_email_validation_error(self):
 
1584
        """Test on_email_validation_error."""
 
1585
        error = dict(error='email error')
 
1586
        self.controller.on_email_validation_error('app', error)
 
1587
        result = '\n'.join(
 
1588
                [('%s: %s' % (k, v)) for k, v in error.iteritems()])
 
1589
        self.assertEqual(self.controller.message_box.critical_args,
 
1590
            ((result, self.view), {}))
 
1591
 
 
1592
    def test_on_email_validated(self):
 
1593
        """Test on_email_validated."""
 
1594
        self.controller.on_email_validated('app_name')
 
1595
        self.assertEqual(self.view.fake_wizard.properties['emit'],
 
1596
            ('app_name', self.email))
915
1597
 
916
1598
 
917
1599
class EmailVerificationControllerValidationTestCase(BaseTestCase):
957
1639
            not self.controller.view.next_button.enabled())
958
1640
 
959
1641
 
960
 
class ErrorControllerTestCase(MockerTestCase):
 
1642
class ErrorControllerTestCase(BaseTestCase):
961
1643
    """Test the success page controller."""
962
1644
 
963
1645
    def setUp(self):
964
1646
        """Set the tests."""
965
1647
        super(ErrorControllerTestCase, self).setUp()
966
 
        self.view = self.mocker.mock()
967
 
        self.backend = self.mocker.mock()
 
1648
        self.view = ErrorPageView()
 
1649
        self.backend = self.view
968
1650
        self.controller = ErrorController()
969
1651
        self.controller.view = self.view
970
1652
        self.controller.backend = self.backend
973
1655
        """Test the process that sets the ui."""
974
1656
        self.controller._title = ERROR
975
1657
        self.controller._subtitle = ERROR
976
 
        self.view.next = -1
977
 
        self.view.ui.error_message_label.setText(ERROR)
978
 
        self.view.header.set_title(ERROR)
979
 
        self.view.header.set_subtitle(ERROR)
980
 
        self.mocker.replay()
981
 
        self.controller.setupUi(self.view)
982
 
 
983
 
    def test_hide_titles(self):
984
 
        """Test how the different titles are set."""
985
 
        self.view.next = -1
986
 
        self.view.ui.error_message_label.setText(ERROR)
987
 
        self.view.header.set_title('')
988
 
        self.view.header.set_subtitle('')
989
 
        self.mocker.replay()
990
 
        self.controller.setupUi(self.view)
991
 
 
992
 
 
993
 
class SuccessControllerTestCase(MockerTestCase):
 
1658
        self.controller.setupUi(self.view)
 
1659
        self.assertEqual(self.view.error_message_label.text(), ERROR)
 
1660
        self.assertEqual(self.view.properties['title'], ERROR)
 
1661
        self.assertEqual(self.view.properties['subtitle'], ERROR)
 
1662
        self.assertEqual(self.view.next, -1)
 
1663
 
 
1664
 
 
1665
class SuccessControllerTestCase(BaseTestCase):
994
1666
    """Test the success page controller."""
995
1667
 
996
1668
    def setUp(self):
997
1669
        """Set the tests."""
998
1670
        super(SuccessControllerTestCase, self).setUp()
999
 
        self.view = self.mocker.mock()
1000
 
        self.backend = self.mocker.mock()
 
1671
        self.view = SuccessPageView()
 
1672
        self.backend = self.view
1001
1673
        self.controller = SuccessController()
1002
1674
        self.controller.view = self.view
1003
1675
        self.controller.backend = self.backend
1006
1678
        """Test the process that sets the ui."""
1007
1679
        self.controller._title = SUCCESS
1008
1680
        self.controller._subtitle = SUCCESS
1009
 
        self.view.next = -1
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)
1015
 
 
1016
 
    def test_hide_titles(self):
1017
 
        """Test how the different titles are set."""
1018
 
        self.view.next = -1
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)
1024
 
 
1025
 
 
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)
 
1686
 
 
1687
 
 
1688
class UbuntuSSOWizardControllerTestCase(BaseTestCase):
1027
1689
    """Test the wizard controller."""
1028
1690
 
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
1039
1700
 
1040
1701
    def test_on_user_cancelation(self):
1041
1702
        """Test that the callback is indeed called."""
1042
 
        self.view.app_name
1043
 
        self.mocker.result(self.app_name)
1044
1703
        self.controller.user_cancellation_callback = self.callback
1045
 
        self.callback(self.app_name)
1046
 
        self.view.close()
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, ), {}))
1049
1708
 
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), {}))
1058
1717
 
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), {}))
1067
1726
 
1068
1727
    def test_show_success_message(self):
1069
1728
        """Test that the correct page will be shown."""
1070
 
        success_page_id = 0
1071
 
        # the buttons layout we expect to have
1072
 
        buttons_layout = []
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
1080
 
        self.view.next()
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
 
1731
        layout = []
 
1732
        layout.append(QWizard.Stretch)
 
1733
        layout.append(QWizard.FinishButton)
 
1734
 
 
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)
1084
1738
 
1085
1739
    def test_show_error_message(self):
1086
1740
        """Test that the correct page will be shown."""
1087
 
        error_page_id = 0
1088
 
        # the buttons layout we expect to have
1089
 
        buttons_layout = []
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
1097
 
        self.view.next()
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
 
1743
        layout = []
 
1744
        layout.append(QWizard.Stretch)
 
1745
        layout.append(QWizard.FinishButton)
 
1746
 
 
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)
1101
1750
 
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)
1113
 
 
1114
 
 
1115
 
class FakeLineEditForForgottenPage(object):
1116
 
 
1117
 
    """Fake Line Edit"""
1118
 
 
1119
 
    def __init__(self):
1120
 
        """Initilize object."""
1121
 
        self.email_text = QString()
1122
 
 
1123
 
    def text(self):
1124
 
        """Fake text for QLineEdit."""
1125
 
        return self.email_text
1126
 
 
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
1133
 
 
1134
 
 
1135
 
class FakeForgottenPasswordPage(FakePageUiStyle):
1136
 
    """Fake the page."""
1137
 
 
1138
 
    def __init__(self):
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)
1146
1762
 
1147
1763
 
1148
1764
class ForgottenPasswordControllerValidationTest(BaseTestCase):
1155
1771
        self.message_box = FakeMessageBox()
1156
1772
        self.controller = ForgottenPasswordController(
1157
1773
            message_box=self.message_box)
1158
 
        self.controller.view = FakeForgottenPasswordPage()
 
1774
        self.view = FakeForgottenPasswordPage()
 
1775
        self.controller.view = self.view
 
1776
        self.patch(self.controller, "get_backend", self.view.fake_backend)
1159
1777
 
1160
1778
    def test_page_initialized(self):
1161
1779
        """Test the initial state of the page when it is loaded."""
1236
1854
            ((msg, self.controller.view), {}))
1237
1855
 
1238
1856
 
1239
 
class ForgottenPasswordControllerTestCase(MockerTestCase):
 
1857
class ForgottenPasswordControllerTestCase(BaseTestCase):
1240
1858
    """Test the controller of the fogotten password page."""
1241
1859
 
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'
1251
1869
 
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)
1260
1875
 
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':
1267
 
                                    self.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(),
 
1883
            EMAIL_LABEL)
 
1884
        self.assertEqual(self.view.send_button.text(),
 
1885
            RESET_PASSWORD)
 
1886
        self.assertEqual(self.view.try_again_button.text(),
 
1887
            TRY_AGAIN_BUTTON)
1273
1888
 
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)
 
1894
        for e in elements:
 
1895
            self.assertTrue(e[0] is self.view.email_address_line_edit)
 
1896
            self.assertIsInstance(e[1], collections.Callable)
1282
1897
 
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)
1294
1911
 
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())
1301
1917
 
1302
1918
    def test_on_password_reset_token_sent(self):
1303
1919
        """Test that the on_password_token_sent callback works as expected."""
1304
 
        page_id = 3
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()
1311
 
 
1312
 
 
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)
 
1924
 
 
1925
 
 
1926
class ResetPasswordControllerTestCase(BaseTestCase):
1314
1927
    """Ensure that the reset password works as expected."""
1315
1928
 
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
1324
1937
 
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(),
 
1942
            RESET_PASSWORD)
 
1943
        self.assertEqual(self.view.properties['subtitle'],
 
1944
            PASSWORD_HELP)
1332
1945
 
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(
1342
 
            MATCH(callable))
1343
 
        self.view.ui.password_line_edit.textChanged.connect(
1344
 
            MATCH(callable))
1345
 
        self.view.ui.confirm_password_line_edit.textChanged.connect(
1346
 
            MATCH(callable))
1347
 
 
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)
1350
1961
 
1351
1962
    def test_add_line_edits_validations(self):
1352
1963
        """Ensure that the line validation have been added."""
1353
 
        line_edit = 'line_edit'
1354
 
        emit = 'emit'
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)
 
1969
        for e in elements:
 
1970
            self.assertTrue(type(e[0]) is QLineEdit)
 
1971
            self.assertIsInstance(e[1], collections.Callable)
1367
1972
 
1368
1973
    def test_set_new_password(self):
1369
1974
        """Test that the correct action is performed."""
1370
 
        app_name = 'app_name'
1371
 
        email = 'email'
1372
 
        code = 'code'
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'
 
1976
        code = 'qwe123'
 
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), {}))
1385
1984
 
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(
1393
1990
                                                                    password))
1394
1991
 
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(
1402
1997
                                                                    password))
1403
1998
 
1404
1999
 
1405
 
class FakeResetPasswordPage(FakePageUiStyle):
1406
 
 
1407
 
    """Fake ResetPasswordPage."""
1408
 
 
1409
 
    def __init__(self, *args):
1410
 
        """Initialize."""
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.
1417
 
 
1418
 
 
1419
2000
class ResetPasswordControllerValidationTest(BaseTestCase):
1420
2001
 
1421
2002
    """Tests for ResetPasswordController, but without Mocker."""
1482
2063
        self.assertFalse(self.controller.view.reset_password_button.enabled())
1483
2064
 
1484
2065
 
1485
 
class FakeWizardForResetPassword(object):
1486
 
 
1487
 
    """Fake Wizard for ResetPasswordController."""
1488
 
 
1489
 
    def __init__(self):
1490
 
        self.current_user = self
1491
 
        self.ui = self
1492
 
        self.email_edit = self
1493
 
        self.email_line_edit = self
1494
 
        self.forgotten = self
1495
 
        self.overlay = self
1496
 
        self.count_back = 0
1497
 
        self.hide_value = False
1498
 
        self.text_value = 'mail@mail.com'
1499
 
 
1500
 
    def wizard(self):
1501
 
        """Fake wizard function for view."""
1502
 
        return self
1503
 
 
1504
 
    def back(self):
1505
 
        """Fake back for wizard."""
1506
 
        self.count_back += 1
1507
 
 
1508
 
    def hide(self):
1509
 
        """Fake hide for overlay."""
1510
 
        self.hide_value = True
1511
 
 
1512
 
    def text(self):
1513
 
        """Fake text for QLineEdit."""
1514
 
        return self.text_value
1515
 
 
1516
 
    # pylint: disable=C0103
1517
 
    def setText(self, text):
1518
 
        """Fake setText for QLineEdit."""
1519
 
        self.text_value = text
1520
 
    # pylint: enable=C0103
1521
 
 
1522
 
 
1523
2066
class ResetPasswordControllerRealControllerTest(BaseTestCase):
1524
2067
 
1525
2068
    """Tests for ResetPasswordController, but without Mocker."""