/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-04 18:38:56 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190304183856-vwfu3gje6wy6pnjn
mandos-ctl: Improve tests

* mandos-ctl (TestIsEnabledCmd.test_is_enabled_run_exits_successfully,
  TestIsEnabledCmd.test_is_enabled_run_exits_with_failure): Call
  run(), not run_on_one_client().

Show diffs side-by-side

added added

removed removed

Lines of Context:
795
795
                self.calls.append(("Get", (interface, property,
796
796
                                           dbus_interface)))
797
797
                return self.attributes[property]
798
 
            def Approve(self, approve, dbus_interface):
799
 
                testcase.assertEqual(dbus_interface, client_interface)
800
 
                self.calls.append(("Approve", (approve,
801
 
                                               dbus_interface)))
802
798
            def __getitem__(self, key):
803
799
                return self.attributes[key]
804
800
            def __setitem__(self, key, value):
855
851
                 Expires="2019-02-05T00:00:00",
856
852
                 LastCheckerStatus=-2)),
857
853
            ])
858
 
        self.client = self.clients["foo"]
859
854
 
860
855
class TestPrintTableCmd(TestCmd):
861
856
    def test_normal(self):
875
870
"""[1:-1]
876
871
        self.assertEqual(output, expected_output)
877
872
    def test_one_client(self):
878
 
        output = PrintTableCmd().output({"foo": self.client})
 
873
        output = PrintTableCmd().output({"foo": self.clients["foo"]})
879
874
        expected_output = """
880
875
Name Enabled Timeout  Last Successful Check
881
876
foo  Yes     00:05:00 2019-02-03T00:00:00  
939
934
        json_data = json.loads(DumpJSONCmd().output(self.clients))
940
935
        self.assertDictEqual(json_data, self.expected_json)
941
936
    def test_one_client(self):
942
 
        clients = {"foo": self.client}
 
937
        clients = {"foo": self.clients["foo"]}
943
938
        json_data = json.loads(DumpJSONCmd().output(clients))
944
939
        expected_json = {"foo": self.expected_json["foo"]}
945
940
        self.assertDictEqual(json_data, expected_json)
949
944
        self.assertTrue(all(IsEnabledCmd().is_enabled(client)
950
945
                            for client in self.clients.values()))
951
946
    def test_is_enabled_does_get_attribute(self):
952
 
        self.assertTrue(IsEnabledCmd().is_enabled(self.client))
953
 
        self.assertListEqual(self.client.calls,
 
947
        client = self.clients["foo"]
 
948
        self.assertTrue(IsEnabledCmd().is_enabled(client))
 
949
        self.assertListEqual(client.calls,
954
950
                             [("Get",
955
951
                               ("se.recompile.Mandos.Client",
956
952
                                "Enabled",
957
953
                                "org.freedesktop.DBus.Properties"))])
958
954
    def test_is_enabled_run_exits_successfully(self):
 
955
        client = self.clients["foo"]
959
956
        with self.assertRaises(SystemExit) as e:
960
 
            IsEnabledCmd().run(None, [self.client])
 
957
            IsEnabledCmd().run(None, [client])
961
958
        if e.exception.code is not None:
962
959
            self.assertEqual(e.exception.code, 0)
963
960
        else:
964
961
            self.assertIsNone(e.exception.code)
965
962
    def test_is_enabled_run_exits_with_failure(self):
966
 
        self.client["Enabled"] = dbus.Boolean(False)
 
963
        client = self.clients["foo"]
 
964
        client["Enabled"] = dbus.Boolean(False)
967
965
        with self.assertRaises(SystemExit) as e:
968
 
            IsEnabledCmd().run(None, [self.client])
 
966
            IsEnabledCmd().run(None, [client])
969
967
        if isinstance(e.exception.code, int):
970
968
            self.assertNotEqual(e.exception.code, 0)
971
969
        else:
972
970
            self.assertIsNotNone(e.exception.code)
973
971
 
974
 
class TestRemoveCmd(TestCmd):
975
 
    def test_remove(self):
976
 
        class MockMandos(object):
977
 
            def __init__(self):
978
 
                self.calls = []
979
 
            def RemoveClient(self, dbus_path):
980
 
                self.calls.append(("RemoveClient", (dbus_path,)))
981
 
        mandos = MockMandos()
982
 
        RemoveCmd().run(mandos, [self.client])
983
 
        self.assertEqual(len(mandos.calls), 1)
984
 
        self.assertListEqual(mandos.calls,
985
 
                             [("RemoveClient",
986
 
                               (self.client.__dbus_object_path__,))])
987
 
 
988
 
class TestApproveCmd(TestCmd):
989
 
    def test_approve(self):
990
 
        ApproveCmd().run(None, [self.client])
991
 
        self.assertListEqual(self.client.calls,
992
 
                             [("Approve", (True, client_interface))])
993
 
class TestDenyCmd(TestCmd):
994
 
    def test_approve(self):
995
 
        DenyCmd().run(None, [self.client])
996
 
        self.assertListEqual(self.client.calls,
997
 
                             [("Approve", (False, client_interface))])
998
 
 
999
972
 
1000
973
 
1001
974
def should_only_run_tests():