1717 "userId" : "test-user" ,
1818}
1919
20+ USER_DATA_NO_LAST_USED = {k : v for k , v in USER_DATA .items () if k != "lastUsedAt" }
21+
2022USERS_DATA = [
2123 USER_DATA ,
2224 {** USER_DATA , "userId" : "test-user-2" },
2325]
2426
27+ USERS_DATA_NO_LAST_USED = [
28+ USER_DATA_NO_LAST_USED ,
29+ {** USER_DATA_NO_LAST_USED , "userId" : "test-user-2" },
30+ ]
31+
2532EXPECTED_CREATED_AT = datetime (2026 , 2 , 24 , 19 , 15 , 18 , 574000 , tzinfo = timezone .utc )
2633EXPECTED_LAST_USED_AT = datetime (2026 , 2 , 25 , 20 , 57 , 36 , 419000 , tzinfo = timezone .utc )
2734
@@ -33,7 +40,7 @@ def test_get_user_without_last_used_time(
3340
3441 def handler (request : Request ) -> Response :
3542 captured ["params" ] = dict (request .args )
36- return Response (json .dumps (USER_DATA ), content_type = "application/json" , status = 200 )
43+ return Response (json .dumps (USER_DATA_NO_LAST_USED ), content_type = "application/json" , status = 200 )
3744
3845 weaviate_no_auth_mock .expect_request ("/v1/users/db/test-user" ).respond_with_handler (handler )
3946
@@ -95,7 +102,7 @@ def test_list_all_without_last_used_time(
95102
96103 def handler (request : Request ) -> Response :
97104 captured ["params" ] = dict (request .args )
98- return Response (json .dumps (USERS_DATA ), content_type = "application/json" , status = 200 )
105+ return Response (json .dumps (USERS_DATA_NO_LAST_USED ), content_type = "application/json" , status = 200 )
99106
100107 weaviate_no_auth_mock .expect_request ("/v1/users/db" ).respond_with_handler (handler )
101108
@@ -133,3 +140,61 @@ def handler(request: Request) -> Response:
133140 assert user .last_used_time == EXPECTED_LAST_USED_AT
134141 assert user .created_at == EXPECTED_CREATED_AT
135142 assert captured ["params" ].get ("includeLastUsedTime" ) in ("True" , "true" )
143+
144+
145+ def test_get_user_missing_created_at (
146+ weaviate_no_auth_mock : HTTPServer , start_grpc_server : grpc .Server
147+ ) -> None :
148+ data = {k : v for k , v in USER_DATA .items () if k != "createdAt" }
149+ weaviate_no_auth_mock .expect_request ("/v1/users/db/test-user" ).respond_with_response (
150+ Response (json .dumps (data ), content_type = "application/json" , status = 200 )
151+ )
152+
153+ with weaviate .connect_to_local (
154+ host = MOCK_IP , port = MOCK_PORT , grpc_port = MOCK_PORT_GRPC
155+ ) as client :
156+ user = client .users .db .get (user_id = "test-user" )
157+
158+ assert user is not None
159+ assert user .created_at is None
160+ assert user .last_used_time == EXPECTED_LAST_USED_AT
161+
162+
163+ def test_get_user_missing_api_key_first_letters (
164+ weaviate_no_auth_mock : HTTPServer , start_grpc_server : grpc .Server
165+ ) -> None :
166+ data = {k : v for k , v in USER_DATA .items () if k != "apiKeyFirstLetters" }
167+ weaviate_no_auth_mock .expect_request ("/v1/users/db/test-user" ).respond_with_response (
168+ Response (json .dumps (data ), content_type = "application/json" , status = 200 )
169+ )
170+
171+ with weaviate .connect_to_local (
172+ host = MOCK_IP , port = MOCK_PORT , grpc_port = MOCK_PORT_GRPC
173+ ) as client :
174+ user = client .users .db .get (user_id = "test-user" )
175+
176+ assert user is not None
177+ assert user .api_key_first_letters is None
178+ assert user .created_at == EXPECTED_CREATED_AT
179+
180+
181+ def test_get_user_last_used_time_parsed_when_include_false (
182+ weaviate_no_auth_mock : HTTPServer , start_grpc_server : grpc .Server
183+ ) -> None :
184+ """If the API returns lastUsedAt the client must parse it regardless of include_last_used_time."""
185+ captured : dict = {}
186+
187+ def handler (request : Request ) -> Response :
188+ captured ["params" ] = dict (request .args )
189+ return Response (json .dumps (USER_DATA ), content_type = "application/json" , status = 200 )
190+
191+ weaviate_no_auth_mock .expect_request ("/v1/users/db/test-user" ).respond_with_handler (handler )
192+
193+ with weaviate .connect_to_local (
194+ host = MOCK_IP , port = MOCK_PORT , grpc_port = MOCK_PORT_GRPC
195+ ) as client :
196+ user = client .users .db .get (user_id = "test-user" ) # include_last_used_time defaults to False
197+
198+ assert user is not None
199+ assert user .last_used_time == EXPECTED_LAST_USED_AT
200+ assert captured ["params" ].get ("includeLastUsedTime" ) in ("False" , "false" )
0 commit comments