@@ -884,47 +884,27 @@ async def test_exchange_device_code_success(self):
884884 "attempts" : 0 ,
885885 }
886886 )
887- svc ._token_repo .find_by_hash_and_type .return_value = token_doc
888- svc ._token_repo .mark_as_used .return_value = True
887+ svc ._token_repo .consume_by_hash .return_value = token_doc
889888 svc ._user_repo .find_by_id .return_value = make_user_doc (email_verified = True )
890889
891890 _user , access , refresh = await svc .exchange_device_code (raw_code )
892891 assert isinstance (access , str )
893892 assert isinstance (refresh , str )
894- svc ._token_repo .mark_as_used .assert_awaited_once ()
893+ svc ._token_repo .consume_by_hash .assert_awaited_once ()
895894
896895 @pytest .mark .asyncio
897896 async def test_exchange_device_code_invalid (self ):
898897 svc = make_auth_service ()
899- svc ._token_repo .find_by_hash_and_type .return_value = None
898+ svc ._token_repo .consume_by_hash .return_value = None
900899
901900 with pytest .raises (AuthenticationError , match = "invalid or expired" ):
902901 await svc .exchange_device_code ("bad-code" )
903902
904903 @pytest .mark .asyncio
905904 async def test_exchange_device_code_expired (self ):
906- from datetime import timedelta
907-
908- from schemas .models .token import TOKEN_TYPE_DEVICE_AUTH , VerificationTokenDoc
909- from shared .crypto import hash_token
910-
905+ """Expired codes are filtered out by consume_by_hash (expires_at in query)."""
911906 svc = make_auth_service ()
912- raw_code = "expired-code"
913- past = datetime (2020 , 1 , 1 , tzinfo = timezone .utc )
914- token_doc = VerificationTokenDoc .from_mongo (
915- {
916- "_id" : ObjectId (),
917- "user_id" : USER_OID ,
918- "email" : "test@example.com" ,
919- "token_hash" : hash_token (raw_code ),
920- "token_type" : TOKEN_TYPE_DEVICE_AUTH ,
921- "expires_at" : past ,
922- "created_at" : past - timedelta (minutes = 5 ),
923- "used_at" : None ,
924- "attempts" : 0 ,
925- }
926- )
927- svc ._token_repo .find_by_hash_and_type .return_value = token_doc
907+ svc ._token_repo .consume_by_hash .return_value = None
928908
929- with pytest .raises (AuthenticationError , match = "expired" ):
930- await svc .exchange_device_code (raw_code )
909+ with pytest .raises (AuthenticationError , match = "invalid or expired" ):
910+ await svc .exchange_device_code ("expired-code" )
0 commit comments