You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.rs 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. mod tests {
  2. use async_std::task::block_on;
  3. use oslo_lib::{State};
  4. use tide::{
  5. http::{Method, Request, Response, StatusCode, Url},
  6. Result, Server,
  7. };
  8. async fn build_app() -> Server<State> {
  9. oslo_lib::build_app().await.unwrap()
  10. }
  11. fn url(path: &str) -> Url {
  12. Url::parse("http://localhost:8080")
  13. .unwrap()
  14. .join(path)
  15. .unwrap()
  16. }
  17. #[test]
  18. fn test_login_page() -> Result<()> {
  19. block_on(async {
  20. let app = build_app().await;
  21. let req = Request::new(Method::Get, url("login"));
  22. let mut res: Response = app.respond(req).await?;
  23. assert_eq!(res.status(), 200);
  24. assert!(res.body_string().await?.contains("Login"));
  25. Ok(())
  26. })
  27. }
  28. #[test]
  29. fn test_logging_in() -> Result<()> {
  30. block_on(async {
  31. let app = build_app().await;
  32. let mut req = Request::new(Method::Post, url("login"));
  33. req.set_content_type(tide::http::mime::MULTIPART_FORM);
  34. let username = std::env::var("ADMIN_USERNAME").unwrap();
  35. let password = std::env::var("ADMIN_PASSWORD").unwrap();
  36. req.replace_body(format!("username={}&password={}", username, password));
  37. let res: Response = app.respond(req).await?;
  38. assert_eq!(res.status(), StatusCode::Found);
  39. assert_eq!(res.header("location").unwrap(), "/");
  40. Ok(())
  41. })
  42. }
  43. #[test]
  44. fn test_invalid_credentials() -> Result<()> {
  45. block_on(async {
  46. let app = build_app().await;
  47. let mut req = Request::new(Method::Post, url("login"));
  48. req.set_content_type(tide::http::mime::MULTIPART_FORM);
  49. req.replace_body("username=invalid&password=credentials");
  50. let res: Response = app.respond(req).await?;
  51. assert_eq!(res.status(), StatusCode::Found);
  52. assert_eq!(res.header("location").unwrap(), "/login");
  53. Ok(())
  54. })
  55. }
  56. #[test]
  57. fn test_accessing_protected_route_as_gest() -> Result<()> {
  58. block_on(async {
  59. let app = build_app().await;
  60. let req = Request::new(Method::Get, url("/posts/1/edit"));
  61. let res: Response = app.respond(req).await?;
  62. assert_eq!(res.status(), StatusCode::Found);
  63. assert_eq!(res.header("location").unwrap(), "/login");
  64. Ok(())
  65. })
  66. }
  67. }