|
| 1 | +import pytest |
| 2 | +import meraki |
| 3 | + |
| 4 | + |
| 5 | +@pytest.fixture(scope='session') |
| 6 | +def api_key(pytestconfig): |
| 7 | + # Replace with a valid Meraki API key |
| 8 | + return pytestconfig.getoption("apikey") |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope='session') |
| 12 | +def dashboard(api_key): |
| 13 | + return meraki.DashboardAPI(api_key, suppress_logging=True) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope='session') |
| 17 | +def org_id(pytestconfig): |
| 18 | + # Replace with a valid organization id |
| 19 | + return pytestconfig.getoption("o") |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope='session') |
| 23 | +def network(dashboard, org_id): |
| 24 | + # Replace with network details |
| 25 | + name = "_GitHubAction Test Network" |
| 26 | + producttypes = ["appliance", "switch", "wireless", "systemsManager", "sensor"] |
| 27 | + network_kwargs = { |
| 28 | + "tags": ["test_tag", "github", "shouldBeDeleted"], |
| 29 | + "timezone": "America/Los_Angeles" |
| 30 | + } |
| 31 | + |
| 32 | + created_network = dashboard.organizations.createOrganizationNetwork(org_id, name, producttypes, **network_kwargs) |
| 33 | + yield created_network |
| 34 | + |
| 35 | + |
| 36 | +def test_get_administered_identities_me(dashboard): |
| 37 | + me = dashboard.administered.getAdministeredIdentitiesMe() |
| 38 | + assert me is not None |
| 39 | + assert isinstance(me["name"], str) |
| 40 | + assert me["authentication"]["api"]["key"]["created"] |
| 41 | + |
| 42 | + |
| 43 | +def test_get_organizations(dashboard): |
| 44 | + organizations = dashboard.organizations.getOrganizations() |
| 45 | + assert organizations is not None |
| 46 | + assert len(organizations) > 0 |
| 47 | + |
| 48 | + |
| 49 | +def test_get_organization(dashboard, org_id): |
| 50 | + organization = dashboard.organizations.getOrganization(org_id) |
| 51 | + assert isinstance(organization, dict) |
| 52 | + assert isinstance(organization["id"], str) |
| 53 | + |
| 54 | + |
| 55 | +def test_create_network(dashboard, org_id, network): |
| 56 | + assert network is not None |
| 57 | + assert network['name'] == "_GitHubAction Test Network" |
| 58 | + |
| 59 | + |
| 60 | +def test_get_networks(dashboard, org_id): |
| 61 | + networks = dashboard.organizations.getOrganizationNetworks(org_id) |
| 62 | + assert networks is not None |
| 63 | + assert len(networks) > 0 |
| 64 | + |
| 65 | + |
| 66 | +def test_update_network(dashboard, network): |
| 67 | + # Replace with updated network details |
| 68 | + new_name = "_GitHubAction Updated Test Network" |
| 69 | + updated_network_data = { |
| 70 | + "name": new_name, |
| 71 | + "tags": ["updated_test_tag", "github", "shouldBeDeleted"] |
| 72 | + } |
| 73 | + updated_network = dashboard.networks.updateNetwork(network['id'], **updated_network_data) |
| 74 | + assert updated_network is not None |
| 75 | + assert updated_network['name'] == new_name |
| 76 | + |
| 77 | + |
| 78 | +def test_create_organization_policy_objects(dashboard, org_id, network): |
| 79 | + policy_objects = [ |
| 80 | + { |
| 81 | + "name": "Ham", |
| 82 | + "category": "network", |
| 83 | + "type": "cidr", |
| 84 | + "cidr": "10.51.1.253", |
| 85 | + "networkIds": [network["id"]] |
| 86 | + }, |
| 87 | + { |
| 88 | + "name": "Hamlet", |
| 89 | + "category": "network", |
| 90 | + "type": "cidr", |
| 91 | + "cidr": "10.17.38.0/24" |
| 92 | + } |
| 93 | + ] |
| 94 | + |
| 95 | + for policy_object in policy_objects: |
| 96 | + new_object = dashboard.organizations.createOrganizationPolicyObject(org_id, **policy_object) |
| 97 | + assert new_object is not None |
| 98 | + assert isinstance(new_object["id"], str) |
| 99 | + |
| 100 | + |
| 101 | +def test_get_organization_policy_objects(dashboard, org_id): |
| 102 | + policy_objects = dashboard.organizations.getOrganizationPolicyObjects(org_id) |
| 103 | + assert policy_objects is not None |
| 104 | + assert len(policy_objects) > 0 |
| 105 | + |
| 106 | + |
| 107 | +def test_get_network_appliance_l3_firewall_rules(dashboard, network): |
| 108 | + rules = dashboard.appliance.getNetworkApplianceFirewallL3FirewallRules(network["id"]) |
| 109 | + assert rules is not None |
| 110 | + assert len(rules) > 0 |
| 111 | + |
| 112 | + |
| 113 | +def test_update_network_appliance_vlan_settings(dashboard, network): |
| 114 | + response = dashboard.appliance.updateNetworkApplianceVlansSettings(network["id"], vlansEnabled=True) |
| 115 | + assert response is not None |
| 116 | + assert response["vlansEnabled"] |
| 117 | + |
| 118 | + |
| 119 | +def test_create_network_appliance_vlan(dashboard, network): |
| 120 | + name = "testy_vlan" |
| 121 | + name2 = "home_base" |
| 122 | + new_vlans = [{ |
| 123 | + "id": 51, |
| 124 | + "name": name, |
| 125 | + "subnet": "10.51.1.0/24", |
| 126 | + "applianceIp": "10.51.1.1" |
| 127 | + }, |
| 128 | + { |
| 129 | + "id": 1738, |
| 130 | + "name": name2, |
| 131 | + "subnet": "10.17.38.0/24", |
| 132 | + "applianceIp": "10.17.38.1" |
| 133 | + } |
| 134 | + ] |
| 135 | + for vlan in new_vlans: |
| 136 | + new_vlan = dashboard.appliance.createNetworkApplianceVlan(network["id"], **vlan) |
| 137 | + assert new_vlan is not None |
| 138 | + assert len(new_vlan) > 0 |
| 139 | + assert new_vlan["name"] == vlan["name"] |
| 140 | + |
| 141 | + |
| 142 | +def test_update_l3_firewall_rules(dashboard, org_id, network): |
| 143 | + policy_objects = dashboard.organizations.getOrganizationPolicyObjects(org_id) |
| 144 | + new_rules = { |
| 145 | + "rules": [ |
| 146 | + { |
| 147 | + "comment": "HamByIP", |
| 148 | + "policy": "deny", |
| 149 | + "protocol": "tcp", |
| 150 | + "srcPort": "1738", |
| 151 | + "srcCidr": "VLAN(1738).*", |
| 152 | + "destPort": "1928", |
| 153 | + "destCidr": f"OBJ({policy_objects[0]['id']})", |
| 154 | + "syslogEnabled": False |
| 155 | + }, |
| 156 | + { |
| 157 | + "comment": "Ham", |
| 158 | + "policy": "deny", |
| 159 | + "protocol": "tcp", |
| 160 | + "srcPort": "Any", |
| 161 | + "srcCidr": f"OBJ({policy_objects[1]['id']})", |
| 162 | + "destPort": "Any", |
| 163 | + "destCidr": "11.1.1.1/32", |
| 164 | + "syslogEnabled": False |
| 165 | + } |
| 166 | + ] |
| 167 | + } |
| 168 | + updated_rules = dashboard.appliance.updateNetworkApplianceFirewallL3FirewallRules(network["id"], |
| 169 | + **new_rules)["rules"] |
| 170 | + assert updated_rules is not None |
| 171 | + assert len(updated_rules) == 3 |
| 172 | + assert updated_rules[0]["comment"] == "HamByIP" |
| 173 | + assert updated_rules[1]["comment"] == "Ham" |
| 174 | + |
| 175 | + |
| 176 | +def test_delete_policy_objects(dashboard, org_id): |
| 177 | + policy_objects = dashboard.organizations.getOrganizationPolicyObjects(org_id) |
| 178 | + for policy_object in policy_objects: |
| 179 | + response = dashboard.organizations.deleteOrganizationPolicyObject(org_id, policy_object["id"]) |
| 180 | + assert response is None |
| 181 | + |
| 182 | + |
| 183 | +def test_delete_network(dashboard, network): |
| 184 | + response = dashboard.networks.deleteNetwork(network['id']) |
| 185 | + assert response is None |
0 commit comments