tests: Add tests for samba-tool passwordsettings commands
[samba.git] / python / samba / tests / samba_tool / passwordsettings.py
1 # Test 'samba-tool domain passwordsettings' sub-commands
2 #
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
4 #
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 3 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, see <http://www.gnu.org/licenses/>.
17 #
18
19 import os
20 import ldb
21 from samba.tests.samba_tool.base import SambaToolCmdTest
22
23 class PwdSettingsCmdTestCase(SambaToolCmdTest):
24     """Tests for 'samba-tool domain passwordsettings' subcommands"""
25
26     def setUp(self):
27         super(PwdSettingsCmdTestCase, self).setUp()
28         self.server = "ldap://%s" % os.environ["DC_SERVER"]
29         self.user_auth = "-U%s%%%s" % (os.environ["DC_USERNAME"],
30                                        os.environ["DC_PASSWORD"])
31         self.ldb = self.getSamDB("-H", self.server, self.user_auth)
32
33     def tearDown(self):
34         super(PwdSettingsCmdTestCase, self).tearDown()
35
36     def test_domain_passwordsettings(self):
37         """Checks the 'set/show' commands for the domain settings (non-PSO)"""
38
39         # check the 'show' cmd for the domain settings
40         (result, out, err) = self.runsublevelcmd("domain", ("passwordsettings",
41                                                  "show"), "-H", self.server,
42                                                  self.user_auth)
43         self.assertCmdSuccess(result, out, err)
44         self.assertEquals(err,"","Shouldn't be any error messages")
45
46         # check an arbitrary setting is displayed correctly
47         min_pwd_len = self.ldb.get_minPwdLength()
48         self.assertIn("Minimum password length: %s" % min_pwd_len, out)
49
50         # check we can change the domain setting
51         self.addCleanup(self.ldb.set_minPwdLength, min_pwd_len)
52         new_len = int(min_pwd_len) + 3
53         (result, out, err) = self.runsublevelcmd("domain", ("passwordsettings",
54                                                  "set"),
55                                                  "--min-pwd-length=%u" % new_len,
56                                                  "-H", self.server,
57                                                  self.user_auth)
58         self.assertCmdSuccess(result, out, err)
59         self.assertEquals(err,"","Shouldn't be any error messages")
60         self.assertIn("successful", out)
61         self.assertEquals(str(new_len), self.ldb.get_minPwdLength())
62
63         # check the updated value is now displayed
64         (result, out, err) = self.runsublevelcmd("domain", ("passwordsettings",
65                                                  "show"), "-H", self.server,
66                                                  self.user_auth)
67         self.assertCmdSuccess(result, out, err)
68         self.assertEquals(err,"","Shouldn't be any error messages")
69         self.assertIn("Minimum password length: %u" % new_len, out)
70