def test_instantiate_with_credentials_config(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = mock.MagicMock() config_from_cred = {} valuefoo = 'foo/foo/foo/' config_from_cred['REST_API_url_extension'] = valuefoo credentials = pyhandle.clientcredentials.PIDClientCredentials( handle_server_url=self.url, username=self.user, password=self.randompassword, handleowner=self.user, REST_API_url_extension=valuefoo ) self.assertEqual(credentials.get_config()['REST_API_url_extension'],valuefoo, 'Config: '+str(credentials.get_config())) # foo/foo/ from the credentials should override default api/handles/ # Run code to be tested - we expect an exception, as it will try to do a GET on the bogus rest api: with self.assertRaises(GenericHandleError): inst = RESTHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify) # So this code can only be reached if something went wrong: self.assertIsInstance(inst, RESTHandleClient) # Check if foo/foo instead of api/handles was stored as path! serverconn = inst._EUDATHandleClient__handlesystemconnector self.assertIn('/foo/', serverconn._HandleSystemConnector__REST_API_url_extension) self.assertEquals(serverconn._HandleSystemConnector__REST_API_url_extension, valuefoo)
def test_instantiate_with_credentials_inexistentuser(self): """Test instantiation of client: Exception if username does not exist.""" # Test variables testusername_inexistent = '100:'+self.inexistent_handle credentials = pyhandle.clientcredentials.PIDClientCredentials( handle_server_url=self.url, username=testusername_inexistent, password=self.randompassword) # Run code to be tested + check exception: # Create instance with credentials with self.assertRaises(HandleNotFoundException): inst = RESTHandleClient.instantiate_with_credentials(credentials, HTTPS_verify=self.https_verify)
def test_instantiate_with_credentials(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = pyhandle.clientcredentials.PIDClientCredentials( handle_server_url=self.url, username=self.user, password=self.randompassword) # Run code to be tested # Create instance with credentials inst = RESTHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify) # Check desired outcomes self.assertIsInstance(inst, RESTHandleClient)
def test_instantiate_with_credentials_config(self, checkpatch): """Test instantiation of client: No exception if password wrong.""" # Define the replacement for the patched method: mock_response = MockResponse(success=True) checkpatch.return_value = mock_response # Test variables credentials = MockCredentials(restapi='foobar') self.assertEqual(credentials.get_config()['REST_API_url_extension'],'foobar', 'Config: '+str(credentials.get_config())) # Run code to be tested # Create instance with credentials inst = RESTHandleClient.instantiate_with_credentials(credentials) self.assertIsInstance(inst, RESTHandleClient)
def test_instantiate_with_credentials_inexistentuser(self, getpatch): """Test instantiation of client: Exception if username does not exist.""" # Define the replacement for the patched method: mock_response = MockResponse(notfound=True) getpatch.return_value = mock_response # Test variables testusername_inexistent = '100:john/doe' credentials = pyhandle.clientcredentials.PIDClientCredentials( handle_server_url='some/url', username=testusername_inexistent, password='******') # Run code to be tested + check exception: # Create instance with credentials with self.assertRaises(HandleNotFoundException): inst = RESTHandleClient.instantiate_with_credentials(credentials)
def test_instantiate_with_credentials(self, getpatch): """Test instantiation of client: No exception if password wrong.""" # Define the replacement for the patched method: mock_response = MockResponse(success=True) getpatch.return_value = mock_response # Test variables credentials = pyhandle.clientcredentials.PIDClientCredentials( handle_server_url='some/url', username='******', password='******') # Run code to be tested # Create instance with credentials inst = RESTHandleClient.instantiate_with_credentials(credentials) # Check desired outcomes self.assertIsInstance(inst, RESTHandleClient)
def test_instantiate_with_credentials_config_override(self, getpatch, checkpatch): """Test instantiation of client: We pass a config value in the credentials and also as an arg in the instantiation. We want the latter to override the first one. """ # Define the replacement for the patched method: # We pretend the username exists! mock_response = MockResponse(success=True) checkpatch.return_value = mock_response # Define the replacement for the patched GET: cont = {"responseCode":1,"handle":"my/testhandle","values":[{"index":111,"type":"TEST1","data":{"format":"string","value":"val1"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"},{"index":2222,"type":"TEST2","data":{"format":"string","value":"val2"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"},{"index":333,"type":"TEST3","data":{"format":"string","value":"val3"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"},{"index":4,"type":"TEST4","data":{"format":"string","value":"val4"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"}]} mock_response = MockResponse(success=True, content=json.dumps(cont)) getpatch.return_value = mock_response # Test variables # Passing mock credentials, give them the value "foobar", which # should be overridden! credentials = MockCredentials(restapi='foobar') self.assertEqual(credentials.get_config()['REST_API_url_extension'],'foobar', 'Config: '+str(credentials.get_config())) # Run code to be tested # Create instance with credentials. It gets the "REST_API_url_extention" # from both the credentials and as a param. inst = RESTHandleClient.instantiate_with_credentials( credentials, REST_API_url_extension='bar/bar/bar') self.assertIsInstance(inst, RESTHandleClient) # How to know now which one was used? # Call a read and check its url! Did it get foobar or barbarbar appended? inst.get_value_from_handle('my/testhandle', 'key') positional_args_passed = getpatch.call_args_list[len(getpatch.call_args_list)-1][0] passed_url = positional_args_passed[0] # Compare with expected URL: self.assertIn('bar/bar/bar',passed_url, 'bar/bar/bar is not specified in the URL '+passed_url)