'use strict';

var express = require('express');
var supertest = require('supertest');
var logger = require('log4js').getLogger('Unit-Test');
var testUtil = require('./common/testutil.js');
var message = require('../atmessager/views/message.js');

var nconf = require('nconf');
nconf.argv()
		 .env()
		 .file({ file: 'config.json' });

var app = testUtil.configExpress(express());

var request = supertest(app);

app.get('/message',message.healthCheck);
app.post('/message',message.sendMessage);

exports['Check view health'] = function(test){

	request.get('/message').end(function(err,res){

  logger.debug('The http status of message view: ' + res.statusCode );

  test.equal(err,null,'It should not had any error!');

	test.equal(res.statusCode,200,'It should return 200!');

	test.done();
});
}

exports['Test send message'] = {

	'Test send message success':function(test){

		var receiver = nconf.get('message').receiver;
		var botname  = nconf.get('message').botname;
		var sender = nconf.get('message').sender;
		var password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':receiver,
		'botname':botname,
		'sender':sender,
		'message':'Hello World!',
		'password':password})
		.end(function(err,res){

		  test.equal(err,null,'It should not had any error!')

			test.equal(res.statusCode,201,'It should return 201!');

			test.done();

		});
	},

	'Test send message failed(wrong receiver name)':function(test){

		var receiver = nconf.get('message').receiver;
		var botname  = nconf.get('message').botname;
		var sender = nconf.get('message').sender;
		var password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':'Hello Man',
		'botname':botname,
		'message':'Hello World!',
		'sender':sender,
		'password':password})
		.end(function(err,res){

			test.equal(res.statusCode,400,'It should return 400!');

			test.done();

		});
	},

	'Test send message failed(wrong bot name)':function(test){

		var receiver = nconf.get('message').receiver;
		var botname  = nconf.get('message').botname;
		var sender = nconf.get('message').sender;
		var password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':receiver,
		'botname':'noBot',
		'message':'Hello World!',
		'sender':sender,
		'password':password})
		.end(function(err,res){

			test.equal(res.statusCode,401,'It should return 401!');

			test.done();

		});
	},

	'Test send message failed(wrong sender)':function(test){

		var receiver = nconf.get('message').receiver;
		var botname  = nconf.get('message').botname;
		var sender = nconf.get('message').sender;
		var password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':receiver,
		'botname':botname,
		'message':'Hello World!',
		'sender':'nobody',
		'password':password})
		.end(function(err,res){

			test.equal(res.statusCode,406,'It should return 406!');

			test.done();

		});
	},

	'Test send message failed(password mismatch)':function(test){

		var receiver = nconf.get('message').receiver;
		var botname  = nconf.get('message').botname;
		var sender = nconf.get('message').sender;
		var password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':receiver,
		'botname':botname,
		'message':'Hello World!',
		'password':''})
		.end(function(err,res){

			test.equal(res.statusCode,402,'It should return 402!');

			test.done();

		});
	},

	'Test send message failed(miss message)':function(test){

		var receiver = nconf.get('message').receiver;
		var botname  = nconf.get('message').botname;
		var sender = nconf.get('message').sender;
		var password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':receiver,
		'botname':botname,
		'sender':sender,
		'password':password})
		.end(function(err,res){

			test.equal(res.statusCode,403,'It should return 403!');

			test.done();

		});
	}
}

@@ -1239,16 +1239,89 @@
 ,res)%7B%0A%0A
+%09%09%09logger.debug('The http status of message view: ' + res.statusCode );%0A%0A
 %09%09  test
@@ -1368,16 +1368,16 @@
 rror!')%0A
-
 %0A%09%09%09test
@@ -1882,32 +1882,105 @@
 tion(err,res)%7B%0A%0A
+%09%09%09logger.debug('The http status of message view: ' + res.statusCode );%0A%0A
 %09%09%09test.equal(re
@@ -2468,32 +2468,105 @@
 tion(err,res)%7B%0A%0A
+%09%09%09logger.debug('The http status of message view: ' + res.statusCode );%0A%0A
 %09%09%09test.equal(re
@@ -3054,32 +3054,105 @@
 tion(err,res)%7B%0A%0A
+%09%09%09logger.debug('The http status of message view: ' + res.statusCode );%0A%0A
 %09%09%09test.equal(re
@@ -3618,32 +3618,105 @@
 tion(err,res)%7B%0A%0A
+%09%09%09logger.debug('The http status of message view: ' + res.statusCode );%0A%0A
 %09%09%09test.equal(re
@@ -4147,32 +4147,32 @@
 ord':password%7D)%0A
-
 %09%09.end(function(
@@ -4182,16 +4182,89 @@
 ,res)%7B%0A%0A
+%09%09%09logger.debug('The http status of message view: ' + res.statusCode );%0A%0A
 %09%09%09test.

Add debug message