Skip to content

Commit

Permalink
Merge pull request #19 from fga-eps-mds/FIX-forbid-duplicated-sectors
Browse files Browse the repository at this point in the history
Fix proibição da criação de setores com mesmo nome
  • Loading branch information
EzequielDeOliveira authored May 14, 2021
2 parents 45e637d + 90b2de5 commit dc1e0fa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
19 changes: 11 additions & 8 deletions src/Controllers/SectorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ const sectorCreate = async (req, res) => {
return res.status(400).json({ status: validFields });
}

const newSector = await Sector.create({
name,
description,
createdAt: moment.utc(moment.tz('America/Sao_Paulo').format('YYYY-MM-DDTHH:mm:ss')).toDate(),
updatedAt: moment.utc(moment.tz('America/Sao_Paulo').format('YYYY-MM-DDTHH:mm:ss')).toDate(),
});

return res.status(200).json(newSector);
try {
const newSector = await Sector.create({
name,
description,
createdAt: moment.utc(moment.tz('America/Sao_Paulo').format('YYYY-MM-DDTHH:mm:ss')).toDate(),
updatedAt: moment.utc(moment.tz('America/Sao_Paulo').format('YYYY-MM-DDTHH:mm:ss')).toDate(),
});
return res.status(200).json(newSector);
} catch (error) {
return res.status(400).json({ error: error.code });
}
};

const sectorUpdate = async (req, res) => {
Expand Down
1 change: 1 addition & 0 deletions src/Models/SectorSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mongoose = require('mongoose');
const sectorSchema = new mongoose.Schema({
name: {
type: String,
unique: true,
require: [true],
},
description: {
Expand Down
71 changes: 33 additions & 38 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const jwt = require('jsonwebtoken');
describe('Sample Test', () => {
let id;
const sector = {
name: 'enfermagem',
description: 'setor de enfermagem',
name: Math.random().toString(36).substr(2, 5),
description: 'setor teste',
};

const token = jwt.sign({ name: "Teste", description: "Teste" }, process.env.SECRET, {
Expand All @@ -18,7 +18,6 @@ describe('Sample Test', () => {
done();
});

// sector/create
it('Post sector', async (done) => {
const res = await request(app).post('/sector/create').set('x-access-token', token).send(sector);
expect(res.statusCode).toBe(200);
Expand All @@ -40,14 +39,12 @@ describe('Sample Test', () => {
done();
});

// sector
it('Get sector', async (done) => {
const res = await request(app).get('/sector/').set('x-access-token', token);
expect(res.statusCode).toBe(200);
done();
});

// sector/:id
it('Get id sector', async (done) => {
const res = await request(app).get(`/sector/${id}`).set('x-access-token', token);
expect(res.statusCode).toBe(200);
Expand All @@ -63,81 +60,79 @@ describe('Sample Test', () => {
done();
});

// sector/update/:id
it('Update sector', async () => {
it('Update sector', async (done) => {
const sector = {
name: "fisioterapia",
description: "setor de fisioterapia"
name: "fisioterapia",
description: "setor de fisioterapia"
};

const res = await request(app)
.put(`/sector/update/${id}`)
.set('x-access-token', token)
.send(sector);
expect(res.statusCode).toBe(200);
expect(res.body.name).toBe(sector.name);
expect(res.body.description).toBe(sector.description);
});
done();
});

// Invalido
it('Update sector error', async () => {
it('Update sector error', async (done) => {
const sector = {
name: "",
description: "Jest description"
}

name: "",
description: "Jest description"
};
const res = await request(app)
.put(`/sector/update/${id}`)
.set('x-access-token', token)
.send(sector);
expect(res.statusCode).toBe(400);
expect(res.body.status).toEqual([ 'invalid name' ]);
});
done();
});

it('Update with invalid id', async () => {
it('Update with invalid id', async (done) => {
const sector = {
name: "fisioterapia",
description: "setor de fisioterapia"
name: "fisioterapia",
description: "setor de fisioterapia"
};

const res = await request(app)
.put(`/sector/update/123abc`)
.set('x-access-token', token)
.send(sector)
.send(sector);
expect(res.statusCode).toBe(400);
expect(res.body.err).toBe('invalid id')
});
expect(res.body.err).toBe('invalid id');
done();
});

it('Update sector without token', async () => {
it('Update sector without token', async (done) => {
const sector = {
name: "Jest test",
description: "Jest description"
}

name: "Jest test",
description: "Jest description"
};
const res = await request(app)
.put(`/sector/update/${id}`)
.send(sector);
expect(res.statusCode).toBe(401);
expect(res.body).toEqual({ auth: false, message: 'No token was provided' });
});
done();
});

it('Update sector with invalid token', async () => {
it('Update sector with invalid token', async (done) => {
const tokenFalho = 'abc123';
const sector = {
name: "Jest test",
description: "Jest description"
name: "Jest test",
description: "Jest description"
}

const res = await request(app)
.put(`/sector/update/${id}`)
.set('x-access-token', tokenFalho)
.send(sector);
expect(res.statusCode).toBe(500);
expect(res.body).toEqual({ auth: false, message: 'It was not possible to authenticate the token.' });
});
done();
});

it('Delete sector', async (done) => {
const res = await request(app).delete(`/sector/delete/${id}`).set('x-access-token', token)
it('Delete sector', async (done) => {
const res = await request(app).delete(`/sector/delete/${id}`).set('x-access-token', token);
expect(res.statusCode).toBe(200);
expect(res.body).toEqual({"message":"success"});
done();
Expand Down

0 comments on commit dc1e0fa

Please sign in to comment.