#
mcaptcha/pages/
mod.rs

    1// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
    2// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
    3//
    4// SPDX-License-Identifier: AGPL-3.0-or-later
    5
    6use actix_auth_middleware::Authentication;
    7use actix_web::web::ServiceConfig;
    8
    9mod auth;
    10pub mod errors;
    11mod panel;
    12pub mod routes;
    13mod sitemap;
    14
    15pub const NAME: &str = "mCaptcha";
    16
    17pub fn services(cfg: &mut ServiceConfig) {
    18    auth::services(cfg);
    19    panel::services(cfg);
    20    errors::services(cfg);
    21    cfg.service(sitemap::sitemap);
    22}
    23
    24pub fn get_middleware() -> Authentication<routes::Routes> {
    25    Authentication::with_identity(routes::ROUTES)
    26}
    27
    28#[cfg(test)]
    29mod tests {
    30    use actix_web::http::StatusCode;
    31    use actix_web::test;
    32
    33    use super::*;
    34    use crate::tests::*;
    35    use crate::*;
    36
    37    #[actix_rt::test]
    38    async fn protected_pages_templates_work_pg_test() {
    39        let data = pg::get_data().await;
    40        protected_pages_templates_work(data).await;
    41    }
    42
    43    #[actix_rt::test]
    44    async fn protected_pages_templates_work_maria_test() {
    45        let data = maria::get_data().await;
    46        protected_pages_templates_work(data).await;
    47    }
    48
    49    async fn protected_pages_templates_work(data: ArcData) {
    50        const NAME: &str = "templateuser";
    51        const PASSWORD: &str = "longpassword";
    52        const EMAIL: &str = "templateuser@a.com";
    53
    54        let data = &data;
    55        delete_user(data, NAME).await;
    56
    57        register_and_signin(data, NAME, EMAIL, PASSWORD).await;
    58        let (_, signin_resp, token_key) = add_levels_util(data, NAME, PASSWORD).await;
    59        let cookies = get_cookie!(signin_resp);
    60
    61        let app = get_app!(data).await;
    62
    63        let edit_sitekey_url = PAGES.panel.sitekey.get_edit_advance(&token_key.key);
    64        let delete_sitekey_url = PAGES.panel.sitekey.get_delete(&token_key.key);
    65        let urls = [
    66            PAGES.home,
    67            PAGES.panel.sitekey.add_advance,
    68            PAGES.panel.sitekey.add_easy,
    69            PAGES.panel.sitekey.list,
    70            PAGES.panel.notifications,
    71            PAGES.panel.settings.home,
    72            PAGES.panel.settings.delete_account,
    73            PAGES.panel.settings.update_secret,
    74            &delete_sitekey_url,
    75            &edit_sitekey_url,
    76        ];
    77
    78        for url in urls.iter() {
    79            let resp =
    80                test::call_service(&app, test::TestRequest::get().uri(url).to_request())
    81                    .await;
    82            assert_eq!(resp.status(), StatusCode::FOUND);
    83
    84            let authenticated_resp = test::call_service(
    85                &app,
    86                test::TestRequest::get()
    87                    .uri(url)
    88                    .cookie(cookies.clone())
    89                    .to_request(),
    90            )
    91            .await;
    92
    93            assert_eq!(authenticated_resp.status(), StatusCode::OK);
    94        }
    95
    96        delete_user(data, NAME).await;
    97    }
    98
    99    #[actix_rt::test]
    100    async fn public_pages_tempaltes_work() {
    101        let app = test::init_service(App::new().configure(services)).await;
    102        let urls = [PAGES.auth.login, PAGES.auth.join, PAGES.sitemap];
    103
    104        for url in urls.iter() {
    105            let resp =
    106                test::call_service(&app, test::TestRequest::get().uri(url).to_request())
    107                    .await;
    108
    109            assert_eq!(resp.status(), StatusCode::OK);
    110        }
    111    }
    112}