From fd042b7f4624e290e049f6bf71847ea982dae0c4 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Thu, 17 Oct 2024 15:24:15 +0200 Subject: [PATCH 01/25] [backend]Add inject execution test --- .../java/io/openbas/rest/InjectApiTest.java | 58 ++++++++++++++++++- .../openbas/utils/fixtures/InjectFixture.java | 10 ++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 9f3f4a4751..166dc485d0 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -16,7 +16,12 @@ import io.openbas.database.model.*; import io.openbas.database.model.InjectorContract; import io.openbas.database.repository.*; +import io.openbas.execution.ExecutableInject; +import io.openbas.execution.ExecutionContext; +import io.openbas.execution.ExecutionContextService; +import io.openbas.execution.Executor; import io.openbas.rest.exercise.service.ExerciseService; +import io.openbas.rest.inject.form.DirectInjectInput; import io.openbas.rest.inject.form.InjectInput; import io.openbas.service.ScenarioService; import io.openbas.utils.fixtures.InjectExpectationFixture; @@ -29,6 +34,13 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import java.util.Collections; + +import static io.openbas.config.SessionHelper.currentUser; +import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; +import static io.openbas.utils.fixtures.UserFixture.getSavedUser; +import static org.mockito.Mockito.verify; + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(PER_CLASS) class InjectApiTest extends IntegrationTest { @@ -44,13 +56,19 @@ class InjectApiTest extends IntegrationTest { @Autowired private ScenarioService scenarioService; @Autowired private ExerciseService exerciseService; @Autowired private ExerciseRepository exerciseRepository; - @Autowired private ScenarioRepository scenarioRepository; + @Autowired private ExecutionContextService executionContextService; + @Autowired + private Executor executor; + @Autowired + private ScenarioRepository scenarioRepository; @Autowired private InjectRepository injectRepository; @Autowired private DocumentRepository documentRepository; @Autowired private CommunicationRepository communicationRepository; @Autowired private InjectExpectationRepository injectExpectationRepository; @Autowired private TeamRepository teamRepository; @Autowired private InjectorContractRepository injectorContractRepository; + @Autowired + private UserRepository userRepository; @BeforeAll void beforeAll() { @@ -339,6 +357,44 @@ void updateInjectForSimulationTest() throws Exception { assertEquals(injectTitle, JsonPath.read(response, "$.inject_title")); } + @DisplayName("Execute an email inject for exercise") + @Test + @Order(11) + @WithMockPlannerUser + void executeEmailInjectForExerciseTest() throws Exception { + // -- PREPARE -- + InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + Inject inject = getInjectForEmailContract(injectorContract); + inject.setUser(userRepository.findById(currentUser().getId()).orElseThrow()); + inject.setExercise(EXERCISE); + Inject savedInject = this.injectRepository.save(inject); + List userInjectContexts = Collections.singletonList( + executionContextService.executionContext(getSavedUser(), savedInject, "Direct execution")); + ExecutableInject injection = new ExecutableInject( + true, true, savedInject, List.of(), savedInject.getAssets(), + savedInject.getAssetGroups(), userInjectContexts + ); + + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(savedInject.getTitle()); + input.setDescription(savedInject.getDescription()); + input.setInjectorContract(savedInject.getInjectorContract().orElseThrow().getId()); + input.setUserIds(List.of(savedInject.getId())); + + // -- EXECUTE -- + mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") + .content(asJsonString(input)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()); + + // -- ASSERT -- + verify(executor).execute(injection); + + //-- THEN --- + injectRepository.delete(savedInject); + } + // -- BULK DELETE -- @DisplayName("Delete list of inject for exercise") diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java index a1c050739c..615e4040b4 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java @@ -6,6 +6,7 @@ public class InjectFixture { public static final String INJECT_EMAIL_NAME = "Test email inject"; + public static final String INJECT_SMS_NAME = "Test sms inject"; public static Inject getInjectForEmailContract(InjectorContract injectorContract) { Inject inject = new Inject(); @@ -15,4 +16,13 @@ public static Inject getInjectForEmailContract(InjectorContract injectorContract inject.setDependsDuration(0L); return inject; } + + public static Inject getInjectForSmsContract(InjectorContract injectorContract) { + Inject inject = new Inject(); + inject.setTitle(INJECT_SMS_NAME); + inject.setInjectorContract(injectorContract); + inject.setEnabled(true); + inject.setDependsDuration(0L); + return inject; + } } From 5312914235c8810b5eb6dfd2417b3cfb2efe892a Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Fri, 18 Oct 2024 12:41:00 +0200 Subject: [PATCH 02/25] execution test with content --- .../java/io/openbas/rest/InjectApiTest.java | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 166dc485d0..1e87cac950 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -1,5 +1,8 @@ package io.openbas.rest; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import static io.openbas.database.model.ExerciseStatus.RUNNING; import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT; import static io.openbas.rest.exercise.ExerciseApi.EXERCISE_URI; @@ -27,13 +30,20 @@ import io.openbas.utils.fixtures.InjectExpectationFixture; import io.openbas.utils.mockUser.WithMockObserverUser; import io.openbas.utils.mockUser.WithMockPlannerUser; +import jakarta.annotation.Resource; import java.time.Instant; import java.util.List; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.util.ResourceUtils; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.Collections; import static io.openbas.config.SessionHelper.currentUser; @@ -57,7 +67,7 @@ class InjectApiTest extends IntegrationTest { @Autowired private ExerciseService exerciseService; @Autowired private ExerciseRepository exerciseRepository; @Autowired private ExecutionContextService executionContextService; - @Autowired + @SpyBean private Executor executor; @Autowired private ScenarioRepository scenarioRepository; @@ -69,6 +79,8 @@ class InjectApiTest extends IntegrationTest { @Autowired private InjectorContractRepository injectorContractRepository; @Autowired private UserRepository userRepository; + @Resource + private ObjectMapper objectMapper; @BeforeAll void beforeAll() { @@ -359,7 +371,6 @@ void updateInjectForSimulationTest() throws Exception { @DisplayName("Execute an email inject for exercise") @Test - @Order(11) @WithMockPlannerUser void executeEmailInjectForExerciseTest() throws Exception { // -- PREPARE -- @@ -367,6 +378,11 @@ void executeEmailInjectForExerciseTest() throws Exception { Inject inject = getInjectForEmailContract(injectorContract); inject.setUser(userRepository.findById(currentUser().getId()).orElseThrow()); inject.setExercise(EXERCISE); + ObjectNode content = objectMapper.createObjectNode(); + content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); + content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); + content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); + inject.setContent(content); Inject savedInject = this.injectRepository.save(inject); List userInjectContexts = Collections.singletonList( executionContextService.executionContext(getSavedUser(), savedInject, "Direct execution")); @@ -380,14 +396,26 @@ void executeEmailInjectForExerciseTest() throws Exception { input.setDescription(savedInject.getDescription()); input.setInjectorContract(savedInject.getInjectorContract().orElseThrow().getId()); input.setUserIds(List.of(savedInject.getId())); + input.setContent(savedInject.getContent()); + MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", + objectMapper.writeValueAsString(input).getBytes()); + + // Getting a test file + File testFile = ResourceUtils.getFile("classpath:xls-test-files/test_file_1.xlsx"); + InputStream in = new FileInputStream(testFile); + MockMultipartFile fileJson = new MockMultipartFile("file", + "my-awesome-file.xls", + "application/xlsx", + in.readAllBytes()); // -- EXECUTE -- mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") - .content(asJsonString(input)) - .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().is2xxSuccessful()); - + .file(inputJson) + .file(fileJson)) + .andExpect(status().is2xxSuccessful()) + /*.andReturn() + .getResponse() + .getContentAsString()*/; // -- ASSERT -- verify(executor).execute(injection); From 04d9e1f2256be9066f15a982cfc2bee0f8f26e17 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Fri, 18 Oct 2024 15:28:11 +0200 Subject: [PATCH 03/25] test --- openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 1e87cac950..f8a4136b1d 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -380,7 +380,7 @@ void executeEmailInjectForExerciseTest() throws Exception { inject.setExercise(EXERCISE); ObjectNode content = objectMapper.createObjectNode(); content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); - content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); + content.set("body", objectMapper.convertValue("Test body g", JsonNode.class)); content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); inject.setContent(content); Inject savedInject = this.injectRepository.save(inject); From f9ea853abbc963f31bed27807370e9ec6e9b63b5 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Mon, 21 Oct 2024 09:16:31 +0200 Subject: [PATCH 04/25] test --- .../java/io/openbas/rest/InjectApiTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index f8a4136b1d..aed91e097e 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -34,6 +34,7 @@ import java.time.Instant; import java.util.List; import org.junit.jupiter.api.*; +import org.mockito.ArgumentCaptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.http.MediaType; @@ -380,7 +381,7 @@ void executeEmailInjectForExerciseTest() throws Exception { inject.setExercise(EXERCISE); ObjectNode content = objectMapper.createObjectNode(); content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); - content.set("body", objectMapper.convertValue("Test body g", JsonNode.class)); + content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); inject.setContent(content); Inject savedInject = this.injectRepository.save(inject); @@ -409,15 +410,21 @@ void executeEmailInjectForExerciseTest() throws Exception { in.readAllBytes()); // -- EXECUTE -- - mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") + String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") .file(inputJson) .file(fileJson)) .andExpect(status().is2xxSuccessful()) - /*.andReturn() + .andReturn() .getResponse() - .getContentAsString()*/; + .getContentAsString(); // -- ASSERT -- - verify(executor).execute(injection); + assertNotNull(response); + assertEquals("ERROR", JsonPath.read(response, "$.status_name")); + ArgumentCaptor executableInjectCaptor = ArgumentCaptor.forClass(ExecutableInject.class); + verify(executor).execute(executableInjectCaptor.capture()); + + ExecutableInject capturedInjection = executableInjectCaptor.getValue(); + assertEquals(injection.getExercise(), capturedInjection.getExercise()); //-- THEN --- injectRepository.delete(savedInject); From 6edbc32716ab3ee15434d8d7a7c3811ececcb064 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Mon, 21 Oct 2024 15:59:05 +0200 Subject: [PATCH 05/25] test --- .../java/io/openbas/rest/InjectApiTest.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index aed91e097e..9f5fac3293 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -49,7 +49,10 @@ import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; +import static io.openbas.utils.fixtures.TeamFixture.getTeam; import static io.openbas.utils.fixtures.UserFixture.getSavedUser; +import static io.openbas.utils.fixtures.UserFixture.getUser; +import static org.mockito.Mockito.*; import static org.mockito.Mockito.verify; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -64,7 +67,8 @@ class InjectApiTest extends IntegrationTest { static String SCENARIO_INJECT_ID; @Autowired private MockMvc mvc; - @Autowired private ScenarioService scenarioService; + @Autowired + private ScenarioService scenarioService; @Autowired private ExerciseService exerciseService; @Autowired private ExerciseRepository exerciseRepository; @Autowired private ExecutionContextService executionContextService; @@ -80,11 +84,14 @@ class InjectApiTest extends IntegrationTest { @Autowired private InjectorContractRepository injectorContractRepository; @Autowired private UserRepository userRepository; + @Autowired + private ExerciseTeamUserRepository exerciseTeamUserRepository; @Resource private ObjectMapper objectMapper; @BeforeAll void beforeAll() { + Scenario scenario = new Scenario(); scenario.setName("Scenario name"); scenario.setFrom("test@test.com"); @@ -384,6 +391,14 @@ void executeEmailInjectForExerciseTest() throws Exception { content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); inject.setContent(content); + User user = userRepository.save(getUser()); + Team team = teamRepository.save(getTeam(user)); + inject.setTeams(List.of(team)); + ExerciseTeamUser exerciseTeamUser = new ExerciseTeamUser(); + exerciseTeamUser.setExercise(EXERCISE); + exerciseTeamUser.setTeam(team); + exerciseTeamUser.setUser(user); + exerciseTeamUserRepository.save(exerciseTeamUser); Inject savedInject = this.injectRepository.save(inject); List userInjectContexts = Collections.singletonList( executionContextService.executionContext(getSavedUser(), savedInject, "Direct execution")); @@ -396,7 +411,7 @@ void executeEmailInjectForExerciseTest() throws Exception { input.setTitle(savedInject.getTitle()); input.setDescription(savedInject.getDescription()); input.setInjectorContract(savedInject.getInjectorContract().orElseThrow().getId()); - input.setUserIds(List.of(savedInject.getId())); + input.setUserIds(List.of(savedInject.getUser().getId())); input.setContent(savedInject.getContent()); MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); @@ -419,7 +434,7 @@ void executeEmailInjectForExerciseTest() throws Exception { .getContentAsString(); // -- ASSERT -- assertNotNull(response); - assertEquals("ERROR", JsonPath.read(response, "$.status_name")); + assertEquals("SUCCESS", JsonPath.read(response, "$.status_name")); ArgumentCaptor executableInjectCaptor = ArgumentCaptor.forClass(ExecutableInject.class); verify(executor).execute(executableInjectCaptor.capture()); @@ -428,6 +443,9 @@ void executeEmailInjectForExerciseTest() throws Exception { //-- THEN --- injectRepository.delete(savedInject); + teamRepository.delete(team); + userRepository.delete(user); + exerciseTeamUserRepository.delete(exerciseTeamUser); } // -- BULK DELETE -- From 1e9363b274cc3569c0af30011593bf34646be3ea Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Mon, 21 Oct 2024 16:21:52 +0200 Subject: [PATCH 06/25] test --- openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 9f5fac3293..fc198cbfa9 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -52,7 +52,6 @@ import static io.openbas.utils.fixtures.TeamFixture.getTeam; import static io.openbas.utils.fixtures.UserFixture.getSavedUser; import static io.openbas.utils.fixtures.UserFixture.getUser; -import static org.mockito.Mockito.*; import static org.mockito.Mockito.verify; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) From 105cb157d28a76992c776fde91040da0b3ee2887 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 10:12:01 +0200 Subject: [PATCH 07/25] test --- .../java/io/openbas/rest/InjectApiTest.java | 129 ++++++++++++------ .../utils/fixtures/ExerciseFixture.java | 1 + .../src/test/resources/application.properties | 31 +---- 3 files changed, 96 insertions(+), 65 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index fc198cbfa9..2f07b6d26a 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -19,10 +19,7 @@ import io.openbas.database.model.*; import io.openbas.database.model.InjectorContract; import io.openbas.database.repository.*; -import io.openbas.execution.ExecutableInject; -import io.openbas.execution.ExecutionContext; -import io.openbas.execution.ExecutionContextService; -import io.openbas.execution.Executor; +import io.openbas.injectors.email.EmailExecutor; import io.openbas.rest.exercise.service.ExerciseService; import io.openbas.rest.inject.form.DirectInjectInput; import io.openbas.rest.inject.form.InjectInput; @@ -33,6 +30,7 @@ import jakarta.annotation.Resource; import java.time.Instant; import java.util.List; +import jakarta.servlet.ServletException; import org.junit.jupiter.api.*; import org.mockito.ArgumentCaptor; import org.springframework.beans.factory.annotation.Autowired; @@ -46,6 +44,8 @@ import java.io.FileInputStream; import java.io.InputStream; import java.util.Collections; +import java.time.Instant; +import java.util.List; import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; @@ -53,6 +53,10 @@ import static io.openbas.utils.fixtures.UserFixture.getSavedUser; import static io.openbas.utils.fixtures.UserFixture.getUser; import static org.mockito.Mockito.verify; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(PER_CLASS) @@ -68,11 +72,10 @@ class InjectApiTest extends IntegrationTest { @Autowired private MockMvc mvc; @Autowired private ScenarioService scenarioService; - @Autowired private ExerciseService exerciseService; - @Autowired private ExerciseRepository exerciseRepository; - @Autowired private ExecutionContextService executionContextService; - @SpyBean - private Executor executor; + @Autowired + private ExerciseService exerciseService; + @Autowired + private ExerciseRepository exerciseRepository; @Autowired private ScenarioRepository scenarioRepository; @Autowired private InjectRepository injectRepository; @@ -83,8 +86,6 @@ class InjectApiTest extends IntegrationTest { @Autowired private InjectorContractRepository injectorContractRepository; @Autowired private UserRepository userRepository; - @Autowired - private ExerciseTeamUserRepository exerciseTeamUserRepository; @Resource private ObjectMapper objectMapper; @@ -383,35 +384,18 @@ void executeEmailInjectForExerciseTest() throws Exception { // -- PREPARE -- InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); - inject.setUser(userRepository.findById(currentUser().getId()).orElseThrow()); - inject.setExercise(EXERCISE); + User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(inject.getTitle()); + input.setDescription(inject.getDescription()); + input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); + input.setUserIds(List.of(user.getId())); ObjectNode content = objectMapper.createObjectNode(); content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); - inject.setContent(content); - User user = userRepository.save(getUser()); - Team team = teamRepository.save(getTeam(user)); - inject.setTeams(List.of(team)); - ExerciseTeamUser exerciseTeamUser = new ExerciseTeamUser(); - exerciseTeamUser.setExercise(EXERCISE); - exerciseTeamUser.setTeam(team); - exerciseTeamUser.setUser(user); - exerciseTeamUserRepository.save(exerciseTeamUser); - Inject savedInject = this.injectRepository.save(inject); - List userInjectContexts = Collections.singletonList( - executionContextService.executionContext(getSavedUser(), savedInject, "Direct execution")); - ExecutableInject injection = new ExecutableInject( - true, true, savedInject, List.of(), savedInject.getAssets(), - savedInject.getAssetGroups(), userInjectContexts - ); + input.setContent(content); - DirectInjectInput input = new DirectInjectInput(); - input.setTitle(savedInject.getTitle()); - input.setDescription(savedInject.getDescription()); - input.setInjectorContract(savedInject.getInjectorContract().orElseThrow().getId()); - input.setUserIds(List.of(savedInject.getUser().getId())); - input.setContent(savedInject.getContent()); MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); @@ -431,20 +415,85 @@ void executeEmailInjectForExerciseTest() throws Exception { .andReturn() .getResponse() .getContentAsString(); + // -- ASSERT -- assertNotNull(response); assertEquals("SUCCESS", JsonPath.read(response, "$.status_name")); + + //-- THEN --- + userRepository.delete(user); + + } + + @DisplayName("Execute an email inject for exercise with no team") + @Test + @WithMockPlannerUser + void executeEmailInjectForExerciseWithNoTeam() throws Exception { + // -- PREPARE -- + InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + Inject inject = getInjectForEmailContract(injectorContract); + User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(inject.getTitle()); + input.setDescription(inject.getDescription()); + input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); + ObjectNode content = objectMapper.createObjectNode(); + content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); + content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); + content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); + input.setContent(content); + + MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", + objectMapper.writeValueAsString(input).getBytes()); ArgumentCaptor executableInjectCaptor = ArgumentCaptor.forClass(ExecutableInject.class); verify(executor).execute(executableInjectCaptor.capture()); - ExecutableInject capturedInjection = executableInjectCaptor.getValue(); - assertEquals(injection.getExercise(), capturedInjection.getExercise()); + // -- EXECUTE -- + String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") + .file(inputJson)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + assertNotNull(response); + assertEquals("ERROR", JsonPath.read(response, "$.status_name")); + assertEquals("Email needs at least one user", JsonPath.read(response, "$.status_traces[0].execution_message")); + + //-- THEN --- + userRepository.delete(user); + } + + @DisplayName("Execute an email inject for exercise with no content") + @Test + @WithMockPlannerUser + void executeEmailInjectForExerciseWithNoContentTest() throws Exception { + // -- PREPARE -- + InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + Inject inject = getInjectForEmailContract(injectorContract); + User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(inject.getTitle()); + input.setDescription(inject.getDescription()); + input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); + input.setUserIds(List.of(user.getId())); + + MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", + objectMapper.writeValueAsString(input).getBytes()); + + //-- ASSERT + Exception exception = assertThrows(ServletException.class, + () -> mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") + .file(inputJson))); + + String expectedMessage = "Inject is empty"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); //-- THEN --- - injectRepository.delete(savedInject); - teamRepository.delete(team); userRepository.delete(user); - exerciseTeamUserRepository.delete(exerciseTeamUser); } // -- BULK DELETE -- diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java index 4b0eeae120..29f5ca8410 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java @@ -44,4 +44,5 @@ public static Exercise createDefaultIncidentResponseExercise() { exercise.setStart(Instant.now()); return exercise; } + } diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties index e7d127e550..3f2b3b8548 100644 --- a/openbas-api/src/test/resources/application.properties +++ b/openbas-api/src/test/resources/application.properties @@ -3,21 +3,17 @@ info.app.name=OpenBAS # suppress inspection "SpringBootApplicationProperties" info.app.version=Testing - # OpenBAS configuration openbas.base-url=http://localhost:8080 openbas.admin.email=admin@openbas.io openbas.admin.password=admin openbas.admin.token=0d17ce9a-f3a8-4c6d-9721-c98dc3dc023f - # Server configuration server.servlet.context-path=/ - # rabbit mq openbas.rabbitmq.management-insecure=true openbas.rabbitmq.trust-store-password= openbas.rabbitmq.trust.store= - # Authenticators ## Local openbas.auth-local-enable=false @@ -25,33 +21,29 @@ openbas.auth-local-enable=false openbas.auth-openid-enable=false ## Kerberos openbas.auth-kerberos-enable=false - spring.datasource.url=jdbc:postgresql://localhost:5433/openbas spring.datasource.username=openbas spring.datasource.password=openbas - # Minio Properties minio.endpoint=localhost minio.port=10000 minio.bucket=openbas minio.access-key=minioadmin minio.access-secret=minioadmin - ############# # INJECTORS # ############# - # Mail config (Always available) -openbas.default-mailer=no-reply@openbas.io openbas.default-reply-to=contact@openbas.io -spring.mail.host=smtp.gmail.com -spring.mail.port=587 -spring.mail.username= -spring.mail.password= +openbas.default-mailer=no-reply@openbas.io +spring.mail.host=ssl0.ovh.net +spring.mail.port=465 +spring.mail.username=openex-dev@filigran.cloud +spring.mail.password=UBT0qxk4zxt!xaw9bzc spring.mail.properties.mail.smtp.ssl.trust=* spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.auth=true -spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.smtp.starttls.enable=false # IMAP Configuration openbas.mail.imap.enabled=false openbas.mail.imap.host=imap.mail.com @@ -65,37 +57,26 @@ openbas.mail.imap.ssl.trust=* openbas.mail.imap.ssl.enable=true openbas.mail.imap.auth=true openbas.mail.imap.starttls.enable=true - # OVH SMS config ovh.sms.enable=false - # Mastodon config mastodon.enable=false - # Airbus LADE config lade.enable=false - # Injector Http config http.enable=false - # Injector Caldera config injector.caldera.enable=false - # XLS Import openbas.xls.import.mail.enable=true openbas.xls.import.sms.enable=true - ############# # COLLECTORS # ############# - # Collectors - ## Collector user collector.users.enable=false - ## Collector MITRE ATT&CK collector.mitre-attack.enable=false - ## Collector Caldera collector.caldera.enable=false From 9dfae1796be831dd2b8009c84e2faeca024748bd Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 10:14:06 +0200 Subject: [PATCH 08/25] Revert "test" This reverts commit ce72c593dd501d86cf6431dd9b4b0f3b7f5e5f52. --- .../java/io/openbas/rest/InjectApiTest.java | 129 ++++++------------ .../utils/fixtures/ExerciseFixture.java | 8 ++ .../src/test/resources/application.properties | 31 ++++- 3 files changed, 76 insertions(+), 92 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 2f07b6d26a..fea57a88e8 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -19,7 +19,10 @@ import io.openbas.database.model.*; import io.openbas.database.model.InjectorContract; import io.openbas.database.repository.*; -import io.openbas.injectors.email.EmailExecutor; +import io.openbas.execution.ExecutableInject; +import io.openbas.execution.ExecutionContext; +import io.openbas.execution.ExecutionContextService; +import io.openbas.execution.Executor; import io.openbas.rest.exercise.service.ExerciseService; import io.openbas.rest.inject.form.DirectInjectInput; import io.openbas.rest.inject.form.InjectInput; @@ -30,12 +33,13 @@ import jakarta.annotation.Resource; import java.time.Instant; import java.util.List; -import jakarta.servlet.ServletException; +import jakarta.mail.internet.MimeMessage; import org.junit.jupiter.api.*; import org.mockito.ArgumentCaptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.http.MediaType; +import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.ResourceUtils; @@ -44,8 +48,6 @@ import java.io.FileInputStream; import java.io.InputStream; import java.util.Collections; -import java.time.Instant; -import java.util.List; import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; @@ -53,10 +55,7 @@ import static io.openbas.utils.fixtures.UserFixture.getSavedUser; import static io.openbas.utils.fixtures.UserFixture.getUser; import static org.mockito.Mockito.verify; -import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.mockito.Mockito.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(PER_CLASS) @@ -77,6 +76,10 @@ class InjectApiTest extends IntegrationTest { @Autowired private ExerciseRepository exerciseRepository; @Autowired + private ExecutionContextService executionContextService; + @SpyBean + private Executor executor; + @Autowired private ScenarioRepository scenarioRepository; @Autowired private InjectRepository injectRepository; @Autowired private DocumentRepository documentRepository; @@ -86,12 +89,13 @@ class InjectApiTest extends IntegrationTest { @Autowired private InjectorContractRepository injectorContractRepository; @Autowired private UserRepository userRepository; + @Autowired + private ExerciseTeamUserRepository exerciseTeamUserRepository; @Resource private ObjectMapper objectMapper; @BeforeAll void beforeAll() { - Scenario scenario = new Scenario(); scenario.setName("Scenario name"); scenario.setFrom("test@test.com"); @@ -384,18 +388,35 @@ void executeEmailInjectForExerciseTest() throws Exception { // -- PREPARE -- InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); - User user = userRepository.findById(currentUser().getId()).orElseThrow(); - DirectInjectInput input = new DirectInjectInput(); - input.setTitle(inject.getTitle()); - input.setDescription(inject.getDescription()); - input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); - input.setUserIds(List.of(user.getId())); + inject.setUser(userRepository.findById(currentUser().getId()).orElseThrow()); + inject.setExercise(EXERCISE); ObjectNode content = objectMapper.createObjectNode(); content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); - input.setContent(content); + inject.setContent(content); + User user = userRepository.save(getUser()); + Team team = teamRepository.save(getTeam(user)); + inject.setTeams(List.of(team)); + ExerciseTeamUser exerciseTeamUser = new ExerciseTeamUser(); + exerciseTeamUser.setExercise(EXERCISE); + exerciseTeamUser.setTeam(team); + exerciseTeamUser.setUser(user); + exerciseTeamUserRepository.save(exerciseTeamUser); + Inject savedInject = this.injectRepository.save(inject); + List userInjectContexts = Collections.singletonList( + executionContextService.executionContext(getSavedUser(), savedInject, "Direct execution")); + ExecutableInject injection = new ExecutableInject( + true, true, savedInject, List.of(), savedInject.getAssets(), + savedInject.getAssetGroups(), userInjectContexts + ); + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(savedInject.getTitle()); + input.setDescription(savedInject.getDescription()); + input.setInjectorContract(savedInject.getInjectorContract().orElseThrow().getId()); + input.setUserIds(List.of(savedInject.getUser().getId())); + input.setContent(savedInject.getContent()); MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); @@ -415,85 +436,21 @@ void executeEmailInjectForExerciseTest() throws Exception { .andReturn() .getResponse() .getContentAsString(); - // -- ASSERT -- assertNotNull(response); assertEquals("SUCCESS", JsonPath.read(response, "$.status_name")); - - //-- THEN --- - userRepository.delete(user); - - } - - @DisplayName("Execute an email inject for exercise with no team") - @Test - @WithMockPlannerUser - void executeEmailInjectForExerciseWithNoTeam() throws Exception { - // -- PREPARE -- - InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); - Inject inject = getInjectForEmailContract(injectorContract); - User user = userRepository.findById(currentUser().getId()).orElseThrow(); - DirectInjectInput input = new DirectInjectInput(); - input.setTitle(inject.getTitle()); - input.setDescription(inject.getDescription()); - input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); - ObjectNode content = objectMapper.createObjectNode(); - content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); - content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); - content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); - input.setContent(content); - - MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", - objectMapper.writeValueAsString(input).getBytes()); ArgumentCaptor executableInjectCaptor = ArgumentCaptor.forClass(ExecutableInject.class); verify(executor).execute(executableInjectCaptor.capture()); + verify(executor).execute(executableInjectCaptor.capture()); - // -- EXECUTE -- - String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") - .file(inputJson)) - .andExpect(status().is2xxSuccessful()) - .andReturn() - .getResponse() - .getContentAsString(); - - // -- ASSERT -- - assertNotNull(response); - assertEquals("ERROR", JsonPath.read(response, "$.status_name")); - assertEquals("Email needs at least one user", JsonPath.read(response, "$.status_traces[0].execution_message")); - - //-- THEN --- - userRepository.delete(user); - } - - @DisplayName("Execute an email inject for exercise with no content") - @Test - @WithMockPlannerUser - void executeEmailInjectForExerciseWithNoContentTest() throws Exception { - // -- PREPARE -- - InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); - Inject inject = getInjectForEmailContract(injectorContract); - User user = userRepository.findById(currentUser().getId()).orElseThrow(); - DirectInjectInput input = new DirectInjectInput(); - input.setTitle(inject.getTitle()); - input.setDescription(inject.getDescription()); - input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); - input.setUserIds(List.of(user.getId())); - - MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", - objectMapper.writeValueAsString(input).getBytes()); - - //-- ASSERT - Exception exception = assertThrows(ServletException.class, - () -> mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") - .file(inputJson))); - - String expectedMessage = "Inject is empty"; - String actualMessage = exception.getMessage(); - - assertTrue(actualMessage.contains(expectedMessage)); + ExecutableInject capturedInjection = executableInjectCaptor.getValue(); + assertEquals(injection.getExercise(), capturedInjection.getExercise()); //-- THEN --- + injectRepository.delete(savedInject); + teamRepository.delete(team); userRepository.delete(user); + exerciseTeamUserRepository.delete(exerciseTeamUser); } // -- BULK DELETE -- diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java index 29f5ca8410..00367656b9 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java @@ -45,4 +45,12 @@ public static Exercise createDefaultIncidentResponseExercise() { return exercise; } + /* + _ create exercise with team containing a user with email (user enabled) + _ create exercise with team containing a user with email (user not enabled) + _ create exercise without team + _ create exercise with team without user + _ create email + */ + } diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties index 3f2b3b8548..e7d127e550 100644 --- a/openbas-api/src/test/resources/application.properties +++ b/openbas-api/src/test/resources/application.properties @@ -3,17 +3,21 @@ info.app.name=OpenBAS # suppress inspection "SpringBootApplicationProperties" info.app.version=Testing + # OpenBAS configuration openbas.base-url=http://localhost:8080 openbas.admin.email=admin@openbas.io openbas.admin.password=admin openbas.admin.token=0d17ce9a-f3a8-4c6d-9721-c98dc3dc023f + # Server configuration server.servlet.context-path=/ + # rabbit mq openbas.rabbitmq.management-insecure=true openbas.rabbitmq.trust-store-password= openbas.rabbitmq.trust.store= + # Authenticators ## Local openbas.auth-local-enable=false @@ -21,29 +25,33 @@ openbas.auth-local-enable=false openbas.auth-openid-enable=false ## Kerberos openbas.auth-kerberos-enable=false + spring.datasource.url=jdbc:postgresql://localhost:5433/openbas spring.datasource.username=openbas spring.datasource.password=openbas + # Minio Properties minio.endpoint=localhost minio.port=10000 minio.bucket=openbas minio.access-key=minioadmin minio.access-secret=minioadmin + ############# # INJECTORS # ############# + # Mail config (Always available) -openbas.default-reply-to=contact@openbas.io openbas.default-mailer=no-reply@openbas.io -spring.mail.host=ssl0.ovh.net -spring.mail.port=465 -spring.mail.username=openex-dev@filigran.cloud -spring.mail.password=UBT0qxk4zxt!xaw9bzc +openbas.default-reply-to=contact@openbas.io +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +spring.mail.username= +spring.mail.password= spring.mail.properties.mail.smtp.ssl.trust=* spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.auth=true -spring.mail.properties.mail.smtp.starttls.enable=false +spring.mail.properties.mail.smtp.starttls.enable=true # IMAP Configuration openbas.mail.imap.enabled=false openbas.mail.imap.host=imap.mail.com @@ -57,26 +65,37 @@ openbas.mail.imap.ssl.trust=* openbas.mail.imap.ssl.enable=true openbas.mail.imap.auth=true openbas.mail.imap.starttls.enable=true + # OVH SMS config ovh.sms.enable=false + # Mastodon config mastodon.enable=false + # Airbus LADE config lade.enable=false + # Injector Http config http.enable=false + # Injector Caldera config injector.caldera.enable=false + # XLS Import openbas.xls.import.mail.enable=true openbas.xls.import.sms.enable=true + ############# # COLLECTORS # ############# + # Collectors + ## Collector user collector.users.enable=false + ## Collector MITRE ATT&CK collector.mitre-attack.enable=false + ## Collector Caldera collector.caldera.enable=false From 34385245e65e373a58c57f06ede337d415c49d4c Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 10:27:25 +0200 Subject: [PATCH 09/25] test --- .../java/io/openbas/rest/InjectApiTest.java | 82 +++++++++---------- .../openbas/utils/fixtures/InjectFixture.java | 9 -- 2 files changed, 40 insertions(+), 51 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index fea57a88e8..be23333648 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -20,8 +20,6 @@ import io.openbas.database.model.InjectorContract; import io.openbas.database.repository.*; import io.openbas.execution.ExecutableInject; -import io.openbas.execution.ExecutionContext; -import io.openbas.execution.ExecutionContextService; import io.openbas.execution.Executor; import io.openbas.rest.exercise.service.ExerciseService; import io.openbas.rest.inject.form.DirectInjectInput; @@ -31,15 +29,14 @@ import io.openbas.utils.mockUser.WithMockObserverUser; import io.openbas.utils.mockUser.WithMockPlannerUser; import jakarta.annotation.Resource; +import jakarta.servlet.ServletException; import java.time.Instant; import java.util.List; -import jakarta.mail.internet.MimeMessage; import org.junit.jupiter.api.*; import org.mockito.ArgumentCaptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.http.MediaType; -import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.ResourceUtils; @@ -47,15 +44,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; -import java.util.Collections; import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; -import static io.openbas.utils.fixtures.TeamFixture.getTeam; -import static io.openbas.utils.fixtures.UserFixture.getSavedUser; -import static io.openbas.utils.fixtures.UserFixture.getUser; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(PER_CLASS) @@ -75,8 +67,6 @@ class InjectApiTest extends IntegrationTest { private ExerciseService exerciseService; @Autowired private ExerciseRepository exerciseRepository; - @Autowired - private ExecutionContextService executionContextService; @SpyBean private Executor executor; @Autowired @@ -89,8 +79,6 @@ class InjectApiTest extends IntegrationTest { @Autowired private InjectorContractRepository injectorContractRepository; @Autowired private UserRepository userRepository; - @Autowired - private ExerciseTeamUserRepository exerciseTeamUserRepository; @Resource private ObjectMapper objectMapper; @@ -388,35 +376,18 @@ void executeEmailInjectForExerciseTest() throws Exception { // -- PREPARE -- InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); - inject.setUser(userRepository.findById(currentUser().getId()).orElseThrow()); - inject.setExercise(EXERCISE); + User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(inject.getTitle()); + input.setDescription(inject.getDescription()); + input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); + input.setUserIds(List.of(user.getId())); ObjectNode content = objectMapper.createObjectNode(); content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); - inject.setContent(content); - User user = userRepository.save(getUser()); - Team team = teamRepository.save(getTeam(user)); - inject.setTeams(List.of(team)); - ExerciseTeamUser exerciseTeamUser = new ExerciseTeamUser(); - exerciseTeamUser.setExercise(EXERCISE); - exerciseTeamUser.setTeam(team); - exerciseTeamUser.setUser(user); - exerciseTeamUserRepository.save(exerciseTeamUser); - Inject savedInject = this.injectRepository.save(inject); - List userInjectContexts = Collections.singletonList( - executionContextService.executionContext(getSavedUser(), savedInject, "Direct execution")); - ExecutableInject injection = new ExecutableInject( - true, true, savedInject, List.of(), savedInject.getAssets(), - savedInject.getAssetGroups(), userInjectContexts - ); + input.setContent(content); - DirectInjectInput input = new DirectInjectInput(); - input.setTitle(savedInject.getTitle()); - input.setDescription(savedInject.getDescription()); - input.setInjectorContract(savedInject.getInjectorContract().orElseThrow().getId()); - input.setUserIds(List.of(savedInject.getUser().getId())); - input.setContent(savedInject.getContent()); MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); @@ -436,6 +407,7 @@ void executeEmailInjectForExerciseTest() throws Exception { .andReturn() .getResponse() .getContentAsString(); + // -- ASSERT -- assertNotNull(response); assertEquals("SUCCESS", JsonPath.read(response, "$.status_name")); @@ -443,14 +415,40 @@ void executeEmailInjectForExerciseTest() throws Exception { verify(executor).execute(executableInjectCaptor.capture()); verify(executor).execute(executableInjectCaptor.capture()); - ExecutableInject capturedInjection = executableInjectCaptor.getValue(); - assertEquals(injection.getExercise(), capturedInjection.getExercise()); //-- THEN --- - injectRepository.delete(savedInject); - teamRepository.delete(team); userRepository.delete(user); - exerciseTeamUserRepository.delete(exerciseTeamUser); + } + + @DisplayName("Execute an email inject for exercise with no content") + @Test + @WithMockPlannerUser + void executeEmailInjectForExerciseWithNoContentTest() throws Exception { + // -- PREPARE -- + InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + Inject inject = getInjectForEmailContract(injectorContract); + User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(inject.getTitle()); + input.setDescription(inject.getDescription()); + input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); + input.setUserIds(List.of(user.getId())); + + MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", + objectMapper.writeValueAsString(input).getBytes()); + + //-- ASSERT + Exception exception = assertThrows(ServletException.class, + () -> mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") + .file(inputJson))); + + String expectedMessage = "Inject is empty"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + + //-- THEN --- + userRepository.delete(user); } // -- BULK DELETE -- diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java index 615e4040b4..83672d66b6 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java @@ -6,7 +6,6 @@ public class InjectFixture { public static final String INJECT_EMAIL_NAME = "Test email inject"; - public static final String INJECT_SMS_NAME = "Test sms inject"; public static Inject getInjectForEmailContract(InjectorContract injectorContract) { Inject inject = new Inject(); @@ -17,12 +16,4 @@ public static Inject getInjectForEmailContract(InjectorContract injectorContract return inject; } - public static Inject getInjectForSmsContract(InjectorContract injectorContract) { - Inject inject = new Inject(); - inject.setTitle(INJECT_SMS_NAME); - inject.setInjectorContract(injectorContract); - inject.setEnabled(true); - inject.setDependsDuration(0L); - return inject; - } } From 61158c8e7f2ee8f29ca76d1b5680419f167b15e5 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 10:33:18 +0200 Subject: [PATCH 10/25] test --- .../src/test/resources/application.properties | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties index e7d127e550..3f2b3b8548 100644 --- a/openbas-api/src/test/resources/application.properties +++ b/openbas-api/src/test/resources/application.properties @@ -3,21 +3,17 @@ info.app.name=OpenBAS # suppress inspection "SpringBootApplicationProperties" info.app.version=Testing - # OpenBAS configuration openbas.base-url=http://localhost:8080 openbas.admin.email=admin@openbas.io openbas.admin.password=admin openbas.admin.token=0d17ce9a-f3a8-4c6d-9721-c98dc3dc023f - # Server configuration server.servlet.context-path=/ - # rabbit mq openbas.rabbitmq.management-insecure=true openbas.rabbitmq.trust-store-password= openbas.rabbitmq.trust.store= - # Authenticators ## Local openbas.auth-local-enable=false @@ -25,33 +21,29 @@ openbas.auth-local-enable=false openbas.auth-openid-enable=false ## Kerberos openbas.auth-kerberos-enable=false - spring.datasource.url=jdbc:postgresql://localhost:5433/openbas spring.datasource.username=openbas spring.datasource.password=openbas - # Minio Properties minio.endpoint=localhost minio.port=10000 minio.bucket=openbas minio.access-key=minioadmin minio.access-secret=minioadmin - ############# # INJECTORS # ############# - # Mail config (Always available) -openbas.default-mailer=no-reply@openbas.io openbas.default-reply-to=contact@openbas.io -spring.mail.host=smtp.gmail.com -spring.mail.port=587 -spring.mail.username= -spring.mail.password= +openbas.default-mailer=no-reply@openbas.io +spring.mail.host=ssl0.ovh.net +spring.mail.port=465 +spring.mail.username=openex-dev@filigran.cloud +spring.mail.password=UBT0qxk4zxt!xaw9bzc spring.mail.properties.mail.smtp.ssl.trust=* spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.auth=true -spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.smtp.starttls.enable=false # IMAP Configuration openbas.mail.imap.enabled=false openbas.mail.imap.host=imap.mail.com @@ -65,37 +57,26 @@ openbas.mail.imap.ssl.trust=* openbas.mail.imap.ssl.enable=true openbas.mail.imap.auth=true openbas.mail.imap.starttls.enable=true - # OVH SMS config ovh.sms.enable=false - # Mastodon config mastodon.enable=false - # Airbus LADE config lade.enable=false - # Injector Http config http.enable=false - # Injector Caldera config injector.caldera.enable=false - # XLS Import openbas.xls.import.mail.enable=true openbas.xls.import.sms.enable=true - ############# # COLLECTORS # ############# - # Collectors - ## Collector user collector.users.enable=false - ## Collector MITRE ATT&CK collector.mitre-attack.enable=false - ## Collector Caldera collector.caldera.enable=false From 65f11efe67bb6660123c1bbfd38d3b1993ba859c Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 10:54:28 +0200 Subject: [PATCH 11/25] test --- openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index be23333648..513f71bb06 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -434,6 +434,11 @@ void executeEmailInjectForExerciseWithNoContentTest() throws Exception { input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); input.setUserIds(List.of(user.getId())); + ObjectNode content = objectMapper.createObjectNode(); + content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); + content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); + content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); + MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); From 6730c3683eab16bc0cdc2a2d33a340a8ea8933f4 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU <49673066+johanah29@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:38:23 +0200 Subject: [PATCH 12/25] test --- .../src/test/resources/application.properties | 82 ------------------- 1 file changed, 82 deletions(-) delete mode 100644 openbas-api/src/test/resources/application.properties diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties deleted file mode 100644 index 3f2b3b8548..0000000000 --- a/openbas-api/src/test/resources/application.properties +++ /dev/null @@ -1,82 +0,0 @@ -# Global configuration -# suppress inspection "SpringBootApplicationProperties" -info.app.name=OpenBAS -# suppress inspection "SpringBootApplicationProperties" -info.app.version=Testing -# OpenBAS configuration -openbas.base-url=http://localhost:8080 -openbas.admin.email=admin@openbas.io -openbas.admin.password=admin -openbas.admin.token=0d17ce9a-f3a8-4c6d-9721-c98dc3dc023f -# Server configuration -server.servlet.context-path=/ -# rabbit mq -openbas.rabbitmq.management-insecure=true -openbas.rabbitmq.trust-store-password= -openbas.rabbitmq.trust.store= -# Authenticators -## Local -openbas.auth-local-enable=false -## Oauth -openbas.auth-openid-enable=false -## Kerberos -openbas.auth-kerberos-enable=false -spring.datasource.url=jdbc:postgresql://localhost:5433/openbas -spring.datasource.username=openbas -spring.datasource.password=openbas -# Minio Properties -minio.endpoint=localhost -minio.port=10000 -minio.bucket=openbas -minio.access-key=minioadmin -minio.access-secret=minioadmin -############# -# INJECTORS # -############# -# Mail config (Always available) -openbas.default-reply-to=contact@openbas.io -openbas.default-mailer=no-reply@openbas.io -spring.mail.host=ssl0.ovh.net -spring.mail.port=465 -spring.mail.username=openex-dev@filigran.cloud -spring.mail.password=UBT0qxk4zxt!xaw9bzc -spring.mail.properties.mail.smtp.ssl.trust=* -spring.mail.properties.mail.smtp.ssl.enable=true -spring.mail.properties.mail.smtp.auth=true -spring.mail.properties.mail.smtp.starttls.enable=false -# IMAP Configuration -openbas.mail.imap.enabled=false -openbas.mail.imap.host=imap.mail.com -openbas.mail.imap.username= -openbas.mail.imap.password=< -openbas.mail.imap.port=993 -openbas.mail.imap.inbox=INBOX -openbas.mail.imap.sent=Sent -# Extra IMAP configuration -openbas.mail.imap.ssl.trust=* -openbas.mail.imap.ssl.enable=true -openbas.mail.imap.auth=true -openbas.mail.imap.starttls.enable=true -# OVH SMS config -ovh.sms.enable=false -# Mastodon config -mastodon.enable=false -# Airbus LADE config -lade.enable=false -# Injector Http config -http.enable=false -# Injector Caldera config -injector.caldera.enable=false -# XLS Import -openbas.xls.import.mail.enable=true -openbas.xls.import.sms.enable=true -############# -# COLLECTORS # -############# -# Collectors -## Collector user -collector.users.enable=false -## Collector MITRE ATT&CK -collector.mitre-attack.enable=false -## Collector Caldera -collector.caldera.enable=false From a70f77b7254dea4432707f0665fc1a496d8d6ac3 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 10:58:33 +0200 Subject: [PATCH 13/25] test --- openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 513f71bb06..be23333648 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -434,11 +434,6 @@ void executeEmailInjectForExerciseWithNoContentTest() throws Exception { input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); input.setUserIds(List.of(user.getId())); - ObjectNode content = objectMapper.createObjectNode(); - content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); - content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); - content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); - MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); From 992714a187b571dde7a9457f882de0fed650468b Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 11:32:16 +0200 Subject: [PATCH 14/25] test --- .../src/test/resources/application.properties | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 openbas-api/src/test/resources/application.properties diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties new file mode 100644 index 0000000000..342ac534b6 --- /dev/null +++ b/openbas-api/src/test/resources/application.properties @@ -0,0 +1,82 @@ +# Global configuration +# suppress inspection "SpringBootApplicationProperties" +info.app.name=OpenBAS +# suppress inspection "SpringBootApplicationProperties" +info.app.version=Testing +# OpenBAS configuration +openbas.base-url=http://localhost:8080 +openbas.admin.email=admin@openbas.io +openbas.admin.password=admin +openbas.admin.token=0d17ce9a-f3a8-4c6d-9721-c98dc3dc023f +# Server configuration +server.servlet.context-path=/ +# rabbit mq +openbas.rabbitmq.management-insecure=true +openbas.rabbitmq.trust-store-password= +openbas.rabbitmq.trust.store= +# Authenticators +## Local +openbas.auth-local-enable=false +## Oauth +openbas.auth-openid-enable=false +## Kerberos +openbas.auth-kerberos-enable=false +spring.datasource.url=jdbc:postgresql://localhost:5433/openbas +spring.datasource.username=openbas +spring.datasource.password=openbas +# Minio Properties +minio.endpoint=localhost +minio.port=10000 +minio.bucket=openbas +minio.access-key=minioadmin +minio.access-secret=minioadmin +############# +# INJECTORS # +############# +# Mail config (Always available) +openbas.default-mailer=no-reply@openbas.io +openbas.default-reply-to=contact@openbas.io +spring.mail.host=smtp.gmail.com +spring.mail.port=587 +spring.mail.username= +spring.mail.password= +spring.mail.properties.mail.smtp.ssl.trust=* +spring.mail.properties.mail.smtp.ssl.enable=true +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true +# IMAP Configuration +openbas.mail.imap.enabled=false +openbas.mail.imap.host=imap.mail.com +openbas.mail.imap.username= +openbas.mail.imap.password=< +openbas.mail.imap.port=993 +openbas.mail.imap.inbox=INBOX +openbas.mail.imap.sent=Sent +# Extra IMAP configuration +openbas.mail.imap.ssl.trust=* +openbas.mail.imap.ssl.enable=true +openbas.mail.imap.auth=true +openbas.mail.imap.starttls.enable=true +# OVH SMS config +ovh.sms.enable=false +# Mastodon config +mastodon.enable=false +# Airbus LADE config +lade.enable=false +# Injector Http config +http.enable=false +# Injector Caldera config +injector.caldera.enable=false +# XLS Import +openbas.xls.import.mail.enable=true +openbas.xls.import.sms.enable=true +############# +# COLLECTORS # +############# +# Collectors +## Collector user +collector.users.enable=false +## Collector MITRE ATT&CK +collector.mitre-attack.enable=false +## Collector Caldera +collector.caldera.enable=false From 8408aca9ec52ec9c92aeccaefaf17bfff651311d Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 11:57:07 +0200 Subject: [PATCH 15/25] test --- openbas-api/src/test/resources/application.properties | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties index 342ac534b6..ed8360db68 100644 --- a/openbas-api/src/test/resources/application.properties +++ b/openbas-api/src/test/resources/application.properties @@ -34,16 +34,16 @@ minio.access-secret=minioadmin # INJECTORS # ############# # Mail config (Always available) -openbas.default-mailer=no-reply@openbas.io openbas.default-reply-to=contact@openbas.io -spring.mail.host=smtp.gmail.com -spring.mail.port=587 +openbas.default-mailer=no-reply@openbas.io +spring.mail.host=ssl0.ovh.net +spring.mail.port=465 spring.mail.username= spring.mail.password= spring.mail.properties.mail.smtp.ssl.trust=* spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.auth=true -spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.smtp.starttls.enable=false # IMAP Configuration openbas.mail.imap.enabled=false openbas.mail.imap.host=imap.mail.com From 8a04a0473d30d93de1daddd1fc9e124549dae791 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 23 Oct 2024 17:22:27 +0200 Subject: [PATCH 16/25] Mock Java mail sender --- .../java/io/openbas/rest/InjectApiTest.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index be23333648..a9fbdd2f21 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -29,14 +29,21 @@ import io.openbas.utils.mockUser.WithMockObserverUser; import io.openbas.utils.mockUser.WithMockPlannerUser; import jakarta.annotation.Resource; +import jakarta.mail.internet.MimeMessage; import jakarta.servlet.ServletException; import java.time.Instant; import java.util.List; import org.junit.jupiter.api.*; import org.mockito.ArgumentCaptor; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentMatchers; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.ResourceUtils; @@ -48,9 +55,12 @@ import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(PER_CLASS) +@ExtendWith(MockitoExtension.class) class InjectApiTest extends IntegrationTest { static Exercise EXERCISE; @@ -81,6 +91,8 @@ class InjectApiTest extends IntegrationTest { private UserRepository userRepository; @Resource private ObjectMapper objectMapper; + @MockBean + private JavaMailSender javaMailSender; @BeforeAll void beforeAll() { @@ -399,6 +411,11 @@ void executeEmailInjectForExerciseTest() throws Exception { "application/xlsx", in.readAllBytes()); + // Mock the behavior of JavaMailSender + doNothing().when(javaMailSender).send(ArgumentMatchers.any(SimpleMailMessage.class)); + MimeMessage mimeMessage = mock(MimeMessage.class); + when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage); + // -- EXECUTE -- String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") .file(inputJson) @@ -420,6 +437,44 @@ void executeEmailInjectForExerciseTest() throws Exception { userRepository.delete(user); } + @DisplayName("Execute an email inject for exercise with no team") + @Test + @WithMockPlannerUser + void executeEmailInjectForExerciseWithNoTeam() throws Exception { + // -- PREPARE -- + InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + Inject inject = getInjectForEmailContract(injectorContract); + User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); + input.setTitle(inject.getTitle()); + input.setDescription(inject.getDescription()); + input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); + ObjectNode content = objectMapper.createObjectNode(); + content.set("subject", objectMapper.convertValue("Subject", JsonNode.class)); + content.set("body", objectMapper.convertValue("Test body", JsonNode.class)); + content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); + input.setContent(content); + + MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", + objectMapper.writeValueAsString(input).getBytes()); + + // -- EXECUTE -- + String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") + .file(inputJson)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + assertNotNull(response); + assertEquals("ERROR", JsonPath.read(response, "$.status_name")); + assertEquals("Email needs at least one user", JsonPath.read(response, "$.status_traces[0].execution_message")); + + //-- THEN --- + userRepository.delete(user); + } + @DisplayName("Execute an email inject for exercise with no content") @Test @WithMockPlannerUser From cac5069ab72884f99c44bdd3a5d326e0878ef743 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Mon, 28 Oct 2024 12:11:56 +0100 Subject: [PATCH 17/25] Apply spotless corrections --- .../java/io/openbas/rest/InjectApiTest.java | 134 +++++++++--------- .../utils/fixtures/ExerciseFixture.java | 14 +- .../openbas/utils/fixtures/InjectFixture.java | 1 - 3 files changed, 74 insertions(+), 75 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index a9fbdd2f21..9e18f1efa5 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -1,19 +1,23 @@ package io.openbas.rest; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; +import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.database.model.ExerciseStatus.RUNNING; import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT; import static io.openbas.rest.exercise.ExerciseApi.EXERCISE_URI; import static io.openbas.rest.inject.InjectApi.INJECT_URI; import static io.openbas.rest.scenario.ScenarioApi.SCENARIO_URI; import static io.openbas.utils.JsonUtils.asJsonString; +import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.jayway.jsonpath.JsonPath; import io.openbas.IntegrationTest; import io.openbas.database.model.*; @@ -31,16 +35,19 @@ import jakarta.annotation.Resource; import jakarta.mail.internet.MimeMessage; import jakarta.servlet.ServletException; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; import java.time.Instant; import java.util.List; import org.junit.jupiter.api.*; -import org.mockito.ArgumentCaptor; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatchers; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.http.MediaType; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @@ -48,16 +55,6 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.ResourceUtils; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; - -import static io.openbas.config.SessionHelper.currentUser; -import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.*; - - @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(PER_CLASS) @ExtendWith(MockitoExtension.class) @@ -71,28 +68,20 @@ class InjectApiTest extends IntegrationTest { static String SCENARIO_INJECT_ID; @Autowired private MockMvc mvc; - @Autowired - private ScenarioService scenarioService; - @Autowired - private ExerciseService exerciseService; - @Autowired - private ExerciseRepository exerciseRepository; - @SpyBean - private Executor executor; - @Autowired - private ScenarioRepository scenarioRepository; + @Autowired private ScenarioService scenarioService; + @Autowired private ExerciseService exerciseService; + @Autowired private ExerciseRepository exerciseRepository; + @SpyBean private Executor executor; + @Autowired private ScenarioRepository scenarioRepository; @Autowired private InjectRepository injectRepository; @Autowired private DocumentRepository documentRepository; @Autowired private CommunicationRepository communicationRepository; @Autowired private InjectExpectationRepository injectExpectationRepository; @Autowired private TeamRepository teamRepository; @Autowired private InjectorContractRepository injectorContractRepository; - @Autowired - private UserRepository userRepository; - @Resource - private ObjectMapper objectMapper; - @MockBean - private JavaMailSender javaMailSender; + @Autowired private UserRepository userRepository; + @Resource private ObjectMapper objectMapper; + @MockBean private JavaMailSender javaMailSender; @BeforeAll void beforeAll() { @@ -386,7 +375,8 @@ void updateInjectForSimulationTest() throws Exception { @WithMockPlannerUser void executeEmailInjectForExerciseTest() throws Exception { // -- PREPARE -- - InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + InjectorContract injectorContract = + this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); User user = userRepository.findById(currentUser().getId()).orElseThrow(); DirectInjectInput input = new DirectInjectInput(); @@ -400,16 +390,15 @@ void executeEmailInjectForExerciseTest() throws Exception { content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); input.setContent(content); - MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", - objectMapper.writeValueAsString(input).getBytes()); + MockMultipartFile inputJson = + new MockMultipartFile( + "input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); // Getting a test file File testFile = ResourceUtils.getFile("classpath:xls-test-files/test_file_1.xlsx"); InputStream in = new FileInputStream(testFile); - MockMultipartFile fileJson = new MockMultipartFile("file", - "my-awesome-file.xls", - "application/xlsx", - in.readAllBytes()); + MockMultipartFile fileJson = + new MockMultipartFile("file", "my-awesome-file.xls", "application/xlsx", in.readAllBytes()); // Mock the behavior of JavaMailSender doNothing().when(javaMailSender).send(ArgumentMatchers.any(SimpleMailMessage.class)); @@ -417,23 +406,25 @@ void executeEmailInjectForExerciseTest() throws Exception { when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage); // -- EXECUTE -- - String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") - .file(inputJson) - .file(fileJson)) - .andExpect(status().is2xxSuccessful()) - .andReturn() - .getResponse() - .getContentAsString(); + String response = + mvc.perform( + multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") + .file(inputJson) + .file(fileJson)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); // -- ASSERT -- assertNotNull(response); assertEquals("SUCCESS", JsonPath.read(response, "$.status_name")); - ArgumentCaptor executableInjectCaptor = ArgumentCaptor.forClass(ExecutableInject.class); + ArgumentCaptor executableInjectCaptor = + ArgumentCaptor.forClass(ExecutableInject.class); verify(executor).execute(executableInjectCaptor.capture()); verify(executor).execute(executableInjectCaptor.capture()); - - //-- THEN --- + // -- THEN --- userRepository.delete(user); } @@ -442,7 +433,8 @@ void executeEmailInjectForExerciseTest() throws Exception { @WithMockPlannerUser void executeEmailInjectForExerciseWithNoTeam() throws Exception { // -- PREPARE -- - InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + InjectorContract injectorContract = + this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); User user = userRepository.findById(currentUser().getId()).orElseThrow(); DirectInjectInput input = new DirectInjectInput(); @@ -455,23 +447,26 @@ void executeEmailInjectForExerciseWithNoTeam() throws Exception { content.set("expectationType", objectMapper.convertValue("none", JsonNode.class)); input.setContent(content); - MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", - objectMapper.writeValueAsString(input).getBytes()); + MockMultipartFile inputJson = + new MockMultipartFile( + "input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); // -- EXECUTE -- - String response = mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") - .file(inputJson)) - .andExpect(status().is2xxSuccessful()) - .andReturn() - .getResponse() - .getContentAsString(); + String response = + mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject").file(inputJson)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); // -- ASSERT -- assertNotNull(response); assertEquals("ERROR", JsonPath.read(response, "$.status_name")); - assertEquals("Email needs at least one user", JsonPath.read(response, "$.status_traces[0].execution_message")); + assertEquals( + "Email needs at least one user", + JsonPath.read(response, "$.status_traces[0].execution_message")); - //-- THEN --- + // -- THEN --- userRepository.delete(user); } @@ -480,7 +475,8 @@ void executeEmailInjectForExerciseWithNoTeam() throws Exception { @WithMockPlannerUser void executeEmailInjectForExerciseWithNoContentTest() throws Exception { // -- PREPARE -- - InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + InjectorContract injectorContract = + this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); User user = userRepository.findById(currentUser().getId()).orElseThrow(); DirectInjectInput input = new DirectInjectInput(); @@ -489,20 +485,24 @@ void executeEmailInjectForExerciseWithNoContentTest() throws Exception { input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); input.setUserIds(List.of(user.getId())); - MockMultipartFile inputJson = new MockMultipartFile("input", null, "application/json", - objectMapper.writeValueAsString(input).getBytes()); + MockMultipartFile inputJson = + new MockMultipartFile( + "input", null, "application/json", objectMapper.writeValueAsString(input).getBytes()); - //-- ASSERT - Exception exception = assertThrows(ServletException.class, - () -> mvc.perform(multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject") - .file(inputJson))); + // -- ASSERT + Exception exception = + assertThrows( + ServletException.class, + () -> + mvc.perform( + multipart(EXERCISE_URI + "/" + EXERCISE.getId() + "/inject").file(inputJson))); String expectedMessage = "Inject is empty"; String actualMessage = exception.getMessage(); assertTrue(actualMessage.contains(expectedMessage)); - //-- THEN --- + // -- THEN --- userRepository.delete(user); } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java index 00367656b9..efe88ae077 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java @@ -45,12 +45,12 @@ public static Exercise createDefaultIncidentResponseExercise() { return exercise; } - /* - _ create exercise with team containing a user with email (user enabled) - _ create exercise with team containing a user with email (user not enabled) - _ create exercise without team - _ create exercise with team without user - _ create email - */ + /* + _ create exercise with team containing a user with email (user enabled) + _ create exercise with team containing a user with email (user not enabled) + _ create exercise without team + _ create exercise with team without user + _ create email + */ } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java index 83672d66b6..a1c050739c 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java @@ -15,5 +15,4 @@ public static Inject getInjectForEmailContract(InjectorContract injectorContract inject.setDependsDuration(0L); return inject; } - } From bad94279f3c3731b345512b3b0890ee84e4fe761 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Mon, 4 Nov 2024 12:05:44 +0100 Subject: [PATCH 18/25] Sync test app properties with master --- openbas-api/src/test/resources/application.properties | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties index ed8360db68..342ac534b6 100644 --- a/openbas-api/src/test/resources/application.properties +++ b/openbas-api/src/test/resources/application.properties @@ -34,16 +34,16 @@ minio.access-secret=minioadmin # INJECTORS # ############# # Mail config (Always available) -openbas.default-reply-to=contact@openbas.io openbas.default-mailer=no-reply@openbas.io -spring.mail.host=ssl0.ovh.net -spring.mail.port=465 +openbas.default-reply-to=contact@openbas.io +spring.mail.host=smtp.gmail.com +spring.mail.port=587 spring.mail.username= spring.mail.password= spring.mail.properties.mail.smtp.ssl.trust=* spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.auth=true -spring.mail.properties.mail.smtp.starttls.enable=false +spring.mail.properties.mail.smtp.starttls.enable=true # IMAP Configuration openbas.mail.imap.enabled=false openbas.mail.imap.host=imap.mail.com From cf41e2b88ceb6bf8ae8072979e70ba6dca067c28 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Mon, 4 Nov 2024 17:29:44 +0100 Subject: [PATCH 19/25] Add assert on email subject --- .../java/io/openbas/rest/InjectApiTest.java | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 9e18f1efa5..0a80becabd 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -33,13 +33,16 @@ import io.openbas.utils.mockUser.WithMockObserverUser; import io.openbas.utils.mockUser.WithMockPlannerUser; import jakarta.annotation.Resource; +import jakarta.mail.Session; import jakarta.mail.internet.MimeMessage; import jakarta.servlet.ServletException; + import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.time.Instant; import java.util.List; + import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -67,21 +70,36 @@ class InjectApiTest extends IntegrationTest { static Team TEAM; static String SCENARIO_INJECT_ID; - @Autowired private MockMvc mvc; - @Autowired private ScenarioService scenarioService; - @Autowired private ExerciseService exerciseService; - @Autowired private ExerciseRepository exerciseRepository; - @SpyBean private Executor executor; - @Autowired private ScenarioRepository scenarioRepository; - @Autowired private InjectRepository injectRepository; - @Autowired private DocumentRepository documentRepository; - @Autowired private CommunicationRepository communicationRepository; - @Autowired private InjectExpectationRepository injectExpectationRepository; - @Autowired private TeamRepository teamRepository; - @Autowired private InjectorContractRepository injectorContractRepository; - @Autowired private UserRepository userRepository; - @Resource private ObjectMapper objectMapper; - @MockBean private JavaMailSender javaMailSender; + @Autowired + private MockMvc mvc; + @Autowired + private ScenarioService scenarioService; + @Autowired + private ExerciseService exerciseService; + @Autowired + private ExerciseRepository exerciseRepository; + @SpyBean + private Executor executor; + @Autowired + private ScenarioRepository scenarioRepository; + @Autowired + private InjectRepository injectRepository; + @Autowired + private DocumentRepository documentRepository; + @Autowired + private CommunicationRepository communicationRepository; + @Autowired + private InjectExpectationRepository injectExpectationRepository; + @Autowired + private TeamRepository teamRepository; + @Autowired + private InjectorContractRepository injectorContractRepository; + @Autowired + private UserRepository userRepository; + @Resource + private ObjectMapper objectMapper; + @MockBean + private JavaMailSender javaMailSender; @BeforeAll void beforeAll() { @@ -401,9 +419,9 @@ void executeEmailInjectForExerciseTest() throws Exception { new MockMultipartFile("file", "my-awesome-file.xls", "application/xlsx", in.readAllBytes()); // Mock the behavior of JavaMailSender + ArgumentCaptor mimeMessageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); doNothing().when(javaMailSender).send(ArgumentMatchers.any(SimpleMailMessage.class)); - MimeMessage mimeMessage = mock(MimeMessage.class); - when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage); + when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null)); // -- EXECUTE -- String response = @@ -422,7 +440,9 @@ void executeEmailInjectForExerciseTest() throws Exception { ArgumentCaptor executableInjectCaptor = ArgumentCaptor.forClass(ExecutableInject.class); verify(executor).execute(executableInjectCaptor.capture()); - verify(executor).execute(executableInjectCaptor.capture()); + + verify(javaMailSender).send(mimeMessageArgumentCaptor.capture()); + assertEquals("Subject", mimeMessageArgumentCaptor.getValue().getSubject()); // -- THEN --- userRepository.delete(user); From f9285826555455c375853c005e34470a2e9a2987 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Mon, 4 Nov 2024 17:37:04 +0100 Subject: [PATCH 20/25] Apply spotless --- .../java/io/openbas/rest/InjectApiTest.java | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 0a80becabd..c47a376988 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -36,13 +36,11 @@ import jakarta.mail.Session; import jakarta.mail.internet.MimeMessage; import jakarta.servlet.ServletException; - import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.time.Instant; import java.util.List; - import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -70,36 +68,21 @@ class InjectApiTest extends IntegrationTest { static Team TEAM; static String SCENARIO_INJECT_ID; - @Autowired - private MockMvc mvc; - @Autowired - private ScenarioService scenarioService; - @Autowired - private ExerciseService exerciseService; - @Autowired - private ExerciseRepository exerciseRepository; - @SpyBean - private Executor executor; - @Autowired - private ScenarioRepository scenarioRepository; - @Autowired - private InjectRepository injectRepository; - @Autowired - private DocumentRepository documentRepository; - @Autowired - private CommunicationRepository communicationRepository; - @Autowired - private InjectExpectationRepository injectExpectationRepository; - @Autowired - private TeamRepository teamRepository; - @Autowired - private InjectorContractRepository injectorContractRepository; - @Autowired - private UserRepository userRepository; - @Resource - private ObjectMapper objectMapper; - @MockBean - private JavaMailSender javaMailSender; + @Autowired private MockMvc mvc; + @Autowired private ScenarioService scenarioService; + @Autowired private ExerciseService exerciseService; + @Autowired private ExerciseRepository exerciseRepository; + @SpyBean private Executor executor; + @Autowired private ScenarioRepository scenarioRepository; + @Autowired private InjectRepository injectRepository; + @Autowired private DocumentRepository documentRepository; + @Autowired private CommunicationRepository communicationRepository; + @Autowired private InjectExpectationRepository injectExpectationRepository; + @Autowired private TeamRepository teamRepository; + @Autowired private InjectorContractRepository injectorContractRepository; + @Autowired private UserRepository userRepository; + @Resource private ObjectMapper objectMapper; + @MockBean private JavaMailSender javaMailSender; @BeforeAll void beforeAll() { @@ -419,7 +402,8 @@ void executeEmailInjectForExerciseTest() throws Exception { new MockMultipartFile("file", "my-awesome-file.xls", "application/xlsx", in.readAllBytes()); // Mock the behavior of JavaMailSender - ArgumentCaptor mimeMessageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class); + ArgumentCaptor mimeMessageArgumentCaptor = + ArgumentCaptor.forClass(MimeMessage.class); doNothing().when(javaMailSender).send(ArgumentMatchers.any(SimpleMailMessage.class)); when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null)); From 983718d7a865f9aa2177579e1994a4bfe03ac698 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Fri, 8 Nov 2024 12:49:14 +0100 Subject: [PATCH 21/25] resolve comments --- .../java/io/openbas/utils/fixtures/ExerciseFixture.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java index efe88ae077..3cd7de50af 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java @@ -3,6 +3,7 @@ import io.openbas.database.model.Exercise; import io.openbas.database.model.ExerciseStatus; import io.openbas.database.model.Team; + import java.time.Instant; import java.util.List; @@ -45,12 +46,4 @@ public static Exercise createDefaultIncidentResponseExercise() { return exercise; } - /* - _ create exercise with team containing a user with email (user enabled) - _ create exercise with team containing a user with email (user not enabled) - _ create exercise without team - _ create exercise with team without user - _ create email - */ - } From 72846b67bae10ee4074b16eaa3866b3ff29995a6 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU <49673066+johanah29@users.noreply.github.com> Date: Fri, 8 Nov 2024 12:55:09 +0100 Subject: [PATCH 22/25] Update application.properties --- .../src/test/resources/application.properties | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/openbas-api/src/test/resources/application.properties b/openbas-api/src/test/resources/application.properties index 342ac534b6..e7d127e550 100644 --- a/openbas-api/src/test/resources/application.properties +++ b/openbas-api/src/test/resources/application.properties @@ -3,17 +3,21 @@ info.app.name=OpenBAS # suppress inspection "SpringBootApplicationProperties" info.app.version=Testing + # OpenBAS configuration openbas.base-url=http://localhost:8080 openbas.admin.email=admin@openbas.io openbas.admin.password=admin openbas.admin.token=0d17ce9a-f3a8-4c6d-9721-c98dc3dc023f + # Server configuration server.servlet.context-path=/ + # rabbit mq openbas.rabbitmq.management-insecure=true openbas.rabbitmq.trust-store-password= openbas.rabbitmq.trust.store= + # Authenticators ## Local openbas.auth-local-enable=false @@ -21,18 +25,22 @@ openbas.auth-local-enable=false openbas.auth-openid-enable=false ## Kerberos openbas.auth-kerberos-enable=false + spring.datasource.url=jdbc:postgresql://localhost:5433/openbas spring.datasource.username=openbas spring.datasource.password=openbas + # Minio Properties minio.endpoint=localhost minio.port=10000 minio.bucket=openbas minio.access-key=minioadmin minio.access-secret=minioadmin + ############# # INJECTORS # ############# + # Mail config (Always available) openbas.default-mailer=no-reply@openbas.io openbas.default-reply-to=contact@openbas.io @@ -57,26 +65,37 @@ openbas.mail.imap.ssl.trust=* openbas.mail.imap.ssl.enable=true openbas.mail.imap.auth=true openbas.mail.imap.starttls.enable=true + # OVH SMS config ovh.sms.enable=false + # Mastodon config mastodon.enable=false + # Airbus LADE config lade.enable=false + # Injector Http config http.enable=false + # Injector Caldera config injector.caldera.enable=false + # XLS Import openbas.xls.import.mail.enable=true openbas.xls.import.sms.enable=true + ############# # COLLECTORS # ############# + # Collectors + ## Collector user collector.users.enable=false + ## Collector MITRE ATT&CK collector.mitre-attack.enable=false + ## Collector Caldera collector.caldera.enable=false From ad6f0c158be16a3905a7bdadc84a796032687a7b Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Tue, 12 Nov 2024 09:20:11 +0100 Subject: [PATCH 23/25] apply spotless --- .../test/java/io/openbas/utils/fixtures/ExerciseFixture.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java index 3cd7de50af..4b0eeae120 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseFixture.java @@ -3,7 +3,6 @@ import io.openbas.database.model.Exercise; import io.openbas.database.model.ExerciseStatus; import io.openbas.database.model.Team; - import java.time.Instant; import java.util.List; @@ -45,5 +44,4 @@ public static Exercise createDefaultIncidentResponseExercise() { exercise.setStart(Instant.now()); return exercise; } - } From 00effccd90d963634dc0880b386270cf881e2c5b Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 13 Nov 2024 12:56:21 +0100 Subject: [PATCH 24/25] delete unused user --- .../java/io/openbas/rest/InjectApiTest.java | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index c47a376988..864f8b74b6 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -36,11 +36,13 @@ import jakarta.mail.Session; import jakarta.mail.internet.MimeMessage; import jakarta.servlet.ServletException; + import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.time.Instant; import java.util.List; + import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -68,21 +70,36 @@ class InjectApiTest extends IntegrationTest { static Team TEAM; static String SCENARIO_INJECT_ID; - @Autowired private MockMvc mvc; - @Autowired private ScenarioService scenarioService; - @Autowired private ExerciseService exerciseService; - @Autowired private ExerciseRepository exerciseRepository; - @SpyBean private Executor executor; - @Autowired private ScenarioRepository scenarioRepository; - @Autowired private InjectRepository injectRepository; - @Autowired private DocumentRepository documentRepository; - @Autowired private CommunicationRepository communicationRepository; - @Autowired private InjectExpectationRepository injectExpectationRepository; - @Autowired private TeamRepository teamRepository; - @Autowired private InjectorContractRepository injectorContractRepository; - @Autowired private UserRepository userRepository; - @Resource private ObjectMapper objectMapper; - @MockBean private JavaMailSender javaMailSender; + @Autowired + private MockMvc mvc; + @Autowired + private ScenarioService scenarioService; + @Autowired + private ExerciseService exerciseService; + @Autowired + private ExerciseRepository exerciseRepository; + @SpyBean + private Executor executor; + @Autowired + private ScenarioRepository scenarioRepository; + @Autowired + private InjectRepository injectRepository; + @Autowired + private DocumentRepository documentRepository; + @Autowired + private CommunicationRepository communicationRepository; + @Autowired + private InjectExpectationRepository injectExpectationRepository; + @Autowired + private TeamRepository teamRepository; + @Autowired + private InjectorContractRepository injectorContractRepository; + @Autowired + private UserRepository userRepository; + @Resource + private ObjectMapper objectMapper; + @MockBean + private JavaMailSender javaMailSender; @BeforeAll void beforeAll() { @@ -440,7 +457,7 @@ void executeEmailInjectForExerciseWithNoTeam() throws Exception { InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); - User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); input.setTitle(inject.getTitle()); input.setDescription(inject.getDescription()); @@ -470,8 +487,6 @@ void executeEmailInjectForExerciseWithNoTeam() throws Exception { "Email needs at least one user", JsonPath.read(response, "$.status_traces[0].execution_message")); - // -- THEN --- - userRepository.delete(user); } @DisplayName("Execute an email inject for exercise with no content") @@ -482,12 +497,11 @@ void executeEmailInjectForExerciseWithNoContentTest() throws Exception { InjectorContract injectorContract = this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); Inject inject = getInjectForEmailContract(injectorContract); - User user = userRepository.findById(currentUser().getId()).orElseThrow(); + DirectInjectInput input = new DirectInjectInput(); input.setTitle(inject.getTitle()); input.setDescription(inject.getDescription()); input.setInjectorContract(inject.getInjectorContract().orElseThrow().getId()); - input.setUserIds(List.of(user.getId())); MockMultipartFile inputJson = new MockMultipartFile( @@ -506,8 +520,6 @@ void executeEmailInjectForExerciseWithNoContentTest() throws Exception { assertTrue(actualMessage.contains(expectedMessage)); - // -- THEN --- - userRepository.delete(user); } // -- BULK DELETE -- From 110548d7d16d32815063be6c03adbc7a2090a4fc Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Wed, 13 Nov 2024 14:06:36 +0100 Subject: [PATCH 25/25] apply spotless --- .../java/io/openbas/rest/InjectApiTest.java | 49 ++++++------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java index 864f8b74b6..ab335e3cd7 100644 --- a/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java @@ -36,13 +36,11 @@ import jakarta.mail.Session; import jakarta.mail.internet.MimeMessage; import jakarta.servlet.ServletException; - import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.time.Instant; import java.util.List; - import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; @@ -70,36 +68,21 @@ class InjectApiTest extends IntegrationTest { static Team TEAM; static String SCENARIO_INJECT_ID; - @Autowired - private MockMvc mvc; - @Autowired - private ScenarioService scenarioService; - @Autowired - private ExerciseService exerciseService; - @Autowired - private ExerciseRepository exerciseRepository; - @SpyBean - private Executor executor; - @Autowired - private ScenarioRepository scenarioRepository; - @Autowired - private InjectRepository injectRepository; - @Autowired - private DocumentRepository documentRepository; - @Autowired - private CommunicationRepository communicationRepository; - @Autowired - private InjectExpectationRepository injectExpectationRepository; - @Autowired - private TeamRepository teamRepository; - @Autowired - private InjectorContractRepository injectorContractRepository; - @Autowired - private UserRepository userRepository; - @Resource - private ObjectMapper objectMapper; - @MockBean - private JavaMailSender javaMailSender; + @Autowired private MockMvc mvc; + @Autowired private ScenarioService scenarioService; + @Autowired private ExerciseService exerciseService; + @Autowired private ExerciseRepository exerciseRepository; + @SpyBean private Executor executor; + @Autowired private ScenarioRepository scenarioRepository; + @Autowired private InjectRepository injectRepository; + @Autowired private DocumentRepository documentRepository; + @Autowired private CommunicationRepository communicationRepository; + @Autowired private InjectExpectationRepository injectExpectationRepository; + @Autowired private TeamRepository teamRepository; + @Autowired private InjectorContractRepository injectorContractRepository; + @Autowired private UserRepository userRepository; + @Resource private ObjectMapper objectMapper; + @MockBean private JavaMailSender javaMailSender; @BeforeAll void beforeAll() { @@ -486,7 +469,6 @@ void executeEmailInjectForExerciseWithNoTeam() throws Exception { assertEquals( "Email needs at least one user", JsonPath.read(response, "$.status_traces[0].execution_message")); - } @DisplayName("Execute an email inject for exercise with no content") @@ -519,7 +501,6 @@ void executeEmailInjectForExerciseWithNoContentTest() throws Exception { String actualMessage = exception.getMessage(); assertTrue(actualMessage.contains(expectedMessage)); - } // -- BULK DELETE --