-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Frontend|Backend]Be able to test emails and sms related injects (#1295)
- Loading branch information
Showing
24 changed files
with
1,453 additions
and
644 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
openbas-api/src/main/java/io/openbas/migration/V3_31__Add_Injects_tests_statuses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.openbas.migration; | ||
|
||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.Connection; | ||
import java.sql.Statement; | ||
|
||
@Component | ||
public class V3_31__Add_Injects_tests_statuses extends BaseJavaMigration { | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
Connection connection = context.getConnection(); | ||
Statement select = connection.createStatement(); | ||
// Create table | ||
select.execute(""" | ||
CREATE TABLE injects_tests_statuses ( | ||
status_id varchar(255) NOT NULL CONSTRAINT inject_test_status_pkey PRIMARY KEY, | ||
status_name VARCHAR(255) NOT NULL, | ||
status_executions text, | ||
tracking_sent_date timestamp, | ||
tracking_ack_date timestamp, | ||
tracking_end_date timestamp, | ||
tracking_total_execution_time bigint, | ||
tracking_total_count int, | ||
tracking_total_error int, | ||
tracking_total_success int, | ||
status_inject VARCHAR(255) NOT NULL CONSTRAINT inject_test_status_inject_id_fkey REFERENCES injects(inject_id) ON DELETE SET NULL, | ||
status_created_at timestamp not null default now(), | ||
status_updated_at timestamp not null default now() | ||
); | ||
CREATE INDEX idx_inject_test_inject ON injects_tests_statuses(status_inject); | ||
"""); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
openbas-api/src/main/java/io/openbas/rest/inject_test_status/InjectTestStatusApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.openbas.rest.inject_test_status; | ||
|
||
import io.openbas.database.model.InjectTestStatus; | ||
import io.openbas.rest.helper.RestBehavior; | ||
import io.openbas.service.InjectTestStatusService; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@PreAuthorize("isAdmin()") | ||
@RequiredArgsConstructor | ||
public class InjectTestStatusApi extends RestBehavior { | ||
|
||
private final InjectTestStatusService injectTestStatusService; | ||
|
||
@GetMapping("/api/injects/{injectId}/test") | ||
public InjectTestStatus testInject(@PathVariable @NotBlank String injectId) { | ||
return injectTestStatusService.testInject(injectId); | ||
} | ||
|
||
@GetMapping("/api/exercise/{exerciseId}/injects/test") | ||
public List<InjectTestStatus> findAllExerciseInjectTests(@PathVariable @NotBlank String exerciseId) { | ||
return injectTestStatusService.findAllInjectTestsByExerciseId(exerciseId); | ||
} | ||
|
||
@GetMapping("/api/scenario/{scenarioId}/injects/test") | ||
public List<InjectTestStatus> findAllScenarioInjectTests(@PathVariable @NotBlank String scenarioId) { | ||
return injectTestStatusService.findAllInjectTestsByScenarioId(scenarioId); | ||
} | ||
|
||
@GetMapping("/api/injects/test/{testId}") | ||
public InjectTestStatus findInjectTestStatus(@PathVariable @NotBlank String testId) { | ||
return injectTestStatusService.findInjectTestStatusById(testId); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
openbas-api/src/main/java/io/openbas/service/InjectTestStatusService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.openbas.service; | ||
|
||
import io.openbas.database.model.*; | ||
import io.openbas.database.repository.InjectRepository; | ||
import io.openbas.database.repository.InjectTestStatusRepository; | ||
import io.openbas.database.repository.UserRepository; | ||
import io.openbas.execution.ExecutableInject; | ||
import io.openbas.execution.ExecutionContext; | ||
import io.openbas.execution.ExecutionContextService; | ||
import io.openbas.execution.Injector; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.java.Log; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static io.openbas.config.SessionHelper.currentUser; | ||
|
||
@Service | ||
@Log | ||
@RequiredArgsConstructor | ||
public class InjectTestStatusService { | ||
|
||
private ApplicationContext context; | ||
private final UserRepository userRepository; | ||
private final InjectRepository injectRepository; | ||
private final ExecutionContextService executionContextService; | ||
private final InjectTestStatusRepository injectTestStatusRepository; | ||
|
||
@Autowired | ||
public void setContext(ApplicationContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Transactional | ||
public InjectTestStatus testInject(String injectId) { | ||
Inject inject = injectRepository.findById(injectId).orElseThrow(); | ||
User user = this.userRepository.findById(currentUser().getId()).orElseThrow(); | ||
List<ExecutionContext> userInjectContexts = List.of( | ||
this.executionContextService.executionContext(user, inject, "Direct test") | ||
); | ||
Injector executor = context.getBean( | ||
inject.getInjectorContract().map(injectorContract -> injectorContract.getInjector().getType()).orElseThrow(), | ||
io.openbas.execution.Injector.class); | ||
ExecutableInject injection = new ExecutableInject(false, true, inject, List.of(), inject.getAssets(), | ||
inject.getAssetGroups(), userInjectContexts); | ||
Execution execution = executor.executeInjection(injection); | ||
|
||
//Save inject test status | ||
Optional<InjectTestStatus> injectTestStatus = this.injectTestStatusRepository.findByInject(inject); | ||
InjectTestStatus injectTestStatusToSave = InjectTestStatus.fromExecutionTest(execution); | ||
injectTestStatus.ifPresent(testStatus -> { | ||
injectTestStatusToSave.setId(testStatus.getId()); | ||
injectTestStatusToSave.setTestCreationDate(testStatus.getTestCreationDate()); | ||
}); | ||
injectTestStatusToSave.setInject(inject); | ||
this.injectTestStatusRepository.save(injectTestStatusToSave); | ||
|
||
return injectTestStatusToSave; | ||
} | ||
|
||
public List<InjectTestStatus> findAllInjectTestsByExerciseId(String exerciseId) { | ||
return injectTestStatusRepository.findAllExerciseInjectTests(exerciseId); | ||
} | ||
|
||
public List<InjectTestStatus> findAllInjectTestsByScenarioId(String scenarioId) { | ||
return injectTestStatusRepository.findAllScenarioInjectTests(scenarioId); | ||
} | ||
|
||
public InjectTestStatus findInjectTestStatusById(String testId) { | ||
return injectTestStatusRepository.findById(testId).orElseThrow(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
openbas-front/src/actions/inject_test/inject-test-actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { simpleCall } from '../../utils/Action'; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const searchExerciseInjectTests = (exerciseId: string) => { | ||
const uri = `/api/exercise/${exerciseId}/injects/test`; | ||
return simpleCall(uri); | ||
}; | ||
|
||
export const searchScenarioInjectTests = (scenarioId: string) => { | ||
const uri = `/api/scenario/${scenarioId}/injects/test`; | ||
return simpleCall(uri); | ||
}; | ||
|
||
export const fetchInjectTestStatus = (testId: string | undefined) => { | ||
const uri = `/api/injects/test/${testId}`; | ||
return simpleCall(uri); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.