mod tests { use async_std::task::block_on; use oslo_lib::{State}; use tide::{ http::{Method, Request, Response, StatusCode, Url}, Result, Server, }; async fn build_app() -> Server { oslo_lib::build_app().await.unwrap() } fn url(path: &str) -> Url { Url::parse("http://localhost:8080") .unwrap() .join(path) .unwrap() } #[test] fn test_login_page() -> Result<()> { block_on(async { let app = build_app().await; let req = Request::new(Method::Get, url("login")); let mut res: Response = app.respond(req).await?; assert_eq!(res.status(), 200); assert!(res.body_string().await?.contains("Login")); Ok(()) }) } #[test] fn test_logging_in() -> Result<()> { block_on(async { let app = build_app().await; let mut req = Request::new(Method::Post, url("login")); req.set_content_type(tide::http::mime::MULTIPART_FORM); let username = std::env::var("ADMIN_USERNAME").unwrap(); let password = std::env::var("ADMIN_PASSWORD").unwrap(); req.replace_body(format!("username={}&password={}", username, password)); let res: Response = app.respond(req).await?; assert_eq!(res.status(), StatusCode::Found); assert_eq!(res.header("location").unwrap(), "/"); Ok(()) }) } #[test] fn test_invalid_credentials() -> Result<()> { block_on(async { let app = build_app().await; let mut req = Request::new(Method::Post, url("login")); req.set_content_type(tide::http::mime::MULTIPART_FORM); req.replace_body("username=invalid&password=credentials"); let res: Response = app.respond(req).await?; assert_eq!(res.status(), StatusCode::Found); assert_eq!(res.header("location").unwrap(), "/login"); Ok(()) }) } #[test] fn test_accessing_protected_route_as_gest() -> Result<()> { block_on(async { let app = build_app().await; let req = Request::new(Method::Get, url("/posts/1/edit")); let res: Response = app.respond(req).await?; assert_eq!(res.status(), StatusCode::Found); assert_eq!(res.header("location").unwrap(), "/login"); Ok(()) }) } }