@@ -26,6 +26,8 @@ async def test_create_token() -> None:
2626
2727 assert result .api_key == "ek_test123"
2828 assert result .expires_at == "2024-12-15T12:10:00Z"
29+ assert result .permissions is None
30+ assert result .constraints is None
2931
3032
3133@pytest .mark .asyncio
@@ -114,3 +116,118 @@ async def test_create_token_without_metadata_sends_null() -> None:
114116
115117 call_kwargs = mock_session .post .call_args
116118 assert call_kwargs .kwargs ["json" ] == {}
119+
120+
121+ @pytest .mark .asyncio
122+ async def test_create_token_with_expires_in () -> None :
123+ """Sends expiresIn in request body."""
124+ client = DecartClient (api_key = "test-api-key" )
125+
126+ mock_response = AsyncMock ()
127+ mock_response .ok = True
128+ mock_response .json = AsyncMock (
129+ return_value = {"apiKey" : "ek_test123" , "expiresAt" : "2024-12-15T12:10:00Z" }
130+ )
131+
132+ mock_session = MagicMock ()
133+ mock_session .post = MagicMock (
134+ return_value = AsyncMock (__aenter__ = AsyncMock (return_value = mock_response ))
135+ )
136+
137+ with patch .object (client , "_get_session" , AsyncMock (return_value = mock_session )):
138+ await client .tokens .create (expires_in = 120 )
139+
140+ call_kwargs = mock_session .post .call_args
141+ assert call_kwargs .kwargs ["json" ] == {"expiresIn" : 120 }
142+
143+
144+ @pytest .mark .asyncio
145+ async def test_create_token_with_allowed_models () -> None :
146+ """Sends allowedModels in request body."""
147+ client = DecartClient (api_key = "test-api-key" )
148+
149+ mock_response = AsyncMock ()
150+ mock_response .ok = True
151+ mock_response .json = AsyncMock (
152+ return_value = {"apiKey" : "ek_test123" , "expiresAt" : "2024-12-15T12:10:00Z" }
153+ )
154+
155+ mock_session = MagicMock ()
156+ mock_session .post = MagicMock (
157+ return_value = AsyncMock (__aenter__ = AsyncMock (return_value = mock_response ))
158+ )
159+
160+ with patch .object (client , "_get_session" , AsyncMock (return_value = mock_session )):
161+ await client .tokens .create (allowed_models = ["lucy_2_rt" ])
162+
163+ call_kwargs = mock_session .post .call_args
164+ assert call_kwargs .kwargs ["json" ] == {"allowedModels" : ["lucy_2_rt" ]}
165+
166+
167+ @pytest .mark .asyncio
168+ async def test_create_token_with_constraints () -> None :
169+ """Sends constraints in request body."""
170+ client = DecartClient (api_key = "test-api-key" )
171+
172+ mock_response = AsyncMock ()
173+ mock_response .ok = True
174+ mock_response .json = AsyncMock (
175+ return_value = {"apiKey" : "ek_test123" , "expiresAt" : "2024-12-15T12:10:00Z" }
176+ )
177+
178+ mock_session = MagicMock ()
179+ mock_session .post = MagicMock (
180+ return_value = AsyncMock (__aenter__ = AsyncMock (return_value = mock_response ))
181+ )
182+
183+ constraints = {"realtime" : {"maxSessionDuration" : 120 }}
184+ with patch .object (client , "_get_session" , AsyncMock (return_value = mock_session )):
185+ await client .tokens .create (constraints = constraints )
186+
187+ call_kwargs = mock_session .post .call_args
188+ assert call_kwargs .kwargs ["json" ] == {
189+ "constraints" : {"realtime" : {"maxSessionDuration" : 120 }}
190+ }
191+
192+
193+ @pytest .mark .asyncio
194+ async def test_create_token_with_all_v2_fields () -> None :
195+ """Sends all v2 fields and parses permissions/constraints from response."""
196+ client = DecartClient (api_key = "test-api-key" )
197+
198+ mock_response = AsyncMock ()
199+ mock_response .ok = True
200+ mock_response .json = AsyncMock (
201+ return_value = {
202+ "apiKey" : "ek_test123" ,
203+ "expiresAt" : "2024-12-15T12:10:00Z" ,
204+ "permissions" : {"models" : ["lucy_2_rt" ]},
205+ "constraints" : {"realtime" : {"maxSessionDuration" : 120 }},
206+ }
207+ )
208+
209+ mock_session = MagicMock ()
210+ mock_session .post = MagicMock (
211+ return_value = AsyncMock (__aenter__ = AsyncMock (return_value = mock_response ))
212+ )
213+
214+ with patch .object (client , "_get_session" , AsyncMock (return_value = mock_session )):
215+ result = await client .tokens .create (
216+ metadata = {"role" : "viewer" },
217+ expires_in = 120 ,
218+ allowed_models = ["lucy_2_rt" ],
219+ constraints = {"realtime" : {"maxSessionDuration" : 120 }},
220+ )
221+
222+ assert result .api_key == "ek_test123"
223+ assert result .expires_at == "2024-12-15T12:10:00Z"
224+ assert result .permissions == {"models" : ["lucy_2_rt" ]}
225+ assert result .constraints == {"realtime" : {"maxSessionDuration" : 120 }}
226+
227+ call_kwargs = mock_session .post .call_args
228+ assert call_kwargs .kwargs ["json" ] == {
229+ "metadata" : {"role" : "viewer" },
230+ "expiresIn" : 120 ,
231+ "allowedModels" : ["lucy_2_rt" ],
232+ "constraints" : {"realtime" : {"maxSessionDuration" : 120 }},
233+ }
0 commit comments