@Override
    public Mono<UserDetails> findByUsername(String username) {
        return userService.getUser(username)
            .onErrorMap(UserNotFoundException.class,
                e -> new BadCredentialsException("Invalid Credentials"))
            .flatMap(user -> {
                var name = user.getMetadata().getName();
                var userBuilder = User.withUsername(name)
                    .password(user.getSpec().getPassword())
                    .disabled(requireNonNullElse(user.getSpec().getDisabled(), false));
                var setAuthorities = roleService.getRolesByUsername(name)
                    // every authenticated user should have authenticated and anonymous roles.
                    .concatWithValues(AUTHENTICATED_ROLE_NAME, ANONYMOUS_ROLE_NAME)
                    .map(roleName -> new SimpleGrantedAuthority(ROLE_PREFIX + roleName))
                    .distinct()
                    .collectList()
                    .doOnNext(userBuilder::authorities);

                return setAuthorities.then(Mono.fromSupplier(() -> {
                    var twoFactorAuthSettings = TwoFactorUtils.getTwoFactorAuthSettings(user);
                    return new HaloUser.Builder(userBuilder.build())
                        .twoFactorAuthEnabled(
                            (!twoFactorAuthDisabled) && twoFactorAuthSettings.isAvailable()
                        )
                        .totpEncryptedSecret(user.getSpec().getTotpEncryptedSecret())
                        .build();
                }));
            });
    }
@Test
    void shouldNotFindUserDetailsByNonExistingUsername() {
        when(userService.getUser("non-existing-user")).thenReturn(
            Mono.error(() -> new UserNotFoundException("non-existing-user")));

        var userDetailsMono = userDetailService.findByUsername("non-existing-user");

        StepVerifier.create(userDetailsMono)
            .expectError(AuthenticationException.class)
            .verify();
    }