From 81e042aff065cad683b5c83a4463c8574a9284c5 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:54:03 +0900 Subject: [PATCH] =?UTF-8?q?test:=20AuthorizationGuard=20=E3=81=AE=20try=5F?= =?UTF-8?q?into=5Fread=20=E9=96=A2=E6=95=B0=E3=81=A8=20try=5Fread=20?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=81=8B=E3=82=89=E5=8F=96=E5=BE=97=E3=81=97?= =?UTF-8?q?=E3=81=9F=E3=83=87=E3=83=BC=E3=82=BF=E3=81=8C=E7=AD=89=E3=81=97?= =?UTF-8?q?=E3=81=84=E3=81=93=E3=81=A8=E7=A2=BA=E8=AA=8D=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/src/types/authorization_guard.rs | 63 +++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/server/domain/src/types/authorization_guard.rs b/server/domain/src/types/authorization_guard.rs index e0947168..cdb73a42 100644 --- a/server/domain/src/types/authorization_guard.rs +++ b/server/domain/src/types/authorization_guard.rs @@ -215,30 +215,31 @@ mod test { user::models::{Role, User}, }; - #[test] - fn authorization_guard_test() { - struct AuthorizationGuardTestStruct { - pub _value: String, - } + #[derive(Clone, PartialEq, Debug)] + struct AuthorizationGuardTestStruct { + pub _value: String, + } - impl AuthorizationGuardDefinitions for AuthorizationGuardTestStruct { - fn can_create(&self, actor: &User) -> bool { - actor.role == Role::Administrator - } + impl AuthorizationGuardDefinitions for AuthorizationGuardTestStruct { + fn can_create(&self, actor: &User) -> bool { + actor.role == Role::Administrator + } - fn can_read(&self, actor: &User) -> bool { - actor.role == Role::Administrator || actor.role == Role::StandardUser - } + fn can_read(&self, actor: &User) -> bool { + actor.role == Role::Administrator || actor.role == Role::StandardUser + } - fn can_update(&self, actor: &User) -> bool { - actor.role == Role::Administrator - } + fn can_update(&self, actor: &User) -> bool { + actor.role == Role::Administrator + } - fn can_delete(&self, actor: &User) -> bool { - actor.role == Role::Administrator - } + fn can_delete(&self, actor: &User) -> bool { + actor.role == Role::Administrator } + } + #[test] + fn authorization_guard_test() { let admin = User { name: "admin".to_string(), id: Uuid::new_v4(), @@ -270,4 +271,30 @@ mod test { assert!(&guard.try_delete(&admin, |_| {}).is_ok()); assert!(&guard.try_delete(&standard_user, |_| {}).is_err()); } + + #[test] + fn verify_same_data_for_try_read_and_try_into_read() { + let user = User { + name: "user".to_string(), + id: Uuid::new_v4(), + role: Role::Administrator, + }; + + let guard = AuthorizationGuard::new(AuthorizationGuardTestStruct { + _value: "test".to_string(), + }); + + let read_guard = guard.into_read(); + + let from_into_read = read_guard.try_read(&user); + assert!(from_into_read.is_ok()); + + let from_into_read = from_into_read.unwrap().to_owned(); + + let read_into = read_guard.try_into_read(&user); + + assert!(read_into.is_ok()); + + assert_eq!(from_into_read, read_into.unwrap()); + } }