def test_instantiate_with_username_and_password_noindex(self): # Try to ceate client instance with username and password with self.assertRaises(HandleSyntaxError): inst = RESTHandleClient.instantiate_with_username_and_password( 'someurl', 'johndoe', 'passywordy')
def create_required_test_handles(self): # Creating an instance that knows how to write: pw = self.testvalues['password'] inst = RESTHandleClient.instantiate_with_username_and_password( self.testvalues['handle_server_url_write'], self.user, pw, HTTPS_verify=self.https_verify) authstring = pyhandle.utilhandle.create_authentication_string(self.user, pw) headers = { 'Content-Type': 'application/json', 'Authorization': 'Basic '+authstring } list_of_all_entries = [ { "index":100, "type":"HS_ADMIN", "data":{ "format":"admin", "value":{ "handle":"21.T14999/B2HANDLE_INTEGRATION_TESTS", "index":300, "permissions":"011111110011" } } }, { "index":111, "type":"TEST1", "data":"val1" }, { "index":2222, "type":"TEST2", "data":"val2" }, { "index":333, "type":"TEST3", "data":"val3" }, { "index":4, "type":"TEST4", "data":"val4" } ] testhandle = self.handle url = self.testvalues['handle_server_url_write']+self.testvalues['url_extension_REST_API']+testhandle veri = self.https_verify head = headers data = json.dumps({'values':list_of_all_entries}) resp = requests.put(url, data=data, headers=head, verify=veri)
def test_instantiate_with_username_and_password_inexistentuser(self, getpatch): # Define the replacement for the patched method: mock_response = MockResponse(notfound=True) getpatch.return_value = mock_response with self.assertRaises(HandleNotFoundException): inst = RESTHandleClient.instantiate_with_username_and_password( 'http://someurl', '100:john/doe', 'passywordy')
def test_instantiate_with_username_and_wrong_password(self): """Test instantiation of client: No exception if password wrong.""" # Create client instance with username and password inst = RESTHandleClient.instantiate_with_username_and_password( self.url, self.user, self.randompassword, HTTPS_verify=self.https_verify) self.assertIsInstance(inst, RESTHandleClient)
def test_instantiate_with_username_and_password_wrongpw(self, getpatch): # Define the replacement for the patched method: mock_response = MockResponse(success=True) getpatch.return_value = mock_response inst = RESTHandleClient.instantiate_with_username_and_password( 'http://someurl', '100:my/testhandle', 'passywordy') self.assertIsInstance(inst, RESTHandleClient)
def test_instantiate_with_nonexistent_username_and_password(self): """Test instantiation of client: Exception if username does not exist.""" testusername_inexistent = '100:'+self.inexistent_handle # Run code to be tested + check exception: with self.assertRaises(HandleNotFoundException): # Create client instance with username and password inst = RESTHandleClient.instantiate_with_username_and_password( self.url, testusername_inexistent, self.randompassword, HTTPS_verify=self.https_verify)
def test_instantiate_with_username_without_index_and_password(self): """Test instantiation of client: Exception if username has no index.""" testusername_without_index = self.user.split(':')[1] # Run code to be tested + check exception: with self.assertRaises(HandleSyntaxError): # Create client instance with username and password inst = RESTHandleClient.instantiate_with_username_and_password( self.url, testusername_without_index, self.randompassword, HTTPS_verify=self.https_verify)
def setUp(self): REQUESTLOGGER.info("\n" + 60 * "*" + "\nsetUp of RESTHandleClientWriteaccessTestCase") self.inst = RESTHandleClient.instantiate_with_username_and_password( self.url, self.user, self.password, HTTPS_verify=self.https_verify, handleowner=self.user) authstring = pyhandle.utilhandle.create_authentication_string( self.user, self.password) self.headers = { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + authstring } list_of_all_entries = [ { "index": 100, "type": "HS_ADMIN", "data": { "format": "admin", "value": { "handle": "21.T14999/B2HANDLE_INTEGRATION_TESTS", "index": 300, "permissions": "011111110011" } } }, { "index": 111, "type": "TEST1", "data": { "format": "string", "value": "val1" } }, { "index": 2222, "type": "TEST2", "data": { "format": "string", "value": "val2" } }, { "index": 333, "type": "TEST3", "data": { "format": "string", "value": "val3" } }, { "index": 4, "type": "TEST4", "data": { "format": "string", "value": "val4" } }, ] testhandle = self.handle url = self.connector.make_handle_URL(testhandle) veri = self.https_verify head = self.headers data = json.dumps({'values': list_of_all_entries}) resp = requests.put(url, data=data, headers=head, verify=veri) log_request_response_to_file('PUT', self.handle, url, head, veri, resp)