def test_get_user(self): """get_user() makes a GET request to /Users/<id>""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m uaac.get_user('foo') m.assert_called_with(urljoin('/Users', 'foo'), 'GET')
def test_get_user(self): """get_user() makes a GET request to /Users/<id>""" uaac = UAAClient("http://example.com", "foo", False) m = Mock() uaac._request = m uaac.get_user("foo") m.assert_called_with(urljoin("/Users", "foo"), "GET")
def test_change_password(self): """change_password() makes a PUT request to /Users/<id>/password""" uaac = UAAClient("http://example.com", "foo", False) m = Mock() uaac._request = m uaac.change_password("foo", "bar", "baz") m.assert_called_with( "/Users/foo/password", "PUT", body={"oldPassword": "******", "password": "******"} )
def test_request_bad(self, requests): """UAAError is reaised when it occurs""" r = Mock() r.status_code = 500 r.text = json.dumps({"error_description": "oh no"}) requests.get.return_value = r uaac = UAAClient("http://example.com", "foo", True) with self.assertRaises(UAAError): uaac._request("/bar", "GET") requests.get.assert_called_with( "http://example.com/bar", headers={"Authorization": "Bearer foo"}, json=None, params=None, auth=None, verify=True, )
def test_idps(self): """idps() makes a GET request to /identity-providers""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m uaac.idps(active_only=True) m.assert_called_with('/identity-providers', 'GET', params={'active_only': 'true'}) uaac.idps(active_only=False) m.assert_called_with('/identity-providers', 'GET', params={'active_only': 'false'})
def test_request_bad(self, requests): """UAAError is reaised when it occurs""" r = Mock() r.status_code = 500 r.text = json.dumps({'error_description': 'oh no'}) requests.get.return_value = r uaac = UAAClient('http://example.com', 'foo', True) with self.assertRaises(UAAError): uaac._request('/bar', 'GET') requests.get.assert_called_with( 'http://example.com/bar', headers={'Authorization': 'Bearer foo'}, json=None, params=None, auth=None, verify=True )
def test_put_user(self): """put_user() makes a PUT request to /Users/<id> with appropriate headers""" uaac = UAAClient("http://example.com", "foo", False) m = Mock() uaac._request = m user = {"id": "foo", "meta": {"version": "123"}} uaac.put_user(user) m.assert_called_with( urljoin("/Users", "foo"), "PUT", body=user, headers={"If-Match": "123"} )
def test_idps(self): """idps() makes a GET request to /identity-providers""" uaac = UAAClient("http://example.com", "foo", False) m = Mock() uaac._request = m uaac.idps(active_only=True) m.assert_called_with( "/identity-providers", "GET", params={"active_only": "true"} ) uaac.idps(active_only=False) m.assert_called_with( "/identity-providers", "GET", params={"active_only": "false"} )
def test_change_password(self): """change_password() makes a PUT request to /Users/<id>/password""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m uaac.change_password('foo', 'bar', 'baz') m.assert_called_with( '/Users/foo/password', 'PUT', body={ 'oldPassword': '******', 'password': '******' } )
def test_invite_users(self): """invite_users() makes a PUT request to /invite_users<id>""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m email = '*****@*****.**' redirect_uri = 'http://www.example.com' uaac.invite_users(email, redirect_uri) m.assert_called_with( '/invite_users', 'POST', body={'emails': [email]}, params={'redirect_uri': redirect_uri} )
def test_users_with_token(self): """invite_users() makes a PUT request to /invite_users<id>""" uaac = UAAClient("http://example.com", "foo", False) m = Mock() uaac._request = m email = "*****@*****.**" redirect_uri = "http://www.example.com" uaac.invite_users(email, redirect_uri, token="foobar") m.assert_called_with( "/invite_users", "POST", body={"emails": [email]}, headers={"Authorization": "Bearer foobar"}, params={"redirect_uri": redirect_uri}, )
def test_users_with_token(self): """invite_users() makes a PUT request to /invite_users<id>""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m email = '*****@*****.**' redirect_uri = 'http://www.example.com' uaac.invite_users(email, redirect_uri, token="foobar") m.assert_called_with( '/invite_users', 'POST', body={'emails': [email]}, headers={'Authorization': 'Bearer foobar'}, params={'redirect_uri': redirect_uri} )
def test_get_client_token(self): """_get_client_token() makes a POST to /oauth/token with the appropriate headers and query params""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m uaac._get_client_token('bar', 'baz') args, kwargs = m.call_args assert args == ('/oauth/token', 'POST') assert kwargs['params'] == { 'grant_type': 'client_credentials', 'response_type': 'token' } assert isinstance(kwargs['auth'], HTTPBasicAuth) assert kwargs['auth'].username == 'bar' assert kwargs['auth'].password == 'baz'
def test_get_client_token(self): """_get_client_token() makes a POST to /oauth/token with the appropriate headers and query params""" uaac = UAAClient("http://example.com", "foo", False) m = Mock() uaac._request = m uaac._get_client_token("bar", "baz") args, kwargs = m.call_args assert args == ("/oauth/token", "POST") assert kwargs["params"] == { "grant_type": "client_credentials", "response_type": "token", } assert isinstance(kwargs["auth"], HTTPBasicAuth) assert kwargs["auth"].username == "bar" assert kwargs["auth"].password == "baz"
def test_put_user(self): """put_user() makes a PUT request to /Users/<id> with appropriate headers""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m user = { 'id': 'foo', 'meta': { 'version': '123' } } uaac.put_user(user) m.assert_called_with( urljoin('/Users', 'foo'), 'PUT', body=user, headers={'If-Match': '123'} )
def test_request_post_body(self, requests): """Body is included in request if provided""" r = Mock() r.status_code = 200 r.text = json.dumps({'test': 'value'}) requests.post.return_value = r uaac = UAAClient('http://example.com', 'foo', False) resp = uaac._request('/bar', 'POST', body='hi') requests.post.assert_called_with( 'http://example.com/bar', headers={'Authorization': 'Bearer foo'}, json='hi', params=None, auth=None, verify=False ) assert resp['test'] == 'value'
def test_request_post_body(self, requests): """Body is included in request if provided""" r = Mock() r.status_code = 200 r.text = json.dumps({"test": "value"}) requests.post.return_value = r uaac = UAAClient("http://example.com", "foo", False) resp = uaac._request("/bar", "POST", body="hi") requests.post.assert_called_with( "http://example.com/bar", headers={"Authorization": "Bearer foo"}, json="hi", params=None, auth=None, verify=False, ) assert resp["test"] == "value"
def test_request_get_insecure(self, requests): """Insecure GET request is made""" r = Mock() r.status_code = 200 r.text = json.dumps({'test': 'value'}) requests.get.return_value = r uaac = UAAClient('http://example.com', 'foo', False) resp = uaac._request('/bar', 'GET') requests.get.assert_called_with( 'http://example.com/bar', headers={'Authorization': 'Bearer foo'}, json=None, params=None, auth=None, verify=False ) assert resp['test'] == 'value'
def test_request_get_auth(self, requests): """Auth value is passed directly to requests""" r = Mock() r.status_code = 200 r.text = json.dumps({'test': 'value'}) requests.get.return_value = r uaac = UAAClient('http://example.com', 'foo', False) resp = uaac._request('/bar', 'GET', auth='this should be basic') requests.get.assert_called_with( 'http://example.com/bar', headers={}, json=None, params=None, auth='this should be basic', verify=False ) assert resp['test'] == 'value'
def test_request_get_insecure(self, requests): """Insecure GET request is made""" r = Mock() r.status_code = 200 r.text = json.dumps({"test": "value"}) requests.get.return_value = r uaac = UAAClient("http://example.com", "foo", False) resp = uaac._request("/bar", "GET") requests.get.assert_called_with( "http://example.com/bar", headers={"Authorization": "Bearer foo"}, json=None, params=None, auth=None, verify=False, ) assert resp["test"] == "value"
def test_request_get_params(self, requests): """Query string is sent if params are provided""" r = Mock() r.status_code = 200 r.text = json.dumps({"test": "value"}) requests.get.return_value = r uaac = UAAClient("http://example.com", "foo", False) resp = uaac._request("/bar", "GET", params={"omg": "lol"}) requests.get.assert_called_with( "http://example.com/bar", headers={"Authorization": "Bearer foo"}, json=None, params={"omg": "lol"}, auth=None, verify=False, ) assert resp["test"] == "value"
def test_request_get_auth(self, requests): """Auth value is passed directly to requests""" r = Mock() r.status_code = 200 r.text = json.dumps({"test": "value"}) requests.get.return_value = r uaac = UAAClient("http://example.com", "foo", False) resp = uaac._request("/bar", "GET", auth="this should be basic") requests.get.assert_called_with( "http://example.com/bar", headers={}, json=None, params=None, auth="this should be basic", verify=False, ) assert resp["test"] == "value"
def test_request_get_params(self, requests): """Query string is sent if params are provided""" r = Mock() r.status_code = 200 r.text = json.dumps({'test': 'value'}) requests.get.return_value = r uaac = UAAClient('http://example.com', 'foo', False) resp = uaac._request('/bar', 'GET', params={'omg': 'lol'}) requests.get.assert_called_with( 'http://example.com/bar', headers={'Authorization': 'Bearer foo'}, json=None, params={'omg': 'lol'}, auth=None, verify=False ) assert resp['test'] == 'value'
def test_users(self): """users() makes a GET request to /Users""" uaac = UAAClient('http://example.com', 'foo', False) m = Mock() uaac._request = m uaac.users() m.assert_called_with('/Users', 'GET', params={'startIndex': 1}, headers={}) uaac.users(start=2) m.assert_called_with('/Users', 'GET', params={'startIndex': 2}, headers={}) uaac.users(list_filter='test filter') m.assert_called_with('/Users', 'GET', params={'filter': 'test filter', 'startIndex': 1}, headers={}) uaac.users(token='FOO') m.assert_called_with('/Users', 'GET', params={'startIndex': 1}, headers={'Authorization': 'Bearer FOO'}) uaac.users('test filter', 'FOO', 9) m.assert_called_with('/Users', 'GET', params={'filter': 'test filter', 'startIndex': 9}, headers={'Authorization': 'Bearer FOO'})
def test_users(self): """users() makes a GET request to /Users""" uaac = UAAClient("http://example.com", "foo", False) m = Mock() uaac._request = m uaac.users() m.assert_called_with("/Users", "GET", params={"startIndex": 1}, headers={}) uaac.users(start=2) m.assert_called_with("/Users", "GET", params={"startIndex": 2}, headers={}) uaac.users(list_filter="test filter") m.assert_called_with( "/Users", "GET", params={"filter": "test filter", "startIndex": 1}, headers={}, ) uaac.users(token="FOO") m.assert_called_with( "/Users", "GET", params={"startIndex": 1}, headers={"Authorization": "Bearer FOO"}, ) uaac.users("test filter", "FOO", 9) m.assert_called_with( "/Users", "GET", params={"filter": "test filter", "startIndex": 9}, headers={"Authorization": "Bearer FOO"}, )