summaryrefslogtreecommitdiff
path: root/src/leap/gui/tests/test_firstrun_login.py
blob: 6c45b8ef7b25c08ec4767c9bde80e872079dc412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import sys
import unittest

import mock

from leap.testing import qunittest
#from leap.testing import pyqt

from PyQt4 import QtGui
#from PyQt4 import QtCore
#import PyQt4.QtCore  # some weirdness with mock module

from PyQt4.QtTest import QTest
from PyQt4.QtCore import Qt

from leap.gui import firstrun

try:
    from collections import OrderedDict
except ImportError:
    # We must be in 2.6
    from leap.util.dicts import OrderedDict


class TestPage(firstrun.login.LogInPage):
    pass


class LogInPageLogicTestCase(qunittest.TestCase):

    # XXX can spy on signal connections
    __name__ = "register user page logic tests"

    def setUp(self):
        self.app = QtGui.QApplication(sys.argv)
        QtGui.qApp = self.app
        self.page = TestPage(None)
        self.page.wizard = mock.MagicMock()

    def tearDown(self):
        QtGui.qApp = None
        self.app = None
        self.page = None

    def test__do_checks(self):
        eq = self.assertEqual

        self.page.userNameLineEdit.setText('testuser@domain')
        self.page.userPasswordLineEdit.setText('testpassword')

        # fake register process
        with mock.patch('leap.base.auth.LeapSRPRegister') as mockAuth:
            mockSignup = mock.MagicMock()

            reqMockup = mock.Mock()
            # XXX should inject bad json to get error
            reqMockup.content = '{"errors": null}'
            mockSignup.register_user.return_value = (True, reqMockup)
            mockAuth.return_value = mockSignup
            checks = [x for x in self.page._do_checks()]

            eq(len(checks), 4)
            labels = [str(x) for (x, y), z in checks]
            eq(labels, ['head_sentinel',
                        'Resolving domain name',
                        'Validating credentials',
                        'end_sentinel'])
            progress = [y for (x, y), z in checks]
            eq(progress, [0, 20, 60, 100])

            # normal run, ie, no exceptions

            checkfuns = [z for (x, y), z in checks]
            checkusername, resolvedomain, valcreds = checkfuns[:-1]

            self.assertTrue(checkusername())
            #self.mocknetchecker.check_name_resolution.assert_called_with(
                #'test_provider1')

            self.assertTrue(resolvedomain())
            #self.mockpcertchecker.is_https_working.assert_called_with(
                #"https://test_provider1", verify=True)

            self.assertTrue(valcreds())

        # XXX missing: inject failing exceptions
        # XXX TODO make it break


class RegisterUserPageUITestCase(qunittest.TestCase):

    # XXX can spy on signal connections
    __name__ = "Register User Page UI tests"

    def setUp(self):
        self.app = QtGui.QApplication(sys.argv)
        QtGui.qApp = self.app

        self.pagename = "signup"
        pages = OrderedDict((
            (self.pagename, TestPage),
            ('providersetupvalidation',
             firstrun.connect.ConnectionPage)))
        self.wizard = firstrun.wizard.FirstRunWizard(None, pages_dict=pages)
        self.page = self.wizard.page(self.wizard.get_page_index(self.pagename))

        self.page.do_checks = mock.Mock()

        # wizard would do this for us
        self.page.initializePage()

    def tearDown(self):
        QtGui.qApp = None
        self.app = None
        self.wizard = None

    # XXX refactor out
    def fill_field(self, field, text):
        """
        fills a field (line edit) that is passed along
        :param field: the qLineEdit
        :param text: the text to be filled
        :type field: QLineEdit widget
        :type text: str
        """
        keyp = QTest.keyPress
        field.setFocus(True)
        for c in text:
            keyp(field, c)
        self.assertEqual(field.text(), text)

    def del_field(self, field):
        """
        deletes entried text in
        field line edit
        :param field: the QLineEdit
        :type field: QLineEdit widget
        """
        keyp = QTest.keyPress
        for c in range(len(field.text())):
            keyp(field, Qt.Key_Backspace)
        self.assertEqual(field.text(), "")

    def test_buttons_disabled_until_textentry(self):
        # it's a commit button this time
        nextbutton = self.wizard.button(QtGui.QWizard.CommitButton)

        self.assertFalse(nextbutton.isEnabled())

        f_username = self.page.userNameLineEdit
        f_password = self.page.userPasswordLineEdit

        self.fill_field(f_username, "testuser")
        self.fill_field(f_password, "testpassword")

        # commit should be enabled
        # XXX Need a workaround here
        # because the isComplete is not being evaluated...
        # (no event loop running??)
        #import ipdb;ipdb.set_trace()
        #self.assertTrue(nextbutton.isEnabled())
        self.assertTrue(self.page.isComplete())

        self.del_field(f_username)
        self.del_field(f_password)

        # after rm fields commit button
        # should be disabled again
        #self.assertFalse(nextbutton.isEnabled())
        self.assertFalse(self.page.isComplete())

    def test_validate_page(self):
        self.assertFalse(self.page.validatePage())
        # XXX TODO MOAR CASES...
        # add errors, False
        # change done, False
        # not done, do_checks called
        # click confirm, True
        # done and do_confirm, True

    def test_next_id(self):
        self.assertEqual(self.page.nextId(), 1)

    def test_paint_event(self):
        self.page.populateErrors = mock.Mock()
        self.page.paintEvent(None)
        self.page.populateErrors.assert_called_with()

    def test_validation_ready(self):
        f_username = self.page.userNameLineEdit
        f_password = self.page.userPasswordLineEdit

        self.fill_field(f_username, "testuser")
        self.fill_field(f_password, "testpassword")

        self.page.done = True
        self.page.on_checks_validation_ready()
        self.assertFalse(f_username.isEnabled())
        self.assertFalse(f_password.isEnabled())

        self.assertEqual(self.page.validationMsg.text(),
                         "Credentials validated.")
        self.assertEqual(self.page.do_confirm_next, True)

    def test_regex(self):
        # XXX enter invalid username with key presses
        # check text is not updated
        pass


if __name__ == "__main__":
    unittest.main()