UserProvisioner { public Mono<Void> provisionUser(MailboxSession session) { if (session != null && !usersRepository.isReadOnly()) { return Mono.fromRunnable(() -> createAccountIfNeeded(session)); } return Mono.empty(); } @Inject @VisibleForTesting  UserProvisioner(UsersRepository usersRepository, MetricFactory metricFactory); Mono<Void> provisionUser(MailboxSession session);  }
@Test public void filterShouldDoNothingOnNullSession() throws UsersRepositoryException { testee.provisionUser(null).block(); assertThat(usersRepository.list()).toIterable() .isEmpty(); }
@Test public void filterShouldAddUsernameWhenNoVirtualHostingAndMailboxSessionContainsUsername() throws UsersRepositoryException { usersRepository.setEnableVirtualHosting(false); MailboxSession mailboxSession = MailboxSessionUtil.create(USERNAME); testee.provisionUser(mailboxSession).block(); assertThat(usersRepository.list()).toIterable() .contains(USERNAME); }
@Test public void filterShouldFailOnInvalidVirtualHosting() { usersRepository.setEnableVirtualHosting(false); MailboxSession mailboxSession = MailboxSessionUtil.create(USERNAME_WITH_DOMAIN); assertThatThrownBy(() -> testee.provisionUser(mailboxSession).block()) .hasCauseInstanceOf(UsersRepositoryException.class); }
@Test public void filterShouldNotTryToAddUserWhenReadOnlyUsersRepository() { UsersRepository usersRepository = mock(UsersRepository.class); when(usersRepository.isReadOnly()).thenReturn(true); testee = new UserProvisioner(usersRepository, new RecordingMetricFactory()); MailboxSession mailboxSession = MailboxSessionUtil.create(USERNAME_WITH_DOMAIN); testee.provisionUser(mailboxSession).block(); verify(usersRepository).isReadOnly(); verifyNoMoreInteractions(usersRepository); }