From d3277907054abbe239ab8d2ff07980f0ac51d69f Mon Sep 17 00:00:00 2001 From: TrefilovAnd Date: Sun, 7 May 2017 19:39:46 +0500 Subject: [PATCH 1/3] test auth --- app.js | 4 +- package.json | 2 + src/tests/authCtrls.js | 154 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 src/tests/authCtrls.js diff --git a/app.js b/app.js index b488cb5..52342a5 100644 --- a/app.js +++ b/app.js @@ -25,7 +25,7 @@ if (process.env.NODE_ENV && process.env.NODE_ENV === 'prod') { // app.use(require('connect-livereload')()); app.set('view engine', '.hbs'); -app.set('port', process.env.PORT || 8080); +app.set('port', process.env.PORT || 8081); let mongoose = require('mongoose'); @@ -51,3 +51,5 @@ app.listen(app.get('port'), function () { app.get('port') + '. Ctrl + C for exit.'); }); + +module.exports = app; diff --git a/package.json b/package.json index 6c3aacd..9c3e4aa 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,8 @@ "babel-plugin-transform-es2015-parameters": "^6.23.0", "body-parser": "~1.17.1", "browser-sync": "^2.18.8", + "chai": "^3.5.0", + "chai-http": "^3.0.0", "cloudinary": "^1.8.0", "connect-livereload": "^0.6.0", "cookie-parser": "~1.4.3", diff --git a/src/tests/authCtrls.js b/src/tests/authCtrls.js new file mode 100644 index 0000000..62c6a06 --- /dev/null +++ b/src/tests/authCtrls.js @@ -0,0 +1,154 @@ +/* eslint no-unused-vars: 'off' */ +// const assert = require('assert'); +let chai = require('chai'); +let chaiHttp = require('chai-http'); +let should = chai.should(); +let server = require('../../app'); +chai.use(chaiHttp); + +const dataWithBadRepeat = { + password: 'pass', + name: 'Super Name', + email: 'mail@mail.ru', + passwordRepeat: 'password' +}; +const dataBadSpaces = { + password: ' ', + name: ' ', + email: ' ', + passwordRepeat: ' ' +}; +const repeatData = { + password: '1', + name: 'Name', + email: 'mail@mail.com', + passwordRepeat: '1' +}; +const dataWithInvalidEmail = { + password: '1', + name: 'Name', + email: 'bugiMail', + passwordRepeat: '1' +}; + +describe('Sign Up', () => { + it('Разные пароли', function (done) { + this.timeout(4000); + chai.request(server) + .post('/signup') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(dataWithBadRepeat) + .end((err, res) => { + res.should.have.status(409); + res.body.should.be.a('object'); + res.body.should.have.property('message').eql('Пароли не совпадают'); + done(); + }); + }); + + it('Строки состящие из пробелов', function (done) { + this.timeout(4000); + chai.request(server) + .post('/signup') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(dataBadSpaces) + .end((err, res) => { + res.should.have.status(409); + res.body.should.be.a('object'); + res.body.should.have.property('message').not.eql('Пользователь с таким email уже зарегестрирован'); + done(); + }); + }); + + it('Почта, которая уже существует', function (done) { + this.timeout(4000); + chai.request(server) + .post('/signup') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(repeatData) + .end((err, res) => { + res.should.have.status(409); + res.body.should.be.a('object'); + res.body.should.have.property('message').eql('Пользователь с таким email уже зарегестрирован'); + done(); + }); + }); + + it('Невалидная почта', function (done) { + this.timeout(4000); + chai.request(server) + .post('/signup') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(dataWithInvalidEmail) + .end((err, res) => { + res.should.have.status(409); + res.body.should.be.a('object'); + res.body.should.have.property('message').not.eql('Пользователь с таким email уже зарегестрирован'); + done(); + }); + }); +}); + +const validData = { + password: '1', + email: 'bugiMail' +}; +const invalidDataPass = { + password: '2', + email: 'bugiMail' +}; +const invalidDataMail = { + password: '1', + email: 'bugiMail@mail.ru' +}; + +describe('Sign In', () => { + it('Валидные данные', function (done) { + this.timeout(4000); + chai.request(server) + .post('/signin') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(validData) + .end((err, res) => { + res.should.have.status(200); + es.body.should.be.a('object'); + res.body.should.have.property('message').eql('OK'); + done(); + }); + }); + + it('Не валидный пароль', function (done) { + this.timeout(4000); + chai.request(server) + .post('/signin') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(invalidDataPass) + .end((err, res) => { + res.should.have.status(403); + res.body.should.be.a('object'); + res.body.should.have.property('message').eql('Неверный пароль'); + done(); + }); + }); + + it('Не валидный email', function (done) { + this.timeout(4000); + chai.request(server) + .post('/signin') + .set('Content-Type', 'application/json') + .set('Accept', 'application/json') + .send(invalidDataMail) + .end((err, res) => { + res.should.have.status(403); + res.body.should.be.a('object'); + res.body.should.have.property('message').eql('Юзера с данным email не существует'); + done(); + }); + }); +}); From fdc428aa69e9798b697c2facf7a6270c42a2ebfa Mon Sep 17 00:00:00 2001 From: TrefilovAnd Date: Sun, 7 May 2017 19:45:44 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BF=D1=83=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B1=D1=83=D0=BA=D0=B2=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tests/authCtrls.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/authCtrls.js b/src/tests/authCtrls.js index 62c6a06..732a561 100644 --- a/src/tests/authCtrls.js +++ b/src/tests/authCtrls.js @@ -116,7 +116,7 @@ describe('Sign In', () => { .send(validData) .end((err, res) => { res.should.have.status(200); - es.body.should.be.a('object'); + res.body.should.be.a('object'); res.body.should.have.property('message').eql('OK'); done(); }); From ba0c149b0eb18eccec2451d01ab7a5883bcd1595 Mon Sep 17 00:00:00 2001 From: TrefilovAnd Date: Sun, 7 May 2017 19:48:31 +0500 Subject: [PATCH 3/3] =?UTF-8?q?8080=20=D0=BF=D0=BE=D1=80=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.js b/app.js index 52342a5..dd9a500 100644 --- a/app.js +++ b/app.js @@ -25,7 +25,7 @@ if (process.env.NODE_ENV && process.env.NODE_ENV === 'prod') { // app.use(require('connect-livereload')()); app.set('view engine', '.hbs'); -app.set('port', process.env.PORT || 8081); +app.set('port', process.env.PORT || 8080); let mongoose = require('mongoose');