Esempio n. 1
0
    def test_apply_not_found(self):
        provider = EasyDNSProvider('test', 'token', 'apikey')

        wanted = Zone('unit.tests.', [])
        wanted.add_record(Record.new(wanted, 'test1', {
            "name": "test1",
            "ttl": 300,
            "type": "A",
            "value": "1.2.3.4",
        }))

        with requests_mock() as mock:
            base = 'https://rest.easydns.net/'
            mock.get('{}{}'.format(base, 'domain/unit.tests'), status_code=404,
                     text='{"id":"not_found","message":"The resource you '
                     'were accessing could not be found."}')
            mock.put('{}{}'.format(base, 'domains/add/unit.tests'),
                     status_code=200,
                     text='{"id":"OK","message":"Zone created."}')
            mock.get('{}{}'.format(base, 'zones/records/parsed/unit.tests'),
                     status_code=404,
                     text='{"id":"not_found","message":"The resource you '
                     'were accessing could not be found."}')
            mock.get('{}{}'.format(base, 'zones/records/all/unit.tests'),
                     status_code=404,
                     text='{"id":"not_found","message":"The resource you '
                     'were accessing could not be found."}')

            plan = provider.plan(wanted)
            self.assertFalse(plan.exists)
            self.assertEquals(1, len(plan.changes))
            with self.assertRaises(Exception) as ctx:
                provider.apply(plan)

            self.assertEquals('Not Found', text_type(ctx.exception))
Esempio n. 2
0
    def test_apply(self):
        provider = EasyDNSProvider('test', 'token', 'apikey')

        resp = Mock()
        resp.json = Mock()
        provider._client._request = Mock(return_value=resp)

        domain_after_creation = {
            "tm": 1000000000,
            "data": [{
                "id": "12341001",
                "domain": "unit.tests",
                "host": "@",
                "ttl": "0",
                "prio": "0",
                "type": "SOA",
                "rdata": "dns1.easydns.com. zone.easydns.com. 2020010101"
                " 3600 600 604800 0",
                "geozone_id": "0",
                "last_mod": "2020-01-01 01:01:01"
            }, {
                "id": "12341002",
                "domain": "unit.tests",
                "host": "@",
                "ttl": "0",
                "prio": "0",
                "type": "NS",
                "rdata": "LOCAL.",
                "geozone_id": "0",
                "last_mod": "2020-01-01 01:01:01"
            }, {
                "id": "12341003",
                "domain": "unit.tests",
                "host": "@",
                "ttl": "0",
                "prio": "0",
                "type": "MX",
                "rdata": "LOCAL.",
                "geozone_id": "0",
                "last_mod": "2020-01-01 01:01:01"
            }],
            "count": 3,
            "total": 3,
            "start": 0,
            "max": 1000,
            "status": 200
        }

        # non-existent domain, create everything
        resp.json.side_effect = [
            EasyDNSClientNotFound,  # no zone in populate
            domain_after_creation
        ]
        plan = provider.plan(self.expected)

        # No root NS, no ignored, no excluded, no unsupported
        n = len(self.expected.records) - 7
        self.assertEquals(n, len(plan.changes))
        self.assertEquals(n, provider.apply(plan))
        self.assertFalse(plan.exists)

        self.assertEquals(23, provider._client._request.call_count)

        provider._client._request.reset_mock()

        # delete 1 and update 1
        provider._client.records = Mock(return_value=[
            {
                "id": "12342001",
                "domain": "unit.tests",
                "host": "www",
                "ttl": "300",
                "prio": "0",
                "type": "A",
                "rdata": "2.2.3.9",
                "geozone_id": "0",
                "last_mod": "2020-01-01 01:01:01"
            }, {
                "id": "12342002",
                "domain": "unit.tests",
                "host": "www",
                "ttl": "300",
                "prio": "0",
                "type": "A",
                "rdata": "2.2.3.8",
                "geozone_id": "0",
                "last_mod": "2020-01-01 01:01:01"
            }, {
                "id": "12342003",
                "domain": "unit.tests",
                "host": "test1",
                "ttl": "3600",
                "prio": "0",
                "type": "A",
                "rdata": "1.2.3.4",
                "geozone_id": "0",
                "last_mod": "2020-01-01 01:01:01"
            }
        ])

        # Domain exists, we don't care about return
        resp.json.side_effect = ['{}']

        wanted = Zone('unit.tests.', [])
        wanted.add_record(Record.new(wanted, 'test1', {
            "name": "test1",
            "ttl": 300,
            "type": "A",
            "value": "1.2.3.4",
        }))

        plan = provider.plan(wanted)
        self.assertTrue(plan.exists)
        self.assertEquals(2, len(plan.changes))
        self.assertEquals(2, provider.apply(plan))
        # recreate for update, and delete for the 2 parts of the other
        provider._client._request.assert_has_calls([
            call('PUT', '/zones/records/add/unit.tests/A', data={
                'rdata': '1.2.3.4',
                'name': 'test1',
                'ttl': 300,
                'type': 'A',
                'host': 'test1',
            }),
            call('DELETE', '/zones/records/unit.tests/12342001'),
            call('DELETE', '/zones/records/unit.tests/12342002'),
            call('DELETE', '/zones/records/unit.tests/12342003')
        ], any_order=True)