def setUp(self): self.rpc_test_double = "{0}".format(random_string(40)) self.rpc_test_datetime = "{0}".format(random_string(40)) self.rpc_test_throw_exception = "{0}".format(random_string(40)) AdvancedHTTPServerRegisterPath("/{0}".format(self.rpc_test_double), self.handler_class.__name__, is_rpc=True)(self._rpc_test_double_handler) AdvancedHTTPServerRegisterPath("/{0}".format(self.rpc_test_datetime), self.handler_class.__name__, is_rpc=True)(self._rpc_test_datetime_handler) AdvancedHTTPServerRegisterPath("/{0}".format(self.rpc_test_throw_exception), self.handler_class.__name__, is_rpc=True)(self._rpc_test_throw_exception) super(AdvancedHTTPServerTests, self).setUp()
def test_authentication_hash(self): username = random_string(8) password = random_string(12) password_hash = hashlib.new('md5', password.encode('utf-8')).hexdigest() self.server.auth_add_creds(username, password_hash, 'md5') self._test_authentication(username, password) password_hash = hashlib.new('md5', password.encode('utf-8')).digest() self.server.auth_add_creds(username, password_hash, 'md5') self._test_authentication(username, password)
def test_rpc_hmac(self): hmac = random_string(16) self.server.rpc_hmac_key = hmac rpc = self.build_rpc_client() self.assertRaisesRegex(AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(hmac_key=random_string(16)) self.assertRaisesRegex(AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(hmac_key=hmac) self.run_rpc_tests(rpc)
def test_rpc_authentication(self): username = random_string(8) password = random_string(12) self.server.auth_add_creds(username, password) rpc = self.build_rpc_client() self.assertRaisesRegex(AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(username=username, password=random_string(12)) self.assertRaisesRegex(AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(username=username, password=password) self.run_rpc_tests(rpc)
def test_authentication_bad_credentials(self): self.server.auth_add_creds(random_string(8), random_string(12)) auth_headers = { 'Authorization': 'Basic ' + base64.b64encode("{0}:{1}".format( random_string(8), random_string(12)).encode('utf-8')).decode('utf-8') } response = self.http_request(self.test_resource, 'GET', headers=auth_headers) self.assertHTTPStatus(response, 401)
def test_rpc_hmac(self): hmac = random_string(16) self.server.rpc_hmac_key = hmac rpc = self.build_rpc_client() self.assertRaisesRegex( AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(hmac_key=random_string(16)) self.assertRaisesRegex( AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(hmac_key=hmac) self.run_rpc_tests(rpc)
def test_rpc_authentication(self): username = random_string(8) password = random_string(12) self.server.auth_add_creds(username, password) rpc = self.build_rpc_client() self.assertRaisesRegex( AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(username=username, password=random_string(12)) self.assertRaisesRegex( AdvancedHTTPServerRPCError, r'the server responded with 401 \'Unauthorized\'', self.run_rpc_tests, rpc) rpc = self.build_rpc_client(username=username, password=password) self.run_rpc_tests(rpc)
def setUp(self): self.rpc_test_double = "{0}".format(random_string(40)) self.rpc_test_datetime = "{0}".format(random_string(40)) self.rpc_test_throw_exception = "{0}".format(random_string(40)) AdvancedHTTPServerRegisterPath("/{0}".format(self.rpc_test_double), self.handler_class.__name__, is_rpc=True)( self._rpc_test_double_handler) AdvancedHTTPServerRegisterPath("/{0}".format(self.rpc_test_datetime), self.handler_class.__name__, is_rpc=True)( self._rpc_test_datetime_handler) AdvancedHTTPServerRegisterPath( "/{0}".format(self.rpc_test_throw_exception), self.handler_class.__name__, is_rpc=True)(self._rpc_test_throw_exception) super(AdvancedHTTPServerTests, self).setUp()
def test_fake_resource(self): response = self.http_request('/' + random_string(30), 'GET') self.assertHTTPStatus(response, 404) response = self.http_request('/' + random_string(30), 'POST') self.assertHTTPStatus(response, 404)
def test_authentication_bad_credentials(self): self.server.auth_add_creds(random_string(8), random_string(12)) auth_headers = {'Authorization': 'Basic ' + base64.b64encode("{0}:{1}".format(random_string(8), random_string(12)).encode('utf-8')).decode('utf-8')} response = self.http_request(self.test_resource, 'GET', headers=auth_headers) self.assertHTTPStatus(response, 401)
def test_authentication_plain(self): username = random_string(8) password = random_string(12) self.server.auth_add_creds(username, password) self._test_authentication(username, password)