Skip to content

Commit

Permalink
(feature) Add testing product (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay288 authored Aug 18, 2022
1 parent 66ea164 commit 441d2b5
Show file tree
Hide file tree
Showing 35 changed files with 2,054 additions and 221 deletions.
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@aws-sdk/client-ec2": "^3.142.0",
"@aws-sdk/client-pricing": "^3.142.0",
"@types/newman": "^5.3.0",
"@types/async-retry": "^1.4.4",
"async-retry": "^1.3.3",
"aws-sdk": "^2.1189.0",
Expand All @@ -29,6 +30,7 @@
"express-session": "^1.17.3",
"js-yaml": "^4.1.0",
"multer": "^1.4.5-lts.1",
"newman": "^5.3.2",
"node-schedule": "^2.1.0",
"node-ssh": "^13.0.0",
"openapi-request-validator": "^12.0.0",
Expand Down
12 changes: 12 additions & 0 deletions backend/src/api/tests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request, Response } from "express";
import ApiResponseHandler from "api-response-handler";
import { runTest } from "~/services/testing/runTests";

export const runTestHandler = async (
req: Request,
res: Response
): Promise<void> => {
console.log(req.body);
runTest(req.body.test);
await ApiResponseHandler.success(res, null);
};
3 changes: 3 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
aws_os_choices,
setup_connection,
} from "./api/setup";
import { runTestHandler } from "./api/tests";
import {
get_connection_for_uuid,
get_ssh_key_for_connection_uuid,
Expand Down Expand Up @@ -102,6 +103,8 @@ app.get(
);
app.post("/api/v1/update_connection", update_connection);

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

const main = async () => {
try {
const datasource = await AppDataSource.initialize();
Expand Down
28 changes: 28 additions & 0 deletions backend/src/models/api-endpoint-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
BaseEntity,
Column,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from "typeorm";
import { ApiEndpoint } from "models/api-endpoint";

@Entity()
export class ApiEndpointTest extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
uuid: string;

@Column({ nullable: false })
name: string;

@Column({ type: "jsonb", nullable: true })
tags: string[];

@Column({ type: "jsonb", nullable: true })
requests: Request[];

@ManyToOne(() => ApiEndpoint)
@JoinColumn()
apiEndpoint: ApiEndpoint;
}
1 change: 1 addition & 0 deletions backend/src/services/get-endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class GetEndpointsService {
return {
...endpoint,
traces: [...traces],
tests: [],
firstDetected: firstDetected?.createdAt,
lastActive: lastActive?.createdAt,
};
Expand Down
61 changes: 61 additions & 0 deletions backend/src/services/testing/runTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import newman from "newman";
import {
CollectionDefinition,
RequestDefinition,
RequestBodyDefinition,
} from "postman-collection";
import { RequestBodyType } from "../../../../common/src/testing/enums";
import { Request } from "../../../../common/src/testing/types";
import { Test } from "../../../../common/src/testing/types";

const requestToItem = (e: Request, i: number) => {
const event = e.tests.trim()
? [
{
listen: "test",
script: {
type: "text/javascript",
exec: e.tests.split("\n"),
},
},
]
: [];
let body: RequestBodyDefinition = { mode: "raw" };
if (e.body.type == RequestBodyType.JSON) {
body.raw = e.body.data as string;
}
let url: string = e.url;
if (e.params.length > 0) {
url = `${url}?${e.params.map((p) => `${p.key}=${p.value}`).join("&")}`;
}
let item = {
name: `Request ${i}`,
event: event,
request: {
url: e.url,
method: e.method,
header: e.headers,
body: body,
} as RequestDefinition,
};
return item;
};

export const runTest = (e: Test) => {
const items = e.requests.map(requestToItem);
const collection: CollectionDefinition = {
info: {
name: "Sample Postman Collection",
},
item: items,
};
const res = newman.run(
{
collection,
},
(a, b) => {
console.log(JSON.stringify(a, null, 4));
console.log(JSON.stringify(b, null, 4));
}
);
};
Loading

0 comments on commit 441d2b5

Please sign in to comment.