Exemplo n.º 1
0
    def test_json_call_if_unknown_return_type(self):
        """check ServiceException raised if unknown return_type"""

        request = Request().withType('unknownType').domain('domaintools.com')

        transport                         = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns        = open(self.root_path + '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        request.set_transport(transport)

        self.assertTrue(json.loads(request.execute())!= None)
Exemplo n.º 2
0
    def test_json_call_if_unknown_return_type(self):
        """check ServiceException raised if unknown return_type"""

        request = Request().withType('unknownType').domain('domaintools.com')

        transport = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns = open(
            self.root_path +
            '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        request.set_transport(transport)

        self.assertTrue(json.loads(request.execute()) != None)
Exemplo n.º 3
0
    def test_add_credentials_for_unsecure_authentication(self):
        """check username and key are really added to options"""

        config = load_config_file(self.root_path+'/domaintools/conf/api.ini')
        configuration = Configuration(config)
        configuration.secure_auth = False

        request = Request(configuration)
        request.add_credentials_options()

        options = request.get_options()

        self.assertTrue(config['username']==options['api_username'] and config['key']==options['api_key'])
Exemplo n.º 4
0
        def test_bad_request_exception(self):
            """test BadRequestException raised for status code 400"""

            request = Request().withType('json').domain('domaintools')

            transport                         = Mock('RestService')
            transport.get_status.mock_returns = 400
            transport.get.mock_returns        = open(self.root_path + '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
            request.set_transport(transport)

            try:
                request.execute()
            except BadRequestException as e:
                self.assertTrue(True)
Exemplo n.º 5
0
    def test_add_credentials_for_unsecure_authentication(self):
        """check username and key are really added to options"""

        config = load_config_file(self.root_path + '/domaintools/conf/api.ini')
        configuration = Configuration(config)
        configuration.secure_auth = False

        request = Request(configuration)
        request.add_credentials_options()

        options = request.get_options()

        self.assertTrue(config['username'] == options['api_username']
                        and config['key'] == options['api_key'])
Exemplo n.º 6
0
    def test_service_exception_if_invalid_options(self):
        """check ServiceException raised if invalid options"""

        try:
            request = Request().where('invalidOptions').execute()
        except ServiceException as e:
            self.assertTrue(True)
Exemplo n.º 7
0
    def setUp(self):
        """to execute before each test"""

        self.root_path = os.path.realpath(os.curdir)
        self.configuration = Configuration(self.root_path +
                                           '/domaintools/conf/api.ini')
        self.request = Request(self.configuration)

        self.request.domain('domaintools.com')

        transport = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns = open(
            self.root_path +
            '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        self.request.set_transport(transport)

        self.response = self.request.execute()
Exemplo n.º 8
0
    def run(self):
        data = self.getData()

        if 'proxy' in self.artifact['config']:
            del self.artifact['config']['proxy']

        if self.service == 'reverse-ip' and self.data_type == 'ip':
            self.service = 'host-domains'

        if self.service == 'reverse-whois':
            query = {}
            query['terms'] = data
            query['mode'] = "purchase"
            data = ''
        else:
            query = {}

        if (self.service == 'reverse-ip' and self.data_type == 'domain') or \
                (self.service == 'host-domains' and self.data_type == 'ip') or \
                (self.service == 'name-server-domains' and self.data_type == 'domain') or \
                (self.service == 'whois/history' and self.data_type == 'domain') or \
                (self.service == 'whois/parsed' and self.data_type == 'domain') or \
                (self.service == 'reverse-whois') or \
                (self.service == 'whois' and self.data_type == 'ip'):
            response = {}

            try:
                configuration = Configuration(self.getParam('config'))
                response = Request(configuration).service(
                    self.service).domain(data).where(query).toJson().execute()

                r = json.loads(response)
                if 'response' in r:
                    self.report(r['response'])
                elif 'error' in r and 'message' in r['error']:
                    self.error(r['error']['message'])
                else:
                    self.report(r)

            except NotFoundException:
                self.error(self.data_type.capitalize() + " not found")
            except NotAuthorizedException:
                self.error("An authorization error occurred")
            except ServiceUnavailableException:
                self.error("DomainTools Service is currenlty unavailable")
            except Exception as e:
                self.unexpectedError(e)

        else:
            self.error('Unknown DomainTools service or invalid data type')
Exemplo n.º 9
0
    def setUp(self):
        """to execute before each test"""

        self.root_path     = os.path.realpath(os.curdir)
        self.configuration = Configuration(self.root_path+'/domaintools/conf/api.ini')
        self.request       = Request(self.configuration)

        self.request.domain('domaintools.com')

        transport                         = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns        = open(self.root_path + '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        self.request.set_transport(transport)

        self.response                     = self.request.execute()
Exemplo n.º 10
0
class TestResponse(unittest.TestCase):

    def setUp(self):
        """to execute before each test"""

        self.root_path     = os.path.realpath(os.curdir)
        self.configuration = Configuration(self.root_path+'/domaintools/conf/api.ini')
        self.request       = Request(self.configuration)

        self.request.domain('domaintools.com')

        transport                         = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns        = open(self.root_path + '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        self.request.set_transport(transport)

        self.response                     = self.request.execute()



    def test_request_attached_to_response(self):
        """check Request instance has been attached to Response instance"""

        self.assertTrue(self.request == self.response.request)
Exemplo n.º 11
0
class TestResponse(unittest.TestCase):
    def setUp(self):
        """to execute before each test"""

        self.root_path = os.path.realpath(os.curdir)
        self.configuration = Configuration(self.root_path +
                                           '/domaintools/conf/api.ini')
        self.request = Request(self.configuration)

        self.request.domain('domaintools.com')

        transport = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns = open(
            self.root_path +
            '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        self.request.set_transport(transport)

        self.response = self.request.execute()

    def test_request_attached_to_response(self):
        """check Request instance has been attached to Response instance"""

        self.assertTrue(self.request == self.response.request)
Exemplo n.º 12
0
    def test_not_authorized_request_exception(self):
        """test NotAuthorizedException raised for status code 403"""

        request = Request().withType('json').domain('domaintools')

        transport = Mock('RestService')
        transport.get_status.mock_returns = 403
        transport.get.mock_returns = open(
            self.root_path +
            '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        request.set_transport(transport)

        try:
            request.execute()
        except NotAuthorizedException as e:
            self.assertTrue(True)

        def test_bad_request_exception(self):
            """test BadRequestException raised for status code 400"""

            request = Request().withType('json').domain('domaintools')

            transport = Mock('RestService')
            transport.get_status.mock_returns = 400
            transport.get.mock_returns = open(
                self.root_path +
                '/tests/fixtures/domain-profile/domaintools.com/good.json'
            ).read()
            request.set_transport(transport)

            try:
                request.execute()
            except BadRequestException as e:
                self.assertTrue(True)

        def test_not_found_request_exception(self):
            """test NotFoundException raised for status code 404"""

            request = Request().withType('json').domain('domaintools')

            transport = Mock('RestService')
            transport.get_status.mock_returns = 404
            transport.get.mock_returns = open(
                self.root_path +
                '/tests/fixtures/domain-profile/domaintools.com/good.json'
            ).read()
            request.set_transport(transport)

            try:
                request.execute()
            except NotFoundRequestException as e:
                self.assertTrue(True)
Exemplo n.º 13
0
    def test_transport_called_on_get(self):
        """test transport is really called"""

        configuration = Configuration(self.root_path +
                                      "/domaintools/conf/api.ini")
        request = Request(configuration)
        request.withType('json').domain('domaintools.com')

        transport = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns = open(
            self.root_path +
            '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        request.set_transport(transport)

        try:
            request.execute()
        except Exception as e:
            pass

        self.assertTrue(transport.get_status() == 200)
Exemplo n.º 14
0
    def test_transport_called_on_get(self):
        """test transport is really called"""

        configuration = Configuration(self.root_path + "/domaintools/conf/api.ini")
        request       = Request(configuration)
        request.withType('json').domain('domaintools.com')

        transport                         = Mock('RestService')
        transport.get_status.mock_returns = 200
        transport.get.mock_returns        = open(self.root_path + '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        request.set_transport(transport)

        try:
            request.execute()
        except Exception as e:
            pass

        self.assertTrue(transport.get_status()==200)
Exemplo n.º 15
0
    def test_service_unavailable_exception(self):
        """test ServiceUnavailableException raised for status code 503"""

        request = Request().withType('json').domain('domaintools')

        transport = Mock('RestService')
        transport.get_status.mock_returns = 503
        transport.get.mock_returns = open(
            self.root_path +
            '/tests/fixtures/domain-profile/domaintools.com/good.json').read()
        request.set_transport(transport)

        try:
            request.execute()
        except ServiceUnavailableException as e:
            self.assertTrue(True)
Exemplo n.º 16
0
    query['mode'] = "purchase"
    data = ''
else:
    query = {}

if (service == 'reverse-ip' and data_type == 'domain') or \
        (service == 'host-domains' and data_type == 'ip') or \
        (service == 'name-server-domains' and data_type == 'domain') or \
        (service == 'whois/history' and data_type == 'domain') or \
        (service == 'whois/parsed' and data_type == 'domain') or \
        (service == 'reverse-whois') or\
        (service == 'whois' and data_type == 'ip'):

    response = {}
    try:
        response = Request(Configuration(get_param('config'))).service(
            service).domain(data).where(query).toJson().execute()
    except NotFoundException:
        error(data_type.capitalize() + " not found")
    except NotAuthorizedException:
        error("An authorization error occurred")
    except:
        error("An unexpected error occurred: " + str(sys.exc_info()[0]) + ":" +
              str(sys.exc_info()[1]))

    r = json.loads(response)
    if 'response' in r:
        json.dump(r['response'], sys.stdout, ensure_ascii=False)
    elif 'error' in r and 'message' in r['error']:
        error(r['error']['message'])
    else:
        json.dump(r, sys.stdout, ensure_ascii=False)
Exemplo n.º 17
0
 def test_toJson_sets_json_as_returnType(self):
     """ check toJson sets json as returnType"""
     request = Request().toJson()
     self.assertTrue(request.get_return_type() == 'json')
Exemplo n.º 18
0
    def test_toHtml_sets_html_as_returnType(self):
        """ check toHtml sets html as returnType"""

        request = Request().toHtml()
        self.assertTrue(request.get_return_type() == 'html')
Exemplo n.º 19
0
    def test_toHtml_sets_html_as_returnType(self):
        """ check toHtml sets html as returnType"""

        request = Request().toHtml()
        self.assertTrue(request.get_return_type() == 'html')
Exemplo n.º 20
0
 def test_toJson_sets_json_as_returnType(self):
     """ check toJson sets json as returnType"""
     request = Request().toJson()
     self.assertTrue(request.get_return_type() == 'json')
from domaintools.api.request       import Request

"""
 EXAMPLE - 2 different calls bringing the same result
 service: whois
 type   : json
 domain : domaintools.com
"""
if __name__ == "__main__":

    # call 1
    print Request().service('whois').domain('domaintools.com').withType('json').execute() + '\n'

    # call 2
    print Request().service('whois').domain('domaintools.com').toJson().execute() + '\n'

Exemplo n.º 22
0
from domaintools.api.request       import Request

if __name__ == "__main__":
	 
    print Request().domain('domaintools.com').toJson().execute()