Skip to content

Commit

Permalink
[backend/frontend] Multiple fixes for testing and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelHassine committed Apr 29, 2024
1 parent 36bd9cf commit 26df09d
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void injectsToRunTest() {
Inject inject = new Inject();
inject.setTitle("Test inject");
inject.setType(TYPE);
inject.setInjectorContract(injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setInjectorContract(this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setEnabled(true);
inject.setExercise(exerciseSaved);
inject.setTeams(List.of(team));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.openbas.database.repository.ExerciseRepository;
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.InjectorContractRepository;
import io.openbas.injectors.email.EmailContract;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,6 +24,7 @@ public class InjectCrudTest {

@Autowired
private ExerciseRepository exerciseRepository;

@Autowired
private InjectorContractRepository injectorContractRepository;

Expand All @@ -40,7 +40,7 @@ void createInjectSuccess() {
Inject inject = new Inject();
inject.setTitle("test");
inject.setType(TYPE);
inject.setInjectorContract(injectorContractRepository.findById(EmailContract.EMAIL_DEFAULT).orElseThrow());
inject.setInjectorContract(this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setExercise(exerciseCreated);
inject.setDependsDuration(0L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.List;

import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.helper.StreamHelper.fromIterable;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -53,7 +54,7 @@ void process() throws Exception {
content.setExpectations(List.of(expectation));
Inject inject = new Inject();
inject.setType(EmailContract.TYPE);
inject.setInjectorContract(injectorContractRepository.findById(EmailContract.EMAIL_DEFAULT).orElseThrow());
inject.setInjectorContract(this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setContent(this.mapper.valueToTree(content));
Iterable<User> users = this.userRepository.findAll();
List<ExecutionContext> userInjectContexts = fromIterable(users).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.openbas.database.model.Scenario;
import io.openbas.database.repository.ChallengeRepository;
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.InjectorContractRepository;
import io.openbas.database.repository.ScenarioRepository;
import io.openbas.injectors.challenge.model.ChallengeContent;
import io.openbas.service.ScenarioService;
Expand Down Expand Up @@ -47,6 +48,8 @@ public class ChallengeApiTest {
private InjectRepository injectRepository;
@Autowired
private ChallengeRepository challengeRepository;
@Autowired
private InjectorContractRepository injectorContractRepository;
@Resource
private ObjectMapper objectMapper;

Expand Down Expand Up @@ -84,7 +87,7 @@ void retrieveChallengesVariableForScenarioTest() throws Exception {
Inject inject = new Inject();
inject.setTitle("Test inject");
inject.setType(TYPE);
inject.setContract(CHALLENGE_PUBLISH);
inject.setInjectorContract(this.injectorContractRepository.findById(CHALLENGE_PUBLISH).orElseThrow());
inject.setContent(this.objectMapper.valueToTree(content));
inject.setDependsDuration(0L);
inject.setScenario(scenario);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void updateInjectForScenarioTest() throws Exception {
String injectTitle = "A new title";
input.setTitle(injectTitle);
input.setType(inject.getType());
input.setContract(inject.getContract());
input.setInjectorContract(inject.getInjectorContract().getId());
input.setDependsDuration(inject.getDependsDuration());

// -- EXECUTE --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.List;

import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.utils.fixtures.ArticleFixture.ARTICLE_NAME;
import static io.openbas.utils.fixtures.ArticleFixture.getArticle;
import static io.openbas.utils.fixtures.DocumentFixture.getDocumentJpeg;
Expand Down Expand Up @@ -196,7 +197,7 @@ void scenarioToExerciseTest() {
}});

// Inject
Inject inject = getInjectForEmailContract();
Inject inject = getInjectForEmailContract(this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setTeams(new ArrayList<>() {{
add(teamSaved);
add(contextualTeamSaved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void beforeAll() {
Inject inject = new Inject();
inject.setTitle("test");
inject.setType(TYPE);
inject.setInjectorContract(injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setInjectorContract(this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setExercise(exerciseCreated);
inject.setDependsDuration(0L);
Inject injectCreated = this.injectRepository.save(inject);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package io.openbas.utils.fixtures;

import io.openbas.database.model.Inject;
import io.openbas.database.model.InjectorContract;

import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.injectors.email.EmailContract.TYPE;

public class InjectFixture {

public static final String INJECT_EMAIL_NAME = "Test email inject";

public static Inject getInjectForEmailContract() {
Inject inject = new Inject();
inject.setTitle(INJECT_EMAIL_NAME);
inject.setType(TYPE);
inject.setEnabled(true);
inject.setDependsDuration(0L);
return inject;
}
public static final String INJECT_EMAIL_NAME = "Test email inject";

public static Inject getInjectForEmailContract(InjectorContract injectorContract) {
Inject inject = new Inject();
inject.setTitle(INJECT_EMAIL_NAME);
inject.setType(TYPE);
inject.setInjectorContract(injectorContract);
inject.setEnabled(true);
inject.setDependsDuration(0L);
return inject;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FunctionComponent, useContext } from 'react';
import { Button } from '@mui/material';
import InjectDefinition from './InjectDefinition';
import { PermissionsContext } from '..';
import { PermissionsContext } from '../Context';
import type { Inject, Tag } from '../../../../utils/api-types';
import { useHelper } from '../../../../store';
import type { InjectHelper } from '../../../../actions/injects/inject-helper';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { InjectContext, PermissionsContext } from '../Context';
import type { Inject, InjectStatus, InjectStatusExecution, Tag } from '../../../../utils/api-types';
import { tryInject } from '../../../../actions/Inject';
import { useAppDispatch } from '../../../../utils/hooks';
import type { Contract } from '../../../../actions/contract/contract';

interface Props {
inject: InjectStore;
Expand Down
7 changes: 0 additions & 7 deletions openbas-front/src/admin/components/common/injects/Injects.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import { useHelper } from '../../../../store';
import ItemBoolean from '../../../../components/ItemBoolean';
import { exportData } from '../../../../utils/Environment';
import Loader from '../../../../components/Loader';
import useDataLoader from '../../../../utils/ServerSideEvent';
import { useAppDispatch } from '../../../../utils/hooks';
import { InjectContext, PermissionsContext } from '../Context';
import CreateInject from './CreateInject';
import UpdateInject from './UpdateInject';
Expand Down Expand Up @@ -159,7 +157,6 @@ const Injects = ({
}) => {
// Standard hooks
const classes = useStyles();
const dispatch = useAppDispatch();
const { t, tPick } = useFormatter();

const [selectedInjectId, setSelectedInjectId] = useState(null);
Expand All @@ -174,9 +171,6 @@ const Injects = ({
searchColumns,
);
// Fetching data
useDataLoader(() => {
dispatch(fetchInjectorContracts());
});
const {
tagsMap,
selectedInject,
Expand Down Expand Up @@ -313,7 +307,6 @@ const Injects = ({
const injectContract = inject.inject_injector_contract.injector_contract_content_parsed;
const injectorContractName = tPick(injectContract?.label);
const duration = splitDuration(inject.inject_depends_duration || 0);
console.log('injectContract', injectContract);
const isDisabled = !injectContract?.config.expose;
const isNoTeam = !(injectContract?.fields.filter((f) => f.key === 'teams').length > 0);
let injectStatus = inject.inject_enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import InjectStatus from '../../common/injects/InjectStatus';
import InjectStatusDetails from '../../common/injects/InjectStatusDetails';
import { resolveUserName } from '../../../../utils/String';
import DryrunProgress from './DryrunProgress';
import { fetchInjectorContracts } from '../../../../actions/InjectorContracts';

const useStyles = makeStyles((theme) => ({
parameters: {
Expand Down Expand Up @@ -327,7 +326,7 @@ const Dryrun = () => {
<ListItemSecondaryAction> &nbsp; </ListItemSecondaryAction>
</ListItem>
{filtering.filterAndSort(dryinjects).map((dryinject) => {
const injectContract = injectorContractsMap[dryinject.dryinject_inject.inject_injector_contract];
const injectContract = dryinject.inject_injector_contract.injector_contract_content_parsed;
const injectorContractName = tPick(injectContract?.label);
return (
<ListItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import QuickInject from './QuickInject';
import { storeHelper } from '../../../../actions/Schema';
import { fetchExercises } from '../../../../actions/Exercise';
import { fetchTags } from '../../../../actions/Tag';
import { fetchInjectorContracts } from '../../../../actions/InjectorContracts';

const styles = (theme) => ({
createButton: {
Expand Down Expand Up @@ -98,7 +97,6 @@ export default R.compose(
connect(select, {
fetchExercises,
fetchTags,
fetchInjectorContracts,
}),
inject18n,
withTheme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import Chart from 'react-apexcharts';
import React, { FunctionComponent } from 'react';
import { useTheme } from '@mui/styles';
import * as R from 'ramda';
import { horizontalBarsChartOptions } from '../../../../utils/Charts';
import Empty from '../../../../components/Empty';
import type { ExerciseStore } from '../../../../actions/exercises/Exercise';
import { horizontalBarsChartOptions } from '../../../../utils/Charts';
import { useFormatter } from '../../../../components/i18n';
import { useAppDispatch } from '../../../../utils/hooks';
import type { Theme } from '../../../../components/Theme';
Expand All @@ -13,22 +12,8 @@ import type { InjectHelper } from '../../../../actions/injects/inject-helper';
import useDataLoader from '../../../../utils/ServerSideEvent';
import { fetchInjects } from '../../../../actions/Inject';
import { fetchExerciseInjectExpectations } from '../../../../actions/Exercise';
import type { InjectExpectationStore } from '../../../../actions/injects/Inject';
import type { Inject } from '../../../../utils/api-types';
import { fetchInjectorContracts } from '../../../../actions/InjectorContracts';
import type { InjectorContractHelper } from '../../../../actions/injector_contracts/injector-contract-helper';
import { horizontalBarsChartOptions } from '../../../../../utils/Charts';
import Empty from '../../../../../components/Empty';
import type { ExerciseStore } from '../../../../../actions/exercises/Exercise';
import { useFormatter } from '../../../../../components/i18n';
import { useAppDispatch } from '../../../../../utils/hooks';
import type { Theme } from '../../../../../components/Theme';
import { useHelper } from '../../../../../store';
import type { InjectHelper } from '../../../../../actions/injects/inject-helper';
import useDataLoader from '../../../../../utils/ServerSideEvent';
import { fetchInjects } from '../../../../../actions/Inject';
import { fetchExerciseInjectExpectations } from '../../../../../actions/Exercise';
import type { InjectExpectationStore, InjectStore } from '../../../../../actions/injects/Inject';
import type { InjectExpectationStore, InjectStore } from '../../../../actions/injects/Inject';
import type { ExerciseStore } from '../../../../actions/exercises/Exercise';

interface Props {
exerciseId: ExerciseStore['exercise_id'];
Expand Down
Loading

0 comments on commit 26df09d

Please sign in to comment.