예제 #1
0
 def test_is_valid_dkim(self, client_mock):
     """Test _is_valid_dkim"""
     provider = OvhProvider('test', 'endpoint', 'application_key',
                            'application_secret', 'consumer_key')
     for dkim in self.valid_dkim:
         self.assertTrue(provider._is_valid_dkim(dkim))
     for dkim in self.invalid_dkim:
         self.assertFalse(provider._is_valid_dkim(dkim))
예제 #2
0
    def test_populate(self, client_mock):
        provider = OvhProvider('test', 'endpoint', 'application_key',
                               'application_secret', 'consumer_key')

        with patch.object(provider._client, 'get') as get_mock:
            zone = Zone('unit.tests.', [])
            get_mock.side_effect = ResourceNotFoundError('boom')
            with self.assertRaises(APIError) as ctx:
                provider.populate(zone)
            self.assertEquals(get_mock.side_effect, ctx.exception)

            get_mock.side_effect = InvalidCredential('boom')
            with self.assertRaises(APIError) as ctx:
                provider.populate(zone)
            self.assertEquals(get_mock.side_effect, ctx.exception)

            zone = Zone('unit.tests.', [])
            get_mock.side_effect = ResourceNotFoundError('This service does '
                                                         'not exist')
            exists = provider.populate(zone)
            self.assertEquals(set(), zone.records)
            self.assertFalse(exists)

            zone = Zone('unit.tests.', [])
            get_returns = [[record['id'] for record in self.api_record]]
            get_returns += self.api_record
            get_mock.side_effect = get_returns
            exists = provider.populate(zone)
            self.assertEquals(self.expected, zone.records)
            self.assertTrue(exists)
예제 #3
0
    def test_populate(self, client_mock):
        provider = OvhProvider('test', 'endpoint', 'application_key',
                               'application_secret', 'consumer_key')

        with patch.object(provider._client, 'get') as get_mock:
            zone = Zone('unit.tests.', [])
            get_mock.side_effect = APIError('boom')
            with self.assertRaises(APIError) as ctx:
                provider.populate(zone)
            self.assertEquals(get_mock.side_effect, ctx.exception)

        with patch.object(provider._client, 'get') as get_mock:
            zone = Zone('unit.tests.', [])
            get_returns = [[record['id'] for record in self.api_record]]
            get_returns += self.api_record
            get_mock.side_effect = get_returns
            provider.populate(zone)
            self.assertEquals(self.expected, zone.records)
