@Override protected Mailbox auth(POP3Session session, String username, String password) throws Exception { MailboxSession mSession = null; try { mSession = manager.login(session.getUser(), password); manager.startProcessingRequest(mSession); MailboxPath inbox = MailboxPath.inbox(mSession); // check if the mailbox exists, if not create it if (!manager.mailboxExists(inbox, mSession)) { Optional<MailboxId> mailboxId = manager.createMailbox(inbox, mSession); LOGGER.info("Provisioning INBOX. {} created.", mailboxId); } MessageManager mailbox = manager.getMailbox(MailboxPath.inbox(mSession), mSession); return new MailboxAdapter(manager, mailbox, mSession); } catch (BadCredentialsException e) { return null; } catch (MailboxException e) { throw new IOException("Unable to access mailbox for user " + session.getUser(), e); } finally { if (mSession != null) { manager.endProcessingRequest(mSession); } } }
private void initAndMockData() { okResponse = mock(StatusResponse.class); mockNextProcessor = mock(ImapProcessor.class); mockMailboxManager = mock(MailboxManager.class); mockStatusResponseFactory = mock(StatusResponseFactory.class); mockResponder = mock(ImapProcessor.Responder.class); mockImapSession = mock(ImapSession.class); mockMailboxSession = new MockMailboxSession("username"); inbox = MailboxPath.inbox(mockMailboxSession); mailboxAnnotations = ImmutableList.of(MailboxAnnotation.newInstance(new MailboxAnnotationKey("/private/key"), "anyValue")); request = new SetAnnotationRequest(TAG, ImapCommand.anyStateCommand("Name"), ImapConstants.INBOX_NAME, mailboxAnnotations); humanTextCaptor = ArgumentCaptor.forClass(HumanReadableText.class); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); }
private void initAndMockData() { statusResponse = mock(StatusResponse.class); mockNextProcessor = mock(ImapProcessor.class); mockMailboxManager = mock(MailboxManager.class); mockStatusResponseFactory = mock(StatusResponseFactory.class); mockResponder = mock(ImapProcessor.Responder.class); mockImapSession = mock(ImapSession.class); mailboxSession = new MockMailboxSession("username"); inbox = MailboxPath.inbox(mailboxSession); keys = ImmutableSet.of(PRIVATE_KEY); annotationRequestBuilder = GetAnnotationRequest.builder() .tag(TAG) .command(ImapCommand.anyStateCommand("Name")) .mailboxName(ImapConstants.INBOX_NAME); humanTextCaptor = ArgumentCaptor.forClass(HumanReadableText.class); captorResponsecode = ArgumentCaptor.forClass(ResponseCode.class); captorAnnotationResponse = ArgumentCaptor.forClass(AnnotationResponse.class); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mailboxSession); }
private static void provisionUser(MailboxManager mailboxManager, String user) throws MailboxException { MailboxSession mailboxSession = mailboxManager.createSystemSession(user); mailboxManager.startProcessingRequest(mailboxSession); createMailbox(mailboxManager, mailboxSession, MailboxPath.inbox(mailboxSession)); IntStream.range(0, SUB_MAILBOXES_COUNT) .mapToObj(i -> MailboxConstants.INBOX + ".SUB_FOLDER_" + i) .peek(name -> createMailbox(mailboxManager, mailboxSession, MailboxPath.forUser(user, name))) .forEach(name -> createSubSubMailboxes(mailboxManager, mailboxSession, name)); mailboxManager.endProcessingRequest(mailboxSession); mailboxManager.logout(mailboxSession, true); }
@Test public void processShouldRespondNoOnUnExistingTargetMailbox() throws Exception { CopyRequest copyRequest = new CopyRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(4, 6)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(false); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class), any(StatusResponse.ResponseCode.class))).thenReturn(noResponse); testee.process(copyRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
@Test public void processShouldRespondNoOnUnExistingTargetMailbox() throws Exception { MoveRequest moveRequest = new MoveRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(5, 6), new IdRange(1,3)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(false); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class), any(StatusResponse.ResponseCode.class))).thenReturn(noResponse); testee.process(moveRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
@Test public void processShouldRespondNoOnMailboxException() throws Exception { CopyRequest copyRequest = new CopyRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(4, 6)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenThrow(new MailboxException()); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class))).thenReturn(noResponse); testee.process(copyRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
@Override public Collection<MailAddress> match(Mail mail) throws MessagingException { try { List<MailAddress> result = new ArrayList<>(); for (MailAddress mailAddress : mail.getRecipients()) { String userName = usersRepository.getUser(mailAddress); MailboxSession mailboxSession = mailboxManager.createSystemSession(userName); MailboxPath mailboxPath = MailboxPath.inbox(mailboxSession); QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(mailboxPath); if (quotaManager.getMessageQuota(quotaRoot).isOverQuotaWithAdditionalValue(SINGLE_EMAIL) || quotaManager.getStorageQuota(quotaRoot).isOverQuotaWithAdditionalValue(mail.getMessageSize())) { result.add(mailAddress); } } return result; } catch (MailboxException e) { throw new MessagingException("Exception while checking quotas", e); } catch (UsersRepositoryException e) { throw new MessagingException("Exception while retrieving username", e); } } }
@Test public void processShouldRespondNoOnMailboxException() throws Exception { MoveRequest moveRequest = new MoveRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(5, 6), new IdRange(1,3)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenThrow(new MailboxException()); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class))).thenReturn(noResponse); testee.process(moveRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class);
when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class);
when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class);
when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class);
MailboxSession session = mailboxManager.login("foo6", "bar6"); MailboxPath mailboxPath = MailboxPath.inbox(session);
@Test public void matchShouldSupportVirtualHosting() throws Exception { when(usersRepository.getUser(MailAddressFixture.ANY_AT_JAMES)).thenReturn(MailAddressFixture.ANY_AT_JAMES.asString()); when(usersRepository.getUser(MailAddressFixture.OTHER_AT_JAMES)).thenReturn(MailAddressFixture.OTHER_AT_JAMES.asString()); String username = MailAddressFixture.ANY_AT_JAMES.asString(); QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(MailboxPath.inbox(mailboxManager.createSystemSession(username))); maxQuotaManager.setMaxStorage(quotaRoot, QuotaSize.size(100)); FakeMail fakeMail = FakeMail.builder() .recipient(MailAddressFixture.ANY_AT_JAMES) .recipient(MailAddressFixture.OTHER_AT_JAMES) .size(150) .build(); Collection<MailAddress> result = testee.match(fakeMail); assertThat(result).containsOnly(MailAddressFixture.ANY_AT_JAMES); }
@Test public void matchShouldNotIncludeRecipientNotOverQuota() throws Exception { String username = MailAddressFixture.ANY_AT_JAMES.getLocalPart(); QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(MailboxPath.inbox(mailboxManager.createSystemSession(username))); maxQuotaManager.setMaxStorage(quotaRoot, QuotaSize.size(100)); FakeMail fakeMail = FakeMail.builder() .recipient(MailAddressFixture.ANY_AT_JAMES) .recipient(MailAddressFixture.OTHER_AT_JAMES) .size(150) .build(); Collection<MailAddress> result = testee.match(fakeMail); assertThat(result).containsOnly(MailAddressFixture.ANY_AT_JAMES); }
public ManagerTestResources(IntegrationResources<T> integrationResources) throws Exception { this.integrationResources = integrationResources; groupMembershipResolver = integrationResources.createGroupMembershipResolver(); mailboxManager = integrationResources.createMailboxManager(groupMembershipResolver); maxQuotaManager = integrationResources.retrieveMaxQuotaManager(mailboxManager); quotaRootResolver = integrationResources.retrieveQuotaRootResolver(mailboxManager); quotaManager = integrationResources.retrieveQuotaManager(mailboxManager); integrationResources.init(); session = mailboxManager.login(USER, USER_PASS); inbox = MailboxPath.inbox(session); subFolder = new MailboxPath(inbox, "INBOX.SUB"); maxQuotaManager.setGlobalMaxMessage(QuotaCount.count(1000)); maxQuotaManager.setGlobalMaxStorage(QuotaSize.size(1000000)); }
inboxPath = MailboxPath.inbox(session); customMailboxPath = new MailboxPath(inboxPath, "custom"); mailboxManager.createMailbox(inboxPath, session);