Skip to content

Commit

Permalink
Merge pull request #331 from sohosai/develop
Browse files Browse the repository at this point in the history
deploy 2024/08/15
  • Loading branch information
arata-nvm authored Aug 16, 2024
2 parents 7cec214 + d849909 commit 0acf14f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
22 changes: 18 additions & 4 deletions crates/sos24-domain/src/entity/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl User {
pub fn set_role(&mut self, actor: &Actor, role: UserRole) -> Result<(), PermissionDeniedError> {
if self.role != role {
ensure!(actor.has_permission(Permissions::UPDATE_USER_ALL));
ensure!(actor.role() >= &role);
self.role = role;
}
Ok(())
Expand All @@ -172,12 +173,13 @@ impl_value_object!(UserName(String));
impl_value_object!(UserKanaName(String));
impl_value_object!(UserPhoneNumber(String)); // ガバガバだが、電話番号が弾かれる事によってjsysの作業が増えることを鑑みて許容する

#[derive(Debug, Clone, PartialEq, Eq)]
// 権限の弱い順に定義
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum UserRole {
Administrator,
CommitteeOperator,
Committee,
General,
Committee,
CommitteeOperator,
Administrator,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -199,3 +201,15 @@ impl TryFrom<String> for UserEmail {
Ok(Self(email))
}
}

#[cfg(test)]
mod tests {
use crate::entity::user::UserRole;

#[test]
fn user_role_ordering() {
assert!(UserRole::Administrator > UserRole::CommitteeOperator);
assert!(UserRole::CommitteeOperator > UserRole::Committee);
assert!(UserRole::Committee > UserRole::General);
}
}
104 changes: 101 additions & 3 deletions crates/sos24-use-case/src/user/interactor/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ impl<R: Repositories> UserUseCase<R> {
new_user.set_name(&actor, UserName::new(user_data.name))?;
new_user.set_kana_name(&actor, UserKanaName::new(user_data.kana_name))?;
new_user.set_phone_number(&actor, UserPhoneNumber::new(user_data.phone_number))?;
new_user.set_role(&actor, UserRole::from(user_data.role))?;

let old_role = new_user.role().clone();
let new_role = UserRole::from(user_data.role);
if old_role != new_role {
new_user.set_role(&actor, new_role)?;
}

let firebase_user_id: FirebaseUserId = new_user.id().clone().into();

Expand Down Expand Up @@ -190,7 +195,7 @@ mod tests {
}

#[tokio::test]
async fn 実委人管理者はロールを含む自分のユーザーを更新できる() {
async fn 実委人管理者は他人のユーザーを更新できる() {
let mut repositories = MockRepositories::default();
repositories
.user_repository_mut()
Expand All @@ -202,12 +207,105 @@ mod tests {
.returning(|_| Ok(()));
let use_case = UserUseCase::new(Arc::new(repositories));

let ctx = TestContext::new(fixture::actor::actor1(UserRole::CommitteeOperator));
let res = use_case
.update(
&ctx,
UpdateUserCommand {
id: fixture::user::id2().value(),
name: fixture::user::name2().value(),
kana_name: fixture::user::kana_name2().value(),
email: fixture::user::email2().value(),
phone_number: fixture::user::phone_number2().value(),
role: UserRoleDto::from(UserRole::CommitteeOperator),
},
)
.await;
assert!(matches!(res, Ok(())));
}

#[tokio::test]
async fn 実委人管理者は自分を管理者に変更できない() {
let mut repositories = MockRepositories::default();
repositories
.user_repository_mut()
.expect_find_by_id()
.returning(|_| Ok(Some(fixture::user::user1(UserRole::CommitteeOperator))));
repositories
.user_repository_mut()
.expect_update()
.returning(|_| Ok(()));
let use_case = UserUseCase::new(Arc::new(repositories));

let ctx = TestContext::new(fixture::actor::actor1(UserRole::CommitteeOperator));
let res = use_case
.update(
&ctx,
UpdateUserCommand {
id: fixture::user::id1().value(),
name: fixture::user::name1().value(),
kana_name: fixture::user::kana_name1().value(),
email: fixture::user::email1().value(),
phone_number: fixture::user::phone_number1().value(),
role: UserRoleDto::from(UserRole::Administrator),
},
)
.await;
assert!(matches!(
res,
Err(UserUseCaseError::PermissionDeniedError(_))
));
}

#[tokio::test]
async fn 管理者は自分を実委人管理者に変更できる() {
let mut repositories = MockRepositories::default();
repositories
.user_repository_mut()
.expect_find_by_id()
.returning(|_| Ok(Some(fixture::user::user1(UserRole::Administrator))));
repositories
.user_repository_mut()
.expect_update()
.returning(|_| Ok(()));
let use_case = UserUseCase::new(Arc::new(repositories));

let ctx = TestContext::new(fixture::actor::actor1(UserRole::Administrator));
let res = use_case
.update(
&ctx,
UpdateUserCommand {
id: fixture::user::id1().value(),
name: fixture::user::name1().value(),
kana_name: fixture::user::kana_name1().value(),
email: fixture::user::email1().value(),
phone_number: fixture::user::phone_number1().value(),
role: UserRoleDto::from(UserRole::CommitteeOperator),
},
)
.await;
assert!(res.is_ok());
}

#[tokio::test]
async fn 管理者は他人を管理者に変更できる() {
let mut repositories = MockRepositories::default();
repositories
.user_repository_mut()
.expect_find_by_id()
.returning(|_| Ok(Some(fixture::user::user2(UserRole::General))));
repositories
.user_repository_mut()
.expect_update()
.returning(|_| Ok(()));
let use_case = UserUseCase::new(Arc::new(repositories));

let ctx = TestContext::new(fixture::actor::actor1(UserRole::Administrator));
let res = use_case
.update(
&ctx,
UpdateUserCommand {
id: fixture::user::id2().value(),
name: fixture::user::name2().value(),
kana_name: fixture::user::kana_name2().value(),
email: fixture::user::email2().value(),
Expand All @@ -216,6 +314,6 @@ mod tests {
},
)
.await;
assert!(matches!(res, Ok(())));
assert!(res.is_ok());
}
}

0 comments on commit 0acf14f

Please sign in to comment.