The `UserClaims` structure is an implementation detail of how the JWT encoding process works. We do not need to expose all of the details of the JWT, such as issuer, audience, expiration, etc. to rest of the application. Route handlers should only be concerned with the information about the user, rather than the metadata about how the user was authenticated.
66 lines
2.2 KiB
Rust
66 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::User;
|
|
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() {
|
|
super::setup();
|
|
let client = Client::tracked(seensite::rocket()).unwrap();
|
|
let ctx: &Context = client.rocket().state().unwrap();
|
|
let user = User::new("test1");
|
|
let token = ctx.make_jwt(&user, 60).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() {
|
|
super::setup();
|
|
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);
|
|
}
|