text: 'use strict';
  var request = require('supertest'),
      assert = require('assert'),
      config = require('../../../config/config'),
      _ = require('lodash'),
      jwt = require('jsonwebtoken'),
      mongoose = require('mongoose'),
      app = require('../../../config/express'),
      Product = mongoose.model('Product');
  
  var credentials,
      token,
      mockup;
  
  describe('Product CRUD routes tests', function () {
  
      before(function (done) {
          mockup = {
              name: 'name',
              tel: '0995682356',
              price: 200,
              image: 'https://cdn.pixabay.com/photo/2018/08/06/16/30/mushroom-3587888_1280.jpg'
          };
          credentials = {
              username: 'username',
              password: 'password',
              firstname: 'first name',
              lastname: 'last name',
              email: 'test@email.com',
              roles: ['user']
          };
          token = jwt.sign(_.omit(credentials, 'password'), config.jwt.secret, {
              expiresIn: 2 * 60 * 60 * 1000
          });
          done();
      });
  
      it('should be Product get use token', (done) => {
          request(app)
              .get('/api/products')
              .set('Authorization', 'Bearer ' + token)
              .expect(200)
              .end((err, res) => {
                  if (err) {
                      return done(err);
                  }
                  var resp = res.body;
                  done();
              });
      });
  
      it('should be Product get by id', function (done) {
  
          request(app)
              .post('/api/products')
              .set('Authorization', 'Bearer ' + token)
              .send(mockup)
              .expect(200)
              .end(function (err, res) {
                  if (err) {
                      return done(err);
                  }
                  var resp = res.body;
                  request(app)
                      .get('/api/products/' + resp.data._id)
                      .set('Authorization', 'Bearer ' + token)
                      .expect(200)
                      .end(function (err, res) {
                          if (err) {
                              return done(err);
                          }
                          var resp = res.body;
                          assert.equal(resp.status, 200);
                          assert.equal(resp.data.name, mockup.name);
                          assert.equal(resp.data.tel, mockup.tel);
                          assert.equal(resp.data.image, mockup.image);
                          assert.equal(resp.data.price, mockup.price);
                          done();
                      });
              });
  
      });
  
      it('should be Product post use token', (done) => {
          request(app)
              .post('/api/products')
              .set('Authorization', 'Bearer ' + token)
              .send(mockup)
              .expect(200)
              .end(function (err, res) {
                  if (err) {
                      return done(err);
                  }
                  var resp = res.body;
                  assert.equal(resp.data.name, mockup.name);
                  assert.equal(resp.data.tel, mockup.tel);
                  assert.equal(resp.data.image, mockup.image);
                  assert.equal(resp.data.price, mockup.price);
  
                  done();
              });
      });
  
      it('should be product put use token', function (done) {
  
          request(app)
              .post('/api/products')
              .set('Authorization', 'Bearer ' + token)
              .send(mockup)
              .expect(200)
              .end(function (err, res) {
                  if (err) {
                      return done(err);
                  }
                  var resp = res.body;
                  var update = {
                      name: 'name update',
                      tel: '1122334455',
                      price: 5600,
                      image: 'ddssaa.jpg'
                  }
                  request(app)
                      .put('/api/products/' + resp.data._id)
                      .set('Authorization', 'Bearer ' + token)
                      .send(update)
                      .expect(200)
                      .end(function (err, res) {
                          if (err) {
                              return done(err);
                          }
                          var resp = res.body;
                          assert.equal(resp.data.name, update.name);
                          assert.equal(resp.data.tel, update.tel);
                          assert.equal(resp.data.image, update.image);
                          assert.equal(resp.data.price, update.price);
                          done();
                      });
              });
  
      });
  
      it('should be product delete use token', function (done) {
  
          request(app)
              .post('/api/products')
              .set('Authorization', 'Bearer ' + token)
              .send(mockup)
              .expect(200)
              .end(function (err, res) {
                  if (err) {
                      return done(err);
                  }
                  var resp = res.body;
                  request(app)
                      .delete('/api/products/' + resp.data._id)
                      .set('Authorization', 'Bearer ' + token)
                      .expect(200)
                      .end(done);
              });
  
      });
  
      it('should be product get not use token', (done) => {
          request(app)
              .get('/api/products')
              .expect(403)
              .expect({
                  status: 403,
                  message: 'User is not authorized'
              })
              .end(done);
      });
  
      it('should be product post not use token', function (done) {
  
          request(app)
              .post('/api/products')
              .send(mockup)
              .expect(403)
              .expect({
                  status: 403,
                  message: 'User is not authorized'
              })
              .end(done);
  
      });
  
      it('should be product put not use token', function (done) {
  
          request(app)
              .post('/api/products')
              .set('Authorization', 'Bearer ' + token)
              .send(mockup)
              .expect(200)
              .end(function (err, res) {
                  if (err) {
                      return done(err);
                  }
                  var resp = res.body;
                  var update = {
                      name: 'name update'
                  }
                  request(app)
                      .put('/api/products/' + resp.data._id)
                      .send(update)
                      .expect(403)
                      .expect({
                          status: 403,
                          message: 'User is not authorized'
                      })
                      .end(done);
              });
  
      });
  
      it('should be product delete not use token', function (done) {
  
          request(app)
              .post('/api/products')
              .set('Authorization', 'Bearer ' + token)
              .send(mockup)
              .expect(200)
              .end(function (err, res) {
                  if (err) {
                      return done(err);
                  }
                  var resp = res.body;
                  request(app)
                      .delete('/api/products/' + resp.data._id)
                      .expect(403)
                      .expect({
                          status: 403,
                          message: 'User is not authorized'
                      })
                      .end(done);
              });
  
      });
  
      afterEach(function (done) {
          Product.remove().exec(done);
      });
  
  });