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

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# -*- coding: utf-8 -*-
#
# Copyright 2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Test the ssl dialog."""

from twisted.internet import defer
from twisted.trial.unittest import TestCase

from ubuntu_sso.qt import ssl_dialog
from ubuntu_sso.utils.ui import (
    CANCEL_BUTTON,
    SSL_HEADER,
    SSL_EXPLANATION,
    SSL_FIRST_REASON,
    SSL_SECOND_REASON,
    SSL_THIRD_REASON,
    SSL_CERT_DETAILS,
    SSL_NOT_SURE,
    SSL_REMEBER_DECITION,
    SSL_HELP_BUTTON,
    SSL_CONNECT_BUTTON,
)

# pylint: disable=W0212, C0103,


class FakeSignal(object):
    """A fake qt signal."""

    def __init__(self):
        """Create a new instance."""
        self.called = []

    def connect(self, cb):
        """Connect signals."""
        self.called.append(('connect', cb))


class FakeLayout(object):
    """A fake layout."""

    def __init__(self, object_name, called):
        """Create a new instance."""
        self.object_name = object_name
        self.called = called

    def insertWidget(self, index, widget):
        """Insert a widget in the layout."""
        if not self.object_name in self.called:
            self.called[self.object_name] = []
        self.called[self.object_name].append(('insertWidget', index, widget))


class FakeWidget(object):
    """A fake widget that contains text."""

    def __init__(self, object_name, called, text=''):
        """Create a new instance."""
        self.object_name = object_name
        self.called = called
        self.internal_text = text
        self.clicked = FakeSignal()

    # pylint: disable=C0103
    def setText(self, text):
        """Set the text of the widget."""
        self.internal_text = text
        if not self.object_name in self.called:
            self.called[self.object_name] = [('setText', text)]
        else:
            self.called[self.object_name].append(('setText', text))

    def text(self):
        """Return the text."""
        if not self.object_name in self.called:
            self.called[self.object_name] = [('text',)]
        else:
            self.called[self.object_name].append(('text',))
        return self.internal_text

    def setPixmap(self, pixmap):
        """Set a pixmap."""
        if not self.object_name in self.called:
            self.called[self.object_name] = [('setPixmap', pixmap)]
        else:
            self.called[self.object_name].append(('setPixmap', pixmap))

    def setVisible(self, visible):
        """Set the ui element visible."""
        if not self.object_name in self.called:
            self.called[self.object_name] = [('setVisible', visible)]
        else:
            self.called[self.object_name].append(('setVisible', visible))

    # pylint: enable=C0103


class FakeQIcon(object):
    """QIcon class."""

    def __init__(self):
        """Create a new instance."""
        self.called = []

    # pylint: disable=C0103
    def fromTheme(self, icon_id):
        """Return the icon with the given id."""
        self.called.append(('fromTheme', icon_id))
        return self
    # pylint: enable=C0103

    # pylint: disable=C0103
    def pixmap(self, h, w):
        """Get the pixmap with the given size."""
        self.called.append(('pixmap', h, w))
        return self
    # pylint: disable=C0103


class FakeExpander(object):
    """A fake expander widget."""

    def __init__(self):
        """Create a new instance."""
        self.called = []
        self.text = None

    def __call__(self, text):
        """Instance callable."""
        self.text = text
        return self

    def addWidget(self, widget):
        """Add a widget to the expander."""
        self.called.append(('addWidget', widget))


class FakeUISSLDialog(object):
    """The fake .ui for the creds dialog."""

    ui_labels = ('title_label', 'intro_label', 'not_sure_label',
                  'remember_checkbox', 'logo_label')
    ui_buttons = ('help_button', 'cancel_button', 'connect_button')
    ui_layouts = ('expander_layout',)

    def __init__(self):
        """Create a new instance."""
        self.called = {}
        for name in self.ui_labels + self.ui_buttons:
            setattr(self, name, FakeWidget(name, self.called))
        for name in self.ui_layouts:
            setattr(self, name, FakeLayout(name, self.called))

    # pylint: disable=C0103
    def setupUi(self, dialog):
        """Set the ui."""
        self.called['FakeUIProxyCredsDialog'] = ('setupUi', dialog)
    # pylint: enable=C0103


