~mandel/ubuntu-sso-client/pinned-certs

« back to all changes in this revision

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

  • Committer: Manuel de la Pena
  • Date: 2012-02-21 16:04:18 UTC
  • mfrom: (865.1.23 ssl-dialog)
  • Revision ID: manuel.delapena@canonical.com-20120221160418-6m3hj9otd7w2uqh4
Merged ssl-dialog into webclient-use-dialog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""Test the ssl dialog."""
 
18
 
 
19
from twisted.internet import defer
 
20
from twisted.trial.unittest import TestCase
 
21
 
 
22
from ubuntu_sso.qt import ssl_dialog
 
23
from ubuntu_sso.utils.ui import (
 
24
    CANCEL_BUTTON,
 
25
    SSL_HEADER,
 
26
    SSL_EXPLANATION,
 
27
    SSL_FIRST_REASON,
 
28
    SSL_SECOND_REASON,
 
29
    SSL_THIRD_REASON,
 
30
    SSL_CERT_DETAILS,
 
31
    SSL_NOT_SURE,
 
32
    SSL_REMEBER_DECITION,
 
33
    SSL_HELP_BUTTON,
 
34
    SSL_CONNECT_BUTTON,
 
35
)
 
36
 
 
37
# pylint: disable=W0212, C0103,
 
38
 
 
39
 
 
40
class FakeSignal(object):
 
41
    """A fake qt signal."""
 
42
 
 
43
    def __init__(self):
 
44
        """Create a new instance."""
 
45
        self.called = []
 
46
 
 
47
    def connect(self, cb):
 
48
        """Connect signals."""
 
49
        self.called.append(('connect', cb))
 
50
 
 
51
 
 
52
class FakeLayout(object):
 
53
    """A fake layout."""
 
54
 
 
55
    def __init__(self, object_name, called):
 
56
        """Create a new instance."""
 
57
        self.object_name = object_name
 
58
        self.called = called
 
59
 
 
60
    def insertWidget(self, index, widget):
 
61
        """Insert a widget in the layout."""
 
62
        if not self.object_name in self.called:
 
63
            self.called[self.object_name] = []
 
64
        self.called[self.object_name].append(('insertWidget', index, widget))
 
65
 
 
66
 
 
67
class FakeWidget(object):
 
68
    """A fake widget that contains text."""
 
69
 
 
70
    def __init__(self, object_name, called, text=''):
 
71
        """Create a new instance."""
 
72
        self.object_name = object_name
 
73
        self.called = called
 
74
        self.internal_text = text
 
75
        self.clicked = FakeSignal()
 
76
 
 
77
    # pylint: disable=C0103
 
78
    def setText(self, text):
 
79
        """Set the text of the widget."""
 
80
        self.internal_text = text
 
81
        if not self.object_name in self.called:
 
82
            self.called[self.object_name] = [('setText', text)]
 
83
        else:
 
84
            self.called[self.object_name].append(('setText', text))
 
85
 
 
86
    def text(self):
 
87
        """Return the text."""
 
88
        if not self.object_name in self.called:
 
89
            self.called[self.object_name] = [('text',)]
 
90
        else:
 
91
            self.called[self.object_name].append(('text',))
 
92
        return self.internal_text
 
93
 
 
94
    def setPixmap(self, pixmap):
 
95
        """Set a pixmap."""
 
96
        if not self.object_name in self.called:
 
97
            self.called[self.object_name] = [('setPixmap', pixmap)]
 
98
        else:
 
99
            self.called[self.object_name].append(('setPixmap', pixmap))
 
100
 
 
101
    def setVisible(self, visible):
 
102
        """Set the ui element visible."""
 
103
        if not self.object_name in self.called:
 
104
            self.called[self.object_name] = [('setVisible', visible)]
 
105
        else:
 
106
            self.called[self.object_name].append(('setVisible', visible))
 
107
 
 
108
    # pylint: enable=C0103
 
109
 
 
110
 
 
111
class FakeQIcon(object):
 
112
    """QIcon class."""
 
113
 
 
114
    def __init__(self):
 
115
        """Create a new instance."""
 
116
        self.called = []
 
117
 
 
118
    # pylint: disable=C0103
 
119
    def fromTheme(self, icon_id):
 
120
        """Return the icon with the given id."""
 
121
        self.called.append(('fromTheme', icon_id))
 
122
        return self
 
123
    # pylint: enable=C0103
 
124
 
 
125
    # pylint: disable=C0103
 
126
    def pixmap(self, h, w):
 
127
        """Get the pixmap with the given size."""
 
128
        self.called.append(('pixmap', h, w))
 
129
        return self
 
130
    # pylint: disable=C0103
 
131
 
 
132
 
 
133
class FakeExpander(object):
 
134
    """A fake expander widget."""
 
135
 
 
136
    def __init__(self):
 
137
        """Create a new instance."""
 
138
        self.called = []
 
139
        self.text = None
 
140
 
 
141
    def __call__(self, text):
 
142
        """Instance callable."""
 
143
        self.text = text
 
144
        return self
 
145
 
 
146
    def addWidget(self, widget):
 
147
        """Add a widget to the expander."""
 
148
        self.called.append(('addWidget', widget))
 
149
 
 
150
 
 
151
class FakeUISSLDialog(object):
 
152
    """The fake .ui for the creds dialog."""
 
153
 
 
154
    ui_labels = ('title_label', 'intro_label', 'not_sure_label',
 
155
                  'remember_checkbox', 'logo_label')
 
156
    ui_buttons = ('help_button', 'cancel_button', 'connect_button')
 
157
    ui_layouts = ('expander_layout',)
 
158
 
 
159
    def __init__(self):
 
160
        """Create a new instance."""
 
161
        self.called = {}
 
162
        for name in self.ui_labels + self.ui_buttons:
 
163
            setattr(self, name, FakeWidget(name, self.called))
 
164
        for name in self.ui_layouts:
 
165
            setattr(self, name, FakeLayout(name, self.called))
 
166
 
 
167
    # pylint: disable=C0103
 
168
    def setupUi(self, dialog):
 
169
        """Set the ui."""
 
170
        self.called['FakeUIProxyCredsDialog'] = ('setupUi', dialog)
 
171
    # pylint: enable=C0103
 
172
 
 
173
 
 
174
class SSLDialogTest(TestCase):
 
175
    """Test the ssl dialog."""
 
176
 
 
177
    @defer.inlineCallbacks
 
178
    def setUp(self):
 
179
        """Set the tests."""
 
180
        yield super(SSLDialogTest, self).setUp()
 
181
        self.domain = 'test-domain'
 
182
        self.details = 'SSL details'
 
183
 
 
184
        self.patch(ssl_dialog, 'Ui_SSLDialog',
 
185
                   FakeUISSLDialog)
 
186
        self.icon = FakeQIcon()
 
187
        self.patch(ssl_dialog, 'QIcon', self.icon)
 
188
 
 
189
        self.expander = FakeExpander()
 
190
        self.patch(ssl_dialog, 'QExpander', self.expander)
 
191
 
 
192
        self.dialog = ssl_dialog.SSLDialog(domain=self.domain,
 
193
                                           details=self.details)
 
194
        self.return_code = None
 
195
 
 
196
        def fake_done(code):
 
197
            """Fake done for the dialog."""
 
198
            self.return_code = code
 
199
 
 
200
        self.patch(self.dialog, 'done', fake_done)
 
201
 
 
202
    def test_init_none_domain(self):
 
203
        """Test the init method when the domain is none."""
 
204
        dialog = ssl_dialog.SSLDialog(domain=None, details=self.details)
 
205
        self.assertEqual(self.details, dialog.details)
 
206
        self.assertEqual('', dialog.domain)
 
207
 
 
208
    def test_init_none_details(self):
 
209
        """Test the init method when the details are none."""
 
210
        dialog = ssl_dialog.SSLDialog(domain=self.domain, details=None)
 
211
        self.assertEqual('', dialog.details)
 
212
        self.assertEqual(self.domain, dialog.domain)
 
213
 
 
214
    def test_set_labels(self):
 
215
        """Test that the labels contain the correct info."""
 
216
        self.assertEqual(SSL_HEADER,
 
217
                         unicode(self.dialog.ui.title_label.text()))
 
218
        explanation = SSL_EXPLANATION % dict(domain=self.domain)
 
219
        intro = ssl_dialog.REASONS_TEMPLATE % dict(explanation=explanation,
 
220
                                              first_reason=SSL_FIRST_REASON,
 
221
                                              second_reason=SSL_SECOND_REASON,
 
222
                                              third_reason=SSL_THIRD_REASON)
 
223
        self.assertEqual(intro, unicode(self.dialog.ui.intro_label.text()))
 
224
        self.assertEqual(SSL_NOT_SURE,
 
225
                unicode(self.dialog.ui.not_sure_label.text()))
 
226
        self.assertEqual(SSL_REMEBER_DECITION,
 
227
                unicode(self.dialog.ui.remember_checkbox.text()))
 
228
 
 
229
    def test_on_cancel_clicked(self):
 
230
        """Test the cancelation action."""
 
231
        self.dialog._on_cancel_clicked()
 
232
        self.assertEqual(ssl_dialog.USER_CANCELATION, self.return_code)
 
233
 
 
234
    def test_on_connect_clicked(self):
 
235
        """Test the connect action."""
 
236
        self.dialog._on_connect_clicked()
 
237
        self.assertEqual(ssl_dialog.CERTIFICATE_ACCEPTED, self.return_code)
 
238
 
 
239
    def test_set_buttons(self):
 
240
        """Test that the buttons are correctly set up."""
 
241
        self.assertEqual(CANCEL_BUTTON,
 
242
                unicode(self.dialog.ui.cancel_button.text()))
 
243
        self.assertEqual(SSL_CONNECT_BUTTON,
 
244
                unicode(self.dialog.ui.connect_button.text()))
 
245
        self.assertEqual(SSL_HELP_BUTTON,
 
246
                unicode(self.dialog.ui.help_button.text()))
 
247
        self.assertIn(('connect', self.dialog._on_connect_clicked),
 
248
                      self.dialog.ui.connect_button.clicked.called)
 
249
        self.assertIn(('connect', self.dialog._on_cancel_clicked),
 
250
                      self.dialog.ui.cancel_button.clicked.called)
 
251
 
 
252
    def test_set_expander(self):
 
253
        """Test that the expander is correctly set."""
 
254
        self.assertEqual(SSL_CERT_DETAILS, self.expander.text)
 
255
        self.assertIn('addWidget', self.expander.called[0])
 
256
        # pylint: disable=E1101,
 
257
        self.assertIn(('insertWidget', 2, self.expander),
 
258
                self.dialog.ui.called['expander_layout'])
 
259
        # pylint: enable=E1101,
 
260
 
 
261
    def test_set_icon(self):
 
262
        """Test that the icon is correctly set."""
 
263
        self.assertIn(('fromTheme', 'gtk-dialog-warning'),
 
264
                      self.icon.called)
 
265
        self.assertIn(('pixmap', 48, 48),
 
266
                      self.icon.called)