'use strict';

process.env.NODE_ENV = 'test';

var request = require('../../helper/setup-supertest')('http://localhost:3001')
  , chai = require('../../helper/setup-chai')
  , status = require('../../../lib/server/status')
  , login = require('../../helper/login')(request)
  , logout = require('../../helper/logout')(request)
  , mongoInit = require('../../init/mongo-init')
  , commentInit = require('../../init/comments-init')
  ;

describe('Add Comment API', () => {

  before((done) => {
    mongoInit.connect().then(commentInit).catch(console.log).fin(done);
  });

  after((done) => {
    commentInit().then(mongoInit.disconnect).catch(console.log).fin(done);
  });

  describe('#addComment', () => {
    var pid = '35bc6f7b9b0d0b0457673daf';

    it('should not allow anonymous users to post new comment', (done) => {
      request
        .post('/api/v1/comments/')
        .send({
          text: 'malicious text',
          pid: pid
        })
        .toPromise()
        .then((res) => {
          res.body.status.should.be.equal(status.codes.UserAuthRequired.code);
          res.body.value.should.have.property('message');
        })
        .then(done)
        .catch(done)
        .done();
    });

    it('should not be an empty comment', (done) => {
      login('test@test.com', 'test')
        .then(() => {
          return request
            .post('/api/v1/comments/')
            .send({
              text: '',
              pid: pid
            })
            .toPromise();
        })
        .then((res) => {
          res.body.status.should.be.equal(status.codes.EmptyComment.code);
          res.body.value.should.have.property('message');
        })
        .then(logout)
        .then(done)
        .catch(done)
        .done();
    });

    it('should allow logged-in users to post new comment', (done) => {
      login('test@test.com', 'test')
        .then(() => {
          return request
            .post('/api/v1/comments/')
            .send({
              text: 'yay im logged in',
              pid: pid
            })
            .toPromise();
        })
        .then((res) => {
          res.body.status.should.be.equal(0);
          res.body.value.should.exist;
          var cid = res.body.value;

          return request
            .get('/api/v1/posts/' + pid)
            .toPromise();
        })
        .then((res) => {
          res.body.value.should.have.property('comments');
          res.body.value.comments.should.have.length.above(0);
          res.body.value.comments[0].should.have.property('text', 'yay im logged in');
        })
        .then(logout)
        .then(done)
        .catch(done)
        .done();
    });
  });

});

@@ -423,16 +423,150 @@
 -init')%0A
+  , PostModel = require('../../../lib/model/post')%0A  , postInit = require('../../init/posts-init')%0A  , mongoose = require('mongoose')%0A
   ;%0A%0Ades
@@ -2712,32 +2712,1067 @@
 n');%0A        %7D)%0A
+        .then(postInit)%0A        .then(commentInit) %0A        .then(logout)%0A        .then(done)%0A        .catch(done)%0A        .done();%0A    %7D);%0A%0A    it('should be added in right post', (done) =%3E %7B%0A      login('test@test.com', 'test')%0A        .then(() =%3E %7B%0A          return request%0A            .post('/api/v1/comments/')%0A            .send(%7B%0A              text: 'This is comment',%0A              pid: pid%0A            %7D)%0A            .toPromise();%0A        %7D)%0A        .then((res) =%3E %7B%0A          res.body.status.should.be.equal(0);%0A          res.body.value.should.exist;%0A          var cid = res.body.value;%0A          console.log(%22cid is %22 + cid);%0A%0A          return request%0A            .get('/api/v1/posts/' + pid)%0A            .toPromise();%0A        %7D)%0A        .then((res) =%3E %7B%0A          res.body.value.should.have.property('comments');%0A          res.body.value.comments.should.have.length.above(0);%0A          res.body.value.comments%5B0%5D.should.have.property('text', 'This is comment');%0A        %7D)%0A        .then(postInit)%0A        .then(commentInit)%0A
         .then(lo

add comment in the right post