|
1 | 1 | import json |
| 2 | +from os import remove as file_remove |
2 | 3 | import unittest |
3 | 4 | from http import HTTPStatus |
4 | 5 | from unittest.mock import patch |
@@ -89,6 +90,34 @@ def test_refresh_request_with_token_format_opaque(self): |
89 | 90 | verify=True, |
90 | 91 | ) |
91 | 92 |
|
| 93 | + def test_refresh_request_with_token_from_cf_config(self): |
| 94 | + requests = FakeRequests() |
| 95 | + session = MockSession() |
| 96 | + proxy = dict(http='', https='') |
| 97 | + cf_config_file_content = {"Target": self.TARGET_ENDPOINT, "RefreshToken": "refresh-token"} |
| 98 | + with open("config.json", "w", encoding="utf-8") as f: |
| 99 | + json.dump(cf_config_file_content, f, ensure_ascii=False, indent=4) |
| 100 | + with patch("oauth2_client.credentials_manager.requests", new=requests), patch( |
| 101 | + "cloudfoundry_client.client.requests", new=requests |
| 102 | + ): |
| 103 | + requests.Session.return_value = session |
| 104 | + self._mock_info_calls(requests) |
| 105 | + requests.post.return_value = MockResponse( |
| 106 | + "%s/oauth/token" % self.AUTHORIZATION_ENDPOINT, |
| 107 | + status_code=HTTPStatus.OK.value, |
| 108 | + text=json.dumps(dict(access_token="access-token", refresh_token="refresh-token")), |
| 109 | + ) |
| 110 | + client = CloudFoundryClient.build_from_cf_config("config.json", proxy=proxy, verify=True) # noqa: F841 |
| 111 | + file_remove('config.json') |
| 112 | + self.assertEqual("Bearer access-token", session.headers.get("Authorization")) |
| 113 | + requests.post.assert_called_with( |
| 114 | + requests.post.return_value.url, |
| 115 | + data=dict(grant_type="refresh_token", scope="", refresh_token="refresh-token"), |
| 116 | + headers=dict(Accept="application/json", Authorization="Basic Y2Y6"), |
| 117 | + proxies=proxy, |
| 118 | + verify=True, |
| 119 | + ) |
| 120 | + |
92 | 121 | def test_grant_password_request_with_login_hint(self): |
93 | 122 | requests = FakeRequests() |
94 | 123 | session = MockSession() |
|
0 commit comments