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 = EUDATHandleClient.instantiate_with_credentials(credentials) self.assertIsInstance(inst, EUDATHandleClient)
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 = EUDATHandleClient.instantiate_with_credentials(credentials) self.assertIsInstance(inst, EUDATHandleClient)
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 = EUDATHandleClient.instantiate_with_credentials( credentials, REST_API_url_extension='bar/bar/bar') self.assertIsInstance(inst, EUDATHandleClient) # 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)
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": "test2", "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 = EUDATHandleClient.instantiate_with_credentials( credentials, REST_API_url_extension='bar/bar/bar') self.assertIsInstance(inst, EUDATHandleClient) # 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)