Back to index

plone3  3.1.7
test_utils.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE.
00013 #
00014 ##############################################################################
00015 
00016 import unittest
00017 
00018 from Products.PluggableAuthService.utils import createViewName
00019 from Products.PluggableAuthService.utils import createKeywords
00020 
00021 
00022 class UtilityTests(unittest.TestCase):
00023 
00024     def test_createViewName(self):
00025         self.assertEqual(createViewName('foo', 'bar'), 'foo-bar')
00026 
00027     def test_createViewName_no_user_handle(self):
00028         self.assertEqual(createViewName('foo', None), 'foo')
00029 
00030     def test_createViewName_latin1_umlaut_in_method(self):
00031         self.assertEqual(createViewName('f\366o'), 'f\366o')
00032 
00033     def test_createViewName_utf8_umlaut_in_method(self):
00034         self.assertEqual(createViewName('f\303\266o'), 'f\303\266o')
00035 
00036     def test_createViewName_unicode_umlaut_in_method(self):
00037         self.assertEqual(createViewName(u'f\366o'), 'f\303\266o')
00038 
00039     def test_createViewName_latin1_umlaut_in_handle(self):
00040         self.assertEqual(createViewName('foo', 'b\344r'), 'foo-b\344r')
00041 
00042     def test_createViewName_utf8_umlaut_in_handle(self):
00043         self.assertEqual(createViewName('foo', 'b\303\244r'), 'foo-b\303\244r')
00044 
00045     def test_createViewName_unicode_umlaut_in_handle(self):
00046         self.assertEqual(createViewName('foo', u'b\344r'), 'foo-b\303\244r')
00047 
00048     def test_createKeywords(self):
00049         _ITEMS = (('foo', 'bar'),)
00050         hashed = _createHashedValue(_ITEMS)
00051         self.assertEqual(createKeywords(foo='bar'),
00052                          {'keywords': hashed})
00053 
00054     def test_createKeywords_multiple(self):
00055         _ITEMS = (('foo', 'bar'), ('baz', 'peng'))
00056         hashed = _createHashedValue(_ITEMS)
00057         self.assertEqual(createKeywords(foo='bar', baz='peng'),
00058                          {'keywords': hashed})
00059 
00060     def test_createKeywords_latin1_umlaut(self):
00061         _ITEMS = (('foo', 'bar'), ('baz', 'M\344dchen'))
00062         hashed = _createHashedValue(_ITEMS)
00063         self.assertEqual(createKeywords(foo='bar', baz='M\344dchen'),
00064                          {'keywords': hashed})
00065 
00066     def test_createKeywords_utf8_umlaut(self):
00067         _ITEMS = (('foo', 'bar'), ('baz', 'M\303\244dchen'))
00068         hashed = _createHashedValue(_ITEMS)
00069         self.assertEqual(createKeywords(foo='bar', baz='M\303\244dchen'),
00070                          {'keywords': hashed})
00071 
00072     def test_createKeywords_unicode_umlaut(self):
00073         _ITEMS = (('foo', 'bar'), ('baz', u'M\344dchen'))
00074         hashed = _createHashedValue(_ITEMS)
00075         self.assertEqual(createKeywords(foo='bar', baz=u'M\344dchen'),
00076                          {'keywords': hashed})
00077 
00078     def test_createKeywords_utf16_umlaut(self):
00079         _ITEMS = (('foo', 'bar'), ('baz', u'M\344dchen'.encode('utf-16')))
00080         hashed = _createHashedValue(_ITEMS)
00081         self.assertEqual(createKeywords(foo='bar',
00082                                         baz=u'M\344dchen'.encode('utf-16')),
00083                          {'keywords': hashed})
00084 
00085     def test_createKeywords_unicode_chinese(self):
00086         _ITEMS = (('foo', 'bar'), ('baz', u'\u03a4\u03b6'))
00087         hashed = _createHashedValue(_ITEMS)
00088         self.assertEqual(createKeywords(foo='bar', baz=u'\u03a4\u03b6'),
00089                 {'keywords': hashed})
00090 
00091 def _createHashedValue(items):
00092     import sha
00093     hasher = sha.new()
00094     items = list(items)
00095     items.sort()
00096     for k, v in items:
00097         if isinstance(k, unicode):
00098             k = k.encode('utf-8')
00099         hasher.update(k)
00100         if isinstance(v, unicode):
00101             v = v.encode('utf-8')
00102         hasher.update(v)
00103     return hasher.hexdigest()
00104 
00105 def test_suite():
00106     return unittest.TestSuite((
00107         unittest.makeSuite(UtilityTests),
00108     ))
00109 
00110 if __name__ == '__main__':
00111     unittest.main(defaultTest='test_suite')