1
# -*- coding: utf-8 -*-
3
# Copyright 2012 Canonical Ltd.
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.
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.
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/>.
17
"""Test the ssl dialog."""
19
from PyQt4.QtGui import QStyle
20
from twisted.internet import defer
21
from twisted.trial.unittest import TestCase
23
from ubuntu_sso import USER_CANCELLATION, USER_SUCCESS
24
from ubuntu_sso.qt import ssl_dialog
25
from ubuntu_sso.utils.ui import (
38
SSL_REMEMBER_DECISION,
43
# pylint: disable=W0212, C0103,
46
class SSLDialogTestCase(TestCase):
47
"""Test the ssl dialog."""
49
@defer.inlineCallbacks
52
yield super(SSLDialogTestCase, self).setUp()
53
self.domain = 'test-domain'
54
self.details = 'SSL details'
57
self.dialog = ssl_dialog.SSLDialog(self.appname,
60
self.return_code = None
63
"""Fake done for the dialog."""
64
self.return_code = code
66
self.patch(self.dialog, 'done', fake_done)
68
def test_init_none_domain(self):
69
"""Test the init method when the domain is none."""
70
dialog = ssl_dialog.SSLDialog(self.appname, domain=None,
72
self.assertEqual(self.details, dialog.details)
73
self.assertEqual('', dialog.domain)
75
def test_init_none_details(self):
76
"""Test the init method when the details are none."""
77
dialog = ssl_dialog.SSLDialog(self.appname, domain=self.domain,
79
self.assertEqual('', dialog.details)
80
self.assertEqual(self.domain, dialog.domain)
82
def test_set_labels(self):
83
"""Test that the labels contain the correct info."""
84
self.assertEqual(SSL_HEADER,
85
unicode(self.dialog.ui.title_label.text()))
86
explanation = SSL_EXPLANATION % dict(domain=self.domain)
87
intro = ssl_dialog.REASONS_TEMPLATE % dict(explanation=explanation,
88
first_reason=SSL_FIRST_REASON,
89
second_reason=SSL_SECOND_REASON,
90
third_reason=SSL_THIRD_REASON)
91
self.assertEqual(intro, unicode(self.dialog.ui.intro_label.text()))
92
self.assertEqual(SSL_NOT_SURE % dict(app_name=self.appname),
93
unicode(self.dialog.ui.not_sure_label.text()))
94
self.assertEqual(SSL_REMEMBER_DECISION,
95
unicode(self.dialog.ui.remember_checkbox.text()))
97
def test_on_cancel_clicked(self):
98
"""Test the cancelation action."""
99
self.dialog._on_cancel_clicked()
100
self.assertEqual(USER_CANCELLATION, self.return_code)
102
def test_on_connect_clicked(self):
103
"""Test the connect action."""
104
self.dialog._on_connect_clicked()
105
self.assertEqual(USER_SUCCESS, self.return_code)
107
def test_set_buttons(self):
108
"""Test that the buttons are correctly set up."""
110
# assert that the buttons have been correctly connected
113
def fake_on_connect_clicked(event):
114
"""Fake the on connect clicked method."""
115
called.append('_on_connect_clicked')
117
self.patch(ssl_dialog.SSLDialog, '_on_connect_clicked',
118
fake_on_connect_clicked)
120
def fake_on_cancel_clicked(event):
121
"""Fake the on cancle clicked."""
122
called.append('_on_cancel_clicked')
124
self.patch(ssl_dialog.SSLDialog, '_on_cancel_clicked',
125
fake_on_cancel_clicked)
127
dialog = ssl_dialog.SSLDialog(self.appname, domain=None,
128
details=self.details)
130
dialog.ui.connect_button.clicked.emit(True)
131
self.assertIn('_on_connect_clicked', called)
133
dialog.ui.cancel_button.clicked.emit(True)
134
self.assertIn('_on_cancel_clicked', called)
136
self.assertEqual(CANCEL_BUTTON,
137
unicode(dialog.ui.cancel_button.text()))
138
self.assertEqual(SSL_CONNECT_BUTTON,
139
unicode(dialog.ui.connect_button.text()))
140
self.assertEqual(SSL_HELP_BUTTON,
141
unicode(dialog.ui.help_button.text()))
143
def test_set_expander(self):
144
"""Test that the expander is correctly set."""
145
self.assertEqual(SSL_CERT_DETAILS, self.dialog.expander.text())
146
self.assertNotEqual(None, self.dialog.expander.content)
147
self.assertEqual(2, self.dialog.ui.expander_layout.indexOf(
148
self.dialog.expander))
150
def test_set_icon(self):
151
"""Test that the icon is correctly set."""
152
self.assertNotEqual(None, self.dialog.ui.logo_label.pixmap())
153
icon = self.dialog.style().standardIcon(QStyle.SP_MessageBoxWarning)
154
self.assertEqual(icon.pixmap(48, 48).toImage(),
155
self.dialog.ui.logo_label.pixmap().toImage())
156
self.assertEqual('', self.dialog.ui.logo_label.text())
159
class FakeArgumentParser(object):
160
"""Fake args parse."""
163
"""Create an instance."""
166
def __call__(self, description=''):
167
"""Instance callable."""
168
self.called.append(('__init__', description))
171
# pylint: disable=W0622
172
def add_argument(self, name, required=False, help=''):
173
"""Add an argument."""
174
self.called.append(('add_argument', name, required, help))
175
# pylint: enable=W0622
177
def parse_args(self):
178
"""Parse the args."""
179
self.called.append(('parse_args',))
183
class ParseArgTestCase(TestCase):
184
"""Test the parse of the args."""
186
def test_parse_args(self):
188
argparse = FakeArgumentParser()
189
self.patch(ssl_dialog.argparse, 'ArgumentParser', argparse)
190
args = ssl_dialog.parse_args()
191
self.assertEqual(argparse, args)
192
self.assertIn(('__init__', SSL_DESCRIPTION), argparse.called)
193
self.assertIn(('add_argument', '--domain', True, SSL_DOMAIN_HELP),
195
self.assertIn(('add_argument', '--details', True, SSL_DETAILS_HELP),
197
self.assertIn(('add_argument', '--appname', True, SSL_APPNAME_HELP),
199
self.assertIn(('parse_args',), argparse.called)