|
|
|
@ -140,7 +140,6 @@ class AdminBootstrapRunnerTest { |
|
|
|
@Test |
|
|
|
void phoneBelongingToStudentFails() { |
|
|
|
enableWith(PHONE); |
|
|
|
when(userRepository.countByRoleAndActiveTrue(User.Role.ADMIN)).thenReturn(0L); |
|
|
|
when(creator.findByPhone(PHONE)).thenReturn(Optional.of( |
|
|
|
new AdminBootstrapCreator.BootstrapUser(9L, "STUDENT", true, true, true, List.of("STUDENT")))); |
|
|
|
|
|
|
|
@ -152,7 +151,6 @@ class AdminBootstrapRunnerTest { |
|
|
|
@Test |
|
|
|
void phoneBelongingToCompanyFails() { |
|
|
|
enableWith(PHONE); |
|
|
|
when(userRepository.countByRoleAndActiveTrue(User.Role.ADMIN)).thenReturn(0L); |
|
|
|
when(creator.findByPhone(PHONE)).thenReturn(Optional.of( |
|
|
|
new AdminBootstrapCreator.BootstrapUser(9L, "COMPANY", true, true, true, List.of("COMPANY")))); |
|
|
|
|
|
|
|
@ -163,7 +161,6 @@ class AdminBootstrapRunnerTest { |
|
|
|
@Test |
|
|
|
void phoneBelongingToInactiveUserFailsAndIsNotRevived() { |
|
|
|
enableWith(PHONE); |
|
|
|
when(userRepository.countByRoleAndActiveTrue(User.Role.ADMIN)).thenReturn(0L); |
|
|
|
when(creator.findByPhone(PHONE)).thenReturn(Optional.of( |
|
|
|
new AdminBootstrapCreator.BootstrapUser(9L, "ADMIN", false, true, true, List.of("ADMIN")))); |
|
|
|
|
|
|
|
@ -171,6 +168,25 @@ class AdminBootstrapRunnerTest { |
|
|
|
verify(creator, never()).createAdmin(any(), any()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void activeAdminMissingAdminRoleCannotBypassStrictIdempotencyCheck() { |
|
|
|
assertInvalidExistingAdminRejected( |
|
|
|
new AdminBootstrapCreator.BootstrapUser(9L, "ADMIN", true, true, true, List.of())); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void activeAdminWithAdditionalRoleCannotBypassStrictIdempotencyCheck() { |
|
|
|
assertInvalidExistingAdminRejected( |
|
|
|
new AdminBootstrapCreator.BootstrapUser( |
|
|
|
9L, "ADMIN", true, true, true, List.of("ADMIN", "STUDENT"))); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void activeAdminWithPasswordCannotBypassStrictIdempotencyCheck() { |
|
|
|
assertInvalidExistingAdminRejected( |
|
|
|
new AdminBootstrapCreator.BootstrapUser(9L, "ADMIN", true, true, false, List.of("ADMIN"))); |
|
|
|
} |
|
|
|
|
|
|
|
// ── 正常创建 ───────────────────────────────────────────────
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -222,7 +238,6 @@ class AdminBootstrapRunnerTest { |
|
|
|
@Test |
|
|
|
void failureMessagesNeverContainPhone() { |
|
|
|
enableWith(PHONE); |
|
|
|
when(userRepository.countByRoleAndActiveTrue(User.Role.ADMIN)).thenReturn(0L); |
|
|
|
when(creator.findByPhone(PHONE)).thenReturn(Optional.of( |
|
|
|
new AdminBootstrapCreator.BootstrapUser(9L, "STUDENT", true, true, true, List.of("STUDENT")))); |
|
|
|
|
|
|
|
@ -234,4 +249,14 @@ class AdminBootstrapRunnerTest { |
|
|
|
assertFalse(haystack != null && haystack.contains(needle), |
|
|
|
"输出不得包含手机号"); |
|
|
|
} |
|
|
|
|
|
|
|
private void assertInvalidExistingAdminRejected(AdminBootstrapCreator.BootstrapUser existing) { |
|
|
|
enableWith(PHONE); |
|
|
|
when(creator.findByPhone(PHONE)).thenReturn(Optional.of(existing)); |
|
|
|
|
|
|
|
IllegalStateException ex = assertThrows(IllegalStateException.class, () -> runner.run(null)); |
|
|
|
|
|
|
|
assertEquals(AdminBootstrapRunner.ERROR_PHONE_IN_USE, ex.getMessage()); |
|
|
|
verify(creator, never()).createAdmin(any(), any()); |
|
|
|
} |
|
|
|
} |
|
|
|
|