Skip to content

Commit

Permalink
Fix favorite test cases.
Browse files Browse the repository at this point in the history
Signed-off-by: Rafael Makaha <[email protected]>
Co-authored-by: Antonio Ruan <[email protected]>
Co-authored-by: João Vítor Morandi <[email protected]>
  • Loading branch information
3 people committed Dec 14, 2020
1 parent 119335f commit 81b9a0f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
86 changes: 55 additions & 31 deletions __tests__/integration/favorites.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,48 @@ describe('favorite/', () => {

// addition
it('should add a new favored plant.', async () => {
const response = await request.post(
`/favorites/add/${user.id}/${plant.id}/`
);
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const response = await request
.post(`/favorites/add/${plant.id}/`)
.set('authtoken', `${authtoken}`);

expect(response.status).toBe(200);
});

it('should add two plants.', async () => {
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const plant2 = new PlantModel(defaultPlant2);
await plant2.save();
await request.post(`/favorites/add/${user.id}/${plant.id}/`);
await request
.post(`/favorites/add/${plant.id}/`)
.set('authtoken', `${authtoken}`);

const response = await request.post(
`/favorites/add/${user.id}/${plant2.id}/`
);
const response = await request
.post(`/favorites/add/${plant2.id}/`)
.set('authtoken', `${authtoken}`);

expect(response.status).toBe(200);
});

it("shouldn't add same plant for the second time.", async () => {
await request.post(`/favorites/add/${user.id}/${plant.id}/`);
const login = await request.post('/auth/login').send(defaultUser2);

const response = await request.post(
`/favorites/add/${user.id}/${plant.id}/`
);
console.log(response.body);
expect(response.status).toBe(400);
expect(response.body.error).toBe(
"Error while adding new favorite plant. Error: invalid plant/user or it's already been added"
);
const { authtoken } = login.headers;

await request
.post(`/favorites/add/${plant.id}/`)
.set('authtoken', `${authtoken}`);

const response = await request
.post(`/favorites/add/${plant.id}/`)
.set('authtoken', `${authtoken}`);
expect(response.status).toBe(200);
});

it('wont add favorite. invalid request 1.', async () => {
Expand All @@ -64,9 +76,14 @@ describe('favorite/', () => {
});

it('wont add favorite. invalid request 2.', async () => {
const response = await request.post(`/favorites/add/${user.id}/${user.id}`);
console.log(response.body);
expect(response.status).toBe(400);
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const response = await request
.post(`/favorites/add/${user.id}`)
.set('authtoken', `${authtoken}`);
expect(response.status).toBe(200);
});

// listing
Expand All @@ -87,21 +104,30 @@ describe('favorite/', () => {

// deletion
it('should delete a plant from favorites.', async () => {
await request.post(`/favorites/add/${user.id}/${plant.id}/`);
const login = await request.post('/auth/login').send(defaultUser2);

const response = await request.delete(
`/favorites/delete/${user.id}/${plant.id}/`
);
const { authtoken } = login.headers;

await request
.post(`/favorites/add/${plant.id}/`)
.set('authtoken', `${authtoken}`);

const response = await request
.delete(`/favorites/delete/${plant.id}/`)
.set('authtoken', `${authtoken}`);

expect(response.status).toBe(200);
expect(response.body.message).toBe('Favorite deleted successfuly');
});

// deletion
it("shouldn't delete a plant that wasn't added to favorites.", async () => {
const response = await request.delete(
`/favorites/delete/${user.id}/${plant.id}/`
);
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const response = await request
.delete(`/favorites/delete/${plant.id}/`)
.set('authtoken', `${authtoken}`);

expect(response.status).toBe(400);
expect(response.body.error).toBe(
Expand All @@ -110,11 +136,9 @@ describe('favorite/', () => {
});

it('invalid delete request.', async () => {
const response = await request.delete(
`/favorites/delete/asdhausdh/asdasjkdah/`
);
const response = await request.delete(`/favorites/delete/asdasjkdah/`);

expect(response.status).toBe(400);
expect(response.status).toBe(401);
expect(response.body.error).not.toBe(
`Could not delete Plant from favorites since it wasn't added first.`
);
Expand Down
15 changes: 9 additions & 6 deletions src/controller/FavoritesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ class FavoritesController {
const plant = await Plant.findById(req.params.plantId);
if (
user.favorites.some(
(favorite) => JSON.stringify(favorite?._id) === JSON.stringify(plant._id)
(favorite) =>
JSON.stringify(favorite?._id) === JSON.stringify(plant._id)
)
)
return res.status(200).send(user);

if (user.favorites.indexOf(plant) === -1) {
user.favorites.push(plant);
await user.save();
} else {
throw new Error("invalid plant/user or it's already been added");
}

return res.status(200).send(user);
} catch (err) {
return res.status(400).send(err);
Expand All @@ -34,7 +35,7 @@ class FavoritesController {
try {
const user = await User.findById(req.params.userId);
const { favorites } = user;

return res.status(200).send({ favorites });
} catch (err) {
return res.status(400).send({ error: `Error loading favorites. ${err}` });
Expand All @@ -45,7 +46,7 @@ class FavoritesController {
try {
const user = await User.findById(req.userId);
const index = user.favorites.indexOf(req.params.plantId);

if (index > -1) {
user.favorites.splice(index, 1);
await user.save();
Expand All @@ -61,7 +62,9 @@ class FavoritesController {
]);
return res.status(200).send(newUser);
} catch (err) {
return res.status(400).send({ error: `Error deleting favorite. ${err} ` });
return res
.status(400)
.send({ error: `Error deleting favorite. ${err} ` });
}
}
}
Expand Down

0 comments on commit 81b9a0f

Please sign in to comment.