'use strict';

var should = require('should'),
  request = require('supertest'),
  path = require('path'),
  mongoose = require('mongoose'),
  User = mongoose.model('User'),
  infrastructureRequest = mongoose.model('infrastructureRequest'),
  express = require(path.resolve('./config/lib/express'));

/**
 * Globals
 */
var app, agent, credentials, user, infrastructureRequest;

/**
 * infrastructureRequest routes tests
 */
describe('infrastructureRequest CRUD tests', function () {

  before(function (done) {
    // Get application
    app = express.init(mongoose);
    agent = request.agent(app);

    done();
  });

  beforeEach(function (done) {
    // Create user credentials
    credentials = {
      username: 'username',
      password: 'M3@n.jsI$Aw3$0m3'
    };

    // Create a new user
    user = new User({
      firstName: 'Full',
      lastName: 'Name',
      displayName: 'Full Name',
      email: 'test@test.com',
      username: credentials.username,
      password: credentials.password,
      provider: 'local'
    });

    // Save a user to the test db and create new infrastructureRequest
    user.save(function () {
      infrastructureRequest = {
        title: 'infrastructureRequest Title',
        content: 'infrastructureRequest Content'
      };

      done();
    });
  });

  it('should be able to save an infrastructureRequest if logged in', function (done) {
    agent.post('/api/auth/signin')
      .send(credentials)
      .expect(200)
      .end(function (signinErr, signinRes) {
        // Handle signin error
        if (signinErr) {
          return done(signinErr);
        }

        // Get the userId
        var userId = user.id;

        // Save a new infrastructureRequest
        agent.post('/api/infrastructureRequests')
          .send(infrastructureRequest)
          .expect(200)
          .end(function (infrastructureRequestSaveErr, infrastructureRequestSaveRes) {
            // Handle infrastructureRequest save error
            if (infrastructureRequestSaveErr) {
              return done(infrastructureRequestSaveErr);
            }

            // Get a list of infrastructureRequests
            agent.get('/api/infrastructureRequests')
              .end(function (infrastructureRequestsGetErr, infrastructureRequestsGetRes) {
                // Handle infrastructureRequest save error
                if (infrastructureRequestsGetErr) {
                  return done(infrastructureRequestsGetErr);
                }

                // Get infrastructureRequests list
                var infrastructureRequests = infrastructureRequestsGetRes.body;

                // Set assertions
                (infrastructureRequests[0].user._id).should.equal(userId);
                (infrastructureRequests[0].title).should.match('infrastructureRequest Title');

                // Call the assertion callback
                done();
              });
          });
      });
  });

  it('should not be able to save an infrastructureRequest if not logged in', function (done) {
    agent.post('/api/infrastructureRequests')
      .send(infrastructureRequest)
      .expect(403)
      .end(function (infrastructureRequestSaveErr, infrastructureRequestSaveRes) {
        // Call the assertion callback
        done(infrastructureRequestSaveErr);
      });
  });

  it('should not be able to save an infrastructureRequest if no title is provided', function (done) {
    // Invalidate title field
    infrastructureRequest.title = '';

    agent.post('/api/auth/signin')
      .send(credentials)
      .expect(200)
      .end(function (signinErr, signinRes) {
        // Handle signin error
        if (signinErr) {
          return done(signinErr);
        }

        // Get the userId
        var userId = user.id;

        // Save a new infrastructureRequest
        agent.post('/api/infrastructureRequests')
          .send(infrastructureRequest)
          .expect(400)
          .end(function (infrastructureRequestSaveErr, infrastructureRequestSaveRes) {
            // Set message assertion
            (infrastructureRequestSaveRes.body.message).should.match('Title cannot be blank');

            // Handle infrastructureRequest save error
            done(infrastructureRequestSaveErr);
          });
      });
  });

  it('should be able to update an infrastructureRequest if signed in', function (done) {
    agent.post('/api/auth/signin')
      .send(credentials)
      .expect(200)
      .end(function (signinErr, signinRes) {
        // Handle signin error
        if (signinErr) {
          return done(signinErr);
        }

        // Get the userId
        var userId = user.id;

        // Save a new infrastructureRequest
        agent.post('/api/infrastructureRequests')
          .send(infrastructureRequest)
          .expect(200)
          .end(function (infrastructureRequestSaveErr, infrastructureRequestSaveRes) {
            // Handle infrastructureRequest save error
            if (infrastructureRequestSaveErr) {
              return done(infrastructureRequestSaveErr);
            }

            // Update infrastructureRequest title
            infrastructureRequest.title = 'WHY YOU GOTTA BE SO MEAN?';

            // Update an existing infrastructureRequest
            agent.put('/api/infrastructureRequests/' + infrastructureRequestSaveRes.body._id)
              .send(infrastructureRequest)
              .expect(200)
              .end(function (infrastructureRequestUpdateErr, infrastructureRequestUpdateRes) {
                // Handle infrastructureRequest update error
                if (infrastructureRequestUpdateErr) {
                  return done(infrastructureRequestUpdateErr);
                }

                // Set assertions
                (infrastructureRequestUpdateRes.body._id).should.equal(infrastructureRequestSaveRes.body._id);
                (infrastructureRequestUpdateRes.body.title).should.match('WHY YOU GOTTA BE SO MEAN?');

                // Call the assertion callback
                done();
              });
          });
      });
  });

  it('should be able to get a list of infrastructureRequests if not signed in', function (done) {
    // Create new infrastructureRequest model instance
    var infrastructureRequestObj = new infrastructureRequest(infrastructureRequest);

    // Save the infrastructureRequest
    infrastructureRequestObj.save(function () {
      // Request infrastructureRequests
      request(app).get('/api/infrastructureRequests')
        .end(function (req, res) {
          // Set assertion
          res.body.should.be.instanceof(Array).and.have.lengthOf(1);

          // Call the assertion callback
          done();
        });

    });
  });

  it('should be able to get a single infrastructureRequest if not signed in', function (done) {
    // Create new infrastructureRequest model instance
    var infrastructureRequestObj = new infrastructureRequest(infrastructureRequest);

    // Save the infrastructureRequest
    infrastructureRequestObj.save(function () {
      request(app).get('/api/infrastructureRequests/' + infrastructureRequestObj._id)
        .end(function (req, res) {
          // Set assertion
          res.body.should.be.instanceof(Object).and.have.property('title', infrastructureRequest.title);

          // Call the assertion callback
          done();
        });
    });
  });

  it('should return proper error for single infrastructureRequest with an invalid Id, if not signed in', function (done) {
    // test is not a valid mongoose Id
    request(app).get('/api/infrastructureRequests/test')
      .end(function (req, res) {
        // Set assertion
        res.body.should.be.instanceof(Object).and.have.property('message', 'infrastructureRequest is invalid');

        // Call the assertion callback
        done();
      });
  });

  it('should return proper error for single infrastructureRequest which doesnt exist, if not signed in', function (done) {
    // This is a valid mongoose Id but a non-existent infrastructureRequest
    request(app).get('/api/infrastructureRequests/559e9cd815f80b4c256a8f41')
      .end(function (req, res) {
        // Set assertion
        res.body.should.be.instanceof(Object).and.have.property('message', 'No infrastructureRequest with that identifier has been found');

        // Call the assertion callback
        done();
      });
  });

  it('should be able to delete an infrastructureRequest if signed in', function (done) {
    agent.post('/api/auth/signin')
      .send(credentials)
      .expect(200)
      .end(function (signinErr, signinRes) {
        // Handle signin error
        if (signinErr) {
          return done(signinErr);
        }

        // Get the userId
        var userId = user.id;

        // Save a new infrastructureRequest
        agent.post('/api/infrastructureRequests')
          .send(infrastructureRequest)
          .expect(200)
          .end(function (infrastructureRequestSaveErr, infrastructureRequestSaveRes) {
            // Handle infrastructureRequest save error
            if (infrastructureRequestSaveErr) {
              return done(infrastructureRequestSaveErr);
            }

            // Delete an existing infrastructureRequest
            agent.delete('/api/infrastructureRequests/' + infrastructureRequestSaveRes.body._id)
              .send(infrastructureRequest)
              .expect(200)
              .end(function (infrastructureRequestDeleteErr, infrastructureRequestDeleteRes) {
                // Handle infrastructureRequest error error
                if (infrastructureRequestDeleteErr) {
                  return done(infrastructureRequestDeleteErr);
                }

                // Set assertions
                (infrastructureRequestDeleteRes.body._id).should.equal(infrastructureRequestSaveRes.body._id);

                // Call the assertion callback
                done();
              });
          });
      });
  });

  it('should not be able to delete an infrastructureRequest if not signed in', function (done) {
    // Set infrastructureRequest user
    infrastructureRequest.user = user;

    // Create new infrastructureRequest model instance
    var infrastructureRequestObj = new infrastructureRequest(infrastructureRequest);

    // Save the infrastructureRequest
    infrastructureRequestObj.save(function () {
      // Try deleting infrastructureRequest
      request(app).delete('/api/infrastructureRequests/' + infrastructureRequestObj._id)
        .expect(403)
        .end(function (infrastructureRequestDeleteErr, infrastructureRequestDeleteRes) {
          // Set message assertion
          (infrastructureRequestDeleteRes.body.message).should.match('User is not authorized');

          // Handle infrastructureRequest error error
          done(infrastructureRequestDeleteErr);
        });

    });
  });

  afterEach(function (done) {
    User.remove().exec(function () {
      infrastructureRequest.remove().exec(done);
    });
  });
});
