def test_not_exist_domain(self, fake_http): fake_http.get(f'{self.API_URL}/', status_code=404, json='') fake_http.head(f'{self.API_URL}/', headers={'X-Total-Count': str(len(self.domain))}) fake_http.post(f'{self.API_URL}/', json={ "name": "unit.tests", "create_date": 1507154178, "id": 100000 }) fake_http.get(f'{self.API_URL}/unit.tests/records/', json=list()) fake_http.head(f'{self.API_URL}/unit.tests/records/', headers={'X-Total-Count': str(len(self.api_record))}) fake_http.post(f'{self.API_URL}/100000/records/', json=list()) provider = SelectelProvider(123, 'test_token') zone = Zone('unit.tests.', []) for record in self.expected: zone.add_record(record) plan = provider.plan(zone) self.assertEquals(8, len(plan.changes)) self.assertEquals(8, provider.apply(plan))
def test_authentication_fail(self, fake_http): fake_http.get(f'{self.API_URL}/', status_code=401) fake_http.head(f'{self.API_URL}/', headers={'X-Total-Count': str(len(self.domain))}) with self.assertRaises(Exception) as ctx: SelectelProvider(123, 'fail_token') self.assertEquals(text_type(ctx.exception), 'Authorization failed. Invalid or empty token.')
def test_domain_list(self, fake_http): fake_http.get(f'{self.API_URL}/', json=self.domain) fake_http.head(f'{self.API_URL}/', headers={'X-Total-Count': str(len(self.domain))}) expected = {'unit.tests': self.domain[0]} provider = SelectelProvider(123, 'test_token') result = provider.domain_list() self.assertEquals(result, expected)
def test_delete_no_exist_record(self, fake_http): fake_http.get(f'{self.API_URL}/', json=self.domain) fake_http.get(f'{self.API_URL}/100000/records/', json=list()) fake_http.head(f'{self.API_URL}/', headers={'X-Total-Count': str(len(self.domain))}) fake_http.head(f'{self.API_URL}/unit.tests/records/', headers={'X-Total-Count': '0'}) provider = SelectelProvider(123, 'test_token') zone = Zone('unit.tests.', []) provider.delete_record('unit.tests', 'NS', zone)
def test_populate(self, fake_http): zone = Zone('unit.tests.', []) fake_http.get(f'{self.API_URL}/unit.tests/records/', json=self.api_record) fake_http.get(f'{self.API_URL}/', json=self.domain) fake_http.head(f'{self.API_URL}/unit.tests/records/', headers={'X-Total-Count': str(len(self.api_record))}) fake_http.head(f'{self.API_URL}/', headers={'X-Total-Count': str(len(self.domain))}) provider = SelectelProvider(123, 'secret_token') provider.populate(zone) self.assertEquals(self.expected, zone.records)
def test_change_record(self, fake_http): exist_record = [ self.aaaa_record, { "content": "6.6.5.7", "ttl": 100, "type": "A", "id": 100001, "name": "delete.unit.tests" }, { "content": "9.8.2.1", "ttl": 100, "type": "A", "id": 100002, "name": "unit.tests" } ] # exist fake_http.get('{}/unit.tests/records/'.format(self.API_URL), json=exist_record) fake_http.get('{}/'.format(self.API_URL), json=self.domain) fake_http.get('{}/100000/records/'.format(self.API_URL), json=exist_record) fake_http.head('{}/unit.tests/records/'.format(self.API_URL), headers={'X-Total-Count': str(len(exist_record))}) fake_http.head('{}/'.format(self.API_URL), headers={'X-Total-Count': str(len(self.domain))}) fake_http.head('{}/100000/records/'.format(self.API_URL), headers={'X-Total-Count': str(len(exist_record))}) fake_http.post('{}/100000/records/'.format(self.API_URL), json=list()) fake_http.delete('{}/100000/records/100001'.format(self.API_URL), text="") fake_http.delete('{}/100000/records/100002'.format(self.API_URL), text="") provider = SelectelProvider(123, 'test_token') zone = Zone('unit.tests.', []) for record in self.expected: zone.add_record(record) plan = provider.plan(zone) self.assertEquals(8, len(plan.changes)) self.assertEquals(8, provider.apply(plan))
def test_apply(self, fake_http): fake_http.get(f'{self.API_URL}/unit.tests/records/', json=list()) fake_http.get(f'{self.API_URL}/', json=self.domain) fake_http.head(f'{self.API_URL}/unit.tests/records/', headers={'X-Total-Count': '0'}) fake_http.head(f'{self.API_URL}/', headers={'X-Total-Count': str(len(self.domain))}) fake_http.post(f'{self.API_URL}/100000/records/', json=list()) provider = SelectelProvider(123, 'test_token') zone = Zone('unit.tests.', []) for record in self.expected: zone.add_record(record) plan = provider.plan(zone) self.assertEquals(8, len(plan.changes)) self.assertEquals(8, provider.apply(plan))
def test_populate_invalid_record(self, fake_http): more_record = self.api_record more_record.append({ "name": "unit.tests", "id": 100001, "content": "support.unit.tests.", "ttl": 300, "ns": "ns1.unit.tests", "type": "SOA", "email": "*****@*****.**" }) zone = Zone('unit.tests.', []) fake_http.get('{}/unit.tests/records/'.format(self.API_URL), json=more_record) fake_http.get('{}/'.format(self.API_URL), json=self.domain) fake_http.head('{}/unit.tests/records/'.format(self.API_URL), headers={'X-Total-Count': str(len(self.api_record))}) fake_http.head('{}/'.format(self.API_URL), headers={'X-Total-Count': str(len(self.domain))}) zone.add_record( Record.new( self.zone, 'unsup', { 'ttl': 200, 'type': 'NAPTR', 'value': { 'order': 40, 'preference': 70, 'flags': 'U', 'service': 'SIP+D2U', 'regexp': '!^.*$!sip:[email protected]!', 'replacement': '.', } })) provider = SelectelProvider(123, 'secret_token') provider.populate(zone) self.assertNotEqual(self.expected, zone.records)
def test_include_change_returns_false(self, fake_http): fake_http.get(f'{self.API_URL}/', json=self.domain) fake_http.head(f'{self.API_URL}/', headers={'X-Total-Count': str(len(self.domain))}) provider = SelectelProvider(123, 'test_token') zone = Zone('unit.tests.', []) exist_record = Record.new(zone, '', { 'ttl': 60, 'type': 'A', 'values': ['1.1.1.1', '2.2.2.2'] }) new = Record.new(zone, '', { 'ttl': 10, 'type': 'A', 'values': ['1.1.1.1', '2.2.2.2'] }) change = Update(exist_record, new) include_change = provider._include_change(change) self.assertFalse(include_change)