class SSLDialogTest(TestCase):
    """Test the ssl dialog."""

    @defer.inlineCallbacks
    def setUp(self):
        """Set the tests."""
        yield super(SSLDialogTest, self).setUp()
        self.domain = 'test-domain'
        self.details = 'SSL details'

        self.patch(ssl_dialog, 'Ui_SSLDialog',
                   FakeUISSLDialog)
        self.icon = FakeQIcon()
        self.patch(ssl_dialog, 'QIcon', self.icon)

        self.expander = FakeExpander()
        self.patch(ssl_dialog, 'QExpander', self.expander)

        self.dialog = ssl_dialog.SSLDialog(domain=self.domain,
                                           details=self.details)
        self.return_code = None

        def fake_done(code):
            """Fake done for the dialog."""
            self.return_code = code

        self.patch(self.dialog, 'done', fake_done)

    def test_init_none_domain(self):
        """Test the init method when the domain is none."""
        dialog = ssl_dialog.SSLDialog(domain=None, details=self.details)
        self.assertEqual(self.details, dialog.details)
        self.assertEqual('', dialog.domain)

    def test_init_none_details(self):
        """Test the init method when the details are none."""
        dialog = ssl_dialog.SSLDialog(domain=self.domain, details=None)
        self.assertEqual('', dialog.details)
        self.assertEqual(self.domain, dialog.domain)

    def test_set_labels(self):
        """Test that the labels contain the correct info."""
        self.assertEqual(SSL_HEADER,
                         unicode(self.dialog.ui.title_label.text()))
        explanation = SSL_EXPLANATION % dict(domain=self.domain)
        intro = ssl_dialog.REASONS_TEMPLATE % dict(explanation=explanation,
                                              first_reason=SSL_FIRST_REASON,
                                              second_reason=SSL_SECOND_REASON,
                                              third_reason=SSL_THIRD_REASON)
        self.assertEqual(intro, unicode(self.dialog.ui.intro_label.text()))
        self.assertEqual(SSL_NOT_SURE,
                unicode(self.dialog.ui.not_sure_label.text()))
        self.assertEqual(SSL_REMEBER_DECITION,
                unicode(self.dialog.ui.remember_checkbox.text()))

    def test_on_cancel_clicked(self):
        """Test the cancelation action."""
        self.dialog._on_cancel_clicked()
        self.assertEqual(ssl_dialog.USER_CANCELATION, self.return_code)

    def test_on_connect_clicked(self):
        """Test the connect action."""
        self.dialog._on_connect_clicked()
        self.assertEqual(ssl_dialog.CERTIFICATE_ACCEPTED, self.return_code)

    def test_set_buttons(self):
        """Test that the buttons are correctly set up."""
        self.assertEqual(CANCEL_BUTTON,
                unicode(self.dialog.ui.cancel_button.text()))
        self.assertEqual(SSL_CONNECT_BUTTON,
                unicode(self.dialog.ui.connect_button.text()))
        self.assertEqual(SSL_HELP_BUTTON,
                unicode(self.dialog.ui.help_button.text()))
        self.assertIn(('connect', self.dialog._on_connect_clicked),
                      self.dialog.ui.connect_button.clicked.called)
        self.assertIn(('connect', self.dialog._on_cancel_clicked),
                      self.dialog.ui.cancel_button.clicked.called)

    def test_set_expander(self):
        """Test that the expander is correctly set."""
        self.assertEqual(SSL_CERT_DETAILS, self.expander.text)
        self.assertIn('addWidget', self.expander.called[0])
        # pylint: disable=E1101,
        self.assertIn(('insertWidget', 2, self.expander),
                self.dialog.ui.called['expander_layout'])
        # pylint: enable=E1101,

    def test_set_icon(self):
        """Test that the icon is correctly set."""
        self.assertIn(('fromTheme', 'gtk-dialog-warning'),
                      self.icon.called)
        self.assertIn(('pixmap', 48, 48),
                      self.icon.called)