예제 #4
0
    def test_apply(self, client_mock):
        provider = OvhProvider('test', 'endpoint', 'application_key',
                               'application_secret', 'consumer_key')

        desired = Zone('unit.tests.', [])

        for r in self.expected:
            desired.add_record(r)

        with patch.object(provider._client, 'post') as get_mock:
            plan = provider.plan(desired)
            get_mock.side_effect = APIError('boom')
            with self.assertRaises(APIError) as ctx:
                provider.apply(plan)
            self.assertEquals(get_mock.side_effect, ctx.exception)

        # Records get by API call
        with patch.object(provider._client, 'get') as get_mock:
            get_returns = [
                [1, 2, 3, 4],
                {'fieldType': 'A', 'ttl': 600, 'target': '5.6.7.8',
                 'subDomain': '', 'id': 100},
                {'fieldType': 'A', 'ttl': 600, 'target': '5.6.7.8',
                 'subDomain': 'fake', 'id': 101},
                {'fieldType': 'TXT', 'ttl': 600, 'target': 'fake txt record',
                 'subDomain': 'txt', 'id': 102},
                {'fieldType': 'DKIM', 'ttl': 600,
                 'target': 'v=DKIM1; %s' % self.valid_dkim_key,
                 'subDomain': 'dkim', 'id': 103}
            ]
            get_mock.side_effect = get_returns

            plan = provider.plan(desired)

            with patch.object(provider._client, 'post') as post_mock, \
                    patch.object(provider._client, 'delete') as delete_mock:
                get_mock.side_effect = [[100], [101], [102], [103]]
                provider.apply(plan)
                wanted_calls = [
                    call(u'/domain/zone/unit.tests/record', fieldType=u'TXT',
                         subDomain='txt', target=u'TXT text', ttl=1400),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'DKIM',
                         subDomain='dkim', target=self.valid_dkim_key,
                         ttl=1300),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'A',
                         subDomain=u'', target=u'1.2.3.4', ttl=100),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'SRV',
                         subDomain='_srv._tcp',
                         target=u'10 20 30 foo-1.unit.tests.', ttl=800),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'SRV',
                         subDomain='_srv._tcp',
                         target=u'40 50 60 foo-2.unit.tests.', ttl=800),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'PTR',
                         subDomain='4', target=u'unit.tests.', ttl=900),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'NS',
                         subDomain='www3', target=u'ns3.unit.tests.', ttl=700),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'NS',
                         subDomain='www3', target=u'ns4.unit.tests.', ttl=700),
                    call(u'/domain/zone/unit.tests/record',
                         fieldType=u'SSHFP', subDomain=u'', ttl=1100,
                         target=u'1 1 bf6b6825d2977c511a475bbefb88a'
                                   u'ad54'
                                   u'a92ac73',
                         ),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'AAAA',
                         subDomain=u'', target=u'1:1ec:1::1', ttl=200),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'MX',
                         subDomain=u'', target=u'10 mx1.unit.tests.', ttl=400),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'CNAME',
                         subDomain='www2', target=u'unit.tests.', ttl=300),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'SPF',
                         subDomain=u'', ttl=1000,
                         target=u'v=spf1 include:unit.texts.'
                                u'rerirect ~all',
                         ),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'A',
                         subDomain='sub', target=u'1.2.3.4', ttl=200),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'NAPTR',
                         subDomain='naptr', ttl=500,
                         target=u'10 100 "S" "SIP+D2U" "!^.*$!sip:'
                                u'info@bar'
                                u'.example.com!" .'
                         ),
                    call(u'/domain/zone/unit.tests/refresh')]

                post_mock.assert_has_calls(wanted_calls)

                # Get for delete calls
                wanted_get_calls = [
                    call(u'/domain/zone/unit.tests/record', fieldType=u'TXT',
                         subDomain='txt'),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'DKIM',
                         subDomain='dkim'),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'A',
                         subDomain=u''),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'A',
                         subDomain='fake')]
                get_mock.assert_has_calls(wanted_get_calls)
                # 4 delete calls for update and delete
                delete_mock.assert_has_calls(
                    [call(u'/domain/zone/unit.tests/record/100'),
                     call(u'/domain/zone/unit.tests/record/101'),
                     call(u'/domain/zone/unit.tests/record/102'),
                     call(u'/domain/zone/unit.tests/record/103')])
