generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement /api/feedbacks web api test and validation (#1749)
- Loading branch information
Showing
3 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
}; |