summaryrefslogtreecommitdiff
path: root/src/leap/gui/tests/test_firstrun_wizard.py
blob: 395604d3c3fed77968bf68e64ce9327ce83bcc62 (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
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


class TestWizard(firstrun.wizard.FirstRunWizard):
    pass


PAGES_DICT = dict((
    ('intro', firstrun.intro.IntroPage),
    ('providerselection',
        firstrun.providerselect.SelectProviderPage),
    ('login', firstrun.login.LogInPage),
    ('providerinfo', firstrun.providerinfo.ProviderInfoPage),
    ('providersetupvalidation',
        firstrun.providersetup.ProviderSetupValidationPage),
    ('signup', firstrun.register.RegisterUserPage),
    ('connect',
        firstrun.connect.ConnectionPage),
    ('lastpage', firstrun.last.LastPage)
))


mockQSettings = mock.MagicMock()
mockQSettings().setValue.return_value = True

#PyQt4.QtCore.QSettings = mockQSettings


class FirstRunWizardTestCase(qunittest.TestCase):

    # XXX can spy on signal connections

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

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

    def test_defaults(self):
        self.assertEqual(self.wizard.pages_dict, PAGES_DICT)

    @mock.patch('PyQt4.QtCore.QSettings', mockQSettings)
    def test_accept(self):
        """
        test the main accept method
        that gets called when user has gone
        thru all the wizard and click on finish button
        """

        self.wizard.success_cb = mock.Mock()
        self.wizard.success_cb.return_value = True

        # dummy values; we inject them in the field
        # mocks (where wizard gets them) and then
        # we check that they are passed to QSettings.setValue
        field_returns = ["testuser", "1234", "testprovider", True]

        def field_side_effects(*args):
            return field_returns.pop(0)

        self.wizard.field = mock.Mock(side_effect=field_side_effects)
        self.wizard.get_random_str = mock.Mock()
        RANDOMSTR = "thisisarandomstringTM"
        self.wizard.get_random_str.return_value = RANDOMSTR

        # mocked settings (see decorator on this method)
        mqs = PyQt4.QtCore.QSettings

        # go! call accept...
        self.wizard.accept()

        # did settings().setValue get called with the proper
        # arguments?
        call = mock.call
        calls = [call("FirstRunWizardDone", True),
                 call("provider_domain", "testprovider"),
                 call("remember_user_and_pass", True),
                 call("username", "testuser@testprovider"),
                 call("testprovider_seed", RANDOMSTR)]
        mqs().setValue.assert_has_calls(calls, any_order=True)

        # assert success callback is success oh boy
        self.wizard.success_cb.assert_called_with()

    def test_random_str(self):
        r = self.wizard.get_random_str(42)
        self.assertTrue(len(r) == 42)

    def test_page_index(self):
        """
        we test both the get_page_index function
        and the correct ordering of names
        """
        # remember it's implemented as an ordered dict

        pagenames = ('intro', 'providerselection', 'login', 'providerinfo',
                     'providersetupvalidation', 'signup', 'connect',
                     'lastpage')
        eq = self.assertEqual
        w = self.wizard
        for index, name in enumerate(pagenames):
            eq(w.get_page_index(name), index)

    def test_validation_errors(self):
        """
        tests getters and setters for validation errors
        """
        page = "testpage"
        eq = self.assertEqual
        w = self.wizard
        eq(w.get_validation_error(page), None)
        w.set_validation_error(page, "error")
        eq(w.get_validation_error(page), "error")
        w.clean_validation_error(page)
        eq(w.get_validation_error(page), None)

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