예제 #5
0
    def test_apply(self, client_mock):
        provider = OvhProvider('test', 'endpoint', 'application_key',
                               'application_secret', 'consumer_key')

        desired = Zone('unit.tests.', [])

        for r in self.expected:
            desired.add_record(r)

        with patch.object(provider._client, 'post') as get_mock:
            plan = provider.plan(desired)
            get_mock.side_effect = APIError('boom')
            with self.assertRaises(APIError) as ctx:
                provider.apply(plan)
            self.assertEquals(get_mock.side_effect, ctx.exception)

        # Records get by API call
        with patch.object(provider._client, 'get') as get_mock:
            get_returns = [
                [1, 2, 3, 4],
                {'fieldType': 'A', 'ttl': 600, 'target': '5.6.7.8',
                 'subDomain': '', 'id': 100},
                {'fieldType': 'A', 'ttl': 600, 'target': '5.6.7.8',
                 'subDomain': 'fake', 'id': 101},
                {'fieldType': 'TXT', 'ttl': 600, 'target': 'fake txt record',
                 'subDomain': 'txt', 'id': 102},
                {'fieldType': 'DKIM', 'ttl': 600,
                 'target': 'v=DKIM1; %s' % self.valid_dkim_key,
                 'subDomain': 'dkim', 'id': 103}
            ]
            get_mock.side_effect = get_returns

            plan = provider.plan(desired)

            with patch.object(provider._client, 'post') as post_mock, \
                    patch.object(provider._client, 'delete') as delete_mock:
                get_mock.side_effect = [[100], [101], [102], [103]]
                provider.apply(plan)
                wanted_calls = [
                    call('/domain/zone/unit.tests/record', fieldType='A',
                         subDomain='', target='1.2.3.4', ttl=100),
                    call('/domain/zone/unit.tests/record', fieldType='AAAA',
                         subDomain='', target='1:1ec:1::1', ttl=200),
                    call('/domain/zone/unit.tests/record', fieldType='MX',
                         subDomain='', target='10 mx1.unit.tests.', ttl=400),
                    call('/domain/zone/unit.tests/record', fieldType='SPF',
                         subDomain='',
                         target='v=spf1 include:unit.texts.redirect ~all',
                         ttl=1000),
                    call('/domain/zone/unit.tests/record', fieldType='SSHFP',
                         subDomain='',
                         target='1 1 bf6b6825d2977c511a475bbefb88aad54a92ac73',
                         ttl=1100),
                    call('/domain/zone/unit.tests/record', fieldType='PTR',
                         subDomain='4', target='unit.tests.', ttl=900),
                    call('/domain/zone/unit.tests/record', fieldType='SRV',
                         subDomain='_srv._tcp',
                         target='10 20 30 foo-1.unit.tests.', ttl=800),
                    call('/domain/zone/unit.tests/record', fieldType='SRV',
                         subDomain='_srv._tcp',
                         target='40 50 60 foo-2.unit.tests.', ttl=800),
                    call('/domain/zone/unit.tests/record', fieldType='CAA',
                         subDomain='caa', target='0 issue "ca.unit.tests"',
                         ttl=1600),
                    call('/domain/zone/unit.tests/record', fieldType='DKIM',
                         subDomain='dkim',
                         target='p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxLaG'
                         '16G4SaEcXVdiIxTg7gKSGbHKQLm30CHib1h9FzS9nkcyvQSyQj1r'
                         'MFyqC//tft3ohx3nvJl+bGCWxdtLYDSmir9PW54e5CTdxEh8MWRk'
                         'BO3StF6QG/tAh3aTGDmkqhIJGLb87iHvpmVKqURmEUzJPv5KPJfW'
                         'LofADI+q9lQIDAQAB', ttl=1300),
                    call('/domain/zone/unit.tests/record', fieldType='NAPTR',
                         subDomain='naptr',
                         target='10 100 "S" "SIP+D2U" "!^.*$!sip:[email protected]'
                         'ple.com!" .', ttl=500),
                    call('/domain/zone/unit.tests/record', fieldType='A',
                         subDomain='sub', target='1.2.3.4', ttl=200),
                    call('/domain/zone/unit.tests/record', fieldType='TXT',
                         subDomain='txt', target='TXT text', ttl=1400),
                    call('/domain/zone/unit.tests/record', fieldType='CNAME',
                         subDomain='www2', target='unit.tests.', ttl=300),
                    call('/domain/zone/unit.tests/record', fieldType='NS',
                         subDomain='www3', target='ns3.unit.tests.', ttl=700),
                    call('/domain/zone/unit.tests/record', fieldType='NS',
                         subDomain='www3', target='ns4.unit.tests.', ttl=700),
                    call('/domain/zone/unit.tests/refresh')]

                post_mock.assert_has_calls(wanted_calls)

                # Get for delete calls
                wanted_get_calls = [
                    call(u'/domain/zone/unit.tests/record', fieldType=u'A',
                         subDomain=u''),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'DKIM',
                         subDomain='dkim'),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'A',
                         subDomain='fake'),
                    call(u'/domain/zone/unit.tests/record', fieldType=u'TXT',
                         subDomain='txt')]
                get_mock.assert_has_calls(wanted_get_calls)
                # 4 delete calls for update and delete
                delete_mock.assert_has_calls(
                    [call(u'/domain/zone/unit.tests/record/100'),
                     call(u'/domain/zone/unit.tests/record/101'),
                     call(u'/domain/zone/unit.tests/record/102'),
                     call(u'/domain/zone/unit.tests/record/103')])