summaryrefslogtreecommitdiff
path: root/src/leap/gui/tests/test_firstrun_providerselect.py
blob: 18d890106d4449e31cf8fc19dbfbeb83e0d5d220 (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
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.providerselect.SelectProviderPage):
    pass


class SelectProviderPageLogicTestCase(qunittest.TestCase):

    # XXX can spy on signal connections

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

        mocknetchecker = mock.Mock()
        self.page.wizard().netchecker.return_value = mocknetchecker
        self.mocknetchecker = mocknetchecker

        mockpcertchecker = mock.Mock()
        self.page.wizard().providercertchecker.return_value = mockpcertchecker
        self.mockpcertchecker = mockpcertchecker

        mockeipconfchecker = mock.Mock()
        self.page.wizard().eipconfigchecker.return_value = mockeipconfchecker
        self.mockeipconfchecker = mockeipconfchecker

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

    def test__do_checks(self):
        eq = self.assertEqual

        self.page.providerNameEdit.setText('test_provider1')

        checks = [x for x in self.page._do_checks()]
        eq(len(checks), 5)
        labels = [str(x) for (x, y), z in checks]
        eq(labels, ['head_sentinel',
                    'Checking if it is a valid provider',
                    'Checking for a secure connection',
                    'Getting info from the provider',
                    'end_sentinel'])
        progress = [y for (x, y), z in checks]
        eq(progress, [0, 20, 40, 80, 100])

        # normal run, ie, no exceptions

        checkfuns = [z for (x, y), z in checks]
        namecheck, httpscheck, fetchinfo = checkfuns[1:-1]

        self.assertTrue(namecheck())
        self.mocknetchecker.check_name_resolution.assert_called_with(
            'test_provider1')

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

        self.assertTrue(fetchinfo())
        self.mockeipconfchecker.fetch_definition.assert_called_with(
            domain="test_provider1")

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


class SelectProviderPageUITestCase(qunittest.TestCase):

    # XXX can spy on signal connections
    __name__ = "Select Provider Page UI tests"

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

        self.pagename = "providerselection"
        pages = OrderedDict((
            (self.pagename, TestPage),
            ('providerinfo',
             firstrun.providerinfo.ProviderInfoPage)))
        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

    def fill_provider(self):
        """
        fills provider line edit
        """
        keyp = QTest.keyPress
        pedit = self.page.providerNameEdit
        pedit.setFocus(True)
        for c in "testprovider":
            keyp(pedit, c)
        self.assertEqual(pedit.text(), "testprovider")

    def del_provider(self):
        """
        deletes entried provider in
        line edit
        """
        keyp = QTest.keyPress
        pedit = self.page.providerNameEdit
        for c in range(len("testprovider")):
            keyp(pedit, Qt.Key_Backspace)
        self.assertEqual(pedit.text(), "")

    def test_buttons_disabled_until_textentry(self):
        nextbutton = self.wizard.button(QtGui.QWizard.NextButton)
        checkbutton = self.page.providerCheckButton

        self.assertFalse(nextbutton.isEnabled())
        self.assertFalse(checkbutton.isEnabled())

        self.fill_provider()
        # checkbutton should be enabled
        self.assertTrue(checkbutton.isEnabled())
        self.assertFalse(nextbutton.isEnabled())

        self.del_provider()
        # after rm provider checkbutton disabled again
        self.assertFalse(checkbutton.isEnabled())
        self.assertFalse(nextbutton.isEnabled())

    def test_check_button_triggers_tests(self):
        checkbutton = self.page.providerCheckButton
        self.assertFalse(checkbutton.isEnabled())
        self.assertFalse(self.page.do_checks.called)

        self.fill_provider()

        self.assertTrue(checkbutton.isEnabled())
        mclick = QTest.mouseClick
        # click!
        mclick(checkbutton, Qt.LeftButton)
        self.waitFor(seconds=0.1)
        self.assertTrue(self.page.do_checks.called)

        # XXX
        # can play with different side_effects for do_checks mock...
        # so we can see what happens with errors and so on

    def test_page_completed_after_checks(self):
        nextbutton = self.wizard.button(QtGui.QWizard.NextButton)
        self.assertFalse(nextbutton.isEnabled())

        self.assertFalse(self.page.isComplete())
        self.fill_provider()
        # simulate checks done
        self.page.done = True
        self.page.on_checks_validation_ready()
        self.assertTrue(self.page.isComplete())
        # cannot test for nexbutton enabled
        # cause it's the the wizard loop
        # that would do that I think

    def test_validate_page(self):
        self.assertTrue(self.page.validatePage())

    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()

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