chibenwa commented on code in PR #2405: URL: https://github.com/apache/james-project/pull/2405#discussion_r1765050801
########## server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SubAddressingTest.java: ########## @@ -0,0 +1,213 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.mailets; + +import static org.apache.james.transport.mailets.WithStorageDirectiveTest.NO_DOMAIN_LIST; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Collection; + +import jakarta.mail.MessagingException; + +import org.apache.commons.lang3.tuple.Pair; +import org.apache.james.core.Username; +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources; +import org.apache.james.mailbox.model.MailboxACL; +import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.user.api.UsersRepository; +import org.apache.james.user.memory.MemoryUsersRepository; +import org.apache.mailet.Attribute; +import org.apache.mailet.AttributeName; +import org.apache.mailet.AttributeValue; +import org.apache.mailet.Mail; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailetConfig; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + + +public class SubAddressingTest { + + private static final String SENDER1 = "sender1@localhost"; + private static final String SENDER2 = "sender2@localhost"; + private static final String TARGET = "targetfolder"; + private static final String UNEXISTING_TARGET = "unexistingfolder"; + + private MailboxManager mailboxManager; + private SubAddressing testee; + private UsersRepository usersRepository; + private Username sender1Username; + private Username sender2Username; + private Username recipientUsername; + private MailboxSession recipientSession; + private MailboxId targetMailboxId; + + @BeforeEach + void setUp() throws MailboxException, MessagingException { + mailboxManager = InMemoryIntegrationResources.defaultResources().getMailboxManager(); + usersRepository = MemoryUsersRepository.withVirtualHosting(NO_DOMAIN_LIST); + + testee = new SubAddressing(usersRepository, mailboxManager); + testee.init(FakeMailetConfig.builder() + .build()); + + sender1Username = Username.of("sender1"); + sender2Username = Username.of("sender2"); + recipientUsername = Username.of("recipient"); + recipientSession = mailboxManager.createSystemSession(recipientUsername); + targetMailboxId = mailboxManager.createMailbox( + MailboxPath.forUser(recipientUsername, TARGET), recipientSession).get(); + } + + @Test + void shouldNotAddStorageDirectiveWhenTargetMailboxDoesNotExist() throws Exception { + Mail mail = mailBuilder(UNEXISTING_TARGET).sender(SENDER1).build(); + testee.service(mail); + + AttributeName recipient = AttributeName.of("DeliveryPaths_recipient@localhost"); + assertThat(mail.attributes().map(this::unbox)) + .doesNotContain(Pair.of(recipient, UNEXISTING_TARGET)); + } + + @Test + void shouldNotAddStorageDirectiveWhenNobodyHasRight() throws Exception { + givePostRightForKey(MailboxACL.ANYBODY_NEGATIVE_KEY); + + Mail mail = mailBuilder(TARGET).sender(SENDER1).build(); + testee.service(mail); + + AttributeName recipient = AttributeName.of("DeliveryPaths_recipient@localhost"); + assertThat(mail.attributes().map(this::unbox)) + .doesNotContain(Pair.of(recipient, TARGET)); + } + + + @Test + void shouldAddStorageDirectiveWhenAnybodyHasRight() throws Exception { + givePostRightForKey(MailboxACL.ANYBODY_KEY); + + Mail mail = mailBuilder(TARGET).sender(SENDER1).build(); + testee.service(mail); + + AttributeName recipient = AttributeName.of("DeliveryPaths_recipient@localhost"); + assertThat(mail.attributes().map(this::unbox)) + .containsOnly(Pair.of(recipient, TARGET)); + } + + @Test + void shouldAddStorageDirectiveWhenSenderIsWhiteListed() throws Exception { + // whitelist sender 1 and send from sender 1 + givePostRightForKey(MailboxACL.EntryKey.createUserEntryKey(sender1Username, false)); + + Mail mail = mailBuilder(TARGET).sender(SENDER1).build(); + testee.service(mail); + + AttributeName recipient = AttributeName.of("DeliveryPaths_recipient@localhost"); + assertThat(mail.attributes().map(this::unbox)) + .containsOnly(Pair.of(recipient, TARGET)); + } + + @Test + void shouldNotAddStorageDirectiveWhenSenderIsNotWhiteListed() throws Exception { + // whitelist sender 1 and send from sender 2 + givePostRightForKey(MailboxACL.EntryKey.createUserEntryKey(sender1Username, false)); + + Mail mail = mailBuilder(TARGET).sender(SENDER2).build(); + testee.service(mail); + + AttributeName recipient = AttributeName.of("DeliveryPaths_recipient@localhost"); + assertThat(mail.attributes().map(this::unbox)) + .doesNotContain(Pair.of(recipient, TARGET)); + } + + @Test + void shouldNotAddStorageDirectiveWhenSenderIsBlackListed() throws Exception { + // blacklist sender 1 and send from sender 1 + givePostRightForKey(MailboxACL.EntryKey.createUserEntryKey(sender1Username, true)); Review Comment: true is a magic constant. Can't guess easily what it means. Extracting it as a constant could allow naming and help express the intent. Also we could improve EntryKey api: ``` // API stability MailboxACL.EntryKey.createUserEntryKey(sender1Username, false|false) MailboxACL.EntryKey.createUserEntryKey(sender1Username) MailboxACL.EntryKey.createNegativeUserEntryKey(sender1Username) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
