Service login (#6)
Rust Build / Rustfmt (push) Successful in 1m24s
Rust Build / Check (push) Successful in 1m29s
Rust Build / Test Suite (push) Successful in 1m29s
Rust Build / build (push) Successful in 1m42s
Rust Build / Clippy (push) Successful in 1m52s
Rust Build / Rustfmt (pull_request) Successful in 53s
Rust Build / Test Suite (pull_request) Successful in 1m25s
Rust Build / Check (pull_request) Successful in 1m47s
Rust Build / Clippy (pull_request) Successful in 1m40s
Rust Build / build (pull_request) Successful in 1m36s
Rust Build / Rustfmt (push) Successful in 1m24s
Rust Build / Check (push) Successful in 1m29s
Rust Build / Test Suite (push) Successful in 1m29s
Rust Build / build (push) Successful in 1m42s
Rust Build / Clippy (push) Successful in 1m52s
Rust Build / Rustfmt (pull_request) Successful in 53s
Rust Build / Test Suite (pull_request) Successful in 1m25s
Rust Build / Check (pull_request) Successful in 1m47s
Rust Build / Clippy (pull_request) Successful in 1m40s
Rust Build / build (pull_request) Successful in 1m36s
Reviewed-on: phoenix/textsender-auth#6
This commit was merged in pull request #6.
This commit is contained in:
+156
-12
@@ -133,6 +133,25 @@ pub mod requests {
|
||||
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
pub async fn login_service_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
passphrase: &str,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
let payload = serde_json::json!({
|
||||
"username": username,
|
||||
"passphrase": passphrase,
|
||||
});
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::POST)
|
||||
.uri(super::callers::endpoints::LOGIN_SERVICE_USER)
|
||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||
.body(axum::body::Body::from(payload.to_string()))
|
||||
.unwrap();
|
||||
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Test user firstname
|
||||
@@ -175,13 +194,16 @@ async fn register_user(
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||
Ok(body) => {
|
||||
let parsed_body: callers::register::response::Response =
|
||||
serde_json::from_slice(&body).unwrap();
|
||||
Ok(parsed_body.data[0].clone())
|
||||
match convert_response::<callers::register::response::Response>(response).await {
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let user = response.data[0].clone();
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,13 +224,82 @@ async fn login_user(
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match axum::body::to_bytes(response.into_body(), usize::MAX).await {
|
||||
Ok(body) => {
|
||||
let parsed_body: callers::login::response::LoginResponse =
|
||||
serde_json::from_slice(&body).unwrap();
|
||||
Ok(parsed_body.data[0].clone())
|
||||
match convert_response::<callers::login::response::LoginResponse>(response).await {
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let user = response.data[0].clone();
|
||||
Ok(user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn register_service_user(
|
||||
app: &axum::Router,
|
||||
) -> Result<textsender_models::user::ServiceUser, std::io::Error> {
|
||||
match requests::register_service_user(&app).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::CREATED != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<callers::register::response::RegisterServiceUserResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let service_user = response.data[0].clone();
|
||||
Ok(service_user)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn login_service_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
passphrase: &str,
|
||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||
match requests::login_service_user(&app, username, passphrase).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::OK != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match convert_response::<callers::login::response::ServiceUserLoginResponse>(
|
||||
response,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.len() > 0 {
|
||||
let login_result = response.data[0].clone();
|
||||
Ok(login_result)
|
||||
} else {
|
||||
Err(std::io::Error::other("No data returned"))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -354,3 +445,56 @@ async fn test_register_service_user() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_login_service_user() {
|
||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||
let db_name = db_mgr::generate_db_name().await;
|
||||
|
||||
match db_mgr::create_database(&tm_pool, &db_name).await {
|
||||
Ok(_) => {
|
||||
println!("Success");
|
||||
}
|
||||
Err(e) => {
|
||||
assert!(false, "Error: {:?}", e.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
||||
|
||||
db::init::migrations(&pool).await;
|
||||
|
||||
let app = init::routes().await.layer(axum::Extension(pool));
|
||||
|
||||
match register_service_user(&app).await {
|
||||
Ok(user) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
user.id.is_nil(),
|
||||
"The service user id should not be nil"
|
||||
);
|
||||
match login_service_user(&app, TEST_SERVICE_USERNAME, TEST_SERVICE_PASSPHRASE).await {
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
login_result.access_token.is_empty(),
|
||||
"Access token is empty when it should not be"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match db_mgr::drop_database(&tm_pool, &db_name).await {
|
||||
Ok(()) => {}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user