|
| 1 | +""" |
| 2 | +/results/usage ルートと Usage ナビゲーションのテスト |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import tempfile |
| 9 | +import shutil |
| 10 | +import types |
| 11 | + |
| 12 | +import fakeredis |
| 13 | +import pytest |
| 14 | +from flask import Flask |
| 15 | + |
| 16 | + |
| 17 | +def _setup_stubs(): |
| 18 | + if "redis" not in sys.modules: |
| 19 | + sys.modules["redis"] = types.ModuleType("redis") |
| 20 | + |
| 21 | + otp_mod = types.ModuleType("utils.otp_manager") |
| 22 | + otp_mod.get_affiliations = lambda email: ["dev"] |
| 23 | + otp_mod.is_allowed = lambda email: True |
| 24 | + sys.modules["utils.otp_manager"] = otp_mod |
| 25 | + |
| 26 | + otp_redis_mod = types.ModuleType("utils.otp_redis_manager") |
| 27 | + otp_redis_mod.get_affiliations = lambda email: ["dev"] |
| 28 | + otp_redis_mod.is_allowed = lambda email: True |
| 29 | + otp_redis_mod.send_otp = lambda email: (True, "stub") |
| 30 | + otp_redis_mod.verify_otp = lambda email, code: True |
| 31 | + otp_redis_mod.invalidate_otp = lambda email: None |
| 32 | + sys.modules["utils.otp_redis_manager"] = otp_redis_mod |
| 33 | + |
| 34 | + |
| 35 | +_setup_stubs() |
| 36 | + |
| 37 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 38 | + |
| 39 | +from routes.results import results_bp |
| 40 | +from routes.estimated import estimated_bp |
| 41 | +from routes.auth import auth_bp |
| 42 | +from routes.admin import admin_bp |
| 43 | +from utils.user_store import UserStore |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def tmp_dirs(): |
| 48 | + received = tempfile.mkdtemp() |
| 49 | + estimated = tempfile.mkdtemp() |
| 50 | + yield received, estimated |
| 51 | + shutil.rmtree(received) |
| 52 | + shutil.rmtree(estimated) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def app(tmp_dirs): |
| 57 | + received, estimated = tmp_dirs |
| 58 | + template_dir = os.path.join(os.path.dirname(__file__), "..", "templates") |
| 59 | + |
| 60 | + app = Flask(__name__, template_folder=template_dir) |
| 61 | + app.config["RECEIVED_DIR"] = received |
| 62 | + app.config["ESTIMATED_DIR"] = estimated |
| 63 | + app.config["SECRET_KEY"] = "test-secret" |
| 64 | + app.config["TESTING"] = True |
| 65 | + |
| 66 | + r_conn = fakeredis.FakeRedis(decode_responses=True) |
| 67 | + app.config["REDIS_CONN"] = r_conn |
| 68 | + app.config["REDIS_PREFIX"] = "test:" |
| 69 | + store = UserStore(r_conn, "test:") |
| 70 | + app.config["USER_STORE"] = store |
| 71 | + app.config["TOTP_ISSUER"] = "BenchKit-Test" |
| 72 | + |
| 73 | + store.create_user("admin@example.com", "SECRET", ["admin"]) |
| 74 | + store.create_user("user@example.com", "SECRET", ["dev"]) |
| 75 | + |
| 76 | + app.register_blueprint(results_bp, url_prefix="/results") |
| 77 | + app.register_blueprint(estimated_bp, url_prefix="/estimated") |
| 78 | + app.register_blueprint(auth_bp, url_prefix="/auth") |
| 79 | + app.register_blueprint(admin_bp, url_prefix="/admin") |
| 80 | + |
| 81 | + @app.route("/systemlist") |
| 82 | + def systemlist(): |
| 83 | + return "" |
| 84 | + |
| 85 | + yield app |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def client(app): |
| 90 | + return app.test_client() |
| 91 | + |
| 92 | + |
| 93 | +def _login_session(client, email, affiliations): |
| 94 | + with client.session_transaction() as sess: |
| 95 | + sess["authenticated"] = True |
| 96 | + sess["user_email"] = email |
| 97 | + sess["user_affiliations"] = affiliations |
| 98 | + |
| 99 | + |
| 100 | +def _write_result(directory, filename, data): |
| 101 | + path = os.path.join(directory, filename) |
| 102 | + with open(path, "w", encoding="utf-8") as f: |
| 103 | + json.dump(data, f, ensure_ascii=False) |
| 104 | + return path |
| 105 | + |
| 106 | + |
| 107 | +class TestUsageRoute: |
| 108 | + def test_unauthenticated_user_is_redirected_to_login(self, client): |
| 109 | + resp = client.get("/results/usage") |
| 110 | + assert resp.status_code == 302 |
| 111 | + assert "/auth/login" in resp.headers["Location"] |
| 112 | + |
| 113 | + def test_non_admin_user_gets_403(self, client): |
| 114 | + _login_session(client, "user@example.com", ["dev"]) |
| 115 | + resp = client.get("/results/usage") |
| 116 | + assert resp.status_code == 403 |
| 117 | + |
| 118 | + def test_admin_user_can_access_usage_page(self, client): |
| 119 | + _login_session(client, "admin@example.com", ["admin"]) |
| 120 | + resp = client.get("/results/usage") |
| 121 | + assert resp.status_code == 200 |
| 122 | + assert "Usage Report" in resp.get_data(as_text=True) |
| 123 | + |
| 124 | + def test_usage_route_uses_default_parameters(self, app, client, monkeypatch): |
| 125 | + _login_session(client, "admin@example.com", ["admin"]) |
| 126 | + |
| 127 | + captured = {} |
| 128 | + |
| 129 | + def fake_aggregate(directory, fiscal_year, period_type): |
| 130 | + captured["directory"] = directory |
| 131 | + captured["fiscal_year"] = fiscal_year |
| 132 | + captured["period_type"] = period_type |
| 133 | + return { |
| 134 | + "apps": [], |
| 135 | + "systems": [], |
| 136 | + "periods": ["FY2025"], |
| 137 | + "table": {}, |
| 138 | + "row_totals": {}, |
| 139 | + "col_totals": {}, |
| 140 | + "grand_totals": {}, |
| 141 | + "available_fiscal_years": [2025], |
| 142 | + } |
| 143 | + |
| 144 | + import routes.results as results_mod |
| 145 | + |
| 146 | + monkeypatch.setattr(results_mod, "aggregate_node_hours", fake_aggregate) |
| 147 | + monkeypatch.setattr(results_mod, "get_fiscal_year", lambda dt: 2025) |
| 148 | + |
| 149 | + resp = client.get("/results/usage") |
| 150 | + assert resp.status_code == 200 |
| 151 | + assert captured["directory"] == app.config["RECEIVED_DIR"] |
| 152 | + assert captured["fiscal_year"] == 2025 |
| 153 | + assert captured["period_type"] == "fiscal_year" |
| 154 | + |
| 155 | + def test_usage_page_shows_no_data_message(self, client): |
| 156 | + _login_session(client, "admin@example.com", ["admin"]) |
| 157 | + resp = client.get("/results/usage") |
| 158 | + assert resp.status_code == 200 |
| 159 | + assert "該当期間のデータがありません" in resp.get_data(as_text=True) |
| 160 | + |
| 161 | + def test_admin_navigation_shows_usage_link(self, client, tmp_dirs): |
| 162 | + received, _ = tmp_dirs |
| 163 | + _write_result( |
| 164 | + received, |
| 165 | + "result_20260401_123456_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.json", |
| 166 | + {"code": "qws", "system": "Fugaku", "Exp": "CASE0", "FOM": 1.0}, |
| 167 | + ) |
| 168 | + _login_session(client, "admin@example.com", ["admin"]) |
| 169 | + resp = client.get("/results/confidential") |
| 170 | + text = resp.get_data(as_text=True) |
| 171 | + assert resp.status_code == 200 |
| 172 | + assert "/results/usage" in text |
| 173 | + |
| 174 | + def test_non_admin_navigation_hides_usage_link(self, client, tmp_dirs): |
| 175 | + received, _ = tmp_dirs |
| 176 | + _write_result( |
| 177 | + received, |
| 178 | + "result_20260401_123456_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.json", |
| 179 | + {"code": "qws", "system": "Fugaku", "Exp": "CASE0", "FOM": 1.0}, |
| 180 | + ) |
| 181 | + _login_session(client, "user@example.com", ["dev"]) |
| 182 | + resp = client.get("/results/confidential") |
| 183 | + text = resp.get_data(as_text=True) |
| 184 | + assert resp.status_code == 200 |
| 185 | + assert "/results/usage" not in text |
0 commit comments