// 测试用户的认证过程
import chai,{expect} from 'chai';   // eslint-disable-line
const should = chai.should(); // eslint-disable-line
import mongoose from 'mongoose';
import {Order} from '../models';
const model_list = [Order];
import {ClearDataForTest,db_connect_string,server} from './lib';
import moment from 'moment';
const order={
  'room_id':'3607521A-9CC3-495C-8BBB-1F38ADA91420',
  'start':'2016-09-10 15:00:00',
  'end':'2016-09-10 17:00:00',
  'order_user':'mike',
  'order_user_id':'B0F33EB0-7703-4C28-956D-62F62C5F3292',
  'order_email':'mike@example.com',
  'persons_number':10,
  'title':'Operation'
};
const order_update={
  'room_id':'3607521A-9CC3-495C-8BBB-1F38ADA91420',
  'start':'2016-09-10 15:00:00',
  'end':'2016-09-10 17:00:00',
  'order_user':'mike',
  'order_user_id':'B0F33EB0-7703-4C28-956D-62F62C5F3292',
  'order_email':'mike@example.com',
  'persons_number':10,
  'title':'Operation'
};

const order_whitin2weeks_1={
  'room_id':'3607521A-9CC3-495C-8BBB-1F38ADA91420',
  'start':moment().add(2, 'days').format('YYYY-MM-DD HH:mm:ss'),
  'end':moment().add(2, 'days').add(2,'hours').format('YYYY-MM-DD HH:mm:ss'),
  'order_user':'mike',
  'order_user_id':'B0F33EB0-7703-4C28-956D-62F62C5F3292',
  'order_email':'mike@example.com',
  'persons_number':10,
  'title':'Operation'
};
const order_whitin2weeks_2={
  'room_id':'3607521A-9CC3-495C-8BBB-1F38ADA91420',
  'start':moment().add(8, 'days').format('YYYY-MM-DD HH:mm:ss'),
  'end':moment().add(8, 'days').add(3,'hours').format('YYYY-MM-DD HH:mm:ss'),
  'order_user':'mike',
  'order_user_id':'B0F33EB0-7703-4C28-956D-62F62C5F3292',
  'order_email':'mike@example.com',
  'persons_number':10,
  'title':'Operation'
};
const order_outof2weeks_1={
  'room_id':'3607521A-9CC3-495C-8BBB-1F38ADA91420',
  'start':moment().add(-15, 'days').format('YYYY-MM-DD HH:mm:ss'),
  'end':moment().add(-15, 'days').add(3,'hours').format('YYYY-MM-DD HH:mm:ss'),
  'order_user':'mike',
  'order_user_id':'B0F33EB0-7703-4C28-956D-62F62C5F3292',
  'order_email':'mike@example.com',
  'persons_number':10,
  'title':'Operation'
};

