SetMessagesUpdateProcessor implements SetMessagesProcessor { @VisibleForTesting void assertUserCanSendFrom(Username connectedUser, Optional<Username> fromUser) throws MailboxSendingNotAllowedException { if (!fromUser.filter(from -> canSendFrom.userCanSendFrom(connectedUser, from)) .isPresent()) { throw new MailboxSendingNotAllowedException(connectedUser, fromUser); } else { LOGGER.debug("{} is allowed to send a mail using {} identity", connectedUser.asString(), fromUser); } } @Inject @VisibleForTesting  SetMessagesUpdateProcessor(
            UpdateMessagePatchConverter updatePatchConverter,
            MessageIdManager messageIdManager,
            SystemMailboxesProvider systemMailboxesProvider,
            Factory mailboxIdFactory,
            MessageSender messageSender,
            MetricFactory metricFactory,
            ReferenceUpdater referenceUpdater,
            CanSendFrom canSendFrom); @Override SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession);  }
@Test public void assertUserCanSendFromShouldNotThrowWhenSenderIsAnAliasOfTheConnectedUser() throws RecipientRewriteTableException, AddressException { Username sender = Username.of("alias@example.com"); recipientRewriteTable.addAliasMapping(MappingSource.fromUser("alias", "example.com"), USER.asString()); assertThatCode(() -> sut.assertUserCanSendFrom(USER, Optional.of(sender))) .doesNotThrowAnyException(); }
@Test public void assertUserCanSendFromShouldThrowWhenSenderIsAnAliasOfAnotherUser() throws RecipientRewriteTableException, AddressException { Username sender = Username.of("alias@example.com"); recipientRewriteTable.addAliasMapping(MappingSource.fromUser("alias", "example.com"), OTHER_USER.asString()); assertThatThrownBy(() -> sut.assertUserCanSendFrom(USER, Optional.of(sender))) .isInstanceOf(MailboxSendingNotAllowedException.class); }