def test_connection_auth(self, mock_method): mock_method.return_value = {'result': 'success'} params = { 'username': self.username, 'auth_data': self.password, 'auth_method': self.auth_method, 'apikey': self.apikey } connection = Connection(self.server) self.assertFalse(connection._is_authed) connection.auth(self.username, self.password, self.auth_method, self.apikey) connection.make_request.assert_called_once_with("auth", params) self.assertTrue(connection._is_authed) mock_method.return_value = {'result': 'exception', 'exception': { 'message': 'Auth', 'id': 1, 'type': 'HolviAuthException' } } mock_method.reset_mock() with self.assertRaises(HolviAuthException): connection.auth(self.username, self.password, self.auth_method, self.apikey) connection.make_request.assert_called_once_with("auth", params) self.assertFalse(connection._is_authed)
def test_connection_make_request(self, MockJSONDecoder, MockUrllib): mock_instance1 = mock.Mock() mock_instance1.open.return_value = StringIO.StringIO("Test response") MockUrllib.return_value = mock_instance1 mock_instance2 = mock.Mock() mock_instance2.decode.return_value = {'result': 'success'} MockJSONDecoder.return_value = mock_instance2 connection = Connection(self.server) response = connection.make_request('method', 'params') self.assertEquals({'result': 'success'}, response) mock_instance1.open.assert_called_once_with(self.server + '/api/1.0/json', '{"params": "params", "method": "method"}') mock_instance2.decode.assert_called_once_with("Test response")
def test_connection_make_transaction(self, MockUrllib): mock_instance1 = mock.Mock() mock_instance1.headers = {'X-HOLVI-RESULT': 'OK'} MockUrllib.return_value = mock_instance1 headers = {} headers['Test-header'] = 'value' connection = Connection(self.server) response = connection.make_transaction(headers, '/fetch') self.assertEquals(MockUrllib.call_args[0][0].headers, headers) self.assertEquals(response.headers['X-HOLVI-RESULT'], 'OK') mock_instance1.headers = {'X-HOLVI-RESULT': 'ERROR: 404 Not found'} with self.assertRaises(HolviDataItemException): response = connection.make_transaction(headers, '/fetch')