1# -*- coding: utf-8 -*-
2#
3# Common functionality for all password change tests
4#
5# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21import samba.tests
22
23
24class PasswordCommon:
25
26    @staticmethod
27    def allow_password_changes(testcase, samdb):
28        """Updates the DC to allow password changes during the current test"""
29
30        # Get the old "dSHeuristics" if it was set
31        dsheuristics = samdb.get_dsheuristics()
32
33        # Reset the "dSHeuristics" as they were before
34        testcase.addCleanup(samdb.set_dsheuristics, dsheuristics)
35
36        # Set the "dSHeuristics" to activate the correct "userPassword" behaviour
37        samdb.set_dsheuristics("000000001")
38
39        # Get the old "minPwdAge"
40        minPwdAge = samdb.get_minPwdAge()
41
42        # Reset the "minPwdAge" as it was before
43        testcase.addCleanup(samdb.set_minPwdAge, minPwdAge)
44
45        # Set it temporarily to "0"
46        samdb.set_minPwdAge("0")
47
48
49class PasswordTestCase(samba.tests.TestCase):
50
51    # this requires that an LDB connection has already been setup (so is not
52    # part of the inherited setUp())
53    def allow_password_changes(self, samdb=None):
54        """Updates the DC to allow password changes during the current test"""
55
56        if samdb is None:
57            samdb = self.ldb
58
59        PasswordCommon.allow_password_changes(self, samdb)
60