chibenwa commented on a change in pull request #869: URL: https://github.com/apache/james-project/pull/869#discussion_r793359807
########## File path: server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/GlobalRateLimitIntegrationTest.java ########## @@ -0,0 +1,388 @@ +/**************************************************************** + * 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.mailets.configuration.Constants.DEFAULT_DOMAIN; +import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP; +import static org.apache.james.mailets.configuration.Constants.PASSWORD; +import static org.apache.james.mailets.configuration.Constants.awaitAtMostOneMinute; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; + +import javax.mail.internet.MimeMessage; + +import org.apache.james.core.Domain; +import org.apache.james.core.builder.MimeMessageBuilder; +import org.apache.james.mailbox.model.MultimailboxesSearchQuery; +import org.apache.james.mailbox.model.SearchQuery; +import org.apache.james.mailbox.probe.MailboxProbe; +import org.apache.james.mailets.TemporaryJamesServer; +import org.apache.james.mailets.configuration.MailetConfiguration; +import org.apache.james.mailets.configuration.MailetContainer; +import org.apache.james.mailets.configuration.ProcessorConfiguration; +import org.apache.james.modules.MailboxProbeImpl; +import org.apache.james.modules.protocols.ImapGuiceProbe; +import org.apache.james.modules.protocols.SmtpGuiceProbe; +import org.apache.james.rate.limiter.memory.MemoryRateLimiterModule; +import org.apache.james.transport.matchers.All; +import org.apache.james.utils.DataProbeImpl; +import org.apache.james.utils.SMTPMessageSender; +import org.apache.james.utils.SMTPMessageSenderExtension; +import org.apache.james.utils.SpoolerProbe; +import org.apache.james.utils.TestIMAPClient; +import org.apache.mailet.base.test.FakeMail; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +public class GlobalRateLimitIntegrationTest { + + private static final String SENDER1 = "sender1@" + DEFAULT_DOMAIN; + private static final String SENDER2 = "sender2@" + DEFAULT_DOMAIN; + private static final String RECIPIENT1 = "recipient1@" + DEFAULT_DOMAIN; + private static final String RECIPIENT2 = "recipient2@" + DEFAULT_DOMAIN; + private static final String RECIPIENT3 = "recipient3@" + DEFAULT_DOMAIN; + private static final String RECIPIENT4 = "recipient4@" + DEFAULT_DOMAIN; + private static final String MESSAGE_CONTENT = "any text"; + private static final String RATE_LIMIT_STATE = "rate_limit_state"; + + private TemporaryJamesServer jamesServer; + private MailboxProbe mailboxProbe; + private MimeMessage message; + + @RegisterExtension + public TestIMAPClient testIMAPClient = new TestIMAPClient(); + @RegisterExtension + public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN); + + @RegisterExtension + SMTPMessageSenderExtension smtpSenderExtension = new SMTPMessageSenderExtension(Domain.of(DEFAULT_DOMAIN)); + + @BeforeEach + void setup(@TempDir File temporaryFolder) throws Exception { + ProcessorConfiguration rateLimitProcessor = ProcessorConfiguration.builder() + .state(RATE_LIMIT_STATE) + .enableJmx(true) + .addMailet(MailetConfiguration.builder() + .matcher(All.class) + .mailet(GlobalRateLimit.class) + .addProperty("duration", "60s") + .addProperty("count", "2") + .addProperty("recipients", "3") + .addProperty("size", "13K") + .addProperty("totalSize", "20K") + .build()) + .addMailet(MailetConfiguration.TO_TRANSPORT) + .build(); + + MailetContainer.Builder mailetContainer = TemporaryJamesServer.simpleMailetContainerConfiguration() + .putProcessor(ProcessorConfiguration.root() + .addMailet(MailetConfiguration.builder() + .matcher(All.class) + .mailet(ToProcessor.class) + .addProperty("processor", RATE_LIMIT_STATE))) + .putProcessor(rateLimitProcessor); + + jamesServer = TemporaryJamesServer.builder() + .withMailetContainer(mailetContainer) + .withOverrides(new MemoryRateLimiterModule()) + .build(temporaryFolder); + jamesServer.start(); + + jamesServer.getProbe(DataProbeImpl.class).fluent() + .addDomain(DEFAULT_DOMAIN) + .addUser(SENDER1, PASSWORD) + .addUser(SENDER2, PASSWORD) + .addUser(RECIPIENT1, PASSWORD) + .addUser(RECIPIENT2, PASSWORD) + .addUser(RECIPIENT3, PASSWORD) + .addUser(RECIPIENT4, PASSWORD); + + + mailboxProbe = jamesServer.getProbe(MailboxProbeImpl.class); + + message = MimeMessageBuilder.mimeMessageBuilder() + .setSubject("test") + .setText(MESSAGE_CONTENT) + .build(); + } + + @AfterEach + void tearDown() { + jamesServer.shutdown(); + } + + @Test + void sendMessageShouldSucceedWhenCountLimitAcceptable() throws Exception { + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) + .authenticate(SENDER1, PASSWORD) + .sendMessage(FakeMail.builder() + .name("name") + .mimeMessage(message) + .sender(SENDER1) + .recipient(RECIPIENT1)); + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) + .authenticate(SENDER2, PASSWORD) + .sendMessage(FakeMail.builder() + .name("name") + .mimeMessage(message) + .sender(SENDER2) + .recipient(RECIPIENT1)); + + testIMAPClient.connect(LOCALHOST_IP, jamesServer.getProbe(ImapGuiceProbe.class).getImapPort()) + .login(RECIPIENT1, PASSWORD) + .select(TestIMAPClient.INBOX) + .awaitMessageCount(awaitAtMostOneMinute, 2); + } + + @Test + void sendMessageShouldFailWhenCountLimitExceeded() throws Exception { + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) + .authenticate(SENDER1, PASSWORD) + .sendMessage(FakeMail.builder() + .name("name") + .mimeMessage(message) + .sender(SENDER1) + .recipient(RECIPIENT1)); + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) + .authenticate(SENDER2, PASSWORD) + .sendMessage(FakeMail.builder() + .name("name") + .mimeMessage(message) + .sender(SENDER2) + .recipient(RECIPIENT1)); + + // await first 2 message + testIMAPClient.connect(LOCALHOST_IP, jamesServer.getProbe(ImapGuiceProbe.class).getImapPort()) + .login(RECIPIENT1, PASSWORD) + .select(TestIMAPClient.INBOX) + .awaitMessageCount(awaitAtMostOneMinute, 2); + + // send third message should exceed count limit + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) + .authenticate(SENDER1, PASSWORD) + .sendMessage(FakeMail.builder() + .name("name") + .mimeMessage(message) + .sender(SENDER1) + .recipient(RECIPIENT2)); + + awaitAtMostOneMinute.untilAsserted(() -> jamesServer.getProbe(SpoolerProbe.class).processingFinished()); + + assertThat(testIMAPClient.connect(LOCALHOST_IP, jamesServer.getProbe(ImapGuiceProbe.class).getImapPort()) + .login(RECIPIENT2, PASSWORD) + .select(TestIMAPClient.INBOX) + .getMessageCount(TestIMAPClient.INBOX)).isEqualTo(0); Review comment: Where is the mail? Can we have an assert on that? Hint: in `var/mail/error` mail repository ########## File path: server/mailet/integration-testing/src/test/java/org/apache/james/transport/mailets/GlobalRateLimitIntegrationTest.java ########## @@ -0,0 +1,388 @@ +/**************************************************************** + * 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.mailets.configuration.Constants.DEFAULT_DOMAIN; +import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP; +import static org.apache.james.mailets.configuration.Constants.PASSWORD; +import static org.apache.james.mailets.configuration.Constants.awaitAtMostOneMinute; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; + +import javax.mail.internet.MimeMessage; + +import org.apache.james.core.Domain; +import org.apache.james.core.builder.MimeMessageBuilder; +import org.apache.james.mailbox.model.MultimailboxesSearchQuery; +import org.apache.james.mailbox.model.SearchQuery; +import org.apache.james.mailbox.probe.MailboxProbe; +import org.apache.james.mailets.TemporaryJamesServer; +import org.apache.james.mailets.configuration.MailetConfiguration; +import org.apache.james.mailets.configuration.MailetContainer; +import org.apache.james.mailets.configuration.ProcessorConfiguration; +import org.apache.james.modules.MailboxProbeImpl; +import org.apache.james.modules.protocols.ImapGuiceProbe; +import org.apache.james.modules.protocols.SmtpGuiceProbe; +import org.apache.james.rate.limiter.memory.MemoryRateLimiterModule; +import org.apache.james.transport.matchers.All; +import org.apache.james.utils.DataProbeImpl; +import org.apache.james.utils.SMTPMessageSender; +import org.apache.james.utils.SMTPMessageSenderExtension; +import org.apache.james.utils.SpoolerProbe; +import org.apache.james.utils.TestIMAPClient; +import org.apache.mailet.base.test.FakeMail; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +public class GlobalRateLimitIntegrationTest { + + private static final String SENDER1 = "sender1@" + DEFAULT_DOMAIN; + private static final String SENDER2 = "sender2@" + DEFAULT_DOMAIN; + private static final String RECIPIENT1 = "recipient1@" + DEFAULT_DOMAIN; + private static final String RECIPIENT2 = "recipient2@" + DEFAULT_DOMAIN; + private static final String RECIPIENT3 = "recipient3@" + DEFAULT_DOMAIN; + private static final String RECIPIENT4 = "recipient4@" + DEFAULT_DOMAIN; + private static final String MESSAGE_CONTENT = "any text"; + private static final String RATE_LIMIT_STATE = "rate_limit_state"; + + private TemporaryJamesServer jamesServer; + private MailboxProbe mailboxProbe; + private MimeMessage message; + + @RegisterExtension + public TestIMAPClient testIMAPClient = new TestIMAPClient(); + @RegisterExtension + public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN); + + @RegisterExtension + SMTPMessageSenderExtension smtpSenderExtension = new SMTPMessageSenderExtension(Domain.of(DEFAULT_DOMAIN)); + + @BeforeEach + void setup(@TempDir File temporaryFolder) throws Exception { + ProcessorConfiguration rateLimitProcessor = ProcessorConfiguration.builder() + .state(RATE_LIMIT_STATE) + .enableJmx(true) + .addMailet(MailetConfiguration.builder() + .matcher(All.class) + .mailet(GlobalRateLimit.class) + .addProperty("duration", "60s") + .addProperty("count", "2") + .addProperty("recipients", "3") + .addProperty("size", "13K") + .addProperty("totalSize", "20K") + .build()) + .addMailet(MailetConfiguration.TO_TRANSPORT) + .build(); + + MailetContainer.Builder mailetContainer = TemporaryJamesServer.simpleMailetContainerConfiguration() + .putProcessor(ProcessorConfiguration.root() + .addMailet(MailetConfiguration.builder() + .matcher(All.class) + .mailet(ToProcessor.class) + .addProperty("processor", RATE_LIMIT_STATE))) Review comment: Why not put GlobalRateLimit directly here? -- 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]