const order_outof2weeks_2={
  'room_id':'3607521A-9CC3-495C-8BBB-1F38ADA91420',
  'start':moment().add(15, 'days').format('YYYY-MM-DD HH:mm:ss'),
  'end':moment().add(15, 'days').add(3,'hours').format('YYYY-MM-DD HH:mm:ss'),
  'order_user':'mike',
  'order_user_id':'B0F33EB0-7703-4C28-956D-62F62C5F3292',
  'order_email':'mike@example.com',
  'persons_number':10,
  'title':'Operation'
};
global.id=null;
describe('Order Test Cases', () => {
  before((done) => {
    if (mongoose.connection.readyState === 0) {
       mongoose.connect(db_connect_string, (err) => {
         if (err) {
           throw err;
         }
         return ClearDataForTest(done,model_list);
       });
     } else {
       return ClearDataForTest(done,model_list);
     }
  });
  after((done) => {
    // in case error:OverwriteModelError: https://github.com/Automattic/mongoose/issues/1251
    mongoose.models = {};
    mongoose.modelSchemas = {};
    mongoose.disconnect(()=>{
      done()
    });
  });
  describe('Order Basic API Test Cases', () => {
      it('it should return a empty Order array', (done) => {
        server.get('/api/orders')
              .expect('Content-type',/json/)
              .end((err, res) => {
               res.status.should.equal(404);
               res.body.data.should.be.a('object');
               res.body.success.should.equal(false);
               res.body.error_message.should.not.equal('');
               done();
             });
      });
      it('it should not create a new Order  without post right arguments', (done) => {
        server.post('/api/Orders')
              .expect('Content-type',/json/)
              .end((err, res) => {
               res.status.should.equal(400);
               res.body.data.should.be.a('object');
               res.body.success.should.equal(false);
               res.body.error_message.should.not.equal('');
               done();
             });
      });

      it('it will create a new Order when you post with right arguments', (done) => {

        server.post('/api/orders')
              .expect('Content-type',/json/)
              .send(order)
              .end((err, res) => {
               res.status.should.equal(200);
               res.body.data.should.be.a('object');
               res.body.success.should.equal(true);
               should.not.exist(res.body.error_message);
               done();
             });
      });
      it('it should return a Order array with one Order', (done) => {
        server.get('/api/orders')
              .expect('Content-type',/json/)
              .end((err, res) => {
               res.status.should.equal(200);
               res.body.data.should.be.a('array');
               res.body.data[0]['room_id'].should.be.equal(order['room_id']);
               res.body.data[0]['order_user'].should.be.equal(order['order_user']);
               moment(res.body.data[0]['start']).format('YYYY-MM-DD HH:mm:ss').should.be.equal(order['start']);
               res.body.success.should.equal(true);
               global.id=res.body.data[0]['_id'];
               should.not.exist(res.body.error_message);
               done();
             });
      });

      it('it should get a Order object with one Order', (done) => {
        server.get('/api/orders/'+global.id)
              .expect('Content-type',/json/)
              .end((err, res) => {
               res.status.should.equal(200);
               res.body.data.should.be.a('object');
               res.body.data['room_id'].should.be.equal(order['room_id']);
               res.body.data['order_user'].should.be.equal(order['order_user']);
               moment(res.body.data['start']).format('YYYY-MM-DD HH:mm:ss').should.be.equal(order['start']);
               res.body.success.should.equal(true);
               should.not.exist(res.body.error_message);
               done();
         });
      });
      it('it should update a Order object with post data Order', (done) => {
        server.put('/api/orders/'+global.id)
              .expect('Content-type',/json/)
              .send(order_update)
              .end((err, res) => {
               res.status.should.equal(200);
               res.body.data['room_id'].should.be.equal(order_update['room_id']);
               res.body.data['order_user'].should.be.equal(order_update['order_user']);
               moment(res.body.data['start']).format('YYYY-MM-DD HH:mm:ss').should.be.equal(order_update['start']);
               res.body.success.should.equal(true);
               should.not.exist(res.body.error_message);
               done();
         });
      });
      // delete by params id
      it('it should delete a Order object with one Order', (done) => {
        server.delete('/api/orders/'+global.id)
              .expect('Content-type',/json/)
              .end((err, res) => {
               res.status.should.equal(200);
               res.body.data.should.be.a('object');
               res.body.data['ok'].should.be.equal(1);
               res.body.success.should.equal(true);
               should.not.exist(res.body.error_message);
               done();
         });
      });
      // delete by json data array
      it('it will create a new Order when you post with right arguments', (done) => {

        server.post('/api/orders')
              .expect('Content-type',/json/)
              .send(order)
              .end((err, res) => {
               res.status.should.equal(200);
               res.body.data.should.be.a('object');
               res.body.success.should.equal(true);
               should.not.exist(res.body.error_message);
               done();
             });
      });
      it('it should return a Order array with one Order', (done) => {
        server.get('/api/orders')
              .expect('Content-type',/json/)
              .end((err, res) => {
               res.status.should.equal(200);
               res.body.data.should.be.a('array');
               res.body.data[0]['room_id'].should.be.equal(order['room_id']);
               res.body.data[0]['order_user'].should.be.equal(order['order_user']);
               moment(res.body.data[0]['start']).format('YYYY-MM-DD HH:mm:ss').should.be.equal(order['start']);
               res.body.success.should.equal(true);
               global.id=res.body.data[0]['_id'];
               should.not.exist(res.body.error_message);
               done();
             });
      });
        // try to delete by json data array
        it('it should delete a Order array with right json data', (done) => {
          server.delete('/api/orders')
                .expect('Content-type',/json/)
                .send({pending_delete_array:[global.id]})
                .end((err, res) => {
                 res.status.should.equal(200);
                 res.body.data.should.be.a('object');
                 res.body.success.should.equal(true);
                 res.body.data.ok.should.equal(1);
                 done();
               });
        });

      it('it should return a empty Order array,after delete', (done) => {
        server.get('/api/orders')
              .expect('Content-type',/json/)
              .end((err, res) => {
               res.status.should.equal(404);
               res.body.data.should.be.a('object');
               res.body.success.should.equal(false);
               res.body.error_message.should.not.equal('');
               done();
             });
      });
  })
  describe('Order Advance API Test Cases', () => {
    it('it should return a empty Order array', (done) => {
      server.get('/api/orders')
            .expect('Content-type',/json/)
            .end((err, res) => {
             res.status.should.equal(404);
             res.body.data.should.be.a('object');
             res.body.success.should.equal(false);
             res.body.error_message.should.not.equal('');
             done();
           });
    });

    it('it will save 4 Orders when you post with right arguments', (done) => {
      server.post('/api/orders')
            .expect('Content-type',/json/)
            .send(order_whitin2weeks_1)
            .end((err, res) => {
             res.status.should.equal(200);
             res.body.data.should.be.a('object');
             res.body.success.should.equal(true);
             should.not.exist(res.body.error_message);
             server.post('/api/orders')
                   .expect('Content-type',/json/)
                   .send(order_whitin2weeks_2)
                   .end((err, res) => {
                    res.status.should.equal(200);
                    res.body.data.should.be.a('object');
                    res.body.success.should.equal(true);
                    should.not.exist(res.body.error_message);
                    server.post('/api/orders')
                          .expect('Content-type',/json/)
                          .send(order_outof2weeks_1)
                          .end((err, res) => {
                           res.status.should.equal(200);
                           res.body.data.should.be.a('object');
                           res.body.success.should.equal(true);
                           should.not.exist(res.body.error_message);
                           server.post('/api/orders')
                                 .expect('Content-type',/json/)
                                 .send(order_outof2weeks_2)
                                 .end((err, res) => {
                                  res.status.should.equal(200);
                                  res.body.data.should.be.a('object');
                                  res.body.success.should.equal(true);
                                  should.not.exist(res.body.error_message);
                                  done();
                                });
                         });
                  });
           });
    });

    it('it should get a Order object within two weeks', (done) => {
      server.get('/api/orders/advance/get2weeks')
            .expect('Content-type',/json/)
            .end((err, res) => {
             res.status.should.equal(200);
             res.body.data.should.be.a('array');
             res.body.data.length.should.be.equal(2);
             res.body.success.should.equal(true);
             should.not.exist(res.body.error_message);
             done();
       });
    });

  });
});
