-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set login error msg in cookies | 10.7 sessions
- Loading branch information
Showing
10 changed files
with
527 additions
and
238 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,44 @@ | ||
use actix_web::http::header::ContentType; | ||
use actix_web::HttpResponse; | ||
use actix_web_flash_messages::{IncomingFlashMessages, Level}; | ||
use std::fmt::Write; | ||
|
||
pub async fn login_form(flash_messages: IncomingFlashMessages) -> HttpResponse { | ||
let mut error_html = String::new(); | ||
for m in flash_messages.iter().filter(|m| m.level() == Level::Error) { | ||
writeln!(error_html, "<p><i>{}</i></p>", m.content()).unwrap(); | ||
} | ||
|
||
pub async fn login_form() -> HttpResponse { | ||
HttpResponse::Ok() | ||
.content_type(ContentType::html()) | ||
.body(include_str!("login.html")) | ||
.body(format!( | ||
r#"<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html; charset=utf-8"> | ||
<title>Login</title> | ||
</head> | ||
<body> | ||
{error_html} | ||
<form action="/login" method="post"> | ||
<label>Username | ||
<input | ||
type="text" | ||
placeholder="Enter Username" | ||
name="username" | ||
> | ||
</label> | ||
<label>Password | ||
<input | ||
type="password" | ||
placeholder="Enter Password" | ||
name="password" | ||
> | ||
</label> | ||
<button type="submit">Login</button> | ||
</form> | ||
</body> | ||
</html>"#, | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::api::helpers::{assert_is_redirected_to, spawn_app}; | ||
|
||
#[tokio::test] | ||
async fn an_error_flash_message_is_set_on_failure() { | ||
// Arrange | ||
let app = spawn_app().await; | ||
let login_body = serde_json::json!( { | ||
"username": "username", | ||
"password": "password" | ||
}); | ||
|
||
// Act | ||
let response = app.post_login(&login_body).await; | ||
Check failure on line 13 in tests/api/login.rs GitHub Actions / Code coverage
Check failure on line 13 in tests/api/login.rs GitHub Actions / Lint
|
||
|
||
// Assert | ||
let html_page = app.get_login_html().await; | ||
assert!(html_page.contains(r#"<p><i>Authentication failed</i></p>"#)); | ||
} | ||
|
||
#[tokio::test] | ||
async fn an_error_flash_message_is_not_set_after_failure_reload() { | ||
// Arrange | ||
let app = spawn_app().await; | ||
let login_body = serde_json::json!( { | ||
"username": "username", | ||
"password": "password" | ||
}); | ||
|
||
// Act | ||
let response = app.post_login(&login_body).await; | ||
|
||
// Assert | ||
assert_is_redirected_to(&response, "/login"); | ||
|
||
// load the login twice | ||
app.get_login_html().await; | ||
let html_page = app.get_login_html().await; | ||
assert!(!html_page.contains(r#"<p><i>Authentication failed</i></p>"#)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod health_check; | ||
mod helpers; | ||
mod login; | ||
mod newsletter; | ||
mod subscriptions; | ||
mod subscriptions_confirm; |