def test_apply(self): provider = MythicBeastsProvider('test', {'unit.tests.': 'mypassword'}) zone = Zone('unit.tests.', []) # Create blank zone with requests_mock() as mock: mock.post(ANY, status_code=200, text='') provider.populate(zone) self.assertEquals(0, len(zone.records)) # Record change failed with requests_mock() as mock: mock.post(ANY, status_code=200, text='') provider.populate(zone) zone.add_record( Record.new(zone, 'prawf', { 'ttl': 300, 'type': 'TXT', 'value': 'prawf', })) plan = provider.plan(zone) with requests_mock() as mock: mock.post(ANY, status_code=400, text='NADD 300 TXT prawf') with self.assertRaises(Exception) as err: provider.apply(plan) self.assertEquals( 'Mythic Beasts could not action command: unit.tests ' 'ADD prawf.unit.tests 300 TXT prawf', err.exception.message) # Check deleting and adding/changing test record existing = 'prawf 300 TXT prawf prawf prawf\ndileu 300 TXT dileu' with requests_mock() as mock: mock.post(ANY, status_code=200, text=existing) # Mash up a new zone with records so a plan # is generated with changes and applied. For some reason # passing self.expected, or just changing each record's zone # doesn't work. Nor does this without a single add_record after wanted = Zone('unit.tests.', []) for record in list(self.expected.records): data = {'type': record._type} data.update(record.data) wanted.add_record(Record.new(wanted, record.name, data)) wanted.add_record( Record.new(wanted, 'prawf', { 'ttl': 60, 'type': 'TXT', 'value': 'prawf yw e', })) plan = provider.plan(wanted) # Octo ignores NS records (15-1) self.assertEquals( 1, len([c for c in plan.changes if isinstance(c, Update)])) self.assertEquals( 1, len([c for c in plan.changes if isinstance(c, Delete)])) self.assertEquals( 14, len([c for c in plan.changes if isinstance(c, Create)])) self.assertEquals(16, provider.apply(plan)) self.assertTrue(plan.exists)
def test_populate(self): provider = None # Null passwords dict with self.assertRaises(AssertionError) as err: provider = MythicBeastsProvider('test', None) self.assertEquals('Passwords must be a dictionary', text_type(err.exception)) # Missing password with requests_mock() as mock: mock.post(ANY, status_code=401, text='ERR Not authenticated') with self.assertRaises(AssertionError) as err: provider = MythicBeastsProvider('test', dict()) zone = Zone('unit.tests.', []) provider.populate(zone) self.assertEquals('Missing password for domain: unit.tests', text_type(err.exception)) # Failed authentication with requests_mock() as mock: mock.post(ANY, status_code=401, text='ERR Not authenticated') with self.assertRaises(Exception) as err: provider = MythicBeastsProvider('test', {'unit.tests.': 'mypassword'}) zone = Zone('unit.tests.', []) provider.populate(zone) self.assertEquals( 'Mythic Beasts unauthorized for zone: unit.tests', err.exception.message) # Check unmatched lines are ignored test_data = 'This should not match' with requests_mock() as mock: mock.post(ANY, status_code=200, text=test_data) provider = MythicBeastsProvider('test', {'unit.tests.': 'mypassword'}) zone = Zone('unit.tests.', []) provider.populate(zone) self.assertEquals(0, len(zone.records)) # Check unsupported records are skipped test_data = '@ 60 NOOP prawf\n@ 60 SPF prawf prawf prawf' with requests_mock() as mock: mock.post(ANY, status_code=200, text=test_data) provider = MythicBeastsProvider('test', {'unit.tests.': 'mypassword'}) zone = Zone('unit.tests.', []) provider.populate(zone) self.assertEquals(0, len(zone.records)) # Check no changes between what we support and what's parsed # from the unit.tests. config YAML. Also make sure we see the same # for both after we've thrown away records we don't support with requests_mock() as mock: with open('tests/fixtures/mythicbeasts-list.txt') as file_handle: mock.post(ANY, status_code=200, text=file_handle.read()) provider = MythicBeastsProvider('test', {'unit.tests.': 'mypassword'}) zone = Zone('unit.tests.', []) provider.populate(zone) self.assertEquals(15, len(zone.records)) self.assertEquals(15, len(self.expected.records)) changes = self.expected.changes(zone, provider) self.assertEquals(0, len(changes))