|
| 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 | +} |
0 commit comments