'use strict';

var should = require('should'),
	request = require('supertest'),
	app = require('../../server'),
	mongoose = require('mongoose'),
	User = mongoose.model('User'),
	Questionresponse = mongoose.model('Questionresponse'),
	agent = request.agent(app);

/**
 * Globals
 */
var credentials, user, questionresponse;

/**
 * Questionresponse routes tests
 */
describe('Questionresponse CRUD tests', function() {
	beforeEach(function(done) {
		// Create user credentials
		credentials = {
			username: 'username',
			password: 'password'
		};

		// 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 Questionresponse
		user.save(function() {
			questionresponse = {
				name: 'Questionresponse Name'
			};

			done();
		});
	});

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

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

				// Save a new Questionresponse
				agent.post('/questionresponses')
					.send(questionresponse)
					.expect(200)
					.end(function(questionresponseSaveErr, questionresponseSaveRes) {
						// Handle Questionresponse save error
						if (questionresponseSaveErr) done(questionresponseSaveErr);

						// Get a list of Questionresponses
						agent.get('/questionresponses')
							.end(function(questionresponsesGetErr, questionresponsesGetRes) {
								// Handle Questionresponse save error
								if (questionresponsesGetErr) done(questionresponsesGetErr);

								// Get Questionresponses list
								var questionresponses = questionresponsesGetRes.body;

								// Set assertions
								(questionresponses[0].user._id).should.equal(userId);
								(questionresponses[0].name).should.match('Questionresponse Name');

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

	it('should not be able to save Questionresponse instance if not logged in', function(done) {
		agent.post('/questionresponses')
			.send(questionresponse)
			.expect(401)
			.end(function(questionresponseSaveErr, questionresponseSaveRes) {
				// Call the assertion callback
				done(questionresponseSaveErr);
			});
	});

	it('should not be able to save Questionresponse instance if no name is provided', function(done) {
		// Invalidate name field
		questionresponse.name = '';

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

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

				// Save a new Questionresponse
				agent.post('/questionresponses')
					.send(questionresponse)
					.expect(400)
					.end(function(questionresponseSaveErr, questionresponseSaveRes) {
						// Set message assertion
						(questionresponseSaveRes.body.message).should.match('Please fill Questionresponse name');
						
						// Handle Questionresponse save error
						done(questionresponseSaveErr);
					});
			});
	});

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

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

				// Save a new Questionresponse
				agent.post('/questionresponses')
					.send(questionresponse)
					.expect(200)
					.end(function(questionresponseSaveErr, questionresponseSaveRes) {
						// Handle Questionresponse save error
						if (questionresponseSaveErr) done(questionresponseSaveErr);

						// Update Questionresponse name
						questionresponse.name = 'WHY YOU GOTTA BE SO MEAN?';

						// Update existing Questionresponse
						agent.put('/questionresponses/' + questionresponseSaveRes.body._id)
							.send(questionresponse)
							.expect(200)
							.end(function(questionresponseUpdateErr, questionresponseUpdateRes) {
								// Handle Questionresponse update error
								if (questionresponseUpdateErr) done(questionresponseUpdateErr);

								// Set assertions
								(questionresponseUpdateRes.body._id).should.equal(questionresponseSaveRes.body._id);
								(questionresponseUpdateRes.body.name).should.match('WHY YOU GOTTA BE SO MEAN?');

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

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

		// Save the Questionresponse
		questionresponseObj.save(function() {
			// Request Questionresponses
			request(app).get('/questionresponses')
				.end(function(req, res) {
					// Set assertion
					res.body.should.be.an.Array.with.lengthOf(1);

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

		});
	});


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

		// Save the Questionresponse
		questionresponseObj.save(function() {
			request(app).get('/questionresponses/' + questionresponseObj._id)
				.end(function(req, res) {
					// Set assertion
					res.body.should.be.an.Object.with.property('name', questionresponse.name);

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

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

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

				// Save a new Questionresponse
				agent.post('/questionresponses')
					.send(questionresponse)
					.expect(200)
					.end(function(questionresponseSaveErr, questionresponseSaveRes) {
						// Handle Questionresponse save error
						if (questionresponseSaveErr) done(questionresponseSaveErr);

						// Delete existing Questionresponse
						agent.delete('/questionresponses/' + questionresponseSaveRes.body._id)
							.send(questionresponse)
							.expect(200)
							.end(function(questionresponseDeleteErr, questionresponseDeleteRes) {
								// Handle Questionresponse error error
								if (questionresponseDeleteErr) done(questionresponseDeleteErr);

								// Set assertions
								(questionresponseDeleteRes.body._id).should.equal(questionresponseSaveRes.body._id);

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

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

		// Create new Questionresponse model instance
		var questionresponseObj = new Questionresponse(questionresponse);

		// Save the Questionresponse
		questionresponseObj.save(function() {
			// Try deleting Questionresponse
			request(app).delete('/questionresponses/' + questionresponseObj._id)
			.expect(401)
			.end(function(questionresponseDeleteErr, questionresponseDeleteRes) {
				// Set message assertion
				(questionresponseDeleteRes.body.message).should.match('User is not logged in');

				// Handle Questionresponse error error
				done(questionresponseDeleteErr);
			});

		});
	});

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