Skip to content

Commit

Permalink
test: AuthorizationGuard のテストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Oct 21, 2024
1 parent 86c9370 commit d9329ec
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions server/domain/src/types/authorization_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,63 @@ pub trait AuthorizationGuardDefinitions<T> {
fn can_create(&self, actor: &User) -> bool;
fn can_read(&self, actor: &User) -> bool;
}

#[cfg(test)]
mod test {
use uuid::Uuid;

use crate::{
types::authorization_guard::{AuthorizationGuard, AuthorizationGuardDefinitions},
user::models::{Role, User},
};

#[test]
fn authorization_guard_test() {
struct AuthorizationGuardTestStruct {
pub _value: String,
}

impl AuthorizationGuardDefinitions<AuthorizationGuardTestStruct> for AuthorizationGuardTestStruct {
fn can_create(&self, user: &User) -> bool {
user.role == Role::Administrator
}

fn can_read(&self, user: &User) -> bool {
user.role == Role::Administrator || user.role == Role::StandardUser
}
}

let admin = User {
name: "admin".to_string(),
id: Uuid::new_v4(),
role: Role::Administrator,
};

let standard_user = User {
name: "standard_user".to_string(),
id: Uuid::new_v4(),
role: Role::StandardUser,
};

let guard_by_admin = AuthorizationGuard::try_new(
&admin,
AuthorizationGuardTestStruct {
_value: "test".to_string(),
},
);

let guard_by_standard_user = AuthorizationGuard::try_new(
&standard_user,
AuthorizationGuardTestStruct {
_value: "test".to_string(),
},
);

assert!(&guard_by_admin.is_ok());
assert!(&guard_by_standard_user.is_err());

let read_guard = guard_by_admin.unwrap().into_read();
assert!(&read_guard.try_read(&admin).is_ok());
assert!(&read_guard.try_read(&standard_user).is_ok())
}
}

0 comments on commit d9329ec

Please sign in to comment.