Skip to content

Commit

Permalink
Merge pull request #626 from GiganticMinecraft/test/verify-same-data-…
Browse files Browse the repository at this point in the history
…from-authorization-read-functions

test: AuthorizationGuardのテストを追加
  • Loading branch information
rito528 authored Dec 14, 2024
2 parents 17bed6c + 81e042a commit 21d6414
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions server/domain/src/types/authorization_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthorizationGuardTestStruct> for AuthorizationGuardTestStruct {
fn can_create(&self, actor: &User) -> bool {
actor.role == Role::Administrator
}
impl AuthorizationGuardDefinitions<AuthorizationGuardTestStruct> 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(),
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 21d6414

Please sign in to comment.