# /**************************************************************************** # * # * Copyright (c) 2001 Novell, Inc. All Rights Reserved. # * # * This program is free software; you can redistribute it and/or # * modify it under the terms of version 2 of the GNU General Public License # * as published by the Free Software Foundation. # * # * 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, contact Novell, Inc. # * # * To contact Novell about this file by physical or electronic mail, you # * may find current contact information at www.novell.com. # * # ****************************************************************************/ import unittest from unittest import mock from bongo_web import admin class AdminBridgeTests(unittest.TestCase): @mock.patch("bongo_web.admin.subprocess.run") def test_password_is_passed_only_through_stdin(self, run): run.return_value = mock.Mock(returncode=0, stdout="changed\n", stderr="") result = admin.perform("user_password", { "user": "alice", "password": "correct horse battery staple"}) self.assertEqual(result, "changed") arguments = run.call_args.args[0] self.assertNotIn("correct horse battery staple", arguments) self.assertEqual(run.call_args.kwargs["input"], "correct horse battery staple\ncorrect horse battery staple\n") def test_rejects_shell_metacharacter_whitespace_and_bad_quota(self): with self.assertRaises(ValueError): admin.perform("user_add", {"user": "bad user"}) with self.assertRaises(ValueError): admin.perform("user_quota", {"user": "alice", "quota": "1G;id"}) @mock.patch("bongo_web.admin._command") def test_snapshot_parses_users_agents_and_server(self, command): command.side_effect = ["admin\nalice", "smtp enabled (priority 10)", "Server: mail.test\nManager: running (pid 1)", "0 cached user(s)"] result = admin.snapshot() self.assertEqual(result["users"], ["admin", "alice"]) self.assertEqual(result["agents"][0]["name"], "smtp") self.assertTrue(result["agents"][0]["enabled"]) self.assertEqual(result["server"]["server"], "mail.test") if __name__ == "__main__": unittest.main()