Skip to content

Commit

Permalink
Enhance test suite with additional API endpoint coverage
Browse files Browse the repository at this point in the history
- Disabled rate limiting during tests by setting `limiter-enabled` to `False` when running under pytest.
- Added new test cases:
  - `test_uptime_head` to check the HEAD request on `/uptime`.
  - Tests for various `/nx` endpoints including `icon`, `banner`, `screen`, `screens`, `full`, `all`, `base`, `dlc`, and `update`.
  - Tests for unsupported platforms and nonexistent games to ensure proper error handling.
  • Loading branch information
Lenochxd committed Sep 3, 2024
1 parent 115f2c4 commit 2313733
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def load_config():
config['app-port'] = config.get('app-port', 80)
config['database-path'] = 'tests/test-db' if 'pytest' in sys.modules else config.get('database-path', '/data/NX-DB')

config['limiter-enabled'] = config.get('limiter-enabled', True)
config['limiter-enabled'] = False if 'pytest' in sys.modules else config.get('limiter-enabled', True)
config['rate-limit'] = config.get('rate-limit', 1)
config['rate-limit-period'] = config.get('rate-limit-period', 5)

Expand Down
12 changes: 12 additions & 0 deletions tests/test-db/fulldb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"titledb": {
"0100000000010000": {
"id": "0100000000010000",
"name": "Super Mario Odyssey™",
"publisher": "Nintendo",
"releaseDate": 20171027,
"description": "mario 64 but bad!!! /hj",
"version": 0
}
}
}
81 changes: 74 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
import warnings
from fastapi.testclient import TestClient
from main import app
from main import app, config
from time import sleep

warnings.filterwarnings("ignore", category=DeprecationWarning, message="The 'app' shortcut is now deprecated")
client = TestClient(app)
Expand All @@ -12,21 +13,87 @@ def test_uptime():
response = client.get("/uptime")
assert response.status_code == 200

def test_uptime_head():
response = client.head("/uptime")
assert response.status_code == 200

def test_get_nx_without_asset_type():
response = client.get(f"/nx/{GAME_ID}")
assert response.status_code == 200
assert response.json().get("console") == "nx"

def test_get_switch_without_asset_type():
response = client.get(f"/switch/{GAME_ID}")
assert response.status_code == 200
assert response.json().get("console") == "nx"

def test_get_nx_icon():
response = client.get(f"/nx/{GAME_ID}/icon")
if response.status_code == 200:
assert response.headers['content-type'] == 'image/jpeg'
else:
assert response.status_code == 404

def test_rate_limiting():
# Exceed the rate limit to test if 429 is returned
for _ in range(10): # Sending more requests than the rate limit allows
response = client.get("/uptime")
assert response.status_code == 429
assert response.json().get("detail") == "Too many requests, please try again later."
def test_get_nx_banner():
response = client.get(f"/nx/{GAME_ID}/banner")
if response.status_code == 200:
assert response.headers['content-type'] == 'image/jpeg'
else:
assert response.status_code == 404

def test_get_nx_screen():
response = client.get(f"/nx/{GAME_ID}/screen")
if response.status_code == 200:
assert response.headers['content-type'] == 'image/jpeg'
else:
assert response.status_code == 404

def test_get_nx_screen_with_id():
response = client.get(f"/nx/{GAME_ID}/screen/2")
if response.status_code == 200:
assert response.headers['content-type'] == 'image/jpeg'
else:
assert response.status_code == 404

def test_get_nx_screens():
response = client.get(f"/nx/{GAME_ID}/screens")
assert response.status_code == 200
assert "count" in response.json()
assert "screenshots" in response.json()

def test_get_nx_full():
response = client.get("/nx/full")
assert response.status_code == 200
assert "titledb" in response.json()
assert isinstance(response.json()['titledb']['0100000000010000'], dict)

def test_get_nx_all():
response = client.get("/nx/all")
assert response.status_code == 200

def test_get_nx_base():
response = client.get(f"/nx/BASE/{GAME_ID}")
assert response.status_code == 200
assert response.json().get("type") == "base"

def test_get_nx_dlc():
response = client.get(f"/nx/DLC/{GAME_ID}")
if response.status_code == 200:
assert response.json().get("type") == "dlc"
else:
assert response.status_code == 400

def test_get_nx_update():
response = client.get(f"/nx/UPDATE/{GAME_ID}")
if response.status_code == 200:
assert response.json().get("type") == "update"
else:
assert response.status_code == 400

def test_get_unsupported_platform():
response = client.get(f"/ps5/{GAME_ID}")
assert response.status_code == 404 # notice how there are no games on ps5

def test_get_nonexistent_game():
response = client.get("/nx/jadeisbetterthanyou")
assert response.status_code == 404

0 comments on commit 2313733

Please sign in to comment.