Skip to content

Commit

Permalink
test(sandside): #193 add units test to UserServiceImpl
Browse files Browse the repository at this point in the history
close #193
  • Loading branch information
Marthym committed Jan 6, 2024
1 parent cf487ec commit f082d75
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ public User withRoles(String... roles) {
.roles(List.copyOf(verifiedRoles))
.build();
}

public UpdatableUser.UpdatableUserBuilder updatable() {
return UpdatableUser.builder()
.login(login)
.name(Optional.of(name))
.mail(mail)
.roles(roles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fr.ght1pc9kc.baywatch.security.api.AuthenticationFacade;
import fr.ght1pc9kc.baywatch.security.api.model.Permission;
import fr.ght1pc9kc.baywatch.security.api.model.Role;
import fr.ght1pc9kc.baywatch.security.api.model.UpdatableUser;
import fr.ght1pc9kc.baywatch.security.api.model.User;
import fr.ght1pc9kc.baywatch.security.domain.exceptions.UnauthenticatedUser;
import fr.ght1pc9kc.baywatch.security.domain.exceptions.UnauthorizedOperation;
Expand All @@ -32,11 +33,16 @@
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -54,7 +60,10 @@ class UserServiceImplTest {
@SuppressWarnings("deprecation")
void setUp() {
when(mockAuthFacade.getConnectedUser()).thenReturn(Mono.just(UserSamples.YODA));
when(mockUserRepository.get(any())).thenReturn(Mono.just(UserSamples.LUKE));
doAnswer(answer -> Stream.of(UserSamples.LUKE, UserSamples.YODA, UserSamples.OBIWAN)
.filter(u -> u.id.equals(answer.getArgument(0, String.class)))
.findAny().map(Mono::just).orElseThrow()
).when(mockUserRepository).get(anyString());
when(mockUserRepository.list(any())).thenReturn(Flux.just(UserSamples.LUKE, UserSamples.OBIWAN, UserSamples.YODA));
when(mockUserRepository.persist(any())).thenReturn(Flux.just(UserSamples.LUKE, UserSamples.OBIWAN, UserSamples.YODA));
when(mockUserRepository.persist(anyString(), anyCollection())).thenReturn(Mono.just(UserSamples.LUKE));
Expand Down Expand Up @@ -127,6 +136,56 @@ void should_create_user_with_admin() {
Assertions.assertThat(actual).isEqualTo(Entity.identify(UserSamples.OBIWAN.id, CURRENT, UserSamples.OBIWAN.self));
}

@Test
void should_update_other_user_as_admin() {
when(mockAuthFacade.getConnectedUser()).thenReturn(Mono.just(UserSamples.YODA));
when(mockUserRepository.update(anyString(), any())).thenReturn(Mono.just(UserSamples.OBIWAN));
UpdatableUser obiChan = UserSamples.OBIWAN.self.updatable()
.name(Optional.of("Obi Chan"))
.build();

Mono<Entity<User>> actual = tested.update(UserSamples.OBIWAN.id, obiChan, "kenobi");

StepVerifier.create(actual)
.assertNext(a -> Assertions.assertThat(a).isNotNull())
.verifyComplete();

verify(mockUserRepository).update(UserSamples.OBIWAN.id, obiChan);
}

@Test
void should_update_my_user() {
when(mockAuthFacade.getConnectedUser()).thenReturn(Mono.just(UserSamples.OBIWAN));
when(mockUserRepository.update(anyString(), any())).thenReturn(Mono.just(UserSamples.OBIWAN));
UpdatableUser obiChan = UserSamples.OBIWAN.self.updatable()
.name(Optional.of("Obi Chan"))
.build();

Mono<Entity<User>> actual = tested.update(UserSamples.OBIWAN.id, obiChan, UserSamples.OBIWAN.self.password);

StepVerifier.create(actual)
.assertNext(a -> Assertions.assertThat(a).isNotNull())
.verifyComplete();

verify(mockUserRepository).update(UserSamples.OBIWAN.id, obiChan);
}

@Test
void should_fail_update_user_with_invalid_password() {
when(mockAuthFacade.getConnectedUser()).thenReturn(Mono.just(UserSamples.OBIWAN));
when(mockUserRepository.update(anyString(), any())).thenReturn(Mono.just(UserSamples.OBIWAN));
UpdatableUser obiChan = UserSamples.OBIWAN.self.updatable()
.name(Optional.of("Obi Chan"))
.build();

Mono<Entity<User>> actual = tested.update(UserSamples.OBIWAN.id, obiChan, "Invalid Password");

StepVerifier.create(actual)
.verifyError(UnauthorizedOperation.class);

verify(mockUserRepository, never()).update(UserSamples.OBIWAN.id, obiChan);
}

@Test
@SuppressWarnings("unchecked")
void should_delete_users_as_admin() {
Expand Down

0 comments on commit f082d75

Please sign in to comment.