-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Felix221123/develop
Welcome email upon sign up
- Loading branch information
Showing
44 changed files
with
2,888 additions
and
312 deletions.
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
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,108 @@ | ||
import request from 'supertest'; | ||
import mongoose, { Document } from 'mongoose'; | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import { app } from '../../src/server'; | ||
import UserBoardModel from '../../src/Models/UserModel'; | ||
import cookieParser from 'cookie-parser'; | ||
|
||
|
||
|
||
|
||
let mongoServer: MongoMemoryServer; | ||
|
||
beforeAll(async () => { | ||
mongoServer = await MongoMemoryServer.create(); | ||
const uri = mongoServer.getUri(); | ||
await mongoose.connect(uri); | ||
|
||
app.use(cookieParser()); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
await mongoServer.stop(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await UserBoardModel.deleteMany({}); | ||
}); | ||
|
||
test('Should create a new board for the user', async () => { | ||
// Step 1: Create a mock user in the database | ||
const user = await UserBoardModel.create({ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
emailAddress: 'john@example.com', | ||
password: 'hashedpassword', | ||
boards: [], | ||
}) as Document; | ||
|
||
// Step 2: Send the request to create a board | ||
const response = await request(app) | ||
.post('/api/user/board/createboard') | ||
.send({ | ||
userID: (user._id as mongoose.Types.ObjectId).toString(), // Ensure we convert the _id to a string | ||
name: 'New Project', | ||
columns: [ | ||
{ name: 'To Do', tasks: [] }, | ||
{ name: 'In Progress', tasks: [] }, | ||
{ name: 'Done', tasks: [] } | ||
], | ||
}); | ||
|
||
// Step 3: Assert the response | ||
expect(response.status).toBe(201); | ||
expect(response.body.user).toHaveProperty('boards'); | ||
expect(response.body.user.boards).toHaveLength(1); | ||
expect(response.body.user.boards[0]).toHaveProperty('name', 'New Project'); | ||
expect(response.body.user.boards[0]).toHaveProperty('columns'); | ||
// Step 4: Validate the columns without checking the generated _id field | ||
const receivedColumns = response.body.user.boards[0].columns.map((column: any) => ({ | ||
name: column.name, | ||
tasks: column.tasks | ||
})); | ||
|
||
expect(receivedColumns).toEqual([ | ||
{ name: 'To Do', tasks: [] }, | ||
{ name: 'In Progress', tasks: [] }, | ||
{ name: 'Done', tasks: [] } | ||
]); | ||
|
||
// Step 4: Verify that the board was added to the database | ||
const updatedUser = await UserBoardModel.findById(user._id) as any; | ||
expect(updatedUser?.boards).toHaveLength(1); | ||
expect(updatedUser?.boards[0].name).toBe('New Project'); | ||
|
||
}); | ||
|
||
|
||
test('Should return 400 for invalid input (missing userID or name)', async () => { | ||
// Step 1: Send a request with missing parameters | ||
const response = await request(app) | ||
.post('/api/user/board/createboard') | ||
.send({ | ||
name: 'New Project', | ||
columns: ['To Do', 'In Progress', 'Done'], | ||
}); | ||
|
||
// Step 2: Assert the response | ||
expect(response.status).toBe(400); | ||
expect(response.body).toHaveProperty('message', 'Invalid input'); | ||
}); | ||
|
||
|
||
test('Should return 404 if user is not found', async () => { | ||
// Step 1: Send a request with an invalid userID | ||
const response = await request(app) | ||
.post('/api/user/board/createboard') | ||
.send({ | ||
// Use an invalid userID to test the 404 case | ||
userID: new mongoose.Types.ObjectId().toString(), | ||
name: 'New Project', | ||
columns: ['To Do', 'In Progress', 'Done'], | ||
}); | ||
|
||
// Step 2: Assert the response | ||
expect(response.status).toBe(404); | ||
expect(response.body).toHaveProperty('message', 'User not found'); | ||
}); |
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,106 @@ | ||
import request from 'supertest'; | ||
import mongoose from 'mongoose'; | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import { app } from '../../src/server'; | ||
import UserBoardModel from '../../src/Models/UserModel'; | ||
import cookieParser from 'cookie-parser'; | ||
|
||
let mongoServer: MongoMemoryServer; | ||
|
||
beforeAll(async () => { | ||
mongoServer = await MongoMemoryServer.create(); | ||
const uri = mongoServer.getUri(); | ||
await mongoose.connect(uri); | ||
|
||
app.use(cookieParser()); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
await mongoServer.stop(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await UserBoardModel.deleteMany({}); | ||
}); | ||
|
||
|
||
|
||
test('Should create a new task in the specified column', async () => { | ||
// Step 1: Create a mock user with a board and column in the database | ||
const user = await UserBoardModel.create({ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
emailAddress: 'john@example.com', | ||
password: 'hashedpassword', | ||
boards: [ | ||
{ | ||
name: 'Project Alpha', | ||
columns: [ | ||
{ name: 'To Do', tasks: [] }, | ||
{ name: 'In Progress', tasks: [] }, | ||
{ name: 'Done', tasks: [] }, | ||
], | ||
}, | ||
], | ||
}) as any; | ||
|
||
// Step 2: Find the created board and column | ||
const boardID = user.boards[0]._id.toString(); | ||
const columnID = user.boards[0].columns[0]._id.toString(); | ||
|
||
// Step 3: Send the request to create a task in the "To Do" column | ||
const response = await request(app) | ||
.post('/api/user/board/createtask') | ||
.send({ | ||
userID: (user._id as mongoose.Types.ObjectId).toString(), // Ensure we convert the _id to a string | ||
boardID: boardID, | ||
columnID: columnID, | ||
taskTitle: 'New Task', | ||
description: 'Description of the task', | ||
subtasks: [ | ||
{ title: 'Subtask 1' }, | ||
{ title: 'Subtask 2' }, | ||
], | ||
}); | ||
|
||
// Step 4: Assert the response, ignoring the _id fields | ||
expect(response.status).toBe(201); | ||
expect(response.body.user).toHaveProperty('boards'); | ||
expect(response.body.user.boards).toHaveLength(1); | ||
|
||
const updatedBoard = response.body.user.boards[0]; | ||
expect(updatedBoard.columns).toHaveLength(3); | ||
|
||
// Omit the _id fields when comparing columns and tasks | ||
const toDoColumn = updatedBoard.columns.find((col: any) => col.name === 'To Do'); | ||
const receivedTasks = toDoColumn.tasks.map((task: any) => ({ | ||
title: task.title, | ||
description: task.description, | ||
subtasks: task.subtasks.map((subtask: any) => ({ | ||
title: subtask.title, | ||
isCompleted: subtask.isCompleted | ||
})) | ||
})); | ||
|
||
expect(receivedTasks).toEqual([ | ||
{ | ||
title: 'New Task', | ||
description: 'Description of the task', | ||
subtasks: [ | ||
{ title: 'Subtask 1', isCompleted: false }, | ||
{ title: 'Subtask 2', isCompleted: false }, | ||
] | ||
} | ||
]); | ||
|
||
// Step 5: Verify that the task was added to the database | ||
const updatedUser = await UserBoardModel.findById(user._id) as any; | ||
const updatedColumn = updatedUser?.boards[0].columns.id(columnID); | ||
expect(updatedColumn?.tasks).toHaveLength(1); | ||
|
||
const createdTask = updatedColumn?.tasks[0]; | ||
expect(createdTask.title).toBe('New Task'); | ||
expect(createdTask.description).toBe('Description of the task'); | ||
expect(createdTask.subtasks).toHaveLength(2); | ||
}); |
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,116 @@ | ||
import request from 'supertest'; | ||
import mongoose, { Document } from 'mongoose'; | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import { app } from '../../src/server'; | ||
import UserBoardModel from '../../src/Models/UserModel'; | ||
import cookieParser from 'cookie-parser'; | ||
|
||
let mongoServer: MongoMemoryServer; | ||
|
||
beforeAll(async () => { | ||
mongoServer = await MongoMemoryServer.create(); | ||
const uri = mongoServer.getUri(); | ||
await mongoose.connect(uri); | ||
|
||
app.use(cookieParser()); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
await mongoServer.stop(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await UserBoardModel.deleteMany({}); | ||
}); | ||
|
||
|
||
|
||
|
||
test('Should delete the specified board from the user', async () => { | ||
// Step 1: Create a mock user with a board | ||
const user = await UserBoardModel.create({ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
emailAddress: 'john@example.com', | ||
password: 'hashedpassword', | ||
boards: [ | ||
{ name: 'Project Alpha', columns: [] }, | ||
{ name: 'Project Beta', columns: [] }, | ||
], | ||
}) as any; | ||
|
||
// Step 2: Find the created board | ||
const boardID = user.boards[0]._id.toString(); // Delete the first board | ||
|
||
// Step 3: Send the request to delete the board | ||
const response = await request(app) | ||
.delete('/api/user/board/deleteboard') | ||
.send({ | ||
userID: (user._id as mongoose.Types.ObjectId).toString(), // Ensure we convert the _id to a string | ||
boardID: boardID, | ||
}); | ||
|
||
// Step 4: Assert the response | ||
expect(response.status).toBe(200); | ||
expect(response.body).toHaveProperty('message', 'Board deleted successfully'); | ||
expect(response.body.user).toHaveProperty('boards'); | ||
expect(response.body.user.boards).toHaveLength(1); | ||
expect(response.body.user.boards[0].name).toBe('Project Beta'); | ||
|
||
// Step 5: Verify that the board was deleted from the database | ||
const updatedUser = await UserBoardModel.findById(user._id) as any; | ||
expect(updatedUser?.boards).toHaveLength(1); | ||
expect(updatedUser?.boards[0].name).toBe('Project Beta'); | ||
}); | ||
|
||
|
||
test('Should return 404 if the board is not found', async () => { | ||
// Step 1: Create a mock user without any boards | ||
const user = await UserBoardModel.create({ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
emailAddress: 'john@example.com', | ||
password: 'hashedpassword', | ||
boards: [], | ||
}) as Document; | ||
|
||
// Step 2: Send a delete request with a valid user ID but nonexistent board ID | ||
const response = await request(app) | ||
.delete('/api/user/board/deleteboard') | ||
.send({ | ||
userID: (user._id as mongoose.Types.ObjectId).toString(), // Ensure we convert the _id to a string | ||
boardID: 'nonexistentBoardID', | ||
}); | ||
|
||
// Step 3: Assert the response | ||
expect(response.status).toBe(404); | ||
expect(response.body).toHaveProperty('message', 'Board not found'); | ||
}); | ||
|
||
|
||
|
||
test('Should return 400 if input is invalid', async () => { | ||
// Step 1: Create a mock user | ||
const user = await UserBoardModel.create({ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
emailAddress: 'john@example.com', | ||
password: 'hashedpassword', | ||
boards: [], | ||
}) as Document; | ||
console.log(user); | ||
|
||
|
||
// Step 2: Send a delete request with missing userID and boardID | ||
const response = await request(app) | ||
.delete('/api/user/board/deleteboard') | ||
.send({ | ||
userID: null, | ||
boardID: null, | ||
}); | ||
|
||
// Step 3: Assert the response | ||
expect(response.status).toBe(400); | ||
expect(response.body).toHaveProperty('message', 'Invalid input'); | ||
}); |
Oops, something went wrong.