From 7693cc33294cc85f3554255b743bb47aed6418ff Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 3 Oct 2022 11:09:37 +0000 Subject: [PATCH] Add more test to add user --- src/server/unittest/auth.rs | 40 +++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/server/unittest/auth.rs b/src/server/unittest/auth.rs index e2e49cd..0731420 100644 --- a/src/server/unittest/auth.rs +++ b/src/server/unittest/auth.rs @@ -8,7 +8,7 @@ use openssl::rsa::{Padding, Rsa}; /// Test authentification methods /// Returns token -pub async fn test(ctx: &UnitTestContext) -> Result<(u64, Vec), PixivDownloaderError> { +pub async fn test(ctx: &UnitTestContext) -> Result<[(u64, Vec); 2], PixivDownloaderError> { let re = Request::builder().uri("/auth").body(Body::empty())?; let res = ctx.request_json(re).await?.unwrap(); assert_eq!(res["has_root_user"].as_bool(), Some(false)); @@ -98,7 +98,7 @@ pub async fn test(ctx: &UnitTestContext) -> Result<(u64, Vec), PixivDownload "id" => 1, "username" => "test1", "name" => "test1", - "password" => b64_password.as_str(), + "password" => b64_password2.as_str(), "is_admin" => true, }, &token, @@ -123,7 +123,7 @@ pub async fn test(ctx: &UnitTestContext) -> Result<(u64, Vec), PixivDownload "id" => 1, "username" => "test1", "name" => "test2", - "password" => b64_password.as_str(), + "password" => b64_password2.as_str(), "is_admin" => false, }, &token, @@ -141,5 +141,37 @@ pub async fn test(ctx: &UnitTestContext) -> Result<(u64, Vec), PixivDownload "is_admin": false, } ); - Ok((token_id, token)) + let re = ctx + .request_json2( + "/auth/token/add", + &json::object! { + "username": "test1", + "password": b64_password2.as_str(), + }, + ) + .await? + .unwrap(); + let result = JSONResult::from_json(re)?.expect("Failed to add token:"); + assert_eq!(Some(1), result["user_id"].as_u64()); + let token2 = base64::decode(result["token"].as_str().unwrap()).unwrap(); + assert_eq!(token2.len(), 64); + let token2_id = result["id"].as_u64().unwrap(); + let re = ctx + .request_json2_sign( + "/auth/user/add", + &json::object! { + "id" => 1, + "username" => "test1", + "name" => "test2", + "password" => b64_password2.as_str(), + "is_admin" => false, + }, + &token2, + token2_id, + ) + .await? + .unwrap(); + let result = JSONResult::from_json(re)?.unwrap_err(); + assert_eq!(result.code, 9); + Ok([(token_id, token), (token2_id, token2)]) }