#![cfg(feature = "sqlite")]
use rustango::core::{Column as _, Model as _};
use rustango::sql::{Auto, Pool};
use rustango_cms::page::{Page, PageStatus};
use rustango_cms::page_type_model::PageType;
use rustango_cms::site::{self, Site};
async fn create_schema(pool: &Pool) {
for schema in [
&PageType::SCHEMA,
&Page::SCHEMA,
&Site::SCHEMA,
&rustango_cms::media::Media::SCHEMA,
&rustango_cms::theme::Theme::SCHEMA,
] {
ddl(pool, schema).await;
}
}
async fn ddl(pool: &Pool, schema: &rustango::core::ModelSchema) {
let sql =
rustango::migrate::ddl::create_table_if_not_exists_sql_with_dialect(pool.dialect(), schema);
for stmt in sql.split(';').map(str::trim).filter(|s| !s.is_empty()) {
rustango::sql::raw_execute_pool(pool, stmt, Vec::new())
.await
.expect("ddl");
}
}
struct Tree {
pool: Pool,
home: i64,
shop: i64,
}
async fn tree() -> Tree {
let pool = Pool::connect("sqlite::memory:").await.expect("mem pool");
create_schema(&pool).await;
let mut pt = PageType {
id: Auto::Unset,
app_label: "cms".to_owned(),
type_name: "TestPage".to_owned(),
verbose_name: "Test page".to_owned(),
default_template: "page.html".to_owned(),
view_mode: "auto".to_owned(),
is_creatable: true,
allowed_parent_types: serde_json::json!([]),
allowed_child_types: serde_json::json!([]),
workflow: String::new(),
created_at: Auto::Unset,
updated_at: Auto::Unset,
};
pt.save(&pool).await.expect("page type");
let type_id = pt.id.get().copied().expect("page type id");
let mk = |title: &str, slug: &str, path: &str, url: &str| {
let pool = pool.clone();
let mut page = Page {
id: Auto::Unset,
page_type_id: type_id,
title: title.to_owned(),
slug: slug.to_owned(),
path: path.to_owned(),
url_path: url.to_owned(),
preview_path: String::new(),
template_override: String::new(),
depth: 1,
parent_id: None,
locale_variant_of: None,
alias_of: None,
theme_id: None,
sort_order: 0,
status: PageStatus::Published.as_str().to_owned(),
published_at: None,
last_published_at: None,
go_live_at: None,
expire_at: None,
seo_title: String::new(),
seo_description: String::new(),
robots_index: true,
sitemap_priority: 0.5,
show_in_menus: true,
og_title: String::new(),
og_description: String::new(),
og_image_media_id: None,
twitter_card: "summary".to_owned(),
notification_pre_published_sent: false,
created_at: Auto::Unset,
updated_at: Auto::Unset,
};
async move {
page.save(&pool).await.expect("page");
page.id.get().copied().expect("page id")
}
};
let home = mk("Home", "", "0001/", "/").await;
let shop = mk("Shop", "shop", "0002/", "/shop").await;
Tree { pool, home, shop }
}
async fn map(pool: &Pool, hostname: &str, root_page_id: i64) {
let mut row = Site {
id: Auto::Unset,
hostname: hostname.to_owned(),
root_page_id,
created_at: Auto::Unset,
};
row.save(pool).await.expect("site row");
}
#[tokio::test]
async fn an_unmapped_host_contributes_no_prefix() {
let t = tree().await;
let prefix = site::prefix_for_host(&t.pool, "anything.example")
.await
.expect("resolve");
assert_eq!(prefix, "");
assert_eq!(site::to_lookup_path(&prefix, "/about"), "/about");
}
#[tokio::test]
async fn a_mapped_host_serves_its_own_root_under_that_root_s_prefix() {
let t = tree().await;
map(&t.pool, "shop.example", t.shop).await;
let prefix = site::prefix_for_host(&t.pool, "shop.example")
.await
.expect("resolve");
assert_eq!(prefix, "/shop");
assert_eq!(site::to_lookup_path(&prefix, "/"), "/shop");
assert_eq!(site::to_lookup_path(&prefix, "/catalog"), "/shop/catalog");
assert_eq!(site::to_public_path(&prefix, "/shop/catalog"), "/catalog");
}
#[tokio::test]
async fn mapping_a_host_to_the_conventional_root_contributes_no_prefix() {
let t = tree().await;
map(&t.pool, "main.example", t.home).await;
let prefix = site::prefix_for_host(&t.pool, "main.example")
.await
.expect("resolve");
assert_eq!(prefix, "");
assert_eq!(site::to_lookup_path(&prefix, "/about"), "/about");
}
#[tokio::test]
async fn host_matching_ignores_case_and_surrounding_space() {
let t = tree().await;
map(&t.pool, "shop.example", t.shop).await;
for raw in ["Shop.Example", "SHOP.EXAMPLE", " shop.example "] {
assert_eq!(
site::prefix_for_host(&t.pool, raw).await.expect("resolve"),
"/shop",
"{raw:?} did not match the mapping"
);
}
}
#[tokio::test]
async fn a_mid_tree_page_serves_a_hostname_from_its_own_path() {
let t = tree().await;
map(&t.pool, "shop.example", t.shop).await;
let mut shop: Page = Page::objects()
.where_(Page::id.eq(t.shop))
.first(&t.pool)
.await
.expect("fetch")
.expect("shop");
shop.parent_id = Some(t.home);
shop.path = "0001/0002/".to_owned();
shop.url_path = "/home/shop".to_owned();
shop.depth = 2;
shop.save(&t.pool).await.expect("move");
let prefix = site::prefix_for_host(&t.pool, "shop.example")
.await
.expect("resolve");
assert_eq!(prefix, "/home/shop");
assert_eq!(
site::to_lookup_path(&prefix, "/catalog"),
"/home/shop/catalog"
);
assert_eq!(
site::to_public_path(&prefix, "/home/shop/catalog"),
"/catalog"
);
}
#[tokio::test]
async fn an_empty_host_header_serves_the_conventional_root() {
let t = tree().await;
map(&t.pool, "", t.shop).await;
assert_eq!(
site::prefix_for_host(&t.pool, "").await.expect("resolve"),
""
);
}
#[tokio::test]
async fn hosts_for_root_lists_every_alias_of_one_root() {
let t = tree().await;
map(&t.pool, "shop.example", t.shop).await;
map(&t.pool, "boutique.example", t.shop).await;
map(&t.pool, "main.example", t.home).await;
assert_eq!(
site::hosts_for_root(&t.pool, t.shop).await.expect("hosts"),
vec!["boutique.example".to_owned(), "shop.example".to_owned()],
);
assert_eq!(
site::hosts_for_root(&t.pool, t.home).await.expect("hosts"),
vec!["main.example".to_owned()],
);
}
#[tokio::test]
async fn hosts_for_root_is_empty_for_an_unmapped_root() {
let t = tree().await;
map(&t.pool, "shop.example", t.shop).await;
assert!(site::hosts_for_root(&t.pool, t.home)
.await
.expect("hosts")
.is_empty());
}
#[tokio::test]
async fn an_empty_tree_contributes_no_prefix() {
let pool = Pool::connect("sqlite::memory:").await.expect("mem pool");
create_schema(&pool).await;
assert_eq!(
site::prefix_for_host(&pool, "anything.example")
.await
.expect("resolve"),
""
);
}