#
mcaptcha/pages/panel/sitekey/
list.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_identity::Identity;
    7use actix_web::{HttpResponse, Responder};
    8use sailfish::TemplateOnce;
    9
    10use db_core::Captcha;
    11
    12use crate::errors::*;
    13use crate::AppData;
    14
    15#[derive(TemplateOnce, Clone)]
    16#[template(path = "panel/sitekey/list/index.html")]
    17pub struct IndexPage {
    18    sitekeys: Vec<Captcha>,
    19}
    20
    21const PAGE: &str = "SiteKeys";
    22
    23impl IndexPage {
    24    fn new(sitekeys: Vec<Captcha>) -> Self {
    25        IndexPage { sitekeys }
    26    }
    27}
    28
    29/// render a list of all sitekeys that a user has
    30#[my_codegen::get(
    31    path = "crate::PAGES.panel.sitekey.list",
    32    wrap = "crate::pages::get_middleware()"
    33)]
    34pub async fn list_sitekeys(data: AppData, id: Identity) -> PageResult<impl Responder> {
    35    let username = id.identity().unwrap();
    36    let res = data.db.get_all_user_captchas(&username).await?;
    37    let body = IndexPage::new(res).render_once().unwrap();
    38    Ok(HttpResponse::Ok()
    39        .content_type("text/html; charset=utf-8")
    40        .body(body))
    41}
    42
    43#[cfg(test)]
    44mod test {
    45    use actix_web::http::StatusCode;
    46    use actix_web::test;
    47    use actix_web::web::Bytes;
    48
    49    use crate::tests::*;
    50    use crate::*;
    51
    52    #[actix_rt::test]
    53    async fn list_sitekeys_work_pg() {
    54        let data = pg::get_data().await;
    55        list_sitekeys_work(data).await;
    56    }
    57
    58    #[actix_rt::test]
    59    async fn protected_routes_work_maria() {
    60        let data = maria::get_data().await;
    61        list_sitekeys_work(data).await;
    62    }
    63
    64    async fn list_sitekeys_work(data: ArcData) {
    65        const NAME: &str = "listsitekeyuser";
    66        const PASSWORD: &str = "longpassworddomain";
    67        const EMAIL: &str = "listsitekeyuser@a.com";
    68
    69        let data = &data;
    70        delete_user(data, NAME).await;
    71
    72        register_and_signin(data, NAME, EMAIL, PASSWORD).await;
    73        let (_, signin_resp, key) = add_levels_util(data, NAME, PASSWORD).await;
    74        let cookies = get_cookie!(signin_resp);
    75
    76        let app = get_app!(data).await;
    77
    78        let list_sitekey_resp = test::call_service(
    79            &app,
    80            test::TestRequest::get()
    81                .uri(PAGES.panel.sitekey.list)
    82                .cookie(cookies.clone())
    83                .to_request(),
    84        )
    85        .await;
    86
    87        assert_eq!(list_sitekey_resp.status(), StatusCode::OK);
    88
    89        let body: Bytes = test::read_body(list_sitekey_resp).await;
    90        let body = String::from_utf8(body.to_vec()).unwrap();
    91
    92        //        Bytes::from(key.key)
    93        //            .iter()
    94        //            .for_each(|e| assert!(body.contains(e)));
    95        //
    96        //        Bytes::from(key.name)
    97        //            .iter()
    98        //            .for_each(|e| assert!(body.contains(e)));
    99
    100        assert!(body.contains(&key.key));
    101        assert!(body.contains(&key.name));
    102    }
    103}