~nataliabidart/ubuntu-sso-client/stable-3-0-update

« back to all changes in this revision

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

- Fixed: There is no feedback on captcha loading/refreshing (LP: #852105).

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
from mocker import MATCH, MockerTestCase
25
25
from PyQt4.QtGui import QWizard
26
26
 
 
27
# pylint: disable=F0401
 
28
try:
 
29
    from PIL import Image
 
30
except ImportError:
 
31
    import Image
 
32
# pylint: enable=F0401
 
33
 
27
34
from ubuntu_sso.qt.controllers import (
28
35
    BackendController,
29
36
    ChooseSignInController,
711
718
        """Simulate to add the message to the label as SetupAccount do."""
712
719
 
713
720
 
 
721
class FakeSetupAccountView(FakeView):
 
722
 
 
723
    """A Fake view."""
 
724
 
 
725
    captcha_file = None
 
726
    captcha_image = None
 
727
    captcha_refresh_executed = False
 
728
    captcha_refreshing_value = False
 
729
 
 
730
    def on_captcha_refresh_complete(self):
 
731
        """Fake the call to refresh finished."""
 
732
        self.captcha_refresh_executed = True
 
733
 
 
734
    def on_captcha_refreshing(self):
 
735
        """Fake the call to refreshing."""
 
736
        self.captcha_refreshing_value = True
 
737
 
 
738
    def fake_open(self, filename):
 
739
        """Fake open for Image."""
 
740
        return self
 
741
 
 
742
    # pylint: disable=W0622
 
743
    def save(self, io, format):
 
744
        """Fake save for Image."""
 
745
    # pylint: enable=W0622
 
746
 
 
747
 
 
748
class FakeControllerForCaptcha(object):
 
749
 
 
750
    """Fake controller to test refresh_captcha method."""
 
751
 
 
752
    def __init__(self):
 
753
        """Initialize FakeControllerForCaptcha."""
 
754
        self.callback_error = False
 
755
 
 
756
    def generate_captcha(self, *args):
 
757
        """Fake generate deferred for captcha."""
 
758
        return self
 
759
 
 
760
    # pylint: disable=C0103
 
761
    def addErrback(self, call):
 
762
        """Fake addErrback."""
 
763
        self.callback_error = call is not None
 
764
    # pylint: enable=C0103
 
765
 
 
766
 
714
767
class FakeEmailVerificationView(FakePageUiStyle):
715
768
    """Fake EmailVerification Page."""
716
769
 
721
774
        self.next_button = self
722
775
 
723
776
 
 
777
class SetupAccountControllerCaptchaTest(BaseTestCase):
 
778
    """Tests for SetupAccountController, but without Mocker."""
 
779
 
 
780
    def setUp(self):
 
781
        """Set the different tests."""
 
782
        super(SetupAccountControllerCaptchaTest, self).setUp()
 
783
        self.message_box = FakeMessageBox()
 
784
        self.controller = SetUpAccountController(message_box=self.message_box)
 
785
        self.patch(self.controller, 'view', FakeSetupAccountView())
 
786
        self.fake_backend = FakeControllerForCaptcha()
 
787
        self.patch(self.controller, 'backend', self.fake_backend)
 
788
 
 
789
    def test_refresh_captcha(self):
 
790
        """Test the Refresh Captcha function."""
 
791
        self.assertFalse(self.controller.view.captcha_refreshing_value)
 
792
        self.controller._refresh_captcha()
 
793
        self.assertTrue(self.controller.view.captcha_refreshing_value)
 
794
        self.assertTrue(self.fake_backend.callback_error)
 
795
 
 
796
 
724
797
class SetupAccountControllerValidationTest(BaseTestCase):
725
798
    """Tests for SetupAccountController, but without Mocker."""
726
799
 
730
803
        self.message_box = FakeMessageBox()
731
804
        self.controller = SetUpAccountController(message_box=self.message_box)
732
805
        self.patch(self.controller, '_refresh_captcha', self._set_called)
733
 
        self.patch(self.controller, 'view', FakeView())
 
806
        self.patch(self.controller, 'view', FakeSetupAccountView())
734
807
 
735
808
    def test_on_user_registration_refresh_captcha(self):
736
809
        """If there is a user reg. error, captcha should refresh."""
764
837
        expected = (('', self.controller.view), {})
765
838
        self.assertEqual(self.message_box.critical_args, expected)
766
839
 
 
840
    def test_on_captcha_generated(self):
 
841
        """Test if the method that shows the overlay is executed."""
 
842
        self.patch(Image, "open", self.controller.view.fake_open)
 
843
        self.assertFalse(self.controller.view.captcha_refresh_executed)
 
844
        self.controller.on_captcha_generated('app_name', 'captcha_id')
 
845
        self.assertTrue(self.controller.view.captcha_refresh_executed)
 
846
 
 
847
    def test_on_captcha_generation_error(self):
 
848
        """Test if the method that hides the overlay is executed."""
 
849
        self.assertFalse(self.controller.view.captcha_refresh_executed)
 
850
        self.controller.on_captcha_generation_error({})
 
851
        self.assertTrue(self.controller.view.captcha_refresh_executed)
 
852
 
767
853
 
768
854
class EmailVerificationControllerTestCase(MockerTestCase):
769
855
    """Test the controller."""