def test_host(self): client = GenericClient(url='http://dummy.org') self.assertEqual(client.host, 'dummy.org') client = GenericClient(url='http://dummy.org:8000') self.assertEqual(client.host, 'dummy.org:8000') client = GenericClient(url='http://dummy.org:8000/api') self.assertEqual(client.host, 'dummy.org:8000') client = GenericClient(url='http://dummy.org/api') self.assertEqual(client.host, 'dummy.org')
def test_403_auth(self): generic_client = GenericClient( url=MOCK_API_URL, auth=('myusername', 'password'), ) with responses.RequestsMock() as rsps: rsps.add(responses.GET, MOCK_API_URL + '/users', status=403) with self.assertRaises( generic_client.NotAuthenticatedError) as excinfo: generic_client.users.all() assert 'myusername' in str(excinfo.exception)
def test_invalid_data(self): client = GenericClient(url='http://dummy.org') with responses.RequestsMock() as rsps: rsps.add( responses.GET, 'http://dummy.org/users', body='[not json]', headers={'Content-Type': 'application/json'}, ) with self.assertRaises(ValueError) as excinfo: client.users.all() assert '[not json]' in str(excinfo.exception)
from unittest import TestCase import responses from genericclient import GenericClient MOCK_API_URL = 'http://dummy.org' generic_client = GenericClient(url=MOCK_API_URL) def request_callback(request): return (200, {}, request.body) class EndpointTestCase(TestCase): def test_endpoint_detail_route(self): with responses.RequestsMock() as rsps: rsps.add_callback( responses.POST, MOCK_API_URL + '/users/2/notify', callback=request_callback, content_type='application/json', ) response = generic_client.users(id=2).notify(unread=3) self.assertEqual(response.data, {'unread': 3}) with responses.RequestsMock() as rsps: rsps.add_callback( responses.GET,
def test_401(self): generic_client = GenericClient(url=MOCK_API_URL, ) with responses.RequestsMock() as rsps: rsps.add(responses.GET, MOCK_API_URL + '/users', status=401) with self.assertRaises(generic_client.NotAuthenticatedError): generic_client.users.all()
from unittest import TestCase import responses from genericclient import GenericClient MOCK_API_URL = 'http://dummy.org' generic_client = GenericClient(url=MOCK_API_URL, trailing_slash=True) # Create your tests here. class ResourceTestCase(TestCase): def test_resource_delete(self): with responses.RequestsMock() as rsps: rsps.add(responses.GET, MOCK_API_URL + '/users/1/', json={ 'id': 1, 'username': '******', 'group': 'watchers', }) user1 = generic_client.users.get(id=1) self.assertEqual(user1.username, 'user1') with responses.RequestsMock() as rsps: rsps.add(responses.DELETE, MOCK_API_URL + '/users/1/', status=204) user1.delete()
def test_session(self): client = GenericClient(url='http://dummy.org', auth=('username', 'password')) self.assertEqual(client.session.auth[0], 'username')