Add more tests

This commit is contained in:
Alex Kotov 2021-03-06 08:04:44 +05:00
parent e11375129d
commit 9685a6dbfc
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,32 @@
#![feature(decl_macro)]
#[macro_use] extern crate rocket;
const COOKIE_NAME: &str = "foobar";
const COOKIE_LEN: usize = 64;
fn client() -> rocket::local::Client {
rocket::local::Client::new(rocket()).unwrap()
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.attach(rocket_csrf::Fairing::new(
rocket_csrf::CsrfConfig::default()
.with_cookie_name(COOKIE_NAME)
.with_cookie_len(COOKIE_LEN)
.with_lifetime(time::Duration::days(3))
))
.mount("/", routes![index])
}
#[get("/")]
fn index() {
}
#[test]
fn add_csrf_token_to_cookies() {
base64::decode(client().get("/").dispatch().cookies().iter().find(|cookie| {
cookie.name() == COOKIE_NAME
}).unwrap().value()).unwrap();
}

50
tests/guard_configured.rs Normal file
View File

@ -0,0 +1,50 @@
#![feature(decl_macro)]
#[macro_use] extern crate rocket;
use rand::RngCore;
use rocket::http::Cookie;
use rocket_csrf::CsrfToken;
use bcrypt::verify;
const COOKIE_NAME: &str = "foobar";
const COOKIE_LEN: usize = 64;
fn client() -> rocket::local::Client {
rocket::local::Client::new(rocket()).unwrap()
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.attach(rocket_csrf::Fairing::new(
rocket_csrf::CsrfConfig::default()
.with_cookie_name(COOKIE_NAME)
.with_cookie_len(COOKIE_LEN)
.with_lifetime(time::Duration::days(3))
))
.mount("/", routes![index])
}
#[get("/")]
fn index(csrf_token: CsrfToken) -> String {
csrf_token.authenticity_token().to_string()
}
#[test]
fn respond_with_valid_authenticity_token() {
let mut raw = [0u8; COOKIE_LEN];
rand::thread_rng().fill_bytes(&mut raw);
let encoded = base64::encode(raw);
let body = client()
.get("/")
.private_cookie(Cookie::new(COOKIE_NAME, encoded.to_string()))
.dispatch()
.body()
.unwrap()
.into_string()
.unwrap();
assert!(verify(&encoded, &body).unwrap());
}