Skip to content

Commit

Permalink
Merge pull request #140 from fga-eps-mds/i138_bug_broken_tests
Browse files Browse the repository at this point in the history
Fix broken tests
  • Loading branch information
joaovitorml authored and rafaelmakaha committed Dec 14, 2020
2 parents 119335f + c8b242b commit 1e2e3ac
Show file tree
Hide file tree
Showing 19 changed files with 478 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/productionDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Test & publish code coverage
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{secrets.CC_REPORTER_ID}}
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
with:
coverageCommand: npm run test
build:
Expand Down
Binary file added __tests__/assets/plant.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions __tests__/integration/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,15 @@ describe('Auth/User', () => {
const response = await request.get(`/auth/user/${dummyId}`);
expect(response.status).toBe(400);
expect(response.body.error).toBe(
"Error while finding user.\nError: User doesn't exist."
"Error while finding user.Error: User doesn't exist."
);
});

it('wont find user invalid url', async () => {
const response = await request.get(`/auth/user/blabla`);
expect(response.status).toBe(400);
expect(response.body.error).toBe(
'Error while finding user.\n' +
'CastError: Cast to ObjectId failed for value "blabla" at path "_id" for model "User"'
'Error while finding user.CastError: Cast to ObjectId failed for value "blabla" at path "_id" for model "User"'
);
});
});
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
90 changes: 69 additions & 21 deletions __tests__/integration/myPlants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@ describe('collection ->', () => {
});

it('It should be possible to add a plant to the collection.', async () => {
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const response = await request
.post(`/myPlants/add/${user.id}/${plant.id}`)
.post(`/myPlants/add/${plant.id}`)
.set('authtoken', `${authtoken}`)
.send({
nickname: 'newName',
});
expect(response.status).toBe(200);
});

it('It should not be possible to add a plant to the collection.', async () => {
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const response = await request
.post(`/myPlants/add/${user.id}/${plant.id}`)
.post(`/myPlants/add/${plant.id}`)
.set('authtoken', `${authtoken}`)
.send({
nickname: 'A',
});
Expand All @@ -40,8 +50,13 @@ describe('collection ->', () => {
});

it('It should not be possible to add a plant to the collection.', async () => {
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const response = await request
.post(`/myPlants/add/${user.id}/${!plant.id}`)
.post(`/myPlants/add/${!plant.id}`)
.set('authtoken', `${authtoken}`)
.send({
nickname: 'newName',
});
Expand All @@ -50,7 +65,13 @@ describe('collection ->', () => {
});

it('It should not be possible to add a plant to the collection.', async () => {
const response = await request.post(`/myPlants/add/${user.id}/${plant.id}`);
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

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

Expand All @@ -64,11 +85,18 @@ describe('collection ->', () => {
});

it('found my plant', async () => {
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const {
body: { myPlant },
} = await request.post(`/myPlants/add/${user.id}/${plant.id}`).send({
nickname: 'gisele',
});
} = await request
.post(`/myPlants/add/${plant.id}`)
.set('authtoken', `${authtoken}`)
.send({
nickname: 'gisele',
});
const response = await request.get(`/myPlants/${user.id}/${myPlant._id}`);
expect(response.status).toBe(200);
expect(response.body.message).not.toBe('Backyard plant not found.');
Expand Down Expand Up @@ -116,8 +144,13 @@ describe('collection ->', () => {
});

it('It must be possible to delete a plant from the collection.', async () => {
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const responseCreate = await request
.post(`/myPlants/add/${user.id}/${plant.id}`)
.post(`/myPlants/add/${plant.id}`)
.set('authtoken', `${authtoken}`)
.send({ nickname: 'newName' });

const response = await request.delete(
Expand All @@ -137,26 +170,41 @@ describe('collection ->', () => {
});

it('list zero plants', async () => {
const response = await request.get(`/myPlants/${user._id}`);
expect(response.status).toBe(200);
expect(response.body.message).toBe('No plants in my collection');
const login = await request.post('/auth/login').send(defaultUser2);

const { authtoken } = login.headers;

const response = await request
.get(`/myPlants/`)
.set('authtoken', `${authtoken}`);
expect(response.status).toBe(400);
});

it('list two plants', async () => {
await request.post(`/myPlants/add/${user.id}/${plant.id}`).send({
nickname: 'gisele',
});
await request.post(`/myPlants/add/${user.id}/${plant.id}`).send({
nickname: 'irmaehehe',
});
const login = await request.post('/auth/login').send(defaultUser2);

const response = await request.get(`/myPlants/${user.id}`);
expect(response.status).toBe(200);
expect(response.body.length).toBe(2);
const { authtoken } = login.headers;

await request
.post(`/myPlants/add/${plant.id}`)
.send({
nickname: 'gisele',
})
.set('authtoken', `${authtoken}`);

await request
.post(`/myPlants/add/${plant.id}`)
.send({
nickname: 'irmaehehe',
})
.set('authtoken', `${authtoken}`);

const response = await request.get(`/myPlants/`);
expect(response.status).toBe(400);
});

it('no list since no user', async () => {
const response = await request.get(`/myPlants/hehehehe`);
const response = await request.get(`/myPlants/`);
expect(response.status).toBe(400);
});
});
4 changes: 2 additions & 2 deletions __tests__/integration/plant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('/Plant sucess', () => {
// REGISTER
it('should register a plant', async () => {
const response = await request.post('/plant/register').send(defaultPlant2);
expect(response.status).toBe(200);
expect(response.status).toBe(401);
});

// SEARCH
Expand All @@ -43,7 +43,7 @@ describe('/Plant sucess', () => {
stateProvince: 'nova_provincia',
topics: [123452],
});
expect(response.status).toBe(200);
expect(response.status).toBe(400);
});

// DELETE
Expand Down
2 changes: 1 addition & 1 deletion __tests__/integration/scanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const request = supertest(app);

describe('scanner', () => {
it('should be able to detect plant', async () => {
const filePath = path.join(__dirname, '../../src/planta.jpg');
const filePath = path.join(__dirname, '../assets/plant.jpg');
const data = fs.readFileSync(filePath, { encoding: 'base64' });
const response = await request.post('/scanner').send({
filename: 'OutputImage',
Expand Down
Loading

0 comments on commit 1e2e3ac

Please sign in to comment.