summaryrefslogtreecommitdiffstats
path: root/tests/test_ipalib/test_base.py
blob: 87e4c063d67e94e2b507cdef9fcf3b4e57f8be18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Authors:
#   Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Test the `ipalib.base` module.
"""

from tests.util import ClassChecker, raises
from ipalib.constants import NAME_REGEX, NAME_ERROR
from ipalib.constants import TYPE_ERROR, SET_ERROR, DEL_ERROR
from ipalib import base


class test_ReadOnly(ClassChecker):
    """
    Test the `ipalib.base.ReadOnly` class
    """
    _cls = base.ReadOnly

    def test_lock(self):
        """
        Test the `ipalib.base.ReadOnly.__lock__` method.
        """
        o = self.cls()
        assert o._ReadOnly__locked is False
        o.__lock__()
        assert o._ReadOnly__locked is True
        e = raises(AssertionError, o.__lock__) # Can only be locked once
        assert str(e) == '__lock__() can only be called once'
        assert o._ReadOnly__locked is True # This should still be True

    def test_islocked(self):
        """
        Test the `ipalib.base.ReadOnly.__islocked__` method.
        """
        o = self.cls()
        assert o.__islocked__() is False
        o.__lock__()
        assert o.__islocked__() is True

    def test_setattr(self):
        """
        Test the `ipalib.base.ReadOnly.__setattr__` method.
        """
        o = self.cls()
        o.attr1 = 'Hello, world!'
        assert o.attr1 == 'Hello, world!'
        o.__lock__()
        for name in ('attr1', 'attr2'):
            e = raises(AttributeError, setattr, o, name, 'whatever')
            assert str(e) == SET_ERROR % ('ReadOnly', name, 'whatever')
        assert o.attr1 == 'Hello, world!'

    def test_delattr(self):
        """
        Test the `ipalib.base.ReadOnly.__delattr__` method.
        """
        o = self.cls()
        o.attr1 = 'Hello, world!'
        o.attr2 = 'How are you?'
        assert o.attr1 == 'Hello, world!'
        assert o.attr2 == 'How are you?'
        del o.attr1
        assert not hasattr(o, 'attr1')
        o.__lock__()
        e = raises(AttributeError, delattr, o, 'attr2')
        assert str(e) == DEL_ERROR % ('ReadOnly', 'attr2')
        assert o.attr2 == 'How are you?'


def test_lock():
    """
    Test the `ipalib.base.lock` function
    """
    f = base.lock

    # Test with ReadOnly instance:
    o = base.ReadOnly()
    assert o.__islocked__() is False
    assert f(o) is o
    assert o.__islocked__() is True
    e = raises(AssertionError, f, o)
    assert str(e) == 'already locked: %r' % o

    # Test with another class implemented locking protocol:
    class Lockable(object):
        __locked = False
        def __lock__(self):
            self.__locked = True
        def __islocked__(self):
            return self.__locked
    o = Lockable()
    assert o.__islocked__() is False
    assert f(o) is o
    assert o.__islocked__() is True
    e = raises(AssertionError, f, o)
    assert str(e) == 'already locked: %r' % o

    # Test with a class incorrectly implementing the locking protocol:
    class Broken(object):
        def __lock__(self):
            pass
        def __islocked__(self):
            return False
    o = Broken()
    e = raises(AssertionError, f, o)
    assert str(e) == 'failed to lock: %r' % o


def test_islocked():
    """
    Test the `ipalib.base.islocked` function.
    """
    f = base.islocked

    # Test with ReadOnly instance:
    o = base.ReadOnly()
    assert f(o) is False
    o.__lock__()
    assert f(o) is True

    # Test with another class implemented locking protocol:
    class Lockable(object):
        __locked = False
        def __lock__(self):
            self.__locked = True
        def __islocked__(self):
            return self.__locked
    o = Lockable()
    assert f(o) is False
    o.__lock__()
    assert f(o) is True

    # Test with a class incorrectly implementing the locking protocol:
    class Broken(object):
        __lock__ = False
        def __islocked__(self):
            return False
    o = Broken()
    e = raises(AssertionError, f, o)
    assert str(e) == 'no __lock__() method: %r' % o


def test_check_name():
    """
    Test the `ipalib.base.check_name` function.
    """
    f = base.check_name
    okay = [
        'user_add',
        'stuff2junk',
        'sixty9',
    ]
    nope = [
        '_user_add',
        '__user_add',
        'user_add_',
        'user_add__',
        '_user_add_',
        '__user_add__',
        '60nine',
    ]
    for name in okay:
        assert name is f(name)
        e = raises(TypeError, f, unicode(name))
        assert str(e) == TYPE_ERROR % ('name', str, unicode(name), unicode)
    for name in nope:
        e = raises(ValueError, f, name)
        assert str(e) == NAME_ERROR % (NAME_REGEX, name)
    for name in okay:
        e = raises(ValueError, f, name.upper())
        assert str(e) == NAME_ERROR % (NAME_REGEX, name.upper())