Advertisement
Guest User

Untitled

a guest
Jul 13th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const should = require('should')
  2. const schema = require('../../models/user')
  3. const mongoose = require('mongoose')
  4. const User = mongoose.model('User', schema)
  5. const app = require('../../app')
  6. const request = require('supertest')
  7.  
  8. describe('Auth: routes', () => {
  9.  
  10.   const baseUrl = '/auth/signin'
  11.   const emailAddress = 'max@example.com'
  12.   const realPassword = 'secret1'
  13.  
  14.   beforeEach(done => {
  15.     User.hashPassword(realPassword, function (err, passwordHash) {
  16.       // Create a User
  17.       const newUser = new User({
  18.         name: 'Name',
  19.         passwordHash: passwordHash,
  20.         email: emailAddress,
  21.       })
  22.  
  23.       newUser.save((err, item) =>{
  24.         should.not.exist(err)
  25.         should.exist(item)
  26.         done()
  27.       })
  28.     })
  29.   })
  30.  
  31.   describe('POST /auth/signin', function () {
  32.  
  33.     it('should response with token if authentication success', (done) => {
  34.       const data = {
  35.         email: emailAddress,
  36.         password: realPassword,
  37.       };
  38.       request(app)
  39.         .post(baseUrl)
  40.         .send(data)
  41.         .expect(200)
  42.         .end(function (err, res) {
  43.           should.not.exist(err)
  44.           should.exist(res.body.data)
  45.           done()
  46.         });
  47.     })
  48.  
  49.     it('should response with error or errors array if authentication fails', (done) => {
  50.       const data = {
  51.         email: 'berry@example.com',
  52.         password: 'fakepassword',
  53.       };
  54.       request(app)
  55.         .post(baseUrl)
  56.         .send(data)
  57.         .expect(400)
  58.         .end(function (err, res) {
  59.           should.not.exist(err);
  60.           should.exist(res.body.error || res.body.errors)
  61.           done();
  62.         });
  63.     })
  64.  
  65.   })
  66.  
  67.   describe('POST /auth/signup', function () {
  68.  
  69.     it('should response with token if signup success', (done) => {
  70.       const data = {
  71.         email: emailAddress,
  72.         password: realPassword,
  73.       };
  74.       request(app)
  75.         .post('/auth/signin')
  76.         .send(data)
  77.         .expect(200)
  78.         .end(function (err, res) {
  79.           should.not.exist(err)
  80.           should.exist(res.body.data)
  81.           done()
  82.         });
  83.     })
  84.  
  85.   })
  86.  
  87. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement