File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import io
2+ import sys
3+ import types
4+ import unittest
5+ from contextlib import redirect_stdout
6+
7+ sys .modules .setdefault (
8+ "ctranslate2" ,
9+ types .SimpleNamespace (get_cuda_device_count = lambda : 0 ),
10+ )
11+ sys .modules .setdefault (
12+ "faster_whisper" ,
13+ types .SimpleNamespace (WhisperModel = object ),
14+ )
15+ sys .modules .setdefault (
16+ "openai" ,
17+ types .SimpleNamespace (OpenAI = object ),
18+ )
19+
20+ from TranscriberModels import APIWhisperTranscriber
21+
22+
23+ class FakeAuthError (Exception ):
24+ status_code = 401
25+ code = "expired_api_key"
26+
27+
28+ class APIWhisperTranscriberTests (unittest .TestCase ):
29+ def test_auth_error_detection_handles_expired_key_response (self ):
30+ error = FakeAuthError ("Invalid API Key: expired_api_key" )
31+
32+ self .assertTrue (APIWhisperTranscriber ._is_auth_error (error ))
33+
34+ def test_auth_error_disables_api_and_logs_once (self ):
35+ transcriber = APIWhisperTranscriber .__new__ (APIWhisperTranscriber )
36+ transcriber .api_available = True
37+ transcriber .auth_error_logged = False
38+
39+ output = io .StringIO ()
40+ with redirect_stdout (output ):
41+ transcriber ._disable_api_after_auth_error (FakeAuthError ("expired_api_key" ))
42+ transcriber ._disable_api_after_auth_error (FakeAuthError ("expired_api_key" ))
43+
44+ self .assertFalse (transcriber .api_available )
45+ self .assertEqual (output .getvalue ().count ("API key rejected by provider" ), 1 )
46+
47+
48+ if __name__ == "__main__" :
49+ unittest .main ()
You can’t perform that action at this time.
0 commit comments