feat: add optional Steam Web API achievements
CI / test (push) Successful in 37s

Load the key from STEAMBASE_STEAM_API_KEY or data/steam_api_key.txt, expose GET /api/steam/achievements/{appid} with a 15-minute in-memory cache, show progress in the game card, and report steam_api in /api/meta. The key is never logged or returned to clients.
This commit is contained in:
strsv committed 2026-11-07 01:22:06 +03:00
1 parent fc113b8794
commit 5b1b23d392
16 files changed
+423 -8

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
"""Tests for the optional Steam Web API client."""
import os
import sys
import tempfile
import unittest
import urllib.error
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
import steam_api
class KeyTests(unittest.TestCase):
def test_env_key_wins(self):
with mock.patch.dict(os.environ, {"STEAMBASE_STEAM_API_KEY": "abc"}, clear=False), \
mock.patch.object(steam_api, "KEY_FILE", "missing-file"):
self.assertEqual(steam_api.load_key(), "abc")
self.assertTrue(steam_api.has_key())
def test_file_key(self):
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "key.txt")
with open(path, "w", encoding="utf-8") as handle:
handle.write("def\n")
with mock.patch.dict(os.environ, {"STEAMBASE_STEAM_API_KEY": ""}, clear=False), \
mock.patch.object(steam_api, "KEY_FILE", path):
self.assertEqual(steam_api.load_key(), "def")
def test_missing_key(self):
with mock.patch.dict(os.environ, {"STEAMBASE_STEAM_API_KEY": ""}, clear=False), \
mock.patch.object(steam_api, "KEY_FILE", "missing-file"):
self.assertIsNone(steam_api.load_key())
self.assertFalse(steam_api.has_key())
def test_steam_id_from_env(self):
with mock.patch.dict(os.environ, {"STEAMBASE_STEAM_ID": "123"}, clear=False):
self.assertEqual(steam_api.steam_id(), "123")
class AchievementTests(unittest.TestCase):
def setUp(self):
steam_api.clear_cache()
self.addCleanup(steam_api.clear_cache)
def test_merge_and_cache(self):
player = [{"apiname": "A", "achieved": 1, "unlocktime": 100},
{"apiname": "B", "achieved": 0, "unlocktime": 0}]
schema = [{"name": "A", "displayName": "First", "description": "Desc A", "icon": "i"},
{"name": "B", "displayName": "Second", "description": "",
"icon": "j", "hidden": 1}]
with mock.patch.object(steam_api, "player_achievements", return_value=player) as player_mock, \
mock.patch.object(steam_api, "schema_achievements", return_value=schema) as schema_mock:
data = steam_api.achievements(1, "765")
self.assertEqual(data["total"], 2)
self.assertEqual(data["achieved"], 1)
self.assertEqual(data["percent"], 50.0)
self.assertEqual(data["achievements"][0]["title"], "First")
self.assertTrue(data["achievements"][1]["hidden"])
steam_api.achievements(1, "765")
self.assertEqual(player_mock.call_count, 1)
self.assertEqual(schema_mock.call_count, 1)
def test_no_stats_returns_none(self):
with mock.patch.object(steam_api, "player_achievements", return_value=None):
self.assertIsNone(steam_api.achievements(1, "765"))
def test_api_error_does_not_leak_key(self):
error = urllib.error.HTTPError("https://example", 403, "Forbidden", {}, None)
with mock.patch.dict(os.environ, {"STEAMBASE_STEAM_API_KEY": "secret-key"}, clear=False), \
mock.patch("urllib.request.urlopen", side_effect=error):
with self.assertRaises(steam_api.SteamApiError) as context:
steam_api._get_json("ISteamUserStats/GetPlayerAchievements/v0001", {"appid": 1})
message = str(context.exception)
self.assertIn("403", message)
self.assertNotIn("secret-key", message)
if __name__ == "__main__":
unittest.main()