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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. mod tests {
  2. use std::path::PathBuf;
  3. use async_std::task::block_on;
  4. use oslo_lib::State;
  5. use serde_json::json;
  6. use tide::{
  7. http::{Method, Request, Response, StatusCode, Url},
  8. Result, Server,
  9. };
  10. async fn build_app() -> Server<State> {
  11. oslo_lib::build_app().await.unwrap()
  12. }
  13. fn url(path: &str) -> Url {
  14. Url::parse("http://localhost:8080")
  15. .unwrap()
  16. .join(path)
  17. .unwrap()
  18. }
  19. #[test]
  20. fn test_login_page() -> Result<()> {
  21. block_on(async {
  22. let app = build_app().await;
  23. let req = Request::new(Method::Get, url("login"));
  24. let mut res: Response = app.respond(req).await?;
  25. assert_eq!(res.status(), 200);
  26. assert!(res.body_string().await?.contains("Login"));
  27. Ok(())
  28. })
  29. }
  30. #[test]
  31. fn test_logging_in() -> Result<()> {
  32. block_on(async {
  33. let app = build_app().await;
  34. let mut req = Request::new(Method::Post, url("login"));
  35. req.set_content_type(tide::http::mime::MULTIPART_FORM);
  36. let username = std::env::var("ADMIN_USERNAME").unwrap();
  37. let password = std::env::var("ADMIN_PASSWORD").unwrap();
  38. req.replace_body(format!("username={}&password={}", username, password));
  39. let res: Response = app.respond(req).await?;
  40. assert_eq!(res.status(), StatusCode::Found);
  41. assert_eq!(res.header("location").unwrap(), "/");
  42. Ok(())
  43. })
  44. }
  45. #[test]
  46. fn test_invalid_credentials() -> Result<()> {
  47. block_on(async {
  48. let app = build_app().await;
  49. let mut req = Request::new(Method::Post, url("login"));
  50. req.set_content_type(tide::http::mime::MULTIPART_FORM);
  51. req.replace_body("username=invalid&password=credentials");
  52. let res: Response = app.respond(req).await?;
  53. assert_eq!(res.status(), StatusCode::Found);
  54. assert_eq!(res.header("location").unwrap(), "/login");
  55. Ok(())
  56. })
  57. }
  58. #[test]
  59. fn test_accessing_protected_route_as_guest() -> Result<()> {
  60. block_on(async {
  61. let app = build_app().await;
  62. let req = Request::new(Method::Get, url("/posts/1/edit"));
  63. let res: Response = app.respond(req).await?;
  64. assert_eq!(res.status(), StatusCode::Found);
  65. assert_eq!(res.header("location").unwrap(), "/login");
  66. Ok(())
  67. })
  68. }
  69. #[test]
  70. fn test_attempting_to_create_post_as_guest() -> Result<()> {
  71. block_on(async {
  72. let app = build_app().await;
  73. let mut req = Request::new(Method::Post, url("/posts"));
  74. req.set_content_type(tide::http::mime::MULTIPART_FORM);
  75. req.replace_body("title=test1&slug=test1&body=test1");
  76. let res: Response = app.respond(req).await?;
  77. assert_eq!(res.status(), StatusCode::Found);
  78. assert_eq!(res.header("location").unwrap(), "/login");
  79. Ok(())
  80. })
  81. }
  82. #[test]
  83. fn test_attempting_to_update_post_as_guest() -> Result<()> {
  84. let data = json!({
  85. "title":"test1",
  86. "slug":"test1",
  87. "body":"test1",
  88. "html":"<p>test1</p>\n",
  89. "date":"2000-01-01",
  90. "draft":0
  91. });
  92. block_on(async {
  93. let app = build_app().await;
  94. let posts_dir = std::env::var("POSTS_DIR")?;
  95. let path = PathBuf::from(posts_dir);
  96. let path = path.join("test1.json");
  97. std::fs::write(&path, data.to_string())?;
  98. let mut req = Request::new(Method::Post, url("/posts/test1"));
  99. req.set_content_type(tide::http::mime::MULTIPART_FORM);
  100. req.replace_body("title=test1&slug=test1&body=test2");
  101. let res: Response = app.respond(req).await?;
  102. assert_eq!(res.status(), StatusCode::Found);
  103. assert_eq!(res.header("location").unwrap(), "/login");
  104. assert_eq!(std::fs::read_to_string(&path)?, data.to_string());
  105. std::fs::remove_file(&path)?;
  106. Ok(())
  107. })
  108. }
  109. #[test]
  110. fn test_attempting_to_delete_post_as_guest() -> Result<()> {
  111. let data = json!({
  112. "title":"test2",
  113. "slug":"test2",
  114. "body":"test2",
  115. "html":"<p>test2</p>\n",
  116. "date":"2000-01-01",
  117. "draft":0
  118. });
  119. block_on(async {
  120. let app = build_app().await;
  121. let posts_dir = std::env::var("POSTS_DIR")?;
  122. let path = PathBuf::from(posts_dir);
  123. let path = path.join("test2.json");
  124. std::fs::write(&path, data.to_string())?;
  125. let req = Request::new(Method::Delete, url("/posts/test2"));
  126. let res: Response = app.respond(req).await?;
  127. assert_eq!(res.status(), StatusCode::Found);
  128. assert_eq!(res.header("location").unwrap(), "/login");
  129. assert_eq!(std::fs::read_to_string(&path)?, data.to_string());
  130. std::fs::remove_file(&path)?;
  131. Ok(())
  132. })
  133. }
  134. }