diff --git a/src/libs/routes/user.js b/src/libs/routes/user.js index 2f410fbcca146d4c48eaef6bfc0f255168d8aac2..223552a59f2160d6940d5590a7ce534f811d244b 100644 --- a/src/libs/routes/user.js +++ b/src/libs/routes/user.js @@ -36,7 +36,7 @@ userApp.post('/', (req, res, next) => { }, (req, res, next) => { if(!emailSyntax(req.body.email)){ - res.json({success: false, msg: 'O email Informado é inválido.'}); + res.json({success: false, msg: 'O email informado é inválido.'}); } else { next(); } diff --git a/src/test/test.js b/src/test/test.js index 5529e056fa9303f731a416d2845b096f434a28a8..65187f1c2b6f003fa1a0bbad61180361deb65638 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -24,6 +24,7 @@ const server = require(`${libs}/app`); const mongoose = require('../libs/db/mongoose'); const Simulation = require('../libs/models/simulation'); +const User = require('../libs/models/user'); chai.use(chaiHttp); @@ -778,3 +779,270 @@ describe('Requires a simulation', () => { }); }); }); + +describe('Saves a user', () => { + + beforeEach(() => { + User.remove({}, (err) => { + console.log('Test collection purged'); + }); + }); + + it('should create a new user', (done) => { + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({email: 'lorem@ipsum.com', + password: '123mudar', + name: 'Tequila Baby', + cpf: '48303270737', + schooling: 'Doutorado', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(true); + User.findOne({'email': 'lorem@ipsum.com'}, (err, user) => { + if (err){ + console.log('MongoDB error: ' + err); + } + + user.should.have.property('email'); + done(); + }); + }); + }); + + it('should not create a user with an email that is already in use', (done) => { + let newUser = new User(); + + newUser.email = 'lorem@ipsum.com'; + newUser.password = '123mudar'; + newUser.name = 'Gute'; + newUser.cpf = '08236017907'; + newUser.schooling = 'Doutorado'; + newUser.course = 'Ciência da Computação'; + newUser.segment = 'Comunidade acadêmica'; + newUser.role = 'Pesquisador'; + newUser.institution_name = 'UFPR'; + newUser.state = 'PR'; + newUser.city = 'Curitiba'; + + newUser.save((err) => { + if (err) { + console.log('MongoDB error:' + err); + } + }).then(function(newuser){ + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({email: 'lorem@ipsum.com', + password: '123mudar', + name: 'Tequila Baby', + cpf: '48303270737', + schooling: 'Doutorado', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + res.body.should.have.property('msg'); + res.body.msg.should.equal('O email informado já está cadastrado.'); + User.findOne({'cpf': '48303270737'}, (err, user) => { + expect(user).to.not.exist; + done(); + }); + }); + }); + }); + + it('should not save an user without email', (done) => { + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({password: '123mudar', + name: 'Tequila Baby', + cpf: '48303270737', + schooling: 'Doutorado', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + res.body.should.have.property('msg'); + res.body.msg.should.equal('O campo Email é obrigatório.'); + User.findOne({'cpf': '48303270737'}, (err, user) => { + expect(user).to.not.exist; + done(); + }); + }); + }); + + it('should not save an user without password', (done) => { + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({email: 'lorem@ipsum.com', + name: 'Tequila Baby', + cpf: '48303270737', + schooling: 'Doutorado', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + res.body.should.have.property('msg'); + res.body.msg.should.equal('O campo Senha é obrigatório.'); + User.findOne({'cpf': '48303270737'}, (err, user) => { + expect(user).to.not.exist; + done(); + }); + }); + }); + + it('should not save an user with invalid email', (done) => { + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({email: 'notavalidemail', + password: '123mudar', + name: 'Tequila Baby', + cpf: '48303270737', + schooling: 'Doutorado', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + res.body.should.have.property('msg'); + res.body.msg.should.equal('O email informado é inválido.'); + User.findOne({'cpf': '48303270737'}, (err, user) => { + expect(user).to.not.exist; + done(); + }); + }); + }); + + it('should not save an user without name', (done) => { + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({email: 'lorem@ipsum.com', + password: '123mudar', + cpf: '48303270737', + schooling: 'Doutorado', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + res.body.should.have.property('msg'); + res.body.msg.should.equal('O campo Nome é obrigatório.'); + User.findOne({'cpf': '48303270737'}, (err, user) => { + expect(user).to.not.exist; + done(); + }); + }); + }); + + it('should not save an user without CPF', (done) => { + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({email: 'lorem@ipsum.com', + password: '123mudar', + name: 'Tequila baby', + schooling: 'Doutorado', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + res.body.should.have.property('msg'); + res.body.msg.should.equal('O campo CPF é obrigatório.'); + User.findOne({'email': 'lorem@ipsum.com'}, (err, user) => { + expect(user).to.not.exist; + done(); + }); + }); + }); + + it('should not save an user without CPF', (done) => { + chai.request(server) + .post('/api/v1/user') + .set('content-type', 'application/x-www-form-urlencoded') + .set('x-apicache-bypass', 'true') + .send({email: 'lorem@ipsum.com', + password: '123mudar', + name: 'Tequila baby', + cpf: '48303270737', + course: 'Ciência da Computação', + segment: 'Comunidade acadêmica', + role: 'Pesquisador', + institution_name: 'UFPR', + state: 'PR', + city: 'Cutiriba'}) + .end((err, res) =>{ + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + res.body.should.have.property('msg'); + res.body.msg.should.equal('O campo Escolaridade é obrigatório.'); + User.findOne({'email': 'lorem@ipsum.com'}, (err, user) => { + expect(user).to.not.exist; + done(); + }); + }); + }); + +});