Add more test to add user

This commit is contained in:
2022-10-03 11:09:37 +00:00
committed by GitHub
parent e1a48dec40
commit 7693cc3329

View File

@@ -8,7 +8,7 @@ use openssl::rsa::{Padding, Rsa};
/// Test authentification methods
/// Returns token
pub async fn test(ctx: &UnitTestContext) -> Result<(u64, Vec<u8>), PixivDownloaderError> {
pub async fn test(ctx: &UnitTestContext) -> Result<[(u64, Vec<u8>); 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<u8>), 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<u8>), 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<u8>), 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)])
}