Beispiel #1
0
class TestGeneralSettings(unittest.TestCase):
    def setUp(self):
        self.helper = Secure_Track_Helper("localhost", ("username", "password"))
        self.patcher = patch('pytos.common.rest_requests.requests.Session.send')
        self.mock_get_uri = self.patcher.start()
        self.mock_get_uri.return_value.status_code = 200

    def tearDown(self):
        self.patcher.stop()

    def test_03_get_change_authorization_status(self):
        self.mock_get_uri.return_value.content = fake_request_response("revisions")
        revisions = self.helper.get_device_revisions_by_id(device_id=158)
        self.assertIsInstance(revisions, Device_Revisions_List)
class TestZonesPoliciesAndRevisions(unittest.TestCase):
    def setUp(self):
        self.helper = Secure_Track_Helper("localhost",
                                          ("username", "password"))
        self.patcher = patch(
            'pytos.common.rest_requests.requests.Session.send')
        self.mock_get_uri = self.patcher.start()
        self.mock_get_uri.return_value.status_code = 200

    def tearDown(self):
        self.patcher.stop()

    def test_01_get_zones(self):
        self.mock_get_uri.return_value.content = fake_request_response("zones")
        zones = self.helper.get_zones()
        self.assertIsInstance(zones, Zone_List)

    def test_02_post_zone(self):
        src_xml = fake_request_response("post_zone")
        src_tree = lxml.etree.fromstring(src_xml)
        src_b = io.BytesIO()
        src_tree.getroottree().write_c14n(src_b)
        comment = 'Name: {}, Created at: {}'.format("New Zone",
                                                    "2017-04-22 10:09:18")
        zone_obj = Zone(None, "New Zone", comment)
        dst_tree = lxml.etree.fromstring(zone_obj.to_xml_string())
        dst_b = io.BytesIO()
        dst_tree.getroottree().write_c14n(dst_b)
        self.assertEqual(src_b.getvalue(), dst_b.getvalue())

    def test_03_post_security_policy_matrix(self):
        self.mock_get_uri.return_value.headers = {'location': '1'}
        self.mock_get_uri.return_value.content = fake_request_response("zones")
        security_policy_name = 'Some Policy Name'
        security_policy = {
            'internal': {
                'external': {
                    'severity': 'critical',
                    'access_type': 'ignored',
                    'allowed_services': ''
                }
            },
            'external': {
                'internal': {
                    'severity': 'high',
                    'access_type': 'restricted',
                    'allowed_services': 'https;Other 53;AOL;udp 88'
                }
            },
            'dmz': {
                'internal': {
                    'severity': 'critical',
                    'access_type': 'blocked',
                    'allowed_services': ''
                },
                'dmz': {
                    'severity': 'low',
                    'access_type': 'ignored',
                    'allowed_services': ''
                }
            }
        }
        policy_id = self.helper.post_security_policy_matrix(
            security_policy_name, security_policy)
        self.assertEqual(policy_id, 1)

    def test_04_post_zone_entry(self):
        self.mock_get_uri.return_value.headers = {'location': '1'}
        self.mock_get_uri.return_value.status_code = 201
        zone_entry = Zone_Entry(1234, "Description", "1.1.1.1", 0,
                                '255.255.255.255', 36)
        with patch('pytos.common.rest_requests.requests.Request'
                   ) as mock_post_uri:
            entry_id = self.helper.post_zone_entry(zone_entry.zoneId,
                                                   zone_entry)
            self.assertEqual(entry_id, 1)
            mock_post_uri.assert_called_with(
                'POST',
                'https://localhost/securetrack/api/zones/36/entries?context=1',
                auth=('username', 'password'),
                data=
                '<zone_entry>\n  <comment>Description</comment>\n  <id>1234</id>\n  <ip>1.1.1.1</ip>\n  <netmask>255.255.255.255</netmask>\n  <zoneId>36</zoneId>\n</zone_entry>',
                headers={'Content-Type': 'application/xml'})

    def test_05_delete_zone_entry(self):
        with patch('pytos.common.rest_requests.requests.Request'
                   ) as mock_post_uri:
            result = self.helper.delete_zone_entry_by_zone_and_entry_id(1, 1)
            self.assertTrue(result)
            mock_post_uri.assert_called_with(
                'DELETE',
                'https://localhost/securetrack/api/zones/1/entries/1?context=1',
                auth=('username', 'password'),
                headers={'Content-Type': 'application/xml'})

    def test_06_modify_zone_entry(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "zone_entries")
        zone_entries = self.helper.get_entries_for_zone_id(13)
        zone_entry = zone_entries[0]
        zone_entry.comment = "Modified entry"
        zone_entry.ip = '101.101.101.101'
        zone_entry.negate = 0
        zone_entry.netmask = '255.255.255.255'
        with patch('pytos.common.rest_requests.requests.Request'
                   ) as mock_post_uri:
            result = self.helper.put_zone_entry(13, zone_entry)
            self.assertTrue(result)
            mock_post_uri.assert_called_with(
                'PUT',
                'https://localhost/securetrack/api/zones/13/entries/54?context=1',
                auth=('username', 'password'),
                data=
                '<zone_entry>\n  <comment>Modified entry</comment>\n  <id>54</id>\n  <ip>101.101.101.101</ip>\n  <negate>0</negate>\n  <netmask>255.255.255.255</netmask>\n  <zoneId>13</zoneId>\n</zone_entry>',
                headers={'Content-Type': 'application/xml'})

    def test_07_get_zone_by_name(self):
        self.mock_get_uri.return_value.content = fake_request_response("zones")
        zone = self.helper.get_zone_by_name("dmz")
        self.assertIsInstance(zone, Zone)
        self.assertEqual(zone.name, "dmz")

    def test_08_get_device_revisions_by_id(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "revisions")
        revisions = self.helper.get_device_revisions_by_id(device_id=155)
        self.assertIsInstance(revisions, Device_Revisions_List)
        self.assertTrue(len(revisions) > 0)

    def test_09_get_policy_analysis(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "policy_analysis_query_result")
        policy_analysis = self.helper.get_policy_analysis(155)
        self.assertIsInstance(policy_analysis, Policy_Analysis_Query_Result)

    def test_10_get_security_policies(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "securitypolicylist")
        policies = self.helper.get_security_policies()
        self.assertIsInstance(policies, Security_Policies_List)

    def test_11_get_security_policy_by_name(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "securitypolicylist")
        policy = self.helper.get_security_policy_by_name("policy")
        self.assertIsInstance(policy, Security_Policy)
        self.assertEqual(policy.name, "policy")

    def test_12_get_security_policy_by_id(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "securitypolicylist")
        policy = self.helper.get_security_policy_by_id(3)
        self.assertEqual(policy.id, 3)

    def test_13_delete_security_policy_matrix(self):
        with patch('pytos.common.rest_requests.requests.Request'
                   ) as mock_post_uri:
            result = self.helper.delete_security_policy_matrix(3)
            self.assertTrue(result)
            mock_post_uri.assert_called_with(
                'DELETE',
                'https://localhost/securetrack/api/security_policies/3',
                auth=('username', 'password'),
                headers={'Content-Type': 'application/xml'})

    def test_14_get_revision_by_id(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "revision")
        revision = self.helper.get_revision_by_id(5685)
        self.assertIsInstance(revision, Device_Revision)
        self.assertTrue(revision.id, 5685)

    def test_15_get_security_policy_device_violations_by_severity(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "security_policy_device_violations")
        violations = self.helper.get_security_policy_device_violations_by_severity(
            159, "CRITICAL", "SECURITY_POLICY")
        self.assertIsInstance(violations, SecurityPolicyDeviceViolations)

    def test_16_get_policies_for_revision(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "policies")
        policies = self.helper.get_policies_for_revision(1)
        self.assertIsInstance(policies, Policy_List)

    def test_17_post_security_policy_exception(self):
        self.mock_get_uri.return_value.headers = {'location': '1'}
        self.mock_get_uri.return_value.status_code = 201
        xml = fake_request_response("exception")
        policy_exception = security_policy.Security_Policy_Exception.from_xml_string(
            xml.decode("utf-8"))
        with patch('pytos.common.rest_requests.requests.Request'
                   ) as mock_post_uri:
            self.helper.post_security_policy_exception(policy_exception)
            mock_post_uri.assert_called_with(
                'POST',
                'https://localhost/securetrack/api/security_policies/exceptions/?context=1',
                auth=('username', 'password'),
                data=policy_exception.to_xml_string(),
                headers={'Content-Type': 'application/xml'})

    def test_18_delete_zone_by_zone_id(self):
        with patch('pytos.common.rest_requests.requests.Request'
                   ) as mock_delete_uri:
            self.helper.delete_zone_by_zone_id(1, True)
            mock_delete_uri.assert_called_with(
                'DELETE',
                'https://localhost/securetrack/api/zones/1',
                auth=('username', 'password'),
                headers={'Content-Type': 'application/xml'})

    def test_19_get_zone_descendants(self):
        self.mock_get_uri.return_value.content = fake_request_response(
            "zone_descendants")
        zone_descendants_list = self.helper.get_zone_descendants("16")
        self.assertIsInstance(zone_descendants_list, ZoneDescendantsList)

    def test_20_delete_security_policy_exception(self):
        with patch('pytos.common.rest_requests.requests.Request'
                   ) as mock_delete_uri:
            self.helper.delete_security_policy_exception(1)
            mock_delete_uri.assert_called_with(
                'DELETE',
                'https://localhost/securetrack/api/security_policies/exceptions/1',
                auth=('username', 'password'),
                headers={'Content-Type': 'application/xml'})