Added test
Rust Build / Rustfmt (pull_request) Successful in 40s
Rust Build / Check (pull_request) Successful in 2m20s
Rust Build / Test Suite (pull_request) Successful in 2m37s
Rust Build / Clippy (pull_request) Successful in 2m7s
Rust Build / build (pull_request) Successful in 3m6s
Rust Build / Rustfmt (pull_request) Successful in 40s
Rust Build / Check (pull_request) Successful in 2m20s
Rust Build / Test Suite (pull_request) Successful in 2m37s
Rust Build / Clippy (pull_request) Successful in 2m7s
Rust Build / build (pull_request) Successful in 3m6s
This commit is contained in:
+110
@@ -134,6 +134,7 @@ pub mod requests {
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
/// Function to call service user login endpoint
|
||||
pub async fn login_service_user(
|
||||
app: &axum::Router,
|
||||
username: &str,
|
||||
@@ -152,6 +153,24 @@ pub mod requests {
|
||||
|
||||
app.clone().oneshot(req).await
|
||||
}
|
||||
|
||||
/// Function to call token refresh endpoint
|
||||
pub async fn refresh_token(
|
||||
app: &axum::Router,
|
||||
access_token: &str,
|
||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
||||
let payload = serde_json::json!({
|
||||
"access_token": access_token,
|
||||
});
|
||||
let req = axum::http::Request::builder()
|
||||
.method(axum::http::Method::POST)
|
||||
.uri(super::callers::endpoints::REFRESH_TOKEN)
|
||||
.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
|
||||
@@ -321,6 +340,39 @@ mod flow {
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn refresh_token(
|
||||
app: &axum::Router,
|
||||
access_token: &str,
|
||||
) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
||||
match requests::refresh_token(app, access_token).await {
|
||||
Ok(response) => {
|
||||
if axum::http::StatusCode::OK != response.status() {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Status code is off {:?}",
|
||||
response.status()
|
||||
)))
|
||||
} else {
|
||||
match util::convert_response::<callers::login::response::RefreshTokenResponse>(
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -516,3 +568,61 @@ async fn test_login_service_user() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_refresh_token() {
|
||||
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 flow::register_user(&app).await {
|
||||
Ok(user) => match flow::login_user(&app, &user.username, TEST_PASSWORD).await {
|
||||
Ok(login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
login_result.access_token.is_empty(),
|
||||
"Access token is empty when it should not be"
|
||||
);
|
||||
match flow::refresh_token(&app, &login_result.access_token).await {
|
||||
Ok(refresh_login_result) => {
|
||||
assert_eq!(
|
||||
false,
|
||||
refresh_login_result.access_token.is_empty(),
|
||||
"Refreshed access token should not be empty"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
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