From 1c0a6416b33592e4d6a2f700cc732e829f4c0e61 Mon Sep 17 00:00:00 2001 From: Sleeps Date: Tue, 6 Oct 2026 16:40:25 +0100 Subject: [PATCH] Test scene search API authentication --- tests/test_api.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/test_api.py diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..6cbca4b --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +import unittest +from types import SimpleNamespace +from unittest.mock import patch + +from fastapi import HTTPException + +from scene_search.api import require_api_key + + +class ApiAuthTests(unittest.TestCase): + def test_api_key_disabled_accepts_request(self) -> None: + fake_settings = SimpleNamespace(scene_api_key="") + with patch("scene_search.api.settings", fake_settings): + require_api_key(authorization=None, x_scene_search_key=None) + + def test_bearer_api_key_is_accepted(self) -> None: + fake_settings = SimpleNamespace(scene_api_key="secret") + with patch("scene_search.api.settings", fake_settings): + require_api_key( + authorization="Bearer secret", + x_scene_search_key=None, + ) + + def test_header_api_key_is_accepted(self) -> None: + fake_settings = SimpleNamespace(scene_api_key="secret") + with patch("scene_search.api.settings", fake_settings): + require_api_key( + authorization=None, + x_scene_search_key="secret", + ) + + def test_wrong_api_key_is_rejected(self) -> None: + fake_settings = SimpleNamespace(scene_api_key="secret") + with patch("scene_search.api.settings", fake_settings): + with self.assertRaises(HTTPException) as caught: + require_api_key( + authorization="Bearer wrong", + x_scene_search_key=None, + ) + + self.assertEqual(caught.exception.status_code, 401) + + +if __name__ == "__main__": + unittest.main()