Skip to content

Commit 7399b10

Browse files
committed
Fix basic auth test assertions
Use "Unauthorized" instead of "not authorized" to match the expected response. Add tests for basic auth error cases.
1 parent ddd52fd commit 7399b10

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

tests/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ async fn test_official_website_basic_auth_example() {
174174
let body = test::read_body(resp).await;
175175
let body_str = String::from_utf8(body.to_vec()).unwrap();
176176
assert!(
177-
body_str.contains("not authorized"),
177+
body_str.contains("Unauthorized"),
178178
"{body_str}\nexpected to contain Unauthorized"
179179
);
180180
}

tests/errors/basic_auth.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::common::{get_request_to, req_path};
2+
use actix_web::{http::StatusCode, test};
3+
use sqlpage::webserver::http::main_handler;
4+
5+
#[actix_web::test]
6+
async fn test_basic_auth_not_provided() {
7+
let resp_result = req_path("/tests/errors/basic_auth.sql").await;
8+
let resp = resp_result.unwrap();
9+
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
10+
assert_eq!(
11+
resp.headers().get("www-authenticate").unwrap(),
12+
"Basic realm=\"Authentication required\", charset=\"UTF-8\""
13+
);
14+
let body = test::read_body(resp).await;
15+
let body_str = String::from_utf8(body.to_vec()).unwrap();
16+
assert!(
17+
body_str.contains("Unauthorized"),
18+
"{body_str}\nexpected to contain Unauthorized"
19+
);
20+
assert!(
21+
!body_str.contains("Success!"),
22+
"{body_str}\nexpected not to contain Success!"
23+
);
24+
}
25+
26+
#[actix_web::test]
27+
async fn test_basic_auth_with_credentials() {
28+
let req = get_request_to("/tests/errors/basic_auth.sql")
29+
.await
30+
.unwrap() // log in with credentials "user:password"
31+
.append_header(("Authorization", "Basic dXNlcjpwYXNzd29yZA=="))
32+
.to_srv_request();
33+
let resp = main_handler(req)
34+
.await
35+
.expect("req with credentials should succeed");
36+
assert_eq!(resp.status(), StatusCode::OK);
37+
let body = test::read_body(resp).await;
38+
let body_str = String::from_utf8(body.to_vec()).unwrap();
39+
assert!(
40+
body_str.contains("Success!"),
41+
"{body_str}\nexpected to contain Success"
42+
);
43+
}

tests/errors/basic_auth.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SELECT 'authentication' AS component,
2+
'$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w' AS password_hash, -- this is a hash of the password 'password'
3+
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
4+
5+
SELECT 'text' AS component, 'Success!' AS contents;

0 commit comments

Comments
 (0)