Skip to content

Commit

Permalink
Implement /api/feedbacks web api test and validation (#1749)
Browse files Browse the repository at this point in the history
  • Loading branch information
entrotech committed Jun 19, 2024
1 parent f412d8f commit 7a19192
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
87 changes: 87 additions & 0 deletions server/__tests__/feedback.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const request = require("supertest");
const sgMail = require("@sendgrid/mail");
const {
setupServer,
teardownServer
} = require("../_jest-setup_/utils/server-setup");

let server;

beforeAll(async () => {
server = await setupServer();
});

afterAll(async () => {
await teardownServer();
});

describe("Feedback API endpoints for end user accounts", () => {
let originalSendgrid = sgMail.send;

beforeAll(async () => {
sgMail.send = jest.fn(async () => {
return { statusCode: 202 };
});
});

afterAll(async () => {
// cleanup state
sgMail.send = originalSendgrid;
});

// POST "/feedbacks" with a valid request body
it("should post feedback", async () => {
const res = await request(server)
.post("/api/feedbacks")
.send({
name: "John Darragh",
email: "[email protected]",
forwardToWebTeam: true,
comment: "This is a test comment.",
selectedProjects: [254]
});
expect(res.statusCode).toEqual(201);
});

// POST "/feedbacks" with a valid request body
it("should post feedback", async () => {
const res = await request(server)
.post("/api/feedbacks")
.send({
name: "John Darragh",
email: "[email protected]",
forwardToWebTeam: true,
comment: "This is a test comment.",
selectedProjects: [254]
});
expect(res.statusCode).toEqual(201);
});

// POST "/feedbacks" with empty comment
it("should not post feedback without comment", async () => {
const res = await request(server)
.post("/api/feedbacks")
.send({
name: "John Darragh",
email: "[email protected]",
forwardToWebTeam: true,
comment: "",
selectedProjects: [254]
});
expect(res.statusCode).toEqual(400);
});

// POST "/feedbacks" with empty name
it("should not post feedback without name", async () => {
const res = await request(server)
.post("/api/feedbacks")
.send({
name: "",
email: "[email protected]",
forwardToWebTeam: true,
comment: "This is a test comment",
selectedProjects: [254]
});
expect(res.statusCode).toEqual(400);
});
});
11 changes: 10 additions & 1 deletion server/app/controllers/feedback.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const feedbackService = require("../services/feedback.service");
const {
validate,
validationErrorMiddleware
} = require("../../middleware/validate");
const feedbackPostSchema = require("../schemas/feedback.post");

const post = async (req, res) => {
try {
Expand All @@ -11,5 +16,9 @@ const post = async (req, res) => {
};

module.exports = {
post
post: [
validate({ body: feedbackPostSchema }),
post,
validationErrorMiddleware
]
};
25 changes: 25 additions & 0 deletions server/app/schemas/feedback.post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
type: "object",
required: ["name", "email", "forwardToWebTeam", "comment"],
properties: {
email: {
type: "string",
minLength: 3,
pattern: "\\S+@\\S+"
},
name: {
type: "string",
minLength: 1
},
comment: {
type: "string",
minLength: 1
},
forwardToWebTeam: {
type: "boolean"
},
selectedProjects: {
type: "array"
}
}
};

0 comments on commit 7a19192

Please sign in to comment.