process.env.NODE_ENV = 'test';

var app = require('../../app');
var should = require('chai').should();
var request = require('supertest');
var User = require('../../models/User');
var Game = require('../../models/Game');
var Assignment = require('../../models/Assignment');
var Invite = require('../../models/Invite');
var fixtures = require('.././fixtures');
var mongoose = require('mongoose');

describe('/games', function() {
  var user, game, cookies;

  before(function(done) {
    mongoose.connect('mongodb://localhost/test');

    user = new User(fixtures.testUser);
    user.save(function(err) {
      request(app)
        .post('/sessions')
        .set('Accept', 'application/json')
        .send({id: user._id})
        .expect('Content-Type', /json/)
        .end(function(err, res) {
          cookies = res.headers['set-cookie'].pop().split(';')[0];

          game = new Game({
            players: [user._id]
          });
          game.save(done);
        });
    });
  });

  after(function(done) {
    User.remove({}, function() {
      Game.remove({}, function() {
        mongoose.disconnect(done);
      });
    });
  });

  describe('POST /games', function() {
    var userEmails = ['user1@example.com', 'user2@example.com'];
    var users = [
      new User({email: userEmails[0]}),
      new User({email: userEmails[1]})
    ];

    it('should create a new game with emails', function(done) {
      var req = request(app).post('/games');
        req.cookies = cookies;
        req.set('Accept', 'application/json')
        .send({ invites: userEmails })
        .expect(200)
        .end(function(err, res) {
          if (res.error) {
            throw new Error(res.error.text);
          }

          should.exist(res.body.players);
          res.body.players.length.should.equal(3);
          res.body.players.should.contain(user._id.toString());
          done();
        });
    });

    it('should create a new game with user ids', function(done) {
      var req = request(app).post('/games');
        req.cookies = cookies;
        req.set('Accept', 'application/json')
        .send({ players: [users[0]._id, users[1]._id] })
        .expect(200)
        .end(function(err, res) {
          should.not.exist(err);
          should.exist(res.body.players);
          res.body.players.length.should.equal(3);
          res.body.players.should.contain(user._id.toString());
          done();
        });
    });

    it('should not create a game with less than three players', function(done) {
      var req = request(app).post('/games');
        req.cookies = cookies;
        req.set('Accept', 'application/json')
        .send({ invites: [userEmails[0]] })
        .expect(400)
        .end(done);
    });
  });

  describe.skip('old tests', function() {
    it('should add a player to a game', function(done) {
      var req = request(app).post('/games/' + game._id + '/players');
      req.cookies = cookies;
      req.set('Accept', 'application/json')
        .expect(200)
        .end(function(err, res) {
          should.not.exist(err);
          should.exist(res.body._id);
          res.body._id.should.equal(game._id.toString());
          done();
        });
    });

    it('should create invites for a new game', function(done) {
      var req = request(app).post('/games');
      req.cookies = cookies;
      req.set('Accept', 'application/json')
        .send({
          invites: [
            'a@example.com',
            'b@example.com'
          ]
        })
        .expect(200)
        .end(function(err, res) {
          should.not.exist(err);
          should.exist(res.body._id);

          Invite.find({game: res.body._id}, function(err, invites) {
            should.not.exist(err);
            should.exist(invites);

            invites.length.should.equal(2);
            invites[0].email.should.equal('a@example.com');
            invites[1].email.should.equal('b@example.com');

            done();
          });
        });
    });

    describe('/assignments', function() {
      var assignment, target;

      before(function(done) {
        target = new User();
        assignment = new Assignment();

        target.save(function(err) {
          if (err) { console.error(err); }
          assignment.assassin = user._id;
          assignment.target = target._id;
          assignment.game = game._id;

          assignment.save(done);
        });
      });

      after(function(done) {
        Assignment.remove({}, done);
      });

      it('should list the assignments for a game', function(done) {
        var req = request(app).get('/games/' + game._id + '/assignments')
        req.cookies = cookies;
        req.set('Accept', 'application/json')
          .expect(200)
          .end(function(err, res) {
            should.exist(res.body.gameId);
            should.exist(res.body.assignments);
            should.exist(res.body.assignments[0]);
            res.body.assignments[0]._id.should.equal(assignment._id.toString());
            done(err);
          });
      });
    });
  });

});

@@ -2219,38 +2219,90 @@
         
-should.not.exist(err);
+if (res.error) %7B%0A            throw new Error(res.error.text);%0A          %7D%0A
 %0A       
@@ -2754,24 +2754,24 @@
 expect(400)%0A
+
         .end
@@ -2797,2329 +2797,8 @@
 );%0A%0A
-  describe.skip('old tests', function() %7B%0A    it('should add a player to a game', function(done) %7B%0A      var req = request(app).post('/games/' + game._id + '/players');%0A      req.cookies = cookies;%0A      req.set('Accept', 'application/json')%0A        .expect(200)%0A        .end(function(err, res) %7B%0A          should.not.exist(err);%0A          should.exist(res.body._id);%0A          res.body._id.should.equal(game._id.toString());%0A          done();%0A        %7D);%0A    %7D);%0A%0A    it('should create invites for a new game', function(done) %7B%0A      var req = request(app).post('/games');%0A      req.cookies = cookies;%0A      req.set('Accept', 'application/json')%0A        .send(%7B%0A          invites: %5B%0A            'a@example.com',%0A            'b@example.com'%0A          %5D%0A        %7D)%0A        .expect(200)%0A        .end(function(err, res) %7B%0A          should.not.exist(err);%0A          should.exist(res.body._id);%0A%0A          Invite.find(%7Bgame: res.body._id%7D, function(err, invites) %7B%0A            should.not.exist(err);%0A            should.exist(invites);%0A%0A            invites.length.should.equal(2);%0A            invites%5B0%5D.email.should.equal('a@example.com');%0A            invites%5B1%5D.email.should.equal('b@example.com');%0A%0A            done();%0A          %7D);%0A        %7D);%0A    %7D);%0A%0A    describe('/assignments', function() %7B%0A      var assignment, target;%0A%0A      before(function(done) %7B%0A        target = new User();%0A        assignment = new Assignment();%0A%0A        target.save(function(err) %7B%0A          if (err) %7B console.error(err); %7D%0A          assignment.assassin = user._id;%0A          assignment.target = target._id;%0A          assignment.game = game._id;%0A%0A          assignment.save(done);%0A        %7D);%0A      %7D);%0A%0A      after(function(done) %7B%0A        Assignment.remove(%7B%7D, done);%0A      %7D);%0A%0A      it('should list the assignments for a game', function(done) %7B%0A        var req = request(app).get('/games/' + game._id + '/assignments')%0A        req.cookies = cookies;%0A        req.set('Accept', 'application/json')%0A          .expect(200)%0A          .end(function(err, res) %7B%0A            should.exist(res.body.gameId);%0A            should.exist(res.body.assignments);%0A            should.exist(res.body.assignments%5B0%5D);%0A            res.body.assignments%5B0%5D._id.should.equal(assignment._id.toString());%0A            done(err);%0A          %7D);%0A      %7D);%0A    %7D);%0A  %7D);%0A%0A
 %7D);%0A

remove outdated tests