Repository navigation
test: add tasks integration test #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
| import jwt from "jsonwebtoken" | ||
| import { makeJWTPayload } from "../../test/index.js" | ||
|
|
||
| export function makeAuthToken( | ||
| overrides?: Partial<ReturnType<typeof makeJWTPayload>> | ||
| ) { | ||
| const payload = makeJWTPayload(overrides) | ||
|
|
||
| return jwt.sign(payload, process.env.JWT_SECRET as string, { | ||
| expiresIn: "1h" | ||
| }) | ||
| } |
This file contains hidden or 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 hidden or 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,57 @@ | ||
| import { prisma } from "../../lib/prisma.js" | ||
| import bcrypt from "bcrypt" | ||
| import { makeRegisterUserData, makeTask } from "../../test/index.js" | ||
| import { SALT_ROUNDS } from "../../services/auth.service.js" | ||
| import { makeTeam } from "../../test/factories/team.js" | ||
|
|
||
| export async function seedUser( | ||
| overrides: Parameters<typeof makeRegisterUserData>[0] = {} | ||
| ) { | ||
| const { password, ...rest } = makeRegisterUserData(overrides) | ||
| return prisma.user.create({ | ||
| data: { | ||
| ...rest, | ||
| password: await bcrypt.hash(password, SALT_ROUNDS) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| export async function seedTeam(overrides: Parameters<typeof makeTeam>[0] = {}) { | ||
| const { id, ...team } = makeTeam() | ||
|
|
||
| return prisma.team.create({ | ||
| data: team | ||
| }) | ||
| } | ||
|
|
||
| export async function seedTask( | ||
| assigned_to: string, | ||
| teamId: string, | ||
| overrides: Parameters<typeof makeTask>[0] = {} | ||
| ) { | ||
| return prisma.task.create({ | ||
| data: makeTask({ assigned_to, teamId, ...overrides }) | ||
| }) | ||
| } | ||
|
|
||
| export async function seedTeamMember(user_id: string, team_id: string) { | ||
| return prisma.teamMember.create({ | ||
| data: { user_id, team_id } | ||
| }) | ||
| } | ||
|
|
||
| export async function seedUserWithTeamAndTask( | ||
| overrides: { | ||
| task?: Parameters<typeof makeTask>[0] | ||
| team?: Parameters<typeof makeTeam>[0] | ||
| user?: Parameters<typeof makeRegisterUserData>[0] | ||
| } = {} | ||
| ) { | ||
| const user = await seedUser(overrides.user) | ||
| const team = await seedTeam(overrides.team) | ||
|
|
||
| await seedTeamMember(user.id, team.id) | ||
| const task = await seedTask(user.id, team.id, overrides.task) | ||
|
|
||
| return { user, team, task } | ||
| } |
This file contains hidden or 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,261 @@ | ||
| import app from "../lib/create-server.js" | ||
| import request from "supertest" | ||
| import { makeAuthToken } from "./helpers/auth.js" | ||
| import { | ||
| seedTask, | ||
| seedTeam, | ||
| seedTeamMember, | ||
| seedUser, | ||
| seedUserWithTeamAndTask | ||
| } from "./helpers/seed.js" | ||
| import { makeRegisterTask, makeUpdatedTask } from "../test/index.js" | ||
|
|
||
| const PATH_TASKS = "/tasks" | ||
|
|
||
| describe("Integration: Tasks", () => { | ||
| it("should return 401 when user is not authenticated", async () => { | ||
| const { body } = await request(app).get(PATH_TASKS).expect(401) | ||
| expect(body.message).toBe("Token not provided") | ||
| }) | ||
|
|
||
| it("should return 401 when token is invalid", async () => { | ||
| const { body } = await request(app) | ||
| .get(PATH_TASKS) | ||
| .expect(401) | ||
| .auth("token-invalid", { | ||
| type: "bearer" | ||
| }) | ||
| expect(body.message).toBe("Token invalid") | ||
| }) | ||
|
|
||
| describe("when task does not exist", () => { | ||
| it.each(["put", "delete"] as const)( | ||
| "[%s] should return 400 when task not found", | ||
| async method => { | ||
| const user = await seedUser({ role: "ADMIN" }) | ||
| const token = makeAuthToken({ | ||
| userId: user.id, | ||
| email: user.email, | ||
| role: user.role | ||
| }) | ||
|
|
||
| const req = request(app) | ||
| [method](`${PATH_TASKS}/no-existing-task-id`) | ||
| .auth(token, { type: "bearer" }) | ||
|
|
||
| if (method === "put") { | ||
| req.send(makeUpdatedTask()) | ||
| } | ||
|
|
||
| const { body } = await req.expect(400) | ||
|
|
||
| expect(body.message).toBe("Task not found") | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| describe("[GET] /tasks", () => { | ||
| it("should return 403 when user role not allowed", async () => { | ||
| const token = makeAuthToken({ | ||
| role: "MEMBER" | ||
| }) | ||
|
|
||
| const { body } = await request(app) | ||
| .get(PATH_TASKS) | ||
| .auth(token, { | ||
| type: "bearer" | ||
| }) | ||
| .expect(403) | ||
|
|
||
| expect(body.message).toBe("Access denied") | ||
| }) | ||
|
|
||
| it("should return 400 when team not found", async () => { | ||
| const token = makeAuthToken({ | ||
| role: "MEMBER" | ||
| }) | ||
|
|
||
| const { body } = await request(app) | ||
| .get(`${PATH_TASKS}/no-existing-team-id`) | ||
| .auth(token, { | ||
| type: "bearer" | ||
| }) | ||
| .expect(400) | ||
|
|
||
| expect(body.message).toBe("Team not found") | ||
| }) | ||
|
|
||
| it("should return all tasks assigned to the user", async () => { | ||
| const { user, task, team } = await seedUserWithTeamAndTask() | ||
|
|
||
| const token = makeAuthToken({ | ||
| userId: user.id, | ||
| email: user.email, | ||
| role: user.role | ||
| }) | ||
|
|
||
| const { body } = await request(app) | ||
| .get(`${PATH_TASKS}/${team.id}`) | ||
| .auth(token, { type: "bearer" }) | ||
| .expect(200) | ||
|
|
||
| expect(body.tasks).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ id: task.id, title: task.title }) | ||
| ]) | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe("[POST] /tasks", () => { | ||
| it("should return 403 when user role not allowed", async () => { | ||
| const team = await seedTeam() | ||
| const user = await seedUser({ | ||
| role: "MEMBER" | ||
| }) | ||
|
|
||
| const token = makeAuthToken({ | ||
| userId: user.id, | ||
| email: user.email, | ||
| role: user.role | ||
| }) | ||
|
|
||
| const payload = makeRegisterTask() | ||
| const { body } = await request(app) | ||
| .post(`${PATH_TASKS}/${team.id}/${user.id}`) | ||
| .auth(token, { type: "bearer" }) | ||
| .send(payload) | ||
| .expect(403) | ||
|
|
||
| expect(body.message).toBe("Access denied") | ||
| }) | ||
|
|
||
| it("should return 201 with task created", async () => { | ||
| const team = await seedTeam() | ||
| const user = await seedUser({ | ||
| role: "ADMIN" | ||
| }) | ||
|
|
||
| const token = makeAuthToken({ | ||
| userId: user.id, | ||
| email: user.email, | ||
| role: user.role | ||
| }) | ||
|
|
||
| const payload = makeRegisterTask() | ||
| const { body } = await request(app) | ||
| .post(`${PATH_TASKS}/${team.id}/${user.id}`) | ||
| .auth(token, { type: "bearer" }) | ||
| .send(payload) | ||
| .expect(201) | ||
|
|
||
| expect(body.task).toEqual( | ||
| expect.objectContaining({ | ||
| title: payload.title, | ||
| description: payload.description, | ||
| status: payload.status, | ||
| priority: payload.priority, | ||
| assigned_to: user.id | ||
| }) | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe("[PUT] /tasks", () => { | ||
| it("should not allow a MEMBER to update a task assigned to another user", async () => { | ||
| const taskOwner = await seedUser({ role: "MEMBER" }) | ||
| const team = await seedTeam() | ||
|
|
||
| await seedTeamMember(taskOwner.id, team.id) | ||
| const task = await seedTask(taskOwner.id, team.id, { | ||
| status: "PENDING", | ||
| priority: "MEDIUM" | ||
| }) | ||
|
|
||
| const unauthorizedMember = await seedUser({ | ||
| role: "MEMBER", | ||
| email: "unauthorized-user@gmail.com" | ||
| }) | ||
| await seedTeamMember(unauthorizedMember.id, team.id) | ||
|
|
||
| const token = makeAuthToken({ | ||
| userId: unauthorizedMember.id, | ||
| email: unauthorizedMember.email, | ||
| role: unauthorizedMember.role | ||
| }) | ||
|
|
||
| const payload = makeUpdatedTask({ | ||
| status: "IN_PROGRESS", | ||
| priority: "HIGH" | ||
| }) | ||
|
|
||
| const { body } = await request(app) | ||
| .put(`${PATH_TASKS}/${task.id}`) | ||
| .auth(token, { type: "bearer" }) | ||
| .send(payload) | ||
| .expect(403) | ||
|
|
||
| expect(body.message).toBe( | ||
| "You do not have permission to update this task" | ||
| ) | ||
| }) | ||
|
|
||
| it("should update a task when the user is the assignee", async () => { | ||
| const { user, task } = await seedUserWithTeamAndTask({ | ||
| task: { | ||
| status: "PENDING", | ||
| priority: "MEDIUM" | ||
| }, | ||
| user: { | ||
| role: "MEMBER" | ||
| } | ||
| }) | ||
|
|
||
| const token = makeAuthToken({ | ||
| userId: user.id, | ||
| email: user.email, | ||
| role: user.role | ||
| }) | ||
|
|
||
| const payload = makeUpdatedTask({ | ||
| status: "IN_PROGRESS", | ||
| priority: "HIGH" | ||
| }) | ||
|
|
||
| const { body } = await request(app) | ||
| .put(`${PATH_TASKS}/${task.id}`) | ||
| .auth(token, { type: "bearer" }) | ||
| .send(payload) | ||
| .expect(200) | ||
|
|
||
| expect(body.task).toEqual( | ||
| expect.objectContaining({ | ||
| status: "IN_PROGRESS", | ||
| priority: "HIGH" | ||
| }) | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe("[DELETE] /tasks", () => { | ||
| it("should return 204 when task is deleted successfully", async () => { | ||
| const user = await seedUser({ | ||
| role: "ADMIN" | ||
| }) | ||
|
|
||
| const team = await seedTeam() | ||
| const task = await seedTask(user.id, team.id) | ||
|
|
||
| const token = makeAuthToken({ | ||
| userId: user.id, | ||
| email: user.email, | ||
| role: user.role | ||
| }) | ||
|
|
||
| await request(app) | ||
| .delete(`${PATH_TASKS}/${task.id}`) | ||
| .auth(token, { type: "bearer" }) | ||
| .expect(204) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
order matter to prevent prisma errors!