StoreMailboxManager implements MailboxManager { @Override public MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { final MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailboxRow = mapper.findMailboxByPath(mailboxPath) .blockOptional() .orElseThrow(() -> { LOGGER.info("Mailbox '{}' not found.", mailboxPath); return new MailboxNotFoundException(mailboxPath); }); if (!assertUserHasAccessTo(mailboxRow, session)) { LOGGER.info("Mailbox '{}' does not belong to user '{}' but to '{}'", mailboxPath, session.getUser(), mailboxRow.getUser()); throw new MailboxNotFoundException(mailboxPath); } LOGGER.debug("Loaded mailbox {}", mailboxPath); return createMessageManager(mailboxRow, session); } @Inject  StoreMailboxManager(MailboxSessionMapperFactory mailboxSessionMapperFactory, SessionProvider sessionProvider,
                               MailboxPathLocker locker, MessageParser messageParser,
                               MessageId.Factory messageIdFactory, MailboxAnnotationManager annotationManager,
                               EventBus eventBus, StoreRightManager storeRightManager,
                               QuotaComponents quotaComponents, MessageSearchIndex searchIndex, MailboxManagerConfiguration configuration,
                               PreDeletionHooks preDeletionHooks); QuotaComponents getQuotaComponents(); Factory getMessageIdFactory(); SessionProvider getSessionProvider(); @Override EnumSet<MailboxCapabilities> getSupportedMailboxCapabilities(); @Override EnumSet<MessageCapabilities> getSupportedMessageCapabilities(); @Override EnumSet<SearchCapabilities> getSupportedSearchCapabilities(); EventBus getEventBus(); MessageSearchIndex getMessageSearchIndex(); MailboxSessionMapperFactory getMapperFactory(); MailboxPathLocker getLocker(); MessageParser getMessageParser(); PreDeletionHooks getPreDeletionHooks(); @Override MailboxSession createSystemSession(Username userName); @Override char getDelimiter(); @Override MailboxSession login(Username userid, String passwd); @Override MailboxSession loginAsOtherUser(Username adminUserid, String passwd, Username otherUserId); @Override void logout(MailboxSession session); @Override MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession session); @Override MessageManager getMailbox(MailboxId mailboxId, MailboxSession session); @Override Optional<MailboxId> createMailbox(MailboxPath mailboxPath, MailboxSession mailboxSession); @Override void deleteMailbox(final MailboxPath mailboxPath, final MailboxSession session); @Override Mailbox deleteMailbox(MailboxId mailboxId, MailboxSession session); @Override List<MailboxRenamedResult> renameMailbox(MailboxPath from, MailboxPath to, RenameOption option,
                                                    MailboxSession session); @Override List<MailboxRenamedResult> renameMailbox(MailboxId mailboxId, MailboxPath newMailboxPath, RenameOption option,
                                                    MailboxSession session); @Override List<MessageRange> copyMessages(MessageRange set, MailboxPath from, MailboxPath to, MailboxSession session); @Override List<MessageRange> copyMessages(MessageRange set, MailboxId from, MailboxId to, MailboxSession session); @Override List<MessageRange> moveMessages(MessageRange set, MailboxPath from, MailboxPath to, MailboxSession session); @Override Flux<MailboxMetaData> search(MailboxQuery expression, MailboxSession session); @Override Flux<MessageId> search(MultimailboxesSearchQuery expression, MailboxSession session, long limit); @Override Mono<Boolean> mailboxExists(MailboxPath mailboxPath, MailboxSession session); @Override void endProcessingRequest(MailboxSession session); @Override void startProcessingRequest(MailboxSession session); @Override List<MailboxPath> list(MailboxSession session); @Override boolean hasRight(MailboxPath mailboxPath, Right right, MailboxSession session); @Override boolean hasRight(MailboxId mailboxId, Right right, MailboxSession session); @Override Rfc4314Rights myRights(MailboxPath mailboxPath, MailboxSession session); @Override Mono<Rfc4314Rights> myRights(MailboxId mailboxId, MailboxSession session); @Override Rfc4314Rights myRights(Mailbox mailbox, MailboxSession session); @Override List<Rfc4314Rights> listRights(MailboxPath mailboxPath, MailboxACL.EntryKey key, MailboxSession session); @Override MailboxACL listRights(MailboxPath mailboxPath, MailboxSession session); @Override void applyRightsCommand(MailboxPath mailboxPath, MailboxACL.ACLCommand mailboxACLCommand, MailboxSession session); @Override void applyRightsCommand(MailboxId mailboxId, MailboxACL.ACLCommand mailboxACLCommand, MailboxSession session); @Override void setRights(MailboxPath mailboxPath, MailboxACL mailboxACL, MailboxSession session); @Override void setRights(MailboxId mailboxId, MailboxACL mailboxACL, MailboxSession session); @Override List<MailboxAnnotation> getAllAnnotations(MailboxPath mailboxPath, MailboxSession session); @Override List<MailboxAnnotation> getAnnotationsByKeys(MailboxPath mailboxPath, MailboxSession session, Set<MailboxAnnotationKey> keys); @Override void updateAnnotations(MailboxPath mailboxPath, MailboxSession session, List<MailboxAnnotation> mailboxAnnotations); @Override boolean hasCapability(MailboxCapabilities capability); @Override List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(MailboxPath mailboxPath, MailboxSession session,
                                                                    Set<MailboxAnnotationKey> keys); @Override List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(MailboxPath mailboxPath, MailboxSession session,
                                                                    Set<MailboxAnnotationKey> keys); @Override boolean hasChildren(MailboxPath mailboxPath, MailboxSession session); static final char SQL_WILDCARD_CHAR; static final EnumSet<MessageCapabilities> DEFAULT_NO_MESSAGE_CAPABILITIES; static final int MAX_ATTEMPTS; static final Duration MIN_BACKOFF; static final RetryBackoffSpec RETRY_BACKOFF_SPEC; }
