Skip to content

Commit c387e50

Browse files
committed
chore: validate non_exisiting_user_is_rejected test
1 parent 7658346 commit c387e50

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/routes/newsletters.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,9 @@ async fn validate_credentials(
161161
.map_err(PublishError::UnexpectedError)?
162162
.ok_or_else(|| PublishError::AuthError(anyhow::anyhow!("Unknown username")))?;
163163

164-
let current_span = tracing::Span::current();
165-
tokio::task::spawn_blocking(move || {
166-
current_span.in_scope(|| verify_password_hash(expected_password_hash, credentials.password))
167-
})
168-
.await
169-
.context("Failed to spawn blocking task")
170-
.map_err(PublishError::UnexpectedError)?;
164+
spawn_blocking_with_tracing(move || {
165+
verify_password_hash(expected_password_hash, credentials.password)
166+
});
171167

172168
Ok(user_id)
173169
}

tests/api/newsletter.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::helpers::{spawn_app, ConfirmationLinks, TestApp};
2+
use uuid::Uuid;
23
use wiremock::matchers::{any, method, path};
34
use wiremock::{Mock, ResponseTemplate};
45

@@ -147,3 +148,31 @@ async fn requests_missing_authorization_are_rejected() {
147148
response.headers()["WWW-Authenticate"]
148149
);
149150
}
151+
152+
#[tokio::test]
153+
async fn non_exisiting_user_is_rejected() {
154+
let app = spawn_app().await;
155+
156+
let username = Uuid::new_v4().to_string();
157+
let password = Uuid::new_v4().to_string();
158+
159+
let response = reqwest::Client::new()
160+
.post(&format!("{}/newsletters", &app.address))
161+
.basic_auth(username, Some(password))
162+
.json(&serde_json::json!({
163+
"title": "Newsletter title",
164+
"content": {
165+
"text": "Newsletter content",
166+
"html": "<p>Newsletter content</p>",
167+
}
168+
}))
169+
.send()
170+
.await
171+
.expect("Failed to execute request.");
172+
173+
assert_eq!(response.status().as_u16(), 401);
174+
assert_eq!(
175+
r#"Basic realm="publish""#,
176+
response.headers()["WWW-Authenticate"]
177+
);
178+
}

0 commit comments

Comments
 (0)