Files
seensite/tests/integration/page.rs
Dustin C. Hatch 76cf57ebe0 Begin integration tests
Refactoring the code a bit here to make the `Rocket` instance available
to the integration tests.  To do this, we have to convert to a library
crate (`lib.rs`) with an executable entry point (`main.rs`).  This
allows the tests, which are separate crates, to import types and
functions from the library.

Besides splitting the `rocket` function into two parts (one in `lib.rs`
that creates the `Rocket<Build>` and another in `main.rs` that becomes
the process entry point), I have reworked the initialization process to
make better use of Rocket's "fairings" feature.  We don't want to call
`process::exit()` in a test, so if there is a problem reading the
configuration or initializing the context, we need to report it to
Rocket instead.
2025-04-05 17:07:39 -05:00

64 lines
2.2 KiB
Rust

use form_urlencoded::Serializer;
use rocket::http::Status;
use rocket::http::{ContentType, Header};
use rocket::local::blocking::Client;
use rocket::serde::json::Value;
use rocket::uri;
use seensite::auth::UserClaims;
use seensite::page::*;
use seensite::Context;
static TEST_URL: &str = r"http://example.org/page1.html";
static TEST_HTML: &str = r#"<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<h1>Example Page</title>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec malesuada,
tellus eu fringilla finibus, turpis sapien faucibus elit, a fringilla dolor
urna volutpat dui. Curabitur eget dui aliquet, gravida velit tempor, porta
ipsum. Donec finibus orci quis velit tincidunt placerat. Aliquam erat volutpat.
Nullam id nisl odio. Praesent egestas fringilla ultricies. Aenean blandit
lectus mauris, quis auctor ipsum porttitor quis. Vivamus egestas cursus erat,
et egestas diam volutpat eu. Vestibulum imperdiet purus ac turpis sodales, sit
amet auctor risus lacinia. Duis feugiat lobortis orci quis sagittis.</p>
</html>
"#;
#[test]
fn test_post_page() {
let client = Client::tracked(seensite::rocket()).unwrap();
let ctx: &Context = client.rocket().state().unwrap();
let claims = UserClaims::new("test1", 60);
let token = ctx.make_jwt(&claims).unwrap();
let data = Serializer::new(String::new())
.append_pair("url", TEST_URL)
.append_pair("data", TEST_HTML)
.finish();
let req = client
.post(uri![post_page])
.header(ContentType::Form)
.header(Header::new("Authorization", format!("Bearer {}", token)))
.body(&data);
let res = req.dispatch();
assert_eq!(res.status(), Status::Ok);
let page = res.into_json::<Value>().unwrap();
assert_eq!(page.get("title").unwrap().as_str().unwrap(), "Example Page");
}
#[test]
fn test_post_page_unauth() {
let client = Client::tracked(seensite::rocket()).unwrap();
let data = Serializer::new(String::new())
.append_pair("url", TEST_URL)
.append_pair("data", TEST_HTML)
.finish();
let req = client.post(uri![post_page]).body(&data);
let res = req.dispatch();
assert_eq!(res.status(), Status::Unauthorized);
}