Skip to main content

headless_lms_server/controllers/mock_suotar/
mod.rs

1/*!
2Mock Suotar: a stand-in for the University of Helsinki Suotar API, and the test/dev control surface
3around it.
4
5Mounted at `/api/v0/mock-suotar` only when `TEST_MODE` and `USE_MOCK_SUOTAR_ENDPOINT` are both on;
6like `mock_sisu`, the gate is runtime route registration rather than `#[cfg]`. The simulator behind
7these routes is [`crate::mock_suotar`].
8*/
9
10pub mod api;
11pub mod commands;
12pub mod control;
13
14use actix_web::{Resource, Scope};
15
16use crate::prelude::*;
17
18/// The route cannot be reached with the flags off, so tripping this means the gate itself broke.
19pub fn assert_enabled(app_conf: &ApplicationConfiguration) {
20    assert!(app_conf.test_mode && app_conf.test_suotar);
21}
22
23/// The mock's whole scope. Unmatched paths and methods fall through the way Suotar's router does,
24/// which is why the contract routes are resources with their own fallback.
25pub fn scope() -> Scope {
26    web::scope("/mock-suotar")
27        .service(
28            web::scope("/control")
29                .configure(control::_add_routes)
30                .default_service(web::to(HttpResponse::NotFound)),
31        )
32        .service(
33            contract_route("/persons/resolve-by-student-numbers")
34                .route(web::post().to(api::resolve_persons)),
35        )
36        .service(
37            contract_route("/enrolments/resolve").route(web::post().to(api::resolve_enrolments)),
38        )
39        .service(
40            contract_route("/enrolments/list-by-course").route(web::post().to(api::list_by_course)),
41        )
42        .service(
43            contract_route("/attainments/import").route(web::post().to(api::import_attainments)),
44        )
45        .service(
46            contract_route("/attainments/verify").route(web::post().to(api::verify_attainments)),
47        )
48        .service(
49            contract_route("/course-codes/validate")
50                .route(web::post().to(api::validate_course_codes)),
51        )
52        .default_service(web::to(api::fall_through))
53}
54
55fn contract_route(path: &str) -> Resource {
56    web::resource(path).default_service(web::to(api::fall_through))
57}