Skip to content

Commit

Permalink
test: add tests for the rest of the flow api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tethik committed Oct 17, 2024
1 parent d612ff2 commit 72fafac
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 3 deletions.
79 changes: 79 additions & 0 deletions api/src/resources/gram/v1/flows/delete.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { DataAccessLayer } from "@gram/core/dist/data/dal.js";
import { _deleteAllTheThings } from "@gram/core/dist/data/utils.js";
import { randomUUID } from "crypto";
import request from "supertest";
import { createTestApp } from "../../../../test-util/app.js";
import { createSampleModel } from "../../../../test-util/model.js";
import { sampleUserToken } from "../../../../test-util/sampleTokens.js";
import { sampleAttributes } from "./post.spec.js";

const token = await sampleUserToken();

describe("Flow.delete", () => {
let app: any;
let dal: DataAccessLayer;

let modelId: string;
let dataFlowId: string;

beforeAll(async () => {
({ app, dal } = await createTestApp());
});

beforeEach(async () => {
await _deleteAllTheThings(dal.pool);
modelId = await createSampleModel(dal);
dataFlowId = randomUUID();
});

afterAll(async () => {
await dal.pool.end();
});

it("should return 401 on un-authenticated request", async () => {
const postres = await request(app)
.post(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token)
.send({
originComponentId: randomUUID(),
summary: "Some summary",
attributes: sampleAttributes(),
});

expect(postres.status).toBe(200);
const flowId = postres.body.flow.id;

const res = await request(app)
.delete(`/api/v1/flows/${flowId}`).send();

expect(res.status).toBe(401);
});

it("should return 200", async () => {
const postres = await request(app)
.post(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token)
.send({
originComponentId: randomUUID(),
summary: "Some summary",
attributes: sampleAttributes(),
});

const flowId = postres.body.flow.id;

const res = await request(app)
.delete(`/api/v1/flows/${flowId}`)
.set("Authorization", token).send();

expect(res.status).toBe(200);
});

it("should return 404 with invalid flow id", async () => {
const flowId = randomUUID();
const res = await request(app)
.delete(`/api/v1/flows/${flowId}`)
.set("Authorization", token).send();

expect(res.status).toBe(404);
});
});
2 changes: 1 addition & 1 deletion api/src/resources/gram/v1/flows/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export function deleteFlow(dal: DataAccessLayer) {

await dal.flowService.deleteFlow(id);

res.status(204).json();
res.status(200).json();
};
}
78 changes: 78 additions & 0 deletions api/src/resources/gram/v1/flows/get.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { DataAccessLayer } from "@gram/core/dist/data/dal.js";
import { _deleteAllTheThings } from "@gram/core/dist/data/utils.js";
import { randomUUID } from "crypto";
import request from "supertest";
import { createTestApp } from "../../../../test-util/app.js";
import { createSampleModel } from "../../../../test-util/model.js";
import { sampleUserToken } from "../../../../test-util/sampleTokens.js";
import { sampleAttributes } from "./post.spec.js";

const token = await sampleUserToken();

describe("Flow.get", () => {
let app: any;
let dal: DataAccessLayer;

let modelId: string;
let dataFlowId: string;

beforeAll(async () => {
({ app, dal } = await createTestApp());
});

beforeEach(async () => {
await _deleteAllTheThings(dal.pool);
modelId = await createSampleModel(dal);
dataFlowId = randomUUID();
});

afterAll(async () => {
await dal.pool.end();
});

it("should return 401 on un-authenticated request", async () => {
const res = await request(app)
.get(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`).send();
expect(res.status).toBe(401);
});

it("should return 200", async () => {
const res = await request(app)
.get(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token)
.send();

// console.log("res.body", JSON.stringify(res.body));

expect(res.status).toBe(200);
expect(res.body.flows).toEqual([]);

const postres = await request(app)
.post(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token)
.send({
originComponentId: randomUUID(),
summary: "Some summary",
attributes: sampleAttributes(),
});

const getRes = await request(app)
.get(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token)
.send();

expect(getRes.status).toBe(200);
expect(getRes.body.flows.length).toBe(1);
expect(getRes.body.flows[0].summary).toBe("Some summary");
});

it("should return 404 with invalid model id", async () => {
const modelId = randomUUID();
const dataFlowId = randomUUID();
const res = await request(app)
.get(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token).send();

expect(res.status).toBe(404);
});
});
97 changes: 97 additions & 0 deletions api/src/resources/gram/v1/flows/patch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { DataAccessLayer } from "@gram/core/dist/data/dal.js";
import { _deleteAllTheThings } from "@gram/core/dist/data/utils.js";
import { randomUUID } from "crypto";
import request from "supertest";
import { createTestApp } from "../../../../test-util/app.js";
import { createSampleModel } from "../../../../test-util/model.js";
import { sampleUserToken } from "../../../../test-util/sampleTokens.js";
import { sampleAttributes } from "./post.spec.js";

const token = await sampleUserToken();

describe("Flow.patch", () => {
let app: any;
let dal: DataAccessLayer;

let modelId: string;
let dataFlowId: string;

beforeAll(async () => {
({ app, dal } = await createTestApp());
});

beforeEach(async () => {
await _deleteAllTheThings(dal.pool);
modelId = await createSampleModel(dal);
dataFlowId = randomUUID();
});

afterAll(async () => {
await dal.pool.end();
});

it("should return 401 on un-authenticated request", async () => {
const postres = await request(app)
.post(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token)
.send({
originComponentId: randomUUID(),
summary: "Some summary",
attributes: sampleAttributes(),
});

expect(postres.status).toBe(200);
const flowId = postres.body.flow.id;

const res = await request(app)
.patch(`/api/v1/flows/${flowId}`)
.send({
originComponentId: randomUUID(),
summary: "new summary",
attributes: sampleAttributes(),
});

expect(res.status).toBe(401);
});

it("should return 200", async () => {
const postres = await request(app)
.post(`/api/v1/flows/model/${modelId}/dataflow/${dataFlowId}`)
.set("Authorization", token)
.send({
originComponentId: randomUUID(),
summary: "Some summary",
attributes: sampleAttributes(),
});

const flowId = postres.body.flow.id;

const res = await request(app)
.patch(`/api/v1/flows/${flowId}`)
.set("Authorization", token)
.send({
originComponentId: randomUUID(),
summary: "new summary",
attributes: sampleAttributes(),
});

// console.log("res.body", JSON.stringify(res.body));

expect(res.status).toBe(200);
});

it("should return 404 with invalid flow id", async () => {
const flowId = Math.floor(Math.random() * 100000000);
const res = await request(app)
.patch(`/api/v1/flows/${flowId}`)
.set("Authorization", token)
.send({
originComponentId: randomUUID(),
summary: "new summary",
attributes: sampleAttributes(),
});

console.log("res.body", JSON.stringify(res.body));
expect(res.status).toBe(404);
});
});
3 changes: 1 addition & 2 deletions api/src/resources/gram/v1/flows/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { _deleteAllTheThings } from "@gram/core/dist/data/utils.js";

const token = await sampleUserToken();

function sampleAttributes() {
export function sampleAttributes() {
const attributes: any = {};
config.attributes.flow.forEach((attr) => {
if (attr.type === "text") {
Expand Down Expand Up @@ -70,7 +70,6 @@ describe("Flow.post", () => {
expect(res.status).toBe(200);
});

// For now this is okay
it("should return 404 with invalid model id", async () => {
const modelId = randomUUID();
const dataFlowId = randomUUID();
Expand Down

0 comments on commit 72fafac

Please sign in to comment.