blob: 0c2067751a1fb34fa9d8a306dd43cd6f18e1f341 [file] [log] [blame]
package org.apache.james.crowdsec;
import java.io.IOException;
import java.net.URL;
import org.apache.james.crowdsec.client.CrowdsecClientConfiguration;
import org.apache.james.protocols.api.Response;
import org.apache.james.protocols.smtp.SMTPSession;
import org.apache.james.protocols.smtp.utils.BaseFakeSMTPSession;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import static org.apache.james.crowdsec.CrowdsecExtension.CROWDSEC_PORT;
import static org.assertj.core.api.Assertions.assertThat;
class CrowdsecSMTPConnectHandlerTest {
@RegisterExtension
static CrowdsecExtension crowdsecExtension = new CrowdsecExtension();
private CrowdsecSMTPConnectHandler connectHandler;
@BeforeEach
void setUpEach() throws IOException {
var crowdsecClientConfiguration = new CrowdsecClientConfiguration(crowdsecExtension.getLocalhostCrowdsecUrl(), CrowdsecClientConfiguration.DEFAULT_API_KEY);
connectHandler = new CrowdsecSMTPConnectHandler(new CrowdsecService(crowdsecClientConfiguration));
}
@Test
void givenIPBannedByCrowdsecDecision() throws IOException, InterruptedException {
crowdsecExtension.banIP("--ip", "127.0.0.1");
SMTPSession session = new BaseFakeSMTPSession() {};
assertThat(connectHandler.onConnect(session)).isEqualTo(Response.DISCONNECT);
}
@Test
void givenIPNotBannedByCrowdsecDecision() throws IOException, InterruptedException {
crowdsecExtension.banIP("--range", "192.182.39.2/24");
SMTPSession session = new BaseFakeSMTPSession() {};
assertThat(connectHandler.onConnect(session)).isEqualTo(CrowdsecSMTPConnectHandler.NOOP);
}
}