Skip to content

Commit b49d710

Browse files
committed
tests: Added e2e testing for list endpoints
1 parent 54cb5bf commit b49d710

File tree

2 files changed

+178
-1
lines changed

2 files changed

+178
-1
lines changed

api/src/interfaces/schemas/list.schema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
const Joi = require('joi');
22

33
const id = Joi.string().uuid();
4-
const name = Joi.string().min(3).max(80);
4+
const name = Joi.string()
5+
.min(3)
6+
.max(80)
7+
.pattern(/^[A-Za-z0-9 ]+$/)
8+
.messages({
9+
'string.pattern.base':
10+
'The name can only contain letters, numbers and spaces.',
11+
});
512

613
const listIdSchema = Joi.object({
714
listId: id.required(),

tests/e2e/lists.e2e.js

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
const request = require('supertest');
2+
3+
const createApp = require('../../api/app');
4+
const { models } = require('../../api/src/infrastructure/store/db/sequelize');
5+
const { upSeed, downSeed } = require('./utils/umzug');
6+
7+
describe('tests for list endpoints', () => {
8+
let accessTokenUser1 = null;
9+
let accessTokenUser2 = null;
10+
let workspace = null;
11+
let project = null;
12+
let list = null;
13+
14+
let app = null;
15+
let server = null;
16+
let api = null;
17+
18+
beforeAll(async () => {
19+
app = createApp();
20+
server = app.listen(9000);
21+
api = request(app);
22+
23+
await upSeed();
24+
25+
const userData = {
26+
email: 'user1@email.com',
27+
password: 'Admin123@',
28+
};
29+
const { body } = await api.post('/api/v1/auth/login').send(userData);
30+
accessTokenUser1 = body.accessToken;
31+
32+
workspace = await models.Workspace.findByPk(
33+
'f4bbaf96-10d4-468e-b947-40e64f473cb6',
34+
);
35+
project = await models.Project.findByPk(
36+
'8f6e2597-057c-4d84-9851-ae6d7ca9a392',
37+
);
38+
list = await models.List.findByPk('7f210809-184f-449d-8cb6-7bdca222201a');
39+
40+
const { body: authLogin } = await api
41+
.post('/api/v1/auth/login')
42+
.send({ email: 'user4@email.com', password: 'Customer123@' });
43+
accessTokenUser2 = authLogin.accessToken;
44+
});
45+
46+
describe('GET /workspaces/{workspaceId}/projects/{projectId}/lists', () => {
47+
test('It should return a list of lists with its cards', async () => {
48+
const { statusCode, body } = await api
49+
.get(`/api/v1/workspaces/${workspace.id}/projects/${project.id}/lists`)
50+
.set({ Authorization: `Bearer ${accessTokenUser1}` });
51+
52+
expect(statusCode).toEqual(200);
53+
expect(Array.isArray(body.lists)).toBeTruthy();
54+
expect(body.lists[0]).toHaveProperty('cards');
55+
expect(Array.isArray(body.lists[0].cards)).toBe(true);
56+
expect(body.lists[0].cards.length).toBeGreaterThan(0);
57+
expect(body.lists[0].projectId).toBe(project.id);
58+
59+
body.lists[0].cards.forEach((card) => {
60+
expect(card).toHaveProperty('id');
61+
expect(card).toHaveProperty('name');
62+
});
63+
});
64+
65+
test('It should return an error because the requester does not belong to the project', async () => {
66+
const { body: authLogin } = await api
67+
.post('/api/v1/auth/login')
68+
.send({ email: 'user4@email.com', password: 'Customer123@' });
69+
70+
const { statusCode } = await api
71+
.get(`/api/v1/workspaces/8bc9b526-4c33-4b88-af81-0fd6a7c05188`)
72+
.set({ Authorization: `Bearer ${authLogin.accessToken}` });
73+
74+
expect(statusCode).toEqual(403);
75+
});
76+
});
77+
78+
describe('POST /workspaces/{workspaceId}/projects/{projectId}/lists', () => {
79+
let inputBody = null;
80+
81+
beforeEach(() => {
82+
inputBody = {
83+
name: 'list 4',
84+
};
85+
});
86+
87+
test('It should return a created workspace without description', async () => {
88+
const { statusCode, body } = await api
89+
.post(`/api/v1/workspaces/${workspace.id}/projects/${project.id}/lists`)
90+
.send(inputBody)
91+
.set({ Authorization: `Bearer ${accessTokenUser1}` });
92+
93+
expect(statusCode).toEqual(201);
94+
expect(body.newList).toHaveProperty('id');
95+
expect(body.newList.name).toEqual(inputBody.name);
96+
});
97+
98+
test('It should return an error because the name is invalid', async () => {
99+
inputBody.name = 'New name 15 @!**';
100+
101+
const { statusCode } = await api
102+
.post(`/api/v1/workspaces/${workspace.id}/projects/${project.id}/lists`)
103+
.send(inputBody)
104+
.set({ Authorization: `Bearer ${accessTokenUser1}` });
105+
expect(statusCode).toEqual(400);
106+
});
107+
});
108+
109+
describe('PATCH /workspaces/{workspaceId}/projects/{projectId}/lists/{listId}', () => {
110+
let inputBody = null;
111+
112+
beforeEach(() => {
113+
inputBody = {
114+
newName: 'In progress 4',
115+
};
116+
});
117+
118+
test('It should return an updated workspace without description', async () => {
119+
const { statusCode, body } = await api
120+
.patch(
121+
`/api/v1/workspaces/${workspace.id}/projects/${project.id}/lists/${list.id}`,
122+
)
123+
.send(inputBody)
124+
.set({ Authorization: `Bearer ${accessTokenUser1}` });
125+
126+
expect(statusCode).toEqual(200);
127+
expect(body.updatedList.id).toBe(list.id);
128+
expect(body.updatedList.name).not.toEqual(list.name);
129+
});
130+
131+
test('It should return an error because the user does not have permissions to perform this action.', async () => {
132+
const { statusCode } = await api
133+
.patch(
134+
`/api/v1/workspaces/${workspace.id}/projects/${project.id}/lists/${list.id}`,
135+
)
136+
.send(inputBody)
137+
.set({ Authorization: `Bearer ${accessTokenUser2}` });
138+
139+
expect(statusCode).toEqual(403);
140+
});
141+
});
142+
143+
describe('DELETE /workspaces/{workspaceId}/projects/{projectId}/lists/{listId}', () => {
144+
test('It should return an error because the user(member role) does not have permissions to delete the workspace', async () => {
145+
const { statusCode } = await api
146+
.delete(
147+
`/api/v1/workspaces/${workspace.id}/projects/${project.id}/lists/${list.id}`,
148+
)
149+
.set({ Authorization: `Bearer ${accessTokenUser2}` });
150+
151+
expect(statusCode).toEqual(403);
152+
});
153+
154+
test('It should return an success message', async () => {
155+
const { statusCode, body } = await api
156+
.delete(
157+
`/api/v1/workspaces/${workspace.id}/projects/${project.id}/lists/${list.id}`,
158+
)
159+
.set({ Authorization: `Bearer ${accessTokenUser1}` });
160+
161+
expect(statusCode).toEqual(200);
162+
expect(body.message).toMatch(/successfully|deleted/);
163+
});
164+
});
165+
166+
afterAll(async () => {
167+
await downSeed();
168+
server.close();
169+
});
170+
});

0 commit comments

Comments
 (0)