"""Test exceptions.""" import phpypam import pytest import vcr import yaml from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam.core.exceptions import PHPyPAMInvalidCredentials, PHPyPAMInvalidSyntax with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) @vcr.use_cassette(cassette_name('test_invalid_syntax_exception'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response) def test_invalid_syntax_exception(): """Test invalid syntax exception. Test if PHPyPAMInvalidSyntax exception is fired corretly. """ connection_kwargs = server.copy() connection_kwargs.update({'app_id': 'faulty data'}) with pytest.raises(PHPyPAMInvalidSyntax, match='Invalid application id'): phpypam.api(**connection_kwargs) @vcr.use_cassette(cassette_name('test_invalid_credentials_exception'), filter_headers=FILTER_REQUEST_HEADERS,
from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam import PHPyPAMEntityNotFoundException with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) my_section = dict( name='foobar', description='new section', permissions='{"3":"1","2":"2"}' ) @vcr.use_cassette(cassette_name('test_create_section'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response ) def test_create_section(pi): """Test to create a new section. Create a section if it doesn't exists """ try: entity = pi.get_entity(controller='sections', controller_path=my_section['name']) except PHPyPAMEntityNotFoundException: print('create entity') entity = pi.create_entity(controller='sections', data=my_section) entity = pi.get_entity(controller='sections', controller_path=my_section['name'])
"""Test controller method.""" import vcr from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS @vcr.use_cassette(cassette_name('test_controllers'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response ) def test_controllers(pi): """Test if controllers method returns correct datatype.""" controllers = pi.controllers() assert isinstance(controllers, set)
import yaml with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam import PHPyPAMEntityNotFoundException my_nameserver = dict( name='my dns', namesrv1='127.0.01', permissions=1, ) @vcr.use_cassette(cassette_name('test_create_nameserver'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response) def test_create_nameserver(pi): """Test to create a new nameserver. Create a nameserver if it doesn't exists """ try: entity = pi.get_entity(controller='tools/nameservers', params={ 'filter_by': 'name', 'filter_value': my_nameserver['name'] }) except PHPyPAMEntityNotFoundException:
import pytest import vcr import yaml from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam.core.exceptions import PHPyPAMException, PHPyPAMEntityNotFoundException with open('tests/vars/server.yml') as conn: connection_params = yaml.safe_load(conn) with open('tests/vars/search_exceptions.yml') as config: not_found_cases = yaml.safe_load(config)['not_found_cases'] @vcr.use_cassette(cassette_name('test_entity_not_found_exception'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response, record_mode='rewrite' ) @pytest.mark.parametrize('case', not_found_cases) def test_entity_not_found_exception(case): """Test entity not found exception. Test if PHPyPAMEntityNotFound exception is fired corretly for all cases. """ pi = phpypam.api(**connection_params) # check whether PHPyPAMEntityNotFoundException is raised
"""Test search for subnet.""" import pytest import vcr import yaml from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam import PHPyPAMEntityNotFoundException with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) @vcr.use_cassette(cassette_name('test_subnet_not_found'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response) def test_subnet_not_found(pi): """Test subnet not found exeption.""" cidr = '10.0.0.0/24' search_kwargs = dict(controller='subnets', controller_path='cidr/' + cidr) pytest.raises(PHPyPAMEntityNotFoundException, pi.get_entity, **search_kwargs)
import vcr import yaml from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam import PHPyPAMEntityNotFoundException with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) my_vlan = dict( name='my vlan', number='1337', ) @vcr.use_cassette(cassette_name('test_create_vlan'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response) def test_create_vlan(pi): """Test to create a new vlan. Create a vlan if it doesn't exists """ try: entity = pi.get_entity(controller='vlan', params={ 'filter_by': 'name', 'filter_value': my_vlan['name'] }) except PHPyPAMEntityNotFoundException:
"""Tests to check connection to API.""" import phpypam import pytest import vcr import yaml from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam.core.exceptions import PHPyPAMInvalidCredentials with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) @vcr.use_cassette(cassette_name('test_connection_success'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response) def test_connection_success(): """Test whether a connection to API can be done.""" pi = phpypam.api(**server) token = pi.get_token() assert isinstance(pi, phpypam.api) assert len(token) == 24 @vcr.use_cassette(cassette_name('test_connection_failure'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response)
"""Test search for address.""" import pytest import vcr import yaml from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam import PHPyPAMEntityNotFoundException with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) @vcr.use_cassette(cassette_name('test_address_not_found'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response) def test_address_not_found(pi): """Test address not found execption.""" addr = '10.10.0.4' search_kwargs = dict(controller='addresses', controller_path='search/' + addr) pytest.raises(PHPyPAMEntityNotFoundException, pi.get_entity, **search_kwargs)
"""Tests to check section search via url parameters.""" import pytest import vcr import yaml from tests.conftest import filter_request_uri, filter_response, cassette_name, FILTER_REQUEST_HEADERS from phpypam import PHPyPAMEntityNotFoundException with open('tests/vars/server.yml') as c: server = yaml.safe_load(c) @vcr.use_cassette(cassette_name('test_search_not_existing_section'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response) def test_search_not_existing_section(pi): """Search for non existing section. Search for a non existign section and get NotFound exception """ search_term = {'filter_by': 'name', 'filter_value': 'non_existing_section'} search_kwargs = dict(controller='sections', params=search_term) pytest.raises(PHPyPAMEntityNotFoundException, pi.get_entity, **search_kwargs) @vcr.use_cassette(cassette_name('test_search_existing_section'), filter_headers=FILTER_REQUEST_HEADERS, before_record_request=filter_request_uri, before_recorde_response=filter_response)