Debian Codegraph @

tests/functional/tokenstorage/v2/conftest.py

raw
1 import time
2 import uuid
3 from unittest import mock
4
5 import pytest
6
7 import globus_sdk
8 from globus_sdk.testing import RegisteredResponse
9 from globus_sdk.token_storage import TokenStorageData
10
11
12 @pytest.fixture
13 def id_token_sub():
14 return str(uuid.UUID(int=1))
15
16
17 @pytest.fixture
18 def cc_auth_client():
19 client = globus_sdk.ConfidentialAppAuthClient("dummy_id", "dummy_secret")
20 with client.retry_config.tune(max_retries=0):
21 yield client
22
23
24 @pytest.fixture
25 def mock_token_data_by_resource_server():
26 expiration_time = int(time.time()) + 3600
27 ret = {
28 "resource_server_1": TokenStorageData(
29 resource_server="resource_server_1",
30 identity_id="user_id",
31 scope="scope1",
32 access_token="access_token_1",
33 refresh_token="refresh_token_1",
34 expires_at_seconds=expiration_time,
35 token_type="Bearer",
36 ),
37 "resource_server_2": TokenStorageData(
38 resource_server="resource_server_2",
39 identity_id="user_id",
40 scope="scope2 scope2:0 scope2:1",
41 access_token="access_token_2",
42 refresh_token="refresh_token_2",
43 expires_at_seconds=expiration_time,
44 token_type="Bearer",
45 ),
46 }
47 return ret
48
49
50 @pytest.fixture
51 def mock_response():
52 res = mock.Mock()
53 expiration_time = int(time.time()) + 3600
54 res.by_resource_server = {
55 "resource_server_1": {
56 "access_token": "access_token_1",
57 "expires_at_seconds": expiration_time,
58 "refresh_token": "refresh_token_1",
59 "resource_server": "resource_server_1",
60 "scope": "scope1",
61 "token_type": "Bearer",
62 },
63 "resource_server_2": {
64 "access_token": "access_token_2",
65 "expires_at_seconds": expiration_time,
66 "refresh_token": "refresh_token_2",
67 "resource_server": "resource_server_2",
68 "scope": "scope2 scope2:0 scope2:1",
69 "token_type": "Bearer",
70 },
71 }
72 res.decode_id_token.return_value = {"sub": "user_id"}
73
74 return res
75
76
77 @pytest.fixture
78 def dependent_token_response(cc_auth_client):
79 expiration_time = int(time.time()) + 3600
80 RegisteredResponse(
81 service="auth",
82 path="/v2/oauth2/token",
83 method="POST",
84 json=[
85 {
86 "access_token": "access_token_1",
87 "expires_at_seconds": expiration_time,
88 "refresh_token": "refresh_token_1",
89 "resource_server": "resource_server_1",
90 "scope": "scope1",
91 "token_type": "Bearer",
92 },
93 {
94 "access_token": "access_token_2",
95 "expires_at_seconds": expiration_time,
96 "refresh_token": "refresh_token_2",
97 "resource_server": "resource_server_2",
98 "scope": "scope2 scope2:0 scope2:1",
99 "token_type": "Bearer",
100 },
101 ],
102 ).add()
103 return cc_auth_client.oauth2_get_dependent_tokens("dummy_tok")
104
105
106 @pytest.fixture
107 def authorization_code_response(cc_auth_client, id_token_sub):
108 cc_auth_client.oauth2_start_flow("https://example.com/redirect-uri", "dummy-scope")
109
110 expiration_time = int(time.time()) + 3600
111 RegisteredResponse(
112 service="auth",
113 path="/v2/oauth2/token",
114 method="POST",
115 json={
116 "access_token": "access_token_1",
117 "expires_at_seconds": expiration_time,
118 "refresh_token": "refresh_token_1",
119 "resource_server": "resource_server_1",
120 "scope": "scope1",
121 "token_type": "Bearer",
122 "id_token": "dummy_id_token",
123 "other_tokens": [
124 {
125 "access_token": "access_token_2",
126 "expires_at_seconds": expiration_time,
127 "refresh_token": "refresh_token_2",
128 "resource_server": "resource_server_2",
129 "scope": "scope2 scope2:0 scope2:1",
130 "token_type": "Bearer",
131 },
132 ],
133 },
134 ).add()
135
136 # because it's more difficult to mock the full decode_id_token() interaction in
137 # detail, directly mock the result of it to return the desired subject (identity_id)
138 # value
139 response = cc_auth_client.oauth2_exchange_code_for_tokens("dummy_code")
140 with mock.patch.object(response, "decode_id_token", lambda: {"sub": id_token_sub}):
141 yield response
142
143
144 @pytest.fixture
145 def refresh_token_response(cc_auth_client):
146 expiration_time = int(time.time()) + 3600
147 RegisteredResponse(
148 service="auth",
149 path="/v2/oauth2/token",
150 method="POST",
151 json={
152 "access_token": "access_token_1",
153 "expires_at_seconds": expiration_time,
154 "refresh_token": "refresh_token_1",
155 "resource_server": "resource_server_1",
156 "scope": "scope1",
157 "token_type": "Bearer",
158 "other_tokens": [],
159 },
160 ).add()
161 return cc_auth_client.oauth2_refresh_token("dummy_token")