li7y/tests/items.rs

72 lines
2.3 KiB
Rust
Raw Normal View History

2024-07-27 22:16:12 +02:00
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use actix_web::{body::MessageBody, test};
use sqlx::{query_as, PgPool};
use uuid::Uuid;
mod common;
#[sqlx::test(fixtures("default"))]
async fn list(pool: PgPool) {
let srv = test::init_service(li7y::app(&common::config(), &pool)).await;
let session_cookie = common::session_cookie(&srv).await;
let req = test::TestRequest::get()
.uri("/items")
.cookie(session_cookie.clone())
.to_request();
let res = test::call_service(&srv, req).await;
assert!(res.status().is_success());
let body = String::from_utf8(res.into_body().try_into_bytes().unwrap().to_vec()).unwrap();
let items: Vec<(Uuid, Option<String>)> = query_as("SELECT id, name FROM items")
.fetch_all(&pool)
.await
.unwrap();
for (id, name) in items {
assert!(body.contains(&format!(r#"href="/item/{id}""#)));
if let Some(name) = name {
assert!(body.contains(&format!(">{name}</a>")));
}
}
}
#[sqlx::test(fixtures("default"))]
async fn show(pool: PgPool) {
let srv = test::init_service(li7y::app(&common::config(), &pool)).await;
let session_cookie = common::session_cookie(&srv).await;
let req = test::TestRequest::get()
.uri("/item/663f45e6-b11a-4197-8ce4-c784ac9ee617")
.cookie(session_cookie.clone())
.to_request();
let res = test::call_service(&srv, req).await;
assert!(res.status().is_success());
let body = String::from_utf8(res.into_body().try_into_bytes().unwrap().to_vec()).unwrap();
assert!(body.contains("<h2>Item 2 <"));
assert!(body.contains("<th>UUID</th><td>663f45e6-b11a-4197-8ce4-c784ac9ee617</td>"));
assert!(body.contains("<th>Name</th><td>Item 2</td>"));
assert!(body
.contains(r#"href="/item-class/8a979306-b4c6-4ef8-900d-68f64abb2975">Subclass 1.1</a>"#));
assert!(body.contains(r#"href="/item/4fc0f5f4-4dca-4c24-844d-1f464cb32afa">Item 1</a>"#));
assert!(body.contains(r#"<li class="breadcrumb-item active">Item 2</li>"#));
assert!(body.contains(
r#"href="/item/049298e2-73db-42fb-957d-a741655648b1">Original Packaging of Item 2</a>"#
));
assert!(body.contains(">Lorem ipsum 3</td>"));
assert!(body.contains(">acquire</"));
}