igdb: auto-refresh twitch token; category -> game_type filter
Static app tokens expire after ~60 days -> every lookup failed with 401. With igdb-client-secret set, the bot fetches the token via client-credentials OAuth itself, refreshes a day before expiry and retries once on 401; a static igdb-access-token still works. IGDB renamed games.category to game_type: the category = 0 filter in search_games silently matched nothing even with a valid token. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+81
-1
@@ -160,7 +160,7 @@ class TestIGDBQuery(unittest.TestCase):
|
||||
"id",
|
||||
"name",
|
||||
"alternative_names",
|
||||
"category",
|
||||
"game_type",
|
||||
"release_dates",
|
||||
"franchise",
|
||||
"language_supports",
|
||||
@@ -174,5 +174,85 @@ class TestIGDBQuery(unittest.TestCase):
|
||||
self.assertEqual(result, [{"id": 1, "name": "Super Mario Bros"}])
|
||||
|
||||
|
||||
class TestIGDBTokenRefresh(unittest.TestCase):
|
||||
@staticmethod
|
||||
def _oauth_response(token="fresh_token", expires_in=5_000_000):
|
||||
response = Mock()
|
||||
response.json.return_value = {"access_token": token, "expires_in": expires_in}
|
||||
response.raise_for_status.return_value = None
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def _api_response(payload, status_code=200):
|
||||
response = Mock()
|
||||
response.status_code = status_code
|
||||
response.json.return_value = payload
|
||||
response.raise_for_status.return_value = None
|
||||
return response
|
||||
|
||||
@patch("fjerkroa_bot.igdblib.requests.post")
|
||||
def test_fetches_token_when_only_secret_configured(self, mock_post):
|
||||
"""Without a static token, the first request fetches one via Twitch OAuth."""
|
||||
mock_post.side_effect = [self._oauth_response(), self._api_response([{"id": 1}])]
|
||||
igdb = IGDBQuery("cid", client_secret="secret")
|
||||
|
||||
result = igdb.send_igdb_request("games", "fields name; limit 1;")
|
||||
|
||||
self.assertEqual(result, [{"id": 1}])
|
||||
oauth_call, api_call = mock_post.call_args_list
|
||||
self.assertEqual(oauth_call.args[0], "https://id.twitch.tv/oauth2/token")
|
||||
self.assertEqual(
|
||||
oauth_call.kwargs["params"],
|
||||
{"client_id": "cid", "client_secret": "secret", "grant_type": "client_credentials"},
|
||||
)
|
||||
self.assertEqual(api_call.kwargs["headers"]["Authorization"], "Bearer fresh_token")
|
||||
|
||||
@patch("fjerkroa_bot.igdblib.requests.post")
|
||||
def test_refreshes_and_retries_on_401(self, mock_post):
|
||||
"""A 401 with a configured secret triggers one refresh and retry."""
|
||||
mock_post.side_effect = [
|
||||
self._api_response(None, status_code=401),
|
||||
self._oauth_response(),
|
||||
self._api_response([{"id": 2}]),
|
||||
]
|
||||
igdb = IGDBQuery("cid", "expired_token", client_secret="secret")
|
||||
|
||||
result = igdb.send_igdb_request("games", "fields name; limit 1;")
|
||||
|
||||
self.assertEqual(result, [{"id": 2}])
|
||||
self.assertEqual(igdb.igdb_api_key, "fresh_token")
|
||||
self.assertEqual(mock_post.call_args_list[2].kwargs["headers"]["Authorization"], "Bearer fresh_token")
|
||||
|
||||
@patch("fjerkroa_bot.igdblib.time.time")
|
||||
@patch("fjerkroa_bot.igdblib.requests.post")
|
||||
def test_proactive_refresh_before_expiry(self, mock_post, mock_time):
|
||||
"""An expired self-fetched token is refreshed before the request."""
|
||||
mock_time.return_value = 1_000_000.0
|
||||
mock_post.side_effect = [self._oauth_response("token_a", expires_in=5_000_000), self._api_response([])]
|
||||
igdb = IGDBQuery("cid", client_secret="secret")
|
||||
igdb.send_igdb_request("games", "fields name;")
|
||||
|
||||
# jump past the token expiry -> next request refreshes first
|
||||
mock_time.return_value = 1_000_000.0 + 5_000_000
|
||||
mock_post.side_effect = [self._oauth_response("token_b"), self._api_response([])]
|
||||
igdb.send_igdb_request("games", "fields name;")
|
||||
|
||||
self.assertEqual(igdb.igdb_api_key, "token_b")
|
||||
|
||||
@patch("fjerkroa_bot.igdblib.requests.post")
|
||||
def test_no_refresh_without_secret(self, mock_post):
|
||||
"""Static-token setups keep the old behavior: no OAuth calls, error -> None."""
|
||||
response = Mock()
|
||||
response.status_code = 401
|
||||
response.raise_for_status.side_effect = requests.RequestException("401 Client Error")
|
||||
mock_post.return_value = response
|
||||
igdb = IGDBQuery("cid", "expired_token")
|
||||
|
||||
result = igdb.send_igdb_request("games", "fields name; limit 1;")
|
||||
|
||||
self.assertIsNone(result)
|
||||
mock_post.assert_called_once()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user