From 4753e07efb0d5b8ad68a068bc512d40733bab932 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Lima <lgl15@inf.ufpr.br> Date: Mon, 24 Oct 2016 12:03:00 -0200 Subject: [PATCH] add some tests for user (wip) --- src/libs/routes/user.js | 2 +- src/test/test.js | 268 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+), 1 deletion(-) diff --git a/src/libs/routes/user.js b/src/libs/routes/user.js index 2f410fbc..223552a5 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 5529e056..65187f1c 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(); + }); + }); + }); + +}); -- GitLab