1 | # -*- coding: utf-8 -*- |
---|
2 | ## $Id: test_interfaces.py 11949 2014-11-13 14:40:27Z henrik $ |
---|
3 | ## |
---|
4 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
5 | ## This program is free software; you can redistribute it and/or modify |
---|
6 | ## it under the terms of the GNU General Public License as published by |
---|
7 | ## the Free Software Foundation; either version 2 of the License, or |
---|
8 | ## (at your option) any later version. |
---|
9 | ## |
---|
10 | ## This program is distributed in the hope that it will be useful, |
---|
11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | ## GNU General Public License for more details. |
---|
14 | ## |
---|
15 | ## You should have received a copy of the GNU General Public License |
---|
16 | ## along with this program; if not, write to the Free Software |
---|
17 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | ## |
---|
19 | |
---|
20 | # Tests for interfaces and included components. |
---|
21 | import grok |
---|
22 | import unittest |
---|
23 | from waeup.ikoba.interfaces import DuplicationError, RoleSource, check_email |
---|
24 | from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase |
---|
25 | |
---|
26 | class RegExTests(unittest.TestCase): |
---|
27 | # Let's test regular expressions here |
---|
28 | |
---|
29 | def check(self, expression, string): |
---|
30 | if expression.match(string) is not None: |
---|
31 | return True |
---|
32 | return False |
---|
33 | |
---|
34 | def test_check_email_allowed(self): |
---|
35 | # the RE check_email does moderate format checks, allowing |
---|
36 | # also strange looking but valid email addresses |
---|
37 | self.assertTrue(check_email('manfred@domain.com') is not None) |
---|
38 | self.assertTrue(check_email('bob@localhost') is not None) |
---|
39 | self.assertTrue(check_email('bob@h5.waeup.org') is not None) |
---|
40 | self.assertTrue(check_email('bob@idn_with_ümlaut.com') is not None) |
---|
41 | # also unicodes with umlauts work |
---|
42 | self.assertTrue(check_email(u'bob@idn_with_ümlaut.com') is not None) |
---|
43 | self.assertTrue( |
---|
44 | check_email('bob_the-complex+mbox@my.place') is not None) |
---|
45 | return |
---|
46 | |
---|
47 | def test_check_email_forbidden(self): |
---|
48 | # the RE check_email does moderate format checks, but doesn't |
---|
49 | # accept everything. |
---|
50 | # no domain at all |
---|
51 | self.assertTrue(check_email('bob') is None) |
---|
52 | self.assertTrue(check_email('bob@') is None) |
---|
53 | # each domain part must contain at least something |
---|
54 | self.assertTrue(check_email('bob@h5.waeup.') is None) |
---|
55 | # whitespaces in addresses |
---|
56 | self.assertTrue(check_email('spacey @domain.com') is None) |
---|
57 | self.assertTrue(check_email('tabby\t@domain.com') is None) |
---|
58 | # two dots next to each other in domain (empty domain part) |
---|
59 | self.assertTrue(check_email('bob@some..domain') is None) |
---|
60 | # commas are forbidden |
---|
61 | self.assertTrue(check_email('bob@foo,alice@bar') is None) |
---|
62 | return |
---|
63 | |
---|
64 | class DuplicationErrorTests(unittest.TestCase): |
---|
65 | |
---|
66 | def test_is_exception(self): |
---|
67 | exc = DuplicationError('Some message') |
---|
68 | self.assertEqual(exc.msg, 'Some message') |
---|
69 | self.assertEqual(exc.entries, []) |
---|
70 | |
---|
71 | def test_as_string(self): |
---|
72 | exc = DuplicationError('Some message') |
---|
73 | self.assertEqual(exc.__str__(), "'Some message'") |
---|
74 | |
---|
75 | |
---|
76 | class RoleSourceTests(FunctionalTestCase): |
---|
77 | |
---|
78 | layer = FunctionalLayer |
---|
79 | |
---|
80 | def setUp(self): |
---|
81 | super(RoleSourceTests, self).setUp() |
---|
82 | # Register a role |
---|
83 | class SomeRole(grok.Role): |
---|
84 | grok.name('waeup.testrole') |
---|
85 | grok.title('RoleWith.DotName') |
---|
86 | grok.permissions('waeup.Public') |
---|
87 | # Register a role not visible to waeup portal as its name does |
---|
88 | # not start with 'waeup.' |
---|
89 | class NonIkobaRole(grok.Role): |
---|
90 | grok.name('nonwaeup.testrole') |
---|
91 | grok.title('Role not suitable for waeup') |
---|
92 | grok.permissions('waeup.Public') |
---|
93 | grok.testing.grok_component('SomeRole', SomeRole) |
---|
94 | grok.testing.grok_component('NonIkobaRole', NonIkobaRole) |
---|
95 | return |
---|
96 | |
---|
97 | def test_getValues(self): |
---|
98 | source = RoleSource() |
---|
99 | result = source.factory.getValues() |
---|
100 | self.assertTrue(u'waeup.testrole' in result) |
---|
101 | self.assertTrue(u'waeup.PortalManager' in result) |
---|
102 | self.assertTrue(u'nonwaeup.testrole' not in result) |
---|
103 | |
---|
104 | def test_getTitle(self): |
---|
105 | source = RoleSource() |
---|
106 | result = source.factory.getTitle(u'waeup.PortalManager') |
---|
107 | self.assertEqual(result, 'Portal Manager') |
---|
108 | |
---|
109 | def test_getTitleWithDot(self): |
---|
110 | # If there is a dot in the role title, we get only the last part |
---|
111 | source = RoleSource() |
---|
112 | result = source.factory.getTitle(u'waeup.testrole') |
---|
113 | self.assertEqual(result, 'DotName') |
---|