|
| 1 | +# Test cases for https://identitydivision.visualstudio.com/devex/_git/AuthLibrariesApiReview?version=GBdev&path=%2FService%20protection%2FIntial%20set%20of%20protection%20measures.md&_a=preview&anchor=common-test-cases |
| 2 | +from time import sleep |
| 3 | +from random import random |
| 4 | +import logging |
| 5 | +from msal.http_decorate import _decorate |
| 6 | +from tests import unittest |
| 7 | +from tests.http_client import MinimalResponse |
| 8 | + |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | +logging.basicConfig(level=logging.DEBUG) |
| 12 | + |
| 13 | + |
| 14 | +class DummyHttpResponse(MinimalResponse): |
| 15 | + def __init__(self, headers=None, **kwargs): |
| 16 | + self.headers = {} if headers is None else headers |
| 17 | + super(DummyHttpResponse, self).__init__(**kwargs) |
| 18 | + |
| 19 | + |
| 20 | +class DummyHttpClient(object): |
| 21 | + def __init__(self, status_code=None, response_headers=None): |
| 22 | + self._status_code = status_code |
| 23 | + self._response_headers = response_headers |
| 24 | + |
| 25 | + def _build_dummy_response(self): |
| 26 | + return DummyHttpResponse( |
| 27 | + status_code=self._status_code, |
| 28 | + headers=self._response_headers, |
| 29 | + text=random(), # So that we'd know whether a new response is received |
| 30 | + ) |
| 31 | + |
| 32 | + def post(self, url, params=None, data=None, headers=None, **kwargs): |
| 33 | + return self._build_dummy_response() |
| 34 | + |
| 35 | + def get(self, url, params=None, headers=None, **kwargs): |
| 36 | + return self._build_dummy_response() |
| 37 | + |
| 38 | + |
| 39 | +class TestHttpDecoration(unittest.TestCase): |
| 40 | + def _test_RetryAfter_N_seconds_should_keep_entry_for_N_seconds( |
| 41 | + self, http_client, retry_after): |
| 42 | + http_cache = {} |
| 43 | + _decorate(http_client, http_cache) |
| 44 | + resp1 = http_client.post("https://example.com") # We implemented POST only |
| 45 | + resp2 = http_client.post("https://example.com") # We implemented POST only |
| 46 | + logger.debug(http_cache) |
| 47 | + self.assertEqual(resp1.text, resp2.text, "Should return a cached response") |
| 48 | + sleep(retry_after + 1) |
| 49 | + resp3 = http_client.post("https://example.com") # We implemented POST only |
| 50 | + self.assertNotEqual(resp1.text, resp3.text, "Should return a new response") |
| 51 | + |
| 52 | + def test_429_with_RetryAfter_N_seconds_should_keep_entry_for_N_seconds(self): |
| 53 | + retry_after = 1 |
| 54 | + self._test_RetryAfter_N_seconds_should_keep_entry_for_N_seconds( |
| 55 | + DummyHttpClient( |
| 56 | + status_code=429, response_headers={"Retry-After": retry_after}), |
| 57 | + retry_after) |
| 58 | + |
| 59 | + def test_5xx_with_RetryAfter_N_seconds_should_keep_entry_for_N_seconds(self): |
| 60 | + retry_after = 1 |
| 61 | + self._test_RetryAfter_N_seconds_should_keep_entry_for_N_seconds( |
| 62 | + DummyHttpClient( |
| 63 | + status_code=503, response_headers={"Retry-After": retry_after}), |
| 64 | + retry_after) |
| 65 | + |
| 66 | + def test_400_with_RetryAfter_N_seconds_should_keep_entry_for_N_seconds(self): |
| 67 | + """Retry-After is supposed to only shown in http 429/5xx, |
| 68 | + but we choose to support Retry-After for arbitrary http response.""" |
| 69 | + retry_after = 1 |
| 70 | + self._test_RetryAfter_N_seconds_should_keep_entry_for_N_seconds( |
| 71 | + DummyHttpClient( |
| 72 | + status_code=400, response_headers={"Retry-After": retry_after}), |
| 73 | + retry_after) |
| 74 | + |
| 75 | + def test_one_RetryAfter_request_should_block_a_similar_request(self): |
| 76 | + http_cache = {} |
| 77 | + http_client = DummyHttpClient( |
| 78 | + status_code=429, response_headers={"Retry-After": 2}) |
| 79 | + _decorate(http_client, http_cache) |
| 80 | + resp1 = http_client.post("https://example.com", data={ |
| 81 | + "scope": "one", "claims": "bar", "grant_type": "authorization_code"}) |
| 82 | + resp2 = http_client.post("https://example.com", data={ |
| 83 | + "scope": "one", "claims": "foo", "grant_type": "password"}) |
| 84 | + logger.debug(http_cache) |
| 85 | + self.assertEqual(resp1.text, resp2.text, "Should return a cached response") |
| 86 | + |
| 87 | + def test_one_RetryAfter_request_should_not_block_a_different_request(self): |
| 88 | + http_cache = {} |
| 89 | + http_client = DummyHttpClient( |
| 90 | + status_code=429, response_headers={"Retry-After": 2}) |
| 91 | + _decorate(http_client, http_cache) |
| 92 | + resp1 = http_client.post("https://example.com", data={"scope": "one"}) |
| 93 | + resp2 = http_client.post("https://example.com", data={"scope": "two"}) |
| 94 | + logger.debug(http_cache) |
| 95 | + self.assertNotEqual(resp1.text, resp2.text, "Should return a new response") |
| 96 | + |
| 97 | + def test_one_invalid_grant_should_block_a_similar_request(self): |
| 98 | + http_cache = {} |
| 99 | + http_client = DummyHttpClient( |
| 100 | + status_code=400) # It covers invalid_grant and interaction_required |
| 101 | + _decorate(http_client, http_cache) |
| 102 | + resp1 = http_client.post("https://example.com", data={"claims": "foo"}) |
| 103 | + logger.debug(http_cache) |
| 104 | + resp1_again = http_client.post("https://example.com", data={"claims": "foo"}) |
| 105 | + self.assertEqual(resp1.text, resp1_again.text, "Should return a cached response") |
| 106 | + resp2 = http_client.post("https://example.com", data={"claims": "bar"}) |
| 107 | + self.assertNotEqual(resp1.text, resp2.text, "Should return a new response") |
| 108 | + resp2_again = http_client.post("https://example.com", data={"claims": "bar"}) |
| 109 | + self.assertEqual(resp2.text, resp2_again.text, "Should return a cached response") |
| 110 | + |
| 111 | + def test_one_foci_app_recovering_from_invalid_grant_should_also_unblock_another(self): |
| 112 | + """ |
| 113 | + Need not test multiple FOCI app's acquire_token_silent() here. By design, |
| 114 | + one FOCI app's successful populating token cache would result in another |
| 115 | + FOCI app's acquire_token_silent() to hit a token without invoking http request. |
| 116 | + """ |
| 117 | + |
| 118 | + def test_forcefresh_behavior(self): |
| 119 | + """ |
| 120 | + The implementation let token cache and http cache operate in different |
| 121 | + layers. They do not couple with each other. |
| 122 | + Therefore, acquire_token_silent(..., force_refresh=True) |
| 123 | + would bypass the token cache yet technically still hit the http cache. |
| 124 | +
|
| 125 | + But that is OK, cause the customer need no force_refresh in the first place. |
| 126 | + After a successful AT/RT acquisition, AT/RT will be in the token cache, |
| 127 | + and a normal acquire_token_silent(...) without force_refresh would just work. |
| 128 | + This was discussed in https://identitydivision.visualstudio.com/DevEx/_git/AuthLibrariesApiReview/pullrequest/3618?_a=files |
| 129 | + """ |
| 130 | + |
| 131 | + def test_http_get_200_should_be_cached(self): |
| 132 | + http_cache = {} |
| 133 | + http_client = DummyHttpClient( |
| 134 | + status_code=200) # It covers UserRealm discovery and OIDC discovery |
| 135 | + _decorate(http_client, http_cache) |
| 136 | + resp1 = http_client.get("https://example.com") |
| 137 | + resp2 = http_client.get("https://example.com") |
| 138 | + logger.debug(http_cache) |
| 139 | + self.assertEqual(resp1.text, resp2.text, "Should return a cached response") |
| 140 | + |
| 141 | + def test_device_flow_retry_should_not_be_cached(self): |
| 142 | + DEVICE_AUTH_GRANT = "urn:ietf:params:oauth:grant-type:device_code" |
| 143 | + http_cache = {} |
| 144 | + http_client = DummyHttpClient(status_code=400) |
| 145 | + _decorate(http_client, http_cache) |
| 146 | + resp1 = http_client.get( |
| 147 | + "https://example.com", data={"grant_type": DEVICE_AUTH_GRANT}) |
| 148 | + resp2 = http_client.get( |
| 149 | + "https://example.com", data={"grant_type": DEVICE_AUTH_GRANT}) |
| 150 | + logger.debug(http_cache) |
| 151 | + self.assertNotEqual(resp1.text, resp2.text, "Should return a new response") |
| 152 | + |
0 commit comments