/* global require, module, process, console, __dirname */
/* jshint -W097 */
'use strict';

var
  serverRootDir = __dirname + '/../../',
  mocha         = require('mocha'),
  expect        = require('chai').expect,
  request       = require('supertest'),
  _             = require('underscore'),
  app           = require(serverRootDir + 'app').app;


describe('api/media', function() {

  var firstRecord;


  // -- GET ALL ---------------------------------

  describe('Get all media objects -> GET /api/media', function() {

    it('responds with json', function(done) {
      request(app)
        .get('/api/media')
        .set('Accept', 'application/json')
        .expect('Content-Type', /application\/json/)
        .expect(200, done);
    });


    it('returns an array of media objects', function(done) {
      request(app)
        .get('/api/media')
        .set('Accept', 'application/json')
        .end(function(err, res) {

          expect(err).to.not.exist;

          expect(_.isArray(res.body)).to.be.true;

          expect(res.body).to.have.length.of.at.least(2);

          // save a reference for later usage
          firstRecord = res.body[0];

          expect(firstRecord).to.have.property('_id');
          expect(firstRecord).to.have.property('name');
          expect(firstRecord).to.have.property('description');
          expect(firstRecord).to.have.property('url');
          expect(firstRecord).to.have.property('active');

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


  // -- GET ONE ---------------------------------

  describe('Get one media object -> GET /api/media/:id', function() {

    it('responds with json', function(done) {
      request(app)
        .get('/api/media/'+firstRecord._id)
        .set('Accept', 'application/json')
        .expect('Content-Type', /application\/json/)
        .expect(200, done);
    });


    it('retuns a 404 error if the model does not exist', function(done) {
      request(app)
        .get('/api/media/a-non-existing-record-id')
        .expect(404, done);
    });


    it('returns a Media object', function(done) {
      request(app)
        .get('/api/media/'+firstRecord._id)
        .set('Accept', 'application/json')
        .end(function(err, res) {

          expect(err).to.not.exist;

          var model = res.body;

          expect(model).to.be.an('object');

          expect(model).to.have.property('_id');
          expect(model).to.have.property('name');
          expect(model).to.have.property('description');
          expect(model).to.have.property('url');
          expect(model).to.have.property('active');

          expect(model._id).not.to.be.null;

          done();
        });
    });

  });


  // -- CREATE ----------------------------------
  describe('Create a new media object -> POST /api/media', function() {

/*
    it('responds with json', function(done) {
      request(app)
        .post('/api/media/'+firstRecord._id)
        .set('Accept', 'application/json')
        .expect('Content-Type', /application\/json/)
        .expect(200, done);
    });
*/



  });


  // -- UPDATE ----------------------------------
  describe('Update a media object -> PUT /api/media/:id', function() {

    /*
    it('responds with json', function(done) {
      request(app)
        .put('/api/media/'+firstRecord._id)
        .set('Accept', 'application/json')
        .expect('Content-Type', /application\/json/)
        .expect(200, done);
    });
    */

    it('retuns a 404 error if the model does not exist', function(done) {
      request(app)
        .put('/api/media/a-non-existing-record-id')
        .expect(404, done);
    });



  });


  // -- DELETE ----------------------------------
  describe('Delete a media object -> DELETE /api/media/:id', function() {

    var deletedModel;

    it('responds with json', function(done) {
      request(app)
        .delete('/api/media/a-non-existing-record-id')
        .set('Accept', 'application/json')
        .expect('Content-Type', /application\/json/)
        .expect(404, done);
    });


    it('retuns a 404 error if the model does not exist', function(done) {
      request(app)
        .delete('/api/media/a-non-existing-record-id')
        .expect(404, done);
    });


    it('returns the deleted model', function(done) {
      request(app)
        .delete('/api/media/'+firstRecord._id)
        .end(function(err, res) {

          expect(err).to.not.exist;

          deletedModel = res.body;

          expect(deletedModel).to.be.an('object');

          expect(deletedModel).to.have.property('_id');
          expect(deletedModel).to.have.property('name');
          expect(deletedModel).to.have.property('description');
          expect(deletedModel).to.have.property('url');
          expect(deletedModel).to.have.property('active');

          expect(deletedModel._id).not.to.be.null;

          done();
        });
    });


    it('deletes the requested model', function(done) {
      request(app).get('/api/media/'+deletedModel._id).expect(404, done);
    });


  });


});

@@ -398,24 +398,253 @@
 stRecord;%0A%0A%0A
+  before(function(done) %7B%0A    // populate the database (the dev. one, this is only for development purposes!)%0A    var exec = require('child_process').exec;%0A%0A    this.timeout(2000);%0A    exec('grunt mongo:populate', done);%0A  %7D);%0A%0A%0A
   // -- GET 
@@ -2972,32 +2972,33 @@
 ---------------%0A
+%0A
   describe('Crea
@@ -3355,32 +3355,33 @@
 ---------------%0A
+%0A
   describe('Upda
@@ -3934,16 +3934,17 @@
 -------%0A
+%0A
   descri

Reset the dev. database before executing the api tests