laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Tests of `auth::pages` that need an application.

use crate::auth::pages::{__Page, __PageKind};
use crate::foundation::StartupError;
use crate::routing::Router;

async fn login() {}
async fn other_login() {}
async fn reset() {}

inventory::submit! {
    __Page { crate_name: "two_logins", register: |auth| auth.__page(__PageKind::Login, login, "src/a.rs:1") }
}
inventory::submit! {
    __Page { crate_name: "two_logins", register: |auth| auth.__page(__PageKind::Login, other_login, "src/b.rs:2") }
}
inventory::submit! {
    __Page { crate_name: "pages", register: |auth| auth.__page(__PageKind::Login, login, "src/a.rs:1") }
}
inventory::submit! {
    __Page { crate_name: "pages", register: |auth| auth.__page(__PageKind::ResetPassword, reset, "src/c.rs:3") }
}

fn start(crate_name: &'static str, routes: fn(&mut Router)) -> Result<crate::Application, StartupError> {
    crate::Application::configure()
        .replace_env(crate::config::Env::default())
        .with_auth(move |auth, _| {
            auth.discover(crate_name);
        })
        .with_routing(move |routing, _| {
            routing.web(routes);
        })
        .try_create()
}

/// The problems that stop the application from creating or serving, as `StartupError::Auth`.
fn problems(result: Result<crate::Application, StartupError>) -> Vec<String> {
    match result.and_then(|app| app.check_auth_pages()) {
        Err(StartupError::Auth(problems)) => problems,
        Err(error) => panic!("expected invalid pages, got {error}"),
        Ok(()) => panic!("expected invalid pages, but the application serves"),
    }
}

#[test]
fn two_handlers_cannot_be_the_same_page() {
    let problems = problems(start("two_logins", |route| {
        route.get("/login", login);
        route.get("/other-login", other_login);
    }));

    assert_eq!(problems.len(), 1);
    assert!(problems[0].contains("src/a.rs:1"), "{problems:?}");
    assert!(problems[0].contains("src/b.rs:2"), "{problems:?}");
    assert!(
        problems[0].contains("are both marked `#[laravel::auth::page(login)]`"),
        "{problems:?}"
    );
}

#[test]
fn marked_pages_need_routes_and_the_reset_page_needs_a_token() {
    let problems = problems(start("pages", |route| {
        route.get("/reset-password", reset);
    }));

    assert_eq!(problems.len(), 2, "{problems:?}");
    assert!(problems[0].contains("`#[laravel::auth::page(login)]` at src/a.rs:1, but no route uses it"));
    assert!(problems[1].contains("its route needs the `{token}` parameter"));
}