@Test void getMailboxShouldThrowWhenUnknownId() { when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(Mono.error(new MailboxNotFoundException(MAILBOX_ID))); assertThatThrownBy(() -> storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession)) .isInstanceOf(MailboxNotFoundException.class); }
@Test void getMailboxShouldReturnMailboxManagerWhenKnownId() throws Exception { Mailbox mockedMailbox = mock(Mailbox.class); when(mockedMailbox.generateAssociatedPath()) .thenReturn(MailboxPath.forUser(CURRENT_USER, "mailboxName")); when(mockedMailbox.getMailboxId()).thenReturn(MAILBOX_ID); when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(Mono.just(mockedMailbox)); MessageManager expected = storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession); assertThat(expected.getId()).isEqualTo(MAILBOX_ID); }
@Test void getMailboxShouldReturnMailboxManagerWhenKnownIdAndDifferentCaseUser() throws Exception { Mailbox mockedMailbox = mock(Mailbox.class); when(mockedMailbox.generateAssociatedPath()) .thenReturn(MailboxPath.forUser(Username.of("uSEr"), "mailboxName")); when(mockedMailbox.getMailboxId()).thenReturn(MAILBOX_ID); when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(Mono.just(mockedMailbox)); MessageManager expected = storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession); assertThat(expected.getId()).isEqualTo(MAILBOX_ID); }
@Test void getMailboxShouldThrowWhenMailboxDoesNotMatchUserWithoutRight() { Username otherUser = Username.of("other.user"); Mailbox mockedMailbox = mock(Mailbox.class); when(mockedMailbox.getACL()).thenReturn(new MailboxACL()); when(mockedMailbox.generateAssociatedPath()) .thenReturn(MailboxPath.forUser(otherUser, "mailboxName")); when(mockedMailbox.getMailboxId()).thenReturn(MAILBOX_ID); when(mockedMailbox.getUser()).thenReturn(otherUser); when(mockedMailboxMapper.findMailboxById(MAILBOX_ID)).thenReturn(Mono.just(mockedMailbox)); when(mockedMailboxMapper.findMailboxByPath(any())).thenReturn(Mono.just(mockedMailbox)); assertThatThrownBy(() -> storeMailboxManager.getMailbox(MAILBOX_ID, mockedMailboxSession)) .isInstanceOf(MailboxNotFoundException.class); }