|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +import sqlite3 |
| 4 | +import sys |
| 5 | + |
| 6 | +from flask import Flask |
| 7 | + |
| 8 | + |
| 9 | +rewards = sys.modules["rewards_mod"] |
| 10 | +ADMIN_KEY = "abcdefghijklmnopqrstuvwxyz123456" |
| 11 | + |
| 12 | + |
| 13 | +def _app_with_balances(tmp_path, monkeypatch): |
| 14 | + db_path = tmp_path / "rewards.db" |
| 15 | + with sqlite3.connect(db_path) as db: |
| 16 | + db.execute("CREATE TABLE balances (miner_id TEXT PRIMARY KEY, amount_i64 INTEGER)") |
| 17 | + db.execute("INSERT INTO balances VALUES (?, ?)", ("alice", 1_000_000)) |
| 18 | + |
| 19 | + monkeypatch.setenv("RC_ADMIN_KEY", ADMIN_KEY) |
| 20 | + app = Flask(__name__) |
| 21 | + app.config["TESTING"] = True |
| 22 | + rewards.register_rewards_rip200(app, str(db_path)) |
| 23 | + return app |
| 24 | + |
| 25 | + |
| 26 | +def test_wallet_balance_admin_guard_uses_module_hmac(tmp_path, monkeypatch): |
| 27 | + app = _app_with_balances(tmp_path, monkeypatch) |
| 28 | + |
| 29 | + response = app.test_client().get( |
| 30 | + "/wallet/balance?miner_id=alice", |
| 31 | + headers={"X-Admin-Key": ADMIN_KEY}, |
| 32 | + ) |
| 33 | + |
| 34 | + assert response.status_code == 200 |
| 35 | + assert response.get_json()["amount_i64"] == 1_000_000 |
| 36 | + |
| 37 | + |
| 38 | +def test_wallet_balance_rejects_wrong_admin_key_without_500(tmp_path, monkeypatch): |
| 39 | + app = _app_with_balances(tmp_path, monkeypatch) |
| 40 | + |
| 41 | + response = app.test_client().get( |
| 42 | + "/wallet/balance?miner_id=alice", |
| 43 | + headers={"X-Admin-Key": "wrong"}, |
| 44 | + ) |
| 45 | + |
| 46 | + assert response.status_code == 401 |
| 47 | + assert response.get_json()["error"].startswith("Unauthorized") |
| 48 | + |
| 49 | + |
| 50 | +def test_all_balances_admin_guard_uses_module_hmac(tmp_path, monkeypatch): |
| 51 | + app = _app_with_balances(tmp_path, monkeypatch) |
| 52 | + |
| 53 | + response = app.test_client().get( |
| 54 | + "/wallet/balances/all", |
| 55 | + headers={"X-Admin-Key": ADMIN_KEY}, |
| 56 | + ) |
| 57 | + |
| 58 | + assert response.status_code == 200 |
| 59 | + assert response.get_json()["total_urtc"] == 1_000_000 |
0 commit comments