Skip to content

Commit

Permalink
[frontend/backend] Be able to replay test (#1336)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanah29 authored Sep 9, 2024
1 parent bbf0283 commit 25ef39d
Show file tree
Hide file tree
Showing 16 changed files with 536 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.openbas.rest.inject_test_status;

import io.openbas.database.model.InjectTestStatus;
import io.openbas.database.repository.InjectTestStatusRepository;
import io.openbas.rest.helper.RestBehavior;
import io.openbas.service.InjectTestStatusService;
import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -26,9 +28,16 @@ public InjectTestStatus testInject(@PathVariable @NotBlank String injectId) {
public List<InjectTestStatus> bulkTestInjects(@RequestBody List<String> injectIds) {
return injectTestStatusService.bulkTestInjects(injectIds);
}

@GetMapping("/api/injects/test/{testId}")
public InjectTestStatus findInjectTestStatus(@PathVariable @NotBlank String testId) {
return injectTestStatusService.findInjectTestStatusById(testId);
}

@Transactional(rollbackOn = Exception.class)
@DeleteMapping("/api/injects/test/{testId}")
public void deleteInjectTest(@PathVariable String testId) {
injectTestStatusService.deleteInjectTest(testId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,8 @@ private InjectTestStatus testInject(Inject inject, User user) {
return this.injectTestStatusRepository.save(injectTestStatus);
}

public void deleteInjectTest(String testId) {
injectTestStatusRepository.deleteById(testId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package io.openbas.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.openbas.config.OpenBASOidcUser;
import io.openbas.database.model.*;
import io.openbas.database.repository.ExerciseRepository;
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.InjectorContractRepository;
import io.openbas.database.repository.UserRepository;
import io.openbas.injectors.channel.model.ChannelContent;
import io.openbas.injectors.email.model.EmailContent;
import io.openbas.utils.fixtures.PaginationFixture;
import io.openbas.utils.pagination.SearchPaginationInput;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


import java.util.Collections;
import java.util.List;

import static io.openbas.injectors.channel.ChannelContract.CHANNEL_PUBLISH;
import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class InjectTestStatusServiceTest {

@Autowired
private InjectRepository injectRepository;

@Autowired
private ExerciseRepository exerciseRepository;

@Autowired
private InjectorContractRepository injectorContractRepository;

@Autowired
private InjectTestStatusService injectTestStatusService;

@Autowired
private UserRepository userRepository;

@Resource
protected ObjectMapper mapper;

private Inject INJECT1;
private Inject INJECT2;
private Inject INJECT3;
private Exercise EXERCISE;


@BeforeAll
void beforeAll() {
Exercise exercise = new Exercise();
exercise.setName("Exercice name");
exercise.setFrom("test@test.com");
exercise.setReplyTos(List.of("test@test.com"));
EXERCISE = this.exerciseRepository.save(exercise);

Inject inject = new Inject();
inject.setTitle("test");
inject.setInjectorContract(this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setExercise(EXERCISE);
inject.setDependsDuration(0L);
EmailContent content = new EmailContent();
content.setSubject("Subject email");
content.setBody("A body");
inject.setContent(this.mapper.valueToTree(content));
INJECT1 = this.injectRepository.save(inject);

Inject inject2 = new Inject();
inject2.setTitle("test2");
inject2.setInjectorContract(this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject2.setExercise(EXERCISE);
inject2.setDependsDuration(0L);
EmailContent content2 = new EmailContent();
content2.setSubject("Subject email");
content2.setBody("A body");
inject2.setContent(this.mapper.valueToTree(content2));
INJECT2 = this.injectRepository.save(inject2);

Inject inject3 = new Inject();
inject3.setTitle("test3");
inject3.setInjectorContract(this.injectorContractRepository.findById(CHANNEL_PUBLISH).orElseThrow());
inject3.setExercise(EXERCISE);
inject3.setDependsDuration(0L);
ChannelContent content3 = new ChannelContent();
content3.setSubject("Subject email");
content3.setBody("A body");
inject3.setContent(this.mapper.valueToTree(content3));
INJECT3 = this.injectRepository.save(inject3);
}

@AfterAll
void afterAll() {
this.injectRepository.delete(INJECT1);
this.injectRepository.delete(INJECT2);
this.injectRepository.delete(INJECT3);
this.exerciseRepository.delete(EXERCISE);
}

@DisplayName("Test an email inject")
@Test
void testInject() {
// Mock the UserDetails with a custom ID
User user = this.userRepository.findByEmailIgnoreCase("admin@openbas.io").orElseThrow();
OpenBASOidcUser oidcUser = new OpenBASOidcUser(user);
Authentication auth = new UsernamePasswordAuthenticationToken(oidcUser, "password", Collections.EMPTY_LIST);
SecurityContextHolder.getContext().setAuthentication(auth);

// -- EXECUTE --
InjectTestStatus test = injectTestStatusService.testInject(INJECT1.getId());
assertNotNull(test);

// -- CLEAN --
this.injectTestStatusService.deleteInjectTest(test.getId());
}

@DisplayName("Test a channel inject")
@Test
void testNonMailInject() {
// Mock the UserDetails with a custom ID
User user = this.userRepository.findByEmailIgnoreCase("admin@openbas.io").orElseThrow();
OpenBASOidcUser oidcUser = new OpenBASOidcUser(user);
Authentication auth = new UsernamePasswordAuthenticationToken(oidcUser, "password", Collections.EMPTY_LIST);
SecurityContextHolder.getContext().setAuthentication(auth);

// -- EXECUTE --
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
injectTestStatusService.testInject(INJECT3.getId());
});

String expectedMessage = "Inject: " + INJECT3.getId() + " is not testable";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));

// -- CLEAN --
SearchPaginationInput searchPaginationInput = PaginationFixture.getDefault()
.size(1110)
.build();
Page<InjectTestStatus> tests = injectTestStatusService.findAllInjectTestsByExerciseId(EXERCISE.getId(),
searchPaginationInput);
tests.stream().forEach(test -> this.injectTestStatusService.deleteInjectTest(test.getId()));
}

@DisplayName("Test multiple injects")
@Test
void testBulkInject() {
// Mock the UserDetails with a custom ID
User user = this.userRepository.findByEmailIgnoreCase("admin@openbas.io").orElseThrow();
OpenBASOidcUser oidcUser = new OpenBASOidcUser(user);
Authentication auth = new UsernamePasswordAuthenticationToken(oidcUser, "password", Collections.EMPTY_LIST);
SecurityContextHolder.getContext().setAuthentication(auth);

// -- EXECUTE --
List<InjectTestStatus> tests = injectTestStatusService.bulkTestInjects(List.of(INJECT1.getId(), INJECT2.getId()));
assertEquals(2, tests.size());

// -- CLEAN --
tests.forEach(test -> this.injectTestStatusService.deleteInjectTest(test.getId()));
}

@DisplayName("Bulk test with non testable injects")
@Test
void bulkTestNonMailInject() {
// Mock the UserDetails with a custom ID
User user = this.userRepository.findByEmailIgnoreCase("admin@openbas.io").orElseThrow();
OpenBASOidcUser oidcUser = new OpenBASOidcUser(user);
Authentication auth = new UsernamePasswordAuthenticationToken(oidcUser, "password", Collections.EMPTY_LIST);
SecurityContextHolder.getContext().setAuthentication(auth);

// -- EXECUTE --
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
injectTestStatusService.bulkTestInjects(Collections.singletonList(INJECT3.getId()));
});

String expectedMessage = "No inject ID is testable";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));

// -- CLEAN --
SearchPaginationInput searchPaginationInput = PaginationFixture.getDefault()
.size(1110)
.build();
Page<InjectTestStatus> tests = injectTestStatusService.findAllInjectTestsByExerciseId(EXERCISE.getId(),
searchPaginationInput);
tests.stream().forEach(test -> this.injectTestStatusService.deleteInjectTest(test.getId()));
}

@DisplayName("Check the number of tests of an exercise")
@Test
void findAllInjectTestsByExerciseId() {
// Mock the UserDetails with a custom ID
User user = this.userRepository.findByEmailIgnoreCase("admin@openbas.io").orElseThrow();
OpenBASOidcUser oidcUser = new OpenBASOidcUser(user);
Authentication auth = new UsernamePasswordAuthenticationToken(oidcUser, "password", Collections.EMPTY_LIST);
SecurityContextHolder.getContext().setAuthentication(auth);

// -- PREPARE --
injectTestStatusService.bulkTestInjects(List.of(INJECT1.getId(), INJECT2.getId()));

SearchPaginationInput searchPaginationInput = PaginationFixture.getDefault()
.size(1110)
.build();

// -- EXECUTE --
Page<InjectTestStatus> tests = injectTestStatusService.findAllInjectTestsByExerciseId(EXERCISE.getId(),
searchPaginationInput);
assertEquals(2, tests.stream().toList().size());

// -- CLEAN --
tests.stream().forEach(test -> this.injectTestStatusService.deleteInjectTest(test.getId()));
}

@DisplayName("Find an inject with ID")
@Test
void findTestById() {
// Mock the UserDetails with a custom ID
User user = this.userRepository.findByEmailIgnoreCase("admin@openbas.io").orElseThrow();
OpenBASOidcUser oidcUser = new OpenBASOidcUser(user);
Authentication auth = new UsernamePasswordAuthenticationToken(oidcUser, "password", Collections.EMPTY_LIST);
SecurityContextHolder.getContext().setAuthentication(auth);

// -- PREPARE --
InjectTestStatus test = injectTestStatusService.testInject(INJECT1.getId());

// -- EXECUTE --
InjectTestStatus foundTest = injectTestStatusService.findInjectTestStatusById(test.getId());
assertNotNull(foundTest);

// -- CLEAN --
this.injectTestStatusService.deleteInjectTest(test.getId());
}

@DisplayName("Delete an inject with ID")
@Test
void deleteInjectTest() {
// Mock the UserDetails with a custom ID
User user = this.userRepository.findByEmailIgnoreCase("admin@openbas.io").orElseThrow();
OpenBASOidcUser oidcUser = new OpenBASOidcUser(user);
Authentication auth = new UsernamePasswordAuthenticationToken(oidcUser, "password", Collections.EMPTY_LIST);
SecurityContextHolder.getContext().setAuthentication(auth);

// -- PREPARE --
InjectTestStatus test = injectTestStatusService.testInject(INJECT1.getId());

// --EXECUTE --
injectTestStatusService.deleteInjectTest(test.getId());

SearchPaginationInput searchPaginationInput = PaginationFixture.getDefault()
.size(1110)
.build();
Page<InjectTestStatus> tests = injectTestStatusService.findAllInjectTestsByExerciseId(EXERCISE.getId(),
searchPaginationInput);
assertEquals(0, tests.stream().toList().size());
}

}
7 changes: 6 additions & 1 deletion openbas-front/src/actions/inject_test/inject-test-actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { simpleCall, simplePostCall } from '../../utils/Action';
import { simpleCall, simpleDelCall, simplePostCall } from '../../utils/Action';
import type { SearchPaginationInput } from '../../utils/api-types';

// eslint-disable-next-line import/prefer-default-export
Expand All @@ -16,3 +16,8 @@ export const fetchInjectTestStatus = (testId: string | undefined) => {
const uri = `/api/injects/test/${testId}`;
return simpleCall(uri);
};

export const deleteInjectTest = (testId: string | undefined) => {
const uri = `/api/injects/test/${testId}`;
return simpleDelCall(uri, testId);
};
Loading

0 comments on commit 25ef39d

Please sign in to comment.