|
3 | 3 | import tempfile
|
4 | 4 | import re
|
5 | 5 |
|
| 6 | +import pytest |
6 | 7 | import requests
|
7 | 8 | from email.utils import formatdate
|
8 | 9 |
|
@@ -170,3 +171,79 @@ def test_signing(self):
|
170 | 171 | set(re.search(r'headers="(.+?)"', post_sig).group(1).split(" ")),
|
171 | 172 | {"(request-target)", "date", "host", "content-length", "digest"},
|
172 | 173 | )
|
| 174 | + |
| 175 | + def test_bearer_token(self): |
| 176 | + """Verify that the authorization header is set when a bearer token is provided""" |
| 177 | + |
| 178 | + bearer_token = ( |
| 179 | + "Bearer eyJraWQiOiJWcmVsOE9zZ0JXaUpHeEpMeFJ4bE1UaVwvbjgyc1hwWktUaTd2UExUNFQ0T" |
| 180 | + "T0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2IiwidG9r" |
| 181 | + "ZW5fdXNlIjoiYWNjZXNzIiwic2NvcGUiOiJ3ZWJcL2dldCB3ZWJcL3Bvc3QiLCJhdXRoX3RpbWUi" |
| 182 | + "OjE1OTM3MjM1NDgsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC51cy1lYXN0LTEuYW1hem9u" |
| 183 | + "YXdzLmNvbVwvdXMtZWFzdC0xX1d6aEZzTGlPRyIsImV4cCI6MTU5MzcyNzE0OCwiaWF0IjoxNTkz" |
| 184 | + "NzIzNTQ4LCJ2ZXJzaW9uIjoyLCJqdGkiOiI4Njk5ZDEwYy05Mjg4LTQ0YmEtODIxNi01OTJjZGU3" |
| 185 | + "MDBhY2MiLCJjbGllbnRfaWQiOiJoMTBlM2hwajliNjc4bXMwOG8zbGlibHQ2In0.YA_yiD-x6UuB" |
| 186 | + "MShprUbUKuB_DO6ogCtd5srfgpJA6Ve_qsf8n19nVMmFsZBy3GxzN92P1ZXiFY99FfNPohhQtaRR" |
| 187 | + "hpeUkir08hgJN2bEHCJ5Ym8r9mr9mlwSG6FoiedgLaUVGwJujD9c2rcA83NEo8ayTyfCynF2AZ2p" |
| 188 | + "MxLHvqOYtvscGMiMzIwlZfJV301iKUVgPODJM5lpJ4iKCpOy2ByCl2_KL1uxIxgMkglpB-i7kgJc" |
| 189 | + "-WmYoJFoN88D89ugnEoAxNfK14N4_RyEkrLNGape9kew79nUeR6fWbVFLiGDDu25_9z-7VB-GGGk" |
| 190 | + "7L_Hb7YgVJ5W2FwESnkDvV1T4Q" |
| 191 | + ) |
| 192 | + |
| 193 | + connection = transcriptic.Connection( |
| 194 | + |
| 195 | + bearer_token=bearer_token, |
| 196 | + organization_id="transcriptic", |
| 197 | + api_root="http://foo:5555", |
| 198 | + user_id="ufoo2", |
| 199 | + ) |
| 200 | + |
| 201 | + get_request = requests.Request("GET", "http://foo:5555/get") |
| 202 | + prepared_get = connection.session.prepare_request(get_request) |
| 203 | + |
| 204 | + authorization_header_value = prepared_get.headers["authorization"] |
| 205 | + self.assertEqual(bearer_token, authorization_header_value) |
| 206 | + |
| 207 | + def test_malformed_bearer_token(self): |
| 208 | + """Verify that an exception is thrown when a malformed JWT bearer token is provided""" |
| 209 | + |
| 210 | + bearer_token = "Bearer myBigBadBearerToken" |
| 211 | + |
| 212 | + with pytest.raises(ValueError, match="Malformed JWT Bearer Token"): |
| 213 | + transcriptic.Connection( |
| 214 | + |
| 215 | + bearer_token=bearer_token, |
| 216 | + organization_id="transcriptic", |
| 217 | + api_root="http://foo:5555", |
| 218 | + user_id="ufoo2", |
| 219 | + ) |
| 220 | + |
| 221 | + def test_user_token_supersedes_bearer_token(self): |
| 222 | + """Verify that the user token and bearer token are mutually exclusive and that |
| 223 | + user token supersedes bearer token""" |
| 224 | + |
| 225 | + user_token = "userTokenFoo" |
| 226 | + with tempfile.NamedTemporaryFile() as config_file: |
| 227 | + with open(config_file.name, "w") as f: |
| 228 | + json.dump( |
| 229 | + { |
| 230 | + |
| 231 | + "token": user_token, |
| 232 | + "bearer_token": "bearerTokenBar", |
| 233 | + "organization_id": "transcriptic", |
| 234 | + "api_root": "http://foo:5555", |
| 235 | + "analytics": True, |
| 236 | + "user_id": "ufoo2", |
| 237 | + "feature_groups": [ |
| 238 | + "can_submit_autoprotocol", |
| 239 | + "can_upload_packages", |
| 240 | + ], |
| 241 | + }, |
| 242 | + f, |
| 243 | + ) |
| 244 | + connection = transcriptic.config.Connection.from_file(config_file.name) |
| 245 | + |
| 246 | + get_request = requests.Request("GET", "http://foo:5555/get") |
| 247 | + prepared_get = connection.session.prepare_request(get_request) |
| 248 | + self.assertFalse("authorization" in prepared_get.headers) |
| 249 | + self.assertTrue("X-User-Email" in prepared_get.headers) |
0 commit comments