Skip to content

Commit b59670c

Browse files
committed
test: cover API auth error handling
1 parent 2101d75 commit b59670c

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

tests/test_transcriber_models.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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()

0 commit comments

Comments
 (0)