def test_challenge_parsing(): authority = "https://login.authority.net/tenant" resource = "https://challenge.resource" challenge = HttpChallenge( "https://request.uri", challenge="Bearer authorization={}, resource={}".format(authority, resource) ) assert challenge.get_authorization_server() == authority assert challenge.get_resource() == resource
def test_challenge_cache(): url_a = get_random_url() challenge_a = HttpChallenge(url_a, "Bearer authorization=authority A, resource=resource A") url_b = get_random_url() challenge_b = HttpChallenge(url_b, "Bearer authorization=authority B, resource=resource B") for url, challenge in zip((url_a, url_b), (challenge_a, challenge_b)): HttpChallengeCache.set_challenge_for_url(url, challenge) assert HttpChallengeCache.get_challenge_for_url(url) == challenge assert HttpChallengeCache.get_challenge_for_url(url + "/some/path") == challenge assert HttpChallengeCache.get_challenge_for_url(url + "/some/path?with-query=string") == challenge assert HttpChallengeCache.get_challenge_for_url(url + ":443") == challenge HttpChallengeCache.remove_challenge_for_url(url) assert not HttpChallengeCache.get_challenge_for_url(url)
def test_enforces_tls(): url = "http://not.secure" HttpChallengeCache.set_challenge_for_url(url, HttpChallenge(url, "Bearer authorization=_, resource=_")) credential = Mock() pipeline = Pipeline(transport=Mock(), policies=[ChallengeAuthPolicy(credential)]) with pytest.raises(ServiceRequestError): pipeline.run(HttpRequest("GET", url))
def test_challenge_cache(): # ensure the test starts with an empty cache HttpChallengeCache.clear() url_a = "https://azure.service.a" challenge_a = HttpChallenge(url_a, "Bearer authorization=authority A, resource=resource A") url_b = "https://azure.service.b" challenge_b = HttpChallenge(url_b, "Bearer authorization=authority B, resource=resource B") for url, challenge in zip((url_a, url_b), (challenge_a, challenge_b)): HttpChallengeCache.set_challenge_for_url(url, challenge) assert HttpChallengeCache.get_challenge_for_url(url) == challenge assert HttpChallengeCache.get_challenge_for_url(url + "/some/path") == challenge assert HttpChallengeCache.get_challenge_for_url(url + "/some/path?with-query=string") == challenge assert HttpChallengeCache.get_challenge_for_url(url + ":443") == challenge HttpChallengeCache.remove_challenge_for_url(url) assert not HttpChallengeCache.get_challenge_for_url(url)