/*global describe, it, before, beforeEach, after, afterEach */

var request = require('request'),
  should = require('should'),
  fs = require('fs'),
  config = require('../config.json');

describe('Trips API', function () {

  // start server
  before(function (done) {
    this.app = require('../index');
    this.server = this.app.listen(8888, done);
  });

  // empty db
  before(function (done) {
    this.timeout(10000);
    require('../models/database').empty(done);
  });

  // create some fixtures
  before(function (done) {
    require('../models/database').batch([
      { type: 'put', key: 'trip_123', value: {id: '123', any: 'field'} },
      { type: 'put', key: 'trip_1234', value: {id: '1234', any: 'field'} },
      { type: 'put', key: 'trip_12345', value: {id: '12345', any: 'field'} },
      { type: 'put', key: 'driver_123', value: {id: '123', any: 'field'} }
    ], done);
  });

  describe('GET /trips', function () {
    it('respond with list of trips', function (done) {
      request({
        url: 'http://localhost:8888/trips',
        json: true
      }, function (err, res, body) {
        should.not.exist(err);
        res.statusCode.should.equal(200);
        body.length.should.equal(3);
        done();
      });
    });
  });
  describe('POST /trips', function () {

    it('need auth', function (done) {

      var formData = {
        some: 'fields'
      };

      request({
        method: 'POST',
        url: 'http://localhost:8888/admin/trips',
        formData: formData
      }, function (err, res, body) {
        should.not.exist(err);
        res.statusCode.should.equal(401);
        done();
      });
    });

    it('create doc', function (done) {
      var context = this;
      var req = request({
        method: 'POST',
        auth: {
          user: config.adminuser,
          pass: config.adminpass
        },
        url: 'http://localhost:8888/admin/trips',
        form: true
      }, function (err, res, body) {
        should.not.exist(err);
        res.statusCode.should.equal(200);
        body = JSON.parse(body);
        body.should.have.property('id');
        context.id = body.id;
        body.should.have.property('attachments');
        body.should.have.property('some', 'fields');
        body.should.not.have.property('attachment1.json');
        done();
      });

      var form = req.form();
      form.append('some', 'fields');
      form.append('what', 'ever');
      form.append("attachment1.json", fs.createReadStream('./package.json'));
      form.append("attachment2.js", fs.createReadStream('./server/index.js'));

    });

  });

  describe('GET /trips/id/attachment', function () {

    it('works', function (done) {
      var url = 'http://localhost:8888/trips/' + this.id + '/index.js';
      request(url, function (err, res, body) {
        should.not.exist(err);
        res.statusCode.should.equal(200);
        done();
      });
    });

  });

  describe('DELETE /admin/trips/:id', function () {
    it('works', function (done) {
      request({
        method: 'DELETE',
        auth: {
          user: config.adminuser,
          pass: config.adminpass
        },
        url: 'http://localhost:8888/admin/trips/' + this.id,
        form: true
      }, function (err, res, body) {
        should.not.exist(err);
        res.statusCode.should.equal(204);
        done();
      });
    });
  });

});
@@ -219,16 +219,85 @@
 n () %7B%0A%0A
+  // place it here to use in various tests%0A  var testTripID = null;%0A%0A
   // sta
@@ -306,16 +306,16 @@
  server%0A
-
   before
@@ -1355,32 +1355,33 @@
  function () %7B%0A%0A
+%0A
     it('need aut
@@ -1764,34 +1764,8 @@
 ) %7B%0A
-      var context = this;%0A
     
@@ -2169,18 +2169,18 @@
     
-context.id
+testTripID
  = b
@@ -2789,22 +2789,25 @@
 ps/' + t
-his.id
+estTripID
  + '/ind
@@ -3255,14 +3255,17 @@
  + t
-his.id
+estTripID
 ,%0A  

fix mocha version