Skip to content

Commit

Permalink
(chore) Wire up saves for endpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AHarmlessPyro committed Aug 18, 2022
1 parent 441d2b5 commit 61e6d90
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
26 changes: 26 additions & 0 deletions backend/src/api/tests/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Request, Response } from "express";
import ApiResponseHandler from "api-response-handler";
import { runTest } from "~/services/testing/runTests";
import { AppDataSource } from "~/data-source";
import { ApiEndpointTest } from "~/models";

export const runTestHandler = async (
req: Request,
Expand All @@ -10,3 +12,27 @@ export const runTestHandler = async (
runTest(req.body.test);
await ApiResponseHandler.success(res, null);
};

export const saveTest = async (req: Request, res: Response): Promise<void> => {
const {
test: { uuid, name, tags, requests },
endpointUuid,
} = req.body;
let resp = await AppDataSource.getRepository(ApiEndpointTest)
.createQueryBuilder()
.insert()
.into(ApiEndpointTest)
.values({
uuid: uuid,
name,
tags,
requests,
apiEndpoint: {
uuid: endpointUuid,
},
})
.orUpdate(["name", "tags", "requests"], ["uuid"])
.execute();

await ApiResponseHandler.success(res, resp);
};
2 changes: 2 additions & 0 deletions backend/src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Alert,
Session,
Connections,
ApiEndpointTest,
} from "models";

export const AppDataSource: DataSource = new DataSource({
Expand All @@ -22,6 +23,7 @@ export const AppDataSource: DataSource = new DataSource({
Alert,
Session,
Connections,
ApiEndpointTest,
],
migrations: [],
logging: false,
Expand Down
5 changes: 3 additions & 2 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
aws_os_choices,
setup_connection,
} from "./api/setup";
import { runTestHandler } from "./api/tests";
import { runTestHandler, saveTest } from "./api/tests";
import {
get_connection_for_uuid,
get_ssh_key_for_connection_uuid,
Expand Down Expand Up @@ -102,9 +102,10 @@ app.get(
get_ssh_key_for_connection_uuid
);
app.post("/api/v1/update_connection", update_connection);

app.post("/api/v1/test/run", runTestHandler);

app.post("/api/v1/tests/save", saveTest);

const main = async () => {
try {
const datasource = await AppDataSource.initialize();
Expand Down
2 changes: 2 additions & 0 deletions backend/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OpenApiSpec } from "models/openapi-spec";
import { Alert } from "models/alert";
import { Session } from "models/sessions";
import { Connections } from "./connections";
import { ApiEndpointTest } from "./api-endpoint-test";

export {
ApiEndpoint,
Expand All @@ -14,4 +15,5 @@ export {
Alert,
Session,
Connections,
ApiEndpointTest,
};
1 change: 1 addition & 0 deletions common/src/testing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface Request {
}

export interface Test {
uuid: string;
name: string;
tags: string[];
requests: Request[];
Expand Down
17 changes: 16 additions & 1 deletion frontend/src/components/TestEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import RequestList from "./requestsList";
import RequestEditor from "./requestEditor";
import { makeNewEmptyRequest, sendRequest } from "./requestUtils";
import { runTest } from "~/api/tests";
import axios from "axios";
import { getAPIURL } from "~/constants";

interface TestEditorProps {
endpoint: ApiEndpointDetailed;
Expand Down Expand Up @@ -141,6 +143,17 @@ const TestEditor: React.FC<TestEditorProps> = React.memo(
});
};

const onSaveRequest = () => {
axios
.post(`${getAPIURL()}/tests/save`, {
test: state.test,
endpointUuid: endpoint.uuid,
})
.then((v) => console.log(v))
.catch((e) => console.log(e))
.finally(() => console.log("Request done"));
};

const onRunClick = () => {
runTest(test);
};
Expand Down Expand Up @@ -170,7 +183,9 @@ const TestEditor: React.FC<TestEditorProps> = React.memo(
<Button colorScheme="blue" onClick={onRunClick}>
Run
</Button>
<Button colorScheme="blue">Save</Button>
<Button colorScheme="blue" onClick={onSaveRequest}>
Save
</Button>
</HStack>
</HStack>
</VStack>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/endpoint/[endpointUUID]/test/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TestEditor from "components/TestEditor";
import { getEndpoint } from "api/endpoints";
import { ApiEndpointDetailed } from "@common/types";
import { makeNewRequest } from "~/components/TestEditor/requestUtils";
import { v4 as uuidv4 } from "uuid";

const NewTest = ({ endpoint }) => {
const parsedEndpoint = superjson.parse(
Expand All @@ -23,6 +24,7 @@ const NewTest = ({ endpoint }) => {
<TestEditor
endpoint={parsedEndpoint}
initTest={{
uuid: uuidv4(),
name: "Untitled Test",
tags: [],
requests: [makeNewRequest(parsedEndpoint)],
Expand Down

0 comments on commit 61e6d90

Please sign in to comment.