'use strict';

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

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

/**
 * hostName routes tests
 */
describe('hostName 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 hostName
    user.save(function () {
      hostName = {
        title: 'hostName Title',
        content: 'hostName Content'
      };

      done();
    });
  });

  it('should be able to save an hostName 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 hostName
        agent.post('/api/hostNames')
          .send(hostName)
          .expect(200)
          .end(function (hostNameSaveErr, hostNameSaveRes) {
            // Handle hostName save error
            if (hostNameSaveErr) {
              return done(hostNameSaveErr);
            }

            // Get a list of hostNames
            agent.get('/api/hostNames')
              .end(function (hostNamesGetErr, hostNamesGetRes) {
                // Handle hostName save error
                if (hostNamesGetErr) {
                  return done(hostNamesGetErr);
                }

                // Get hostNames list
                var hostNames = hostNamesGetRes.body;

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

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

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

  it('should not be able to save an hostName if no title is provided', function (done) {
    // Invalidate title field
    hostName.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 hostName
        agent.post('/api/hostNames')
          .send(hostName)
          .expect(400)
          .end(function (hostNameSaveErr, hostNameSaveRes) {
            // Set message assertion
            (hostNameSaveRes.body.message).should.match('Title cannot be blank');

            // Handle hostName save error
            done(hostNameSaveErr);
          });
      });
  });

  it('should be able to update an hostName 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 hostName
        agent.post('/api/hostNames')
          .send(hostName)
          .expect(200)
          .end(function (hostNameSaveErr, hostNameSaveRes) {
            // Handle hostName save error
            if (hostNameSaveErr) {
              return done(hostNameSaveErr);
            }

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

            // Update an existing hostName
            agent.put('/api/hostNames/' + hostNameSaveRes.body._id)
              .send(hostName)
              .expect(200)
              .end(function (hostNameUpdateErr, hostNameUpdateRes) {
                // Handle hostName update error
                if (hostNameUpdateErr) {
                  return done(hostNameUpdateErr);
                }

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

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

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

    // Save the hostName
    hostNameObj.save(function () {
      // Request hostNames
      request(app).get('/api/hostNames')
        .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 hostName if not signed in', function (done) {
    // Create new hostName model instance
    var hostNameObj = new hostName(hostName);

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

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

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

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

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

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

  it('should be able to delete an hostName 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 hostName
        agent.post('/api/hostNames')
          .send(hostName)
          .expect(200)
          .end(function (hostNameSaveErr, hostNameSaveRes) {
            // Handle hostName save error
            if (hostNameSaveErr) {
              return done(hostNameSaveErr);
            }

            // Delete an existing hostName
            agent.delete('/api/hostNames/' + hostNameSaveRes.body._id)
              .send(hostName)
              .expect(200)
              .end(function (hostNameDeleteErr, hostNameDeleteRes) {
                // Handle hostName error error
                if (hostNameDeleteErr) {
                  return done(hostNameDeleteErr);
                }

                // Set assertions
                (hostNameDeleteRes.body._id).should.equal(hostNameSaveRes.body._id);

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

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

    // Create new hostName model instance
    var hostNameObj = new hostName(hostName);

    // Save the hostName
    hostNameObj.save(function () {
      // Try deleting hostName
      request(app).delete('/api/hostNames/' + hostNameObj._id)
        .expect(403)
        .end(function (hostNameDeleteErr, hostNameDeleteRes) {
          // Set message assertion
          (hostNameDeleteRes.body.message).should.match('User is not authorized');

          // Handle hostName error error
          done(hostNameDeleteErr);
        });

    });
  });

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