15
15
# You should have received a copy of the GNU General Public License along
16
16
# with this program. If not, see <http://www.gnu.org/licenses/>.
18
# In addition, as a special exception, the copyright holders give
19
# permission to link the code of portions of this program with the
20
# OpenSSL library under certain conditions as described in each
21
# individual source file, and distribute linked combinations
23
# You must obey the GNU General Public License in all respects
24
# for all of the code used other than OpenSSL. If you modify
25
# file(s) with this exception, you may extend this exception to your
26
# version of the file(s), but you are not obligated to do so. If you
27
# do not wish to do so, delete this exception statement from your
28
# version. If you delete this exception statement from all source
29
# files in the program, then also delete it here.
30
17
"""Test the common functions."""
32
19
from PyQt4 import QtGui
33
20
from twisted.internet import defer
34
21
from twisted.trial.unittest import TestCase
36
from ubuntu_sso.qt import (
37
build_general_error_message,
39
GENERIC_BACKEND_ERROR,
41
from ubuntu_sso.qt.common import (
23
from ubuntu_sso.qt.common import (check_as_invalid,
44
25
password_assistance,
45
26
password_check_match,
255
234
line_edit = QtGui.QLineEdit()
256
235
check_as_invalid(line_edit)
257
236
self.assertTrue(line_edit.property("formError").toBool())
260
class ElidedTextTestCase(TestCase):
261
"""The test case for the maybe_elide_text function."""
265
@defer.inlineCallbacks
268
yield super(ElidedTextTestCase, self).setUp()
269
self.ui = QtGui.QLabel()
271
def test_text_not_elided_if_too_short(self):
272
"""If text is shorter than max_width, do not elide."""
273
text = build_string_for_pixels(self.ui, self.max_width - 10)
275
maybe_elide_text(self.ui, text, self.max_width)
277
self.assertEqual(self.ui.toolTip(), '')
278
self.assertEqual(self.ui.text(), text)
279
self.assertNotIn(u'\u2026', self.ui.text())
281
def test_text_not_elided_if_equals_max_width(self):
282
"""If text is equal than max_width, do not elide."""
283
text = build_string_for_pixels(self.ui, self.max_width)
285
maybe_elide_text(self.ui, text, self.max_width)
287
self.assertEqual(self.ui.toolTip(), '')
288
self.assertEqual(self.ui.text(), text)
289
self.assertNotIn(u'\u2026', self.ui.text())
291
def test_text_elided_if_bigger_than_max_width(self):
292
"""If text is equal than max_width, do not elide."""
293
text = build_string_for_pixels(self.ui, self.max_width * 2)
295
maybe_elide_text(self.ui, text, self.max_width)
297
self.assertEqual(self.ui.toolTip(), text)
298
expected = unicode(self.ui.text())
299
self.assertTrue(expected.endswith(u'\u2026'))
300
self.assertTrue(text.startswith(expected[:-1]))
303
class BuildGeneralErrorMessageTestCase(TestCase):
304
"""Test passwords conditions."""
306
def test_with_message(self):
307
"""Test build_general_error_message with 'message' key."""
308
error = "error message"
309
err_dict = {'message': error}
311
result = build_general_error_message(err_dict)
313
self.assertEqual(result, error)
315
def test_with_all(self):
316
"""Test build_general_error_message with 'all' key."""
317
error = "error message"
318
err_dict = {'__all__': error}
320
result = build_general_error_message(err_dict)
322
self.assertEqual(result, error)
324
def test_with_message_and_all(self):
325
"""Test build_general_error_message with 'all' and 'message' key."""
326
error = "error message"
327
error2 = "error message2"
328
err_dict = {'__all__': error, 'message': error2}
330
result = build_general_error_message(err_dict)
332
expected = '\n'.join((error, error2))
333
self.assertEqual(result, expected)
335
def test_with_all_and_error_message(self):
336
"""Test for 'all' and 'error_message' key."""
337
error = "error message"
338
error2 = "error message2"
339
err_dict = {'__all__': error, 'error_message': error2}
340
result = build_general_error_message(err_dict)
341
expected = '\n'.join((error, error2))
342
self.assertEqual(result, expected)
344
def test_with_random_keys(self):
345
"""Test build_general_error_message with random keys."""
346
error = "error message"
347
error2 = "error message2"
348
err_dict = {'my_bad': error, 'odd_error': error2}
350
result = build_general_error_message(err_dict)
352
expected = '\n'.join(
353
[('%s: %s' % (k, v)) for k, v in err_dict.iteritems()])
354
self.assertEqual(result, expected)
356
def test_with_random_keys_with_errtype(self):
357
"""Test build_general_error_message with random keys and errtype."""
358
error = "error message"
359
error2 = "error message2"
360
err_dict = {'my_bad': error, 'odd_error': error2, 'errtype': 'Danger'}
362
result = build_general_error_message(err_dict)
364
expected = '\n'.join(
365
[('%s: %s' % (k, v)) \
366
for k, v in {'my_bad': error, 'odd_error': error2}.iteritems()])
367
self.assertEqual(result, expected)
369
def test_with_not_dict(self):
370
"""Test build_general_error_message with argument not dict."""
371
error = "error message"
372
err_dict = Exception(error)
374
result = build_general_error_message(err_dict)
376
expected = GENERIC_BACKEND_ERROR
377
self.assertEqual(result, expected)