def test_domain_token_auth(self, client):
        domain_name = "dnsimple-domain-token.test"

        domain = client.add_domain(domain_name)
        assert domain

        token_client = DNSimple(domain_token=domain["domain"]["token"], sandbox=True)

        with pytest.raises(DNSimpleException) as exception:
            token_client.domains()

        assert "Authentication failed" in str(exception.value)
        assert token_client.domain(domain_name)["domain"]["name"] == domain_name

        client.delete(domain_name)

        assert len(client.domains()) == 0
Exemple #2
0
    def test_domain_token_auth(self, client):
        domain_name = 'dnsimple-domain-token.test'

        domain = client.add_domain(domain_name)
        assert domain

        token_client = DNSimple(domain_token=domain['domain']['token'],
                                sandbox=True)

        with pytest.raises(DNSimpleException) as exception:
            token_client.domains()

        assert 'Authentication failed' in str(exception.value)
        assert token_client.domain(
            domain_name)['domain']['name'] == domain_name

        client.delete(domain_name)

        assert len(client.domains()) == 0
Exemple #3
0
class DNSimpleV1():
    """class which uses dnsimple-python < 2"""

    def __init__(self, account_email, account_api_token, sandbox, module):
        """init"""
        self.module = module
        self.account_email = account_email
        self.account_api_token = account_api_token
        self.sandbox = sandbox
        self.dnsimple_client()

    def dnsimple_client(self):
        """creates a dnsimple client object"""
        if self.account_email and self.account_api_token:
            self.client = DNSimple(sandbox=self.sandbox, email=self.account_email, api_token=self.account_api_token)
        else:
            self.client = DNSimple(sandbox=self.sandbox)

    def get_all_domains(self):
        """returns a list of all domains"""
        domain_list = self.client.domains()
        return [d['domain'] for d in domain_list]

    def get_domain(self, domain):
        """returns a single domain by name or id"""
        try:
            dr = self.client.domain(domain)['domain']
        except DNSimpleException as e:
            exception_string = str(e.args[0]['message'])
            if re.match(r"^Domain .+ not found$", exception_string):
                dr = None
            else:
                raise
        return dr

    def create_domain(self, domain):
        """create a single domain"""
        return self.client.add_domain(domain)['domain']

    def delete_domain(self, domain):
        """delete a single domain"""
        self.client.delete(domain)

    def get_records(self, domain, dnsimple_filter=None):
        """return dns ressource records which match a specified filter"""
        return [r['record'] for r in self.client.records(str(domain), params=dnsimple_filter)]

    def delete_record(self, domain, rid):
        """delete a single dns ressource record"""
        self.client.delete_record(str(domain), rid)

    def update_record(self, domain, rid, ttl=None, priority=None):
        """update a single dns ressource record"""
        data = {}
        if ttl:
            data['ttl'] = ttl
        if priority:
            data['priority'] = priority
        return self.client.update_record(str(domain), str(rid), data)['record']

    def create_record(self, domain, name, record_type, content, ttl=None, priority=None):
        """create a single dns ressource record"""
        data = {
            'name': name,
            'type': record_type,
            'content': content,
        }
        if ttl:
            data['ttl'] = ttl
        if priority:
            data['priority'] = priority
        return self.client.add_record(str(domain), data)['record']