File: test_updates.py

package info (click to toggle)
freeipa 4.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 367,240 kB
  • sloc: javascript: 562,763; python: 310,289; ansic: 49,809; sh: 7,176; makefile: 2,589; xml: 343; sed: 16
file content (260 lines) | stat: -rw-r--r-- 9,375 bytes parent folder | download | duplicates (4)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Authors:
#   Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2009  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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
"""
Test the `ipaserver/install/ldapupdate.py` module.
"""

from __future__ import absolute_import

import os

import pytest

from ipalib import api
from ipalib import errors
from ipalib.constants import FQDN
from ipaserver.install.ldapupdate import LDAPUpdate, BadSyntax
from ipapython import ipaldap
from ipaplatform.constants import constants as platformconstants
from ipapython.dn import DN

"""
The updater works through files only so this is just a thin-wrapper controlling
which file we test at any given point.

IMPORTANT NOTE: It is easy for these tests to get out of sync. Any changes
made to the update files may require changes to the test cases as well.
Some cases pull records from LDAP and do comparisons to ensure that updates
have occurred as expected.

The DM password needs to be set in ~/.ipa/.dmpw
"""


@pytest.mark.tier0
@pytest.mark.needs_ipaapi
class TestUpdate:
    """
    Test the LDAP updater.
    """

    @pytest.fixture(autouse=True)
    def update_setup(self, request):
        pwfile = api.env.dot_ipa + os.sep + ".dmpw"
        if os.path.isfile(pwfile):
            with open(pwfile, "r") as fp:
                self.dm_password = fp.read().rstrip()
        else:
            pytest.skip("No directory manager password")
        self.updater = LDAPUpdate()
        self.ld = ipaldap.LDAPClient.from_hostname_secure(FQDN)
        self.ld.simple_bind(bind_dn=ipaldap.DIRMAN_DN,
                            bind_password=self.dm_password)
        self.testdir = os.path.abspath(os.path.dirname(__file__))
        if not os.path.isfile(os.path.join(self.testdir,
                                                "0_reset.update")):
            pytest.skip("Unable to find test update files")

        self.container_dn = DN(self.updater._template_str('cn=test, cn=accounts, $SUFFIX'))
        self.user_dn = DN(self.updater._template_str('uid=tuser, cn=test, cn=accounts, $SUFFIX'))

        def fin():
            if self.ld:
                self.ld.unbind()
        request.addfinalizer(fin)

    def test_0_reset(self):
        """
        Reset the updater test data to a known initial state (test_0_reset)
        """
        try:
            modified = self.updater.update([os.path.join(self.testdir,
                                                         "0_reset.update")])
        except errors.NotFound:
            # Just means the entry doesn't exist yet
            modified = True

        assert modified

        with pytest.raises(errors.NotFound):
            self.ld.get_entries(
                self.container_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])

        with pytest.raises(errors.NotFound):
            self.ld.get_entries(
                self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])

    def test_1_add(self):
        """
        Test the updater with an add directive (test_1_add)
        """
        modified = self.updater.update([os.path.join(self.testdir,
                                                     "1_add.update")])

        assert modified

        entries = self.ld.get_entries(
            self.container_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]

        objectclasses = entry.get('objectclass')
        for item in ('top', 'nsContainer'):
            assert item in objectclasses

        assert entry.single_value['cn'] == 'test'

        entries = self.ld.get_entries(
            self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]

        objectclasses = entry.get('objectclass')
        for item in ('top', 'person', 'posixaccount', 'krbprincipalaux', 'inetuser'):
            assert item in objectclasses

        actual = entry.single_value['loginshell']
        assert actual == platformconstants.DEFAULT_ADMIN_SHELL
        assert entry.single_value['sn'] == 'User'
        assert entry.single_value['uid'] == 'tuser'
        assert entry.single_value['cn'] == 'Test User'


    def test_2_update(self):
        """
        Test the updater when adding an attribute to an existing entry (test_2_update)
        """
        modified = self.updater.update([os.path.join(self.testdir,
                                                     "2_update.update")])
        assert modified

        entries = self.ld.get_entries(
            self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]
        assert entry.single_value['gecos'] == 'Test User'

    def test_3_update(self):
        """
        Test the updater forcing an attribute to a given value (test_3_update)
        """
        modified = self.updater.update([os.path.join(self.testdir,
                                                     "3_update.update")])
        assert modified

        entries = self.ld.get_entries(
            self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]
        assert entry.single_value['gecos'] == 'Test User New'

    def test_4_update(self):
        """
        Test the updater adding a new value to a single-valued attribute (test_4_update)
        """
        modified = self.updater.update([os.path.join(self.testdir,
                                                     "4_update.update")])
        assert modified

        entries = self.ld.get_entries(
            self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]
        assert entry.single_value['gecos'] == 'Test User New2'

    def test_5_update(self):
        """
        Test the updater adding a new value to a multi-valued attribute (test_5_update)
        """
        modified = self.updater.update([os.path.join(self.testdir,
                                                     "5_update.update")])
        assert modified

        entries = self.ld.get_entries(
            self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]
        actual = sorted(entry.get('cn'))
        expected = sorted(['Test User', 'Test User New'])
        assert actual == expected

    def test_6_update(self):
        """
        Test the updater removing a value from a multi-valued attribute (test_6_update)
        """
        modified = self.updater.update([os.path.join(self.testdir,
                                                     "6_update.update")])
        assert modified

        entries = self.ld.get_entries(
            self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]
        assert sorted(entry.get('cn')) == sorted(['Test User'])

    def test_6_update_1(self):
        """
        Test the updater removing a non-existent value from a multi-valued attribute (test_6_update_1)
        """
        modified = self.updater.update([os.path.join(self.testdir,
                                                     "6_update.update")])
        assert not modified

        entries = self.ld.get_entries(
            self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])
        assert len(entries) == 1
        entry = entries[0]
        assert sorted(entry.get('cn')) == sorted(['Test User'])

    def test_7_cleanup(self):
        """
        Reset the test data to a known initial state (test_7_cleanup)
        """
        try:
            modified = self.updater.update([os.path.join(self.testdir,
                                                         "0_reset.update")])
        except errors.NotFound:
            # Just means the entry doesn't exist yet
            modified = True

        assert modified

        with pytest.raises(errors.NotFound):
            self.ld.get_entries(
                self.container_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])

        with pytest.raises(errors.NotFound):
            self.ld.get_entries(
                self.user_dn, self.ld.SCOPE_BASE, 'objectclass=*', ['*'])

    def test_8_badsyntax(self):
        """
        Test the updater with an unknown keyword (test_8_badsyntax)
        """
        with pytest.raises(BadSyntax):
            self.updater.update(
                [os.path.join(self.testdir, "8_badsyntax.update")])

    def test_9_badsyntax(self):
        """
        Test the updater with an incomplete line (test_9_badsyntax)
        """
        with pytest.raises(BadSyntax):
            self.updater.update(
                [os.path.join(self.testdir, "9_badsyntax.update")])