def test_happy_path(self): response = util.create_response() self.setup_expected_auth_code_token_request_response( 200, response['wireResponse']) context = adal.AuthenticationContext(cp['authUrl']) # action token_response = context.acquire_token_with_authorization_code( self.authorization_code, self.redirect_uri, response['resource'], cp['clientId'], cp['clientSecret']) # assert # the caching layer adds a few extra fields, let us pop them out for easier verification for k in ['_clientId', '_authority', 'resource']: token_response.pop(k, None) self.assertTrue( util.is_match_token_response(response['decodedResponse'], token_response), 'The response did not match what was expected') # verify a request on the wire was made req = httpretty.last_request() util.match_standard_request_headers(req) # verify the same token entry was cached cached_items = list(context.cache.read_items()) self.assertTrue(len(cached_items) == 1) _, cached_entry = cached_items[0] self.assertEqual(cached_entry, token_response)
def test_happy_path(self): response = util.create_response() self.setup_expected_auth_code_token_request_response(200, response['wireResponse']) token_response = adal._acquire_token_with_authorization_code(cp['authUrl'], cp['clientId'], cp['clientSecret'], self.authorization_code, self.redirect_uri, response['resource']) self.assertTrue(util.is_match_token_response(response['decodedResponse'], token_response), 'The response did not match what was expected') req = httpretty.last_request() util.match_standard_request_headers(req)
def test_url_extra_slashes(self): util.setup_expected_instance_discovery_request( 200, cp['authorityHosts']['global'], {'tenant_discovery_endpoint': 'http://foobar'}, self.nonHardCodedAuthorizeEndpoint) authority_url = self.nonHardCodedAuthority + '/' # This should pass for one or more than one slashes authority = Authority(authority_url, True) obj = util.create_empty_adal_object() authority.validate(obj['call_context']) req = httpretty.last_request() util.match_standard_request_headers(req)
def test_create_from_url_no_header(self): httpretty.register_uri(httpretty.GET, uri=self.testUrl, body='foo', status=401) def _callback(err, resp): self.assertIsNotNone(err, "Did not receive expected error.") self.assertTrue(str(err).find('header') >= 0, 'Error did not include message about missing header') adal.authentication_parameters.create_authentication_parameters_from_url(self.testUrl, _callback) req = httpretty.last_request() util.match_standard_request_headers(req)
def test_negative_no_root(self): user_realm_response = 'noroot' httpretty.register_uri(httpretty.GET, uri=self.testUrl, body=user_realm_response, status=200) user_realm = adal.user_realm.UserRealm(cp['callContext'], self.user, self.authority) def _callback(err): self.assertIsNotNone(err,'Did not receive expected error') user_realm.discover(_callback) util.match_standard_request_headers(httpretty.last_request())
def test_negative_wrong_field(self): user_realm_response = '{\"account_type\":\"Manageddf\",\"federation_protocol\":\"SAML20fgfg\",\"federation_metadata\":\"https://adfs.federatedtenant.com/adfs/services/trust/mex\",\"federation_active_auth_url\":\"https://adfs.federatedtenant.com/adfs/services/trust/2005/usernamemixed\",\"version\":\"0.8\"}' httpretty.register_uri(httpretty.GET, uri=self.testUrl, body=user_realm_response, status=200) user_realm = adal.user_realm.UserRealm(cp['callContext'], self.user, self.authority) def _callback(err): self.assertIsNotNone(err,'Did not receive expected error') user_realm.discover(_callback) util.match_standard_request_headers(httpretty.last_request())
def test_url_extra_path_elements(self): util.setup_expected_instance_discovery_request( 200, cp["authorityHosts"]["global"], {"tenant_discovery_endpoint": "http://foobar"}, self.nonHardCodedAuthorizeEndpoint, ) authority_url = self.nonHardCodedAuthority + "/extra/path" authority = Authority(authority_url, True) obj = util.create_empty_adal_object() authority.validate(obj["call_context"]) req = httpretty.last_request() util.match_standard_request_headers(req)
def test_negative_fed_err(self): user_realm_response = '{\"account_type\":\"Federated\",\"federation_protocol\":\"wstrustww\",\"federation_metadata_url\":\"https://adfs.federatedtenant.com/adfs/services/trust/mex\",\"federation_active_auth_url\":\"https://adfs.federatedtenant.com/adfs/services/trust/2005/usernamemixed\",\"ver\":\"0.8\"}' httpretty.register_uri(httpretty.GET, uri=self.testUrl, body=user_realm_response, status=200) user_realm = adal.user_realm.UserRealm(cp['callContext'], self.user, self.authority) try: user_realm.discover() except Exception as exp: receivedException = True pass finally: self.assertTrue(receivedException,'Did not receive expected error') util.match_standard_request_headers(httpretty.last_request())
def test_negative_empty_json(self): user_realm_response = '{}' httpretty.register_uri(httpretty.GET, uri=self.testUrl, body=user_realm_response, status=200) user_realm = adal.user_realm.UserRealm(cp['callContext'], self.user, self.authority) try: user_realm.discover() except Exception as exp: receivedException = True pass finally: self.assertTrue(receivedException,'Did not receive expected error') util.match_standard_request_headers(httpretty.last_request())
def test_happy_path_federated(self): user_realm_response = '{\"account_type\":\"Federated\",\"federation_protocol\":\"wstrust\",\"federation_metadata_url\":\"https://adfs.federatedtenant.com/adfs/services/trust/mex\",\"federation_active_auth_url\":\"https://adfs.federatedtenant.com/adfs/services/trust/2005/usernamemixed\",\"ver\":\"0.8\"}' httpretty.register_uri(httpretty.GET, uri=self.testUrl, body=user_realm_response, status=200) user_realm = adal.user_realm.UserRealm(cp['callContext'], self.user, self.authority) def _callback(err): self.assertIsNone(err, "Error raised during function: {0}".format(err)) self.assertEqual(user_realm.federation_metadata_url, 'https://adfs.federatedtenant.com/adfs/services/trust/mex', 'Returned Mex URL does not match expected value: {0}'.format(user_realm.federation_metadata_url)) self.assertAlmostEqual(user_realm.federation_active_auth_url, 'https://adfs.federatedtenant.com/adfs/services/trust/2005/usernamemixed', 'Returned active auth URL does not match expected value: {0}'.format(user_realm.federation_active_auth_url)) user_realm.discover(_callback) util.match_standard_request_headers(httpretty.last_request())
def test_create_from_url_no_header(self): httpretty.register_uri(httpretty.GET, uri=self.testUrl, body='foo', status=401) receivedException = False try: adal.authentication_parameters.create_authentication_parameters_from_url(self.testUrl) except Exception as err: receivedException = True self.assertTrue(str(err).find('header') >= 0, 'Error did not include message about missing header') pass finally: self.assertTrue(receivedException) req = httpretty.last_request() util.match_standard_request_headers(req)
def test_happy_path(self): response = util.create_response() self.setup_expected_auth_code_token_request_response( 200, response['wireResponse']) token_response = adal._acquire_token_with_authorization_code( cp['authUrl'], cp['clientId'], cp['clientSecret'], self.authorization_code, self.redirect_uri, response['resource']) self.assertTrue( util.is_match_token_response(response['decodedResponse'], token_response), 'The response did not match what was expected') req = httpretty.last_request() util.match_standard_request_headers(req)
def test_create_from_url_happy_string_url(self): httpretty.register_uri(httpretty.GET, uri=self.testUrl, body='foo', status=401, **{'www-authenticate':'Bearer authorization_uri="foobar,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="foo"'}) def _callback(err, parameters): self.assertIsNone(err, "An error was raised during function {0}".format(err)) test_params = { 'authorizationUri' : 'foobar,lkfj,;l,', 'resource' : 'clark, &^()- q32,shark', } self.assertEqual(parameters.authorization_uri, test_params['authorizationUri'], 'Parsed authorizationUri did not match expected value.: {0}'.format(parameters.authorization_uri)) self.assertEqual(parameters.resource, test_params['resource'], 'Parsed resource did not match expected value.: {0}'.format(parameters.resource)) adal.authentication_parameters.create_authentication_parameters_from_url(self.testUrl, _callback, None) req = httpretty.last_request() util.match_standard_request_headers(req)
def test_create_from_url_happy_string_url(self): httpretty.register_uri(httpretty.GET, uri=self.testUrl, body='foo', status=401, **{'www-authenticate':'Bearer authorization_uri="foobar,lkfj,;l,", fruitcake="f",resource="clark, &^()- q32,shark" , f="foo"'}) # maybe try-catch here to catch the error parameters = adal.authentication_parameters.create_authentication_parameters_from_url(self.testUrl) test_params = { 'authorizationUri' : 'foobar,lkfj,;l,', 'resource' : 'clark, &^()- q32,shark', } self.assertEqual(parameters.authorization_uri, test_params['authorizationUri'], 'Parsed authorizationUri did not match expected value.: {0}'.format(parameters.authorization_uri)) self.assertEqual(parameters.resource, test_params['resource'], 'Parsed resource did not match expected value.: {0}'.format(parameters.resource)) req = httpretty.last_request() util.match_standard_request_headers(req)
def callback(err): if err: self.assertFalse(err, 'Received unexpected error: ' + err.args[0]) req = httpretty.last_request() util.match_standard_request_headers(req)