def test_additional_property(self): httpretty.register_uri(httpretty.GET, self.base_href + "/tenants/current", location=self.tenant_href, status=302) httpretty.register_uri(httpretty.GET, self.tenant_href, body=json.dumps(self.tenant_body), content_type="application/json") httpretty.register_uri(httpretty.GET, self.dir_href, body=json.dumps(self.dir_body), content_type="application/json") expansion = Expansion('accounts', 'groups') expansion.add_property('applications', offset=0, limit=5) directory = self.client.directories.get(self.dir_href, expansion) directory.name # replace the comma inside parentheses to split easier path = re.sub('(applications\%28.*)(\%2C)(.*\%29)', '\g<1>;\g<3>', HTTPretty.last_request.path) params = path.split('expand=')[1].split('%2C') self.assertTrue( sorted(params) == sorted(['applications%28offset%3A0;limit%3A5%29', 'accounts', 'groups']) or sorted(params) == sorted(['applications%28limit%3A5;offset%3A0%29', 'accounts', 'groups']) )
def test_pagination(self): httpretty.register_uri(httpretty.GET, self.base_href + "/tenants/current", location=self.tenant_href, status=302) httpretty.register_uri(httpretty.GET, self.tenant_href, body=json.dumps(self.tenant_body), content_type="application/json") httpretty.register_uri(httpretty.GET, self.dir_href, body=json.dumps(self.dir_body), content_type="application/json") expansion = Expansion() expansion.add_property('groups', limit=10) directory = self.client.directories.get(self.dir_href, expansion) directory.name self.assertTrue( self.dir_path + '?expand=groups%28limit%3A10%29' == HTTPretty.last_request.path)
def test_additional_property(self): httpretty.register_uri(httpretty.GET, self.base_href + "/tenants/current", location=self.tenant_href, status=302) httpretty.register_uri(httpretty.GET, self.tenant_href, body=json.dumps(self.tenant_body), content_type="application/json") httpretty.register_uri(httpretty.GET, self.dir_href, body=json.dumps(self.dir_body), content_type="application/json") expansion = Expansion('accounts', 'groups') expansion.add_property('applications', offset=0, limit=5) directory = self.client.directories.get(self.dir_href, expansion) directory.name # replace the comma inside parentheses to split easier path = re.sub('(applications\%28.*)(\%2C)(.*\%29)', '\g<1>;\g<3>', HTTPretty.last_request.path) params = path.split('expand=')[1].split('%2C') self.assertTrue( sorted(params) == sorted([ 'applications%28offset%3A0;limit%3A5%29', 'accounts', 'groups' ]) or sorted(params) == sorted([ 'applications%28limit%3A5;offset%3A0%29', 'accounts', 'groups' ]))
def test_authenticate_with_expansion_succeeds(self): authenticator = JwtAuthenticator(self.app) expansion = Expansion() expansion.add_property('account') result = authenticator.authenticate( self.access_token.token, expand=expansion) self.assertIsInstance(result, AuthToken) self.assertEqual(result.account.href, self.acc.href) self.assertEqual(result.application.href, self.app.href) self.assertEqual(result.jwt, self.access_token.token) self.assertTrue('claims' in result.expanded_jwt)
def test_authenticate(self): httpretty.register_uri(httpretty.GET, self.base_href + "/tenants/current", location=self.tenant_href, status=302) httpretty.register_uri(httpretty.GET, self.tenant_href, body=json.dumps(self.tenant_body), content_type="application/json") httpretty.register_uri(httpretty.GET, self.app_href, body=json.dumps(self.app_body), content_type="application/json") body = {"account": {"href": self.acc_href}, "attr": {"key": "value"}} httpretty.register_uri(httpretty.POST, self.app_href + "/loginAttempts", body=json.dumps(body), content_type="application/json") httpretty.register_uri(httpretty.GET, self.acc_href, body=json.dumps(body), content_type="application/json") application = self.client.applications.get(self.app_href) result = application.authenticate_account("USERNAME", "PAŠVORD") account = result.account self.assertEqual(HTTPretty.last_request.method, "POST") self.assertEqual(HTTPretty.last_request.path, "%s/%s" % (self.app_path, "loginAttempts")) self.assertEqual(result.attr, body['attr']) self.assertEqual(application.href, self.app_href) self.assertEqual(account.href, self.acc_href) expansion = Expansion('account') account = application.authenticate_account("USERNAME", "PAŠVORD", expand=expansion) self.assertEqual(HTTPretty.last_request.method, "POST") self.assertEqual( HTTPretty.last_request.path, "%s/%s?expand=account" % (self.app_path, "loginAttempts")) httpretty.register_uri(httpretty.GET, self.dir_href, body=json.dumps(self.dir_body), content_type="application/json") directory = self.client.directories.get(self.dir_href) account = application.authenticate_account("USERNAME", "PAŠVORD", account_store=directory) if isinstance(HTTPretty.last_request.body, bytes): req_body = json.loads(HTTPretty.last_request.body.decode()) elif isinstance(HTTPretty.last_request.body, str): req_body = json.loads(HTTPretty.last_request.body) self.assertEqual(req_body['accountStore'], {'href': directory.href}) self.assertEqual(HTTPretty.last_request.method, "POST") self.assertEqual(HTTPretty.last_request.path, "%s/%s" % (self.app_path, "loginAttempts"))
from stormpath.resources import Expansion expansion = Expansion() expansion.add_property('customData') account = client.accounts.get(account_href, expansion)