source: main/waeup.ikoba/trunk/src/waeup/ikoba/tests/test_interfaces.py @ 15796

Last change on this file since 15796 was 12630, checked in by Henrik Bettermann, 10 years ago

Fix test.

Email addresses are now TextLines?.

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1# -*- coding: utf-8 -*-
2## $Id: test_interfaces.py 12630 2015-02-24 06:01:08Z 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.
21import grok
22import unittest
23from waeup.ikoba.interfaces import (
24    DuplicationError, RoleSource, check_email, check_id, check_uuid)
25from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase
26
27class RegExTests(unittest.TestCase):
28    # Let's test regular expressions here
29
30    def check(self, expression, string):
31        if expression.match(string) is not None:
32            return True
33        return False
34
35    def test_check_email_allowed(self):
36        # the RE check_email does moderate format checks, allowing
37        # also strange looking but valid email addresses
38        self.assertTrue(check_email('manfred@domain.com') is not None)
39        self.assertTrue(check_email('bob@localhost') is not None)
40        self.assertTrue(check_email('bob@h5.waeup.org') is not None)
41        self.assertTrue(check_email('bob@idn_with_ümlaut.com') is not None)
42        # also unicodes with umlauts work
43        self.assertTrue(check_email(u'bob@idn_with_ümlaut.com') is not None)
44        self.assertTrue(
45            check_email('bob_the-complex+mbox@my.place') is not None)
46        return
47
48    def test_check_email_forbidden(self):
49        # the RE check_email does moderate format checks, but doesn't
50        # accept everything.
51        # no domain at all
52        self.assertTrue(check_email('bob') is None)
53        self.assertTrue(check_email('bob@') is None)
54        # each domain part must contain at least something
55        self.assertTrue(check_email('bob@h5.waeup.') is None)
56        # whitespaces in addresses
57        self.assertTrue(check_email('spacey @domain.com') is None)
58        self.assertTrue(check_email('tabby\t@domain.com') is None)
59        # two dots next to each other in domain (empty domain part)
60        self.assertTrue(check_email('bob@some..domain') is None)
61        # commas are forbidden
62        self.assertTrue(check_email('bob@foo,alice@bar') is None)
63        return
64
65    def test_check_id(self):
66        self.assertTrue(check_id('abcABC') is not None)
67        # No spaces
68        self.assertTrue(check_id('abc ABC') is None)
69        # More than 10 characters are not allowed
70        self.assertTrue(check_id('abcdefghijk') is None)
71        # Only underscores and hyphens are allowed
72        self.assertTrue(check_id('a+b') is None)
73        self.assertTrue(check_id('a/b') is None)
74        self.assertTrue(check_id('a-b') is not None)
75        self.assertTrue(check_id('a_b') is not None)
76        # 32 charachters are allowed
77        self.assertTrue(
78            check_uuid('dfceb5c587364faf95e17b3cbaf98d5f') is not None)
79        return
80
81
82class DuplicationErrorTests(unittest.TestCase):
83
84    def test_is_exception(self):
85        exc = DuplicationError('Some message')
86        self.assertEqual(exc.msg, 'Some message')
87        self.assertEqual(exc.entries, [])
88
89    def test_as_string(self):
90        exc = DuplicationError('Some message')
91        self.assertEqual(exc.__str__(), "'Some message'")
92
93
94class RoleSourceTests(FunctionalTestCase):
95
96    layer = FunctionalLayer
97
98    def setUp(self):
99        super(RoleSourceTests, self).setUp()
100        # Register a role
101        class SomeRole(grok.Role):
102            grok.name('waeup.testrole')
103            grok.title('RoleWith.DotName')
104            grok.permissions('waeup.Public')
105        # Register a role not visible to waeup portal as its name does
106        # not start with 'waeup.'
107        class NonIkobaRole(grok.Role):
108            grok.name('nonwaeup.testrole')
109            grok.title('Role not suitable for waeup')
110            grok.permissions('waeup.Public')
111        grok.testing.grok_component('SomeRole', SomeRole)
112        grok.testing.grok_component('NonIkobaRole', NonIkobaRole)
113        return
114
115    def test_getValues(self):
116        source = RoleSource()
117        result = source.factory.getValues()
118        self.assertTrue(u'waeup.testrole' in result)
119        self.assertTrue(u'waeup.PortalManager' in result)
120        self.assertTrue(u'nonwaeup.testrole' not in result)
121
122    def test_getTitle(self):
123        source = RoleSource()
124        result = source.factory.getTitle(u'waeup.PortalManager')
125        self.assertEqual(result, 'Portal Manager')
126
127    def test_getTitleWithDot(self):
128        # If there is a dot in the role title, we get only the last part
129        source = RoleSource()
130        result = source.factory.getTitle(u'waeup.testrole')
131        self.assertEqual(result, 'DotName')
Note: See TracBrowser for help on using the repository browser.