'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 password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':receiver,
		'botname':botname,
		'message':'Hello World!',
		'password':password,
		'testMode':true})
		.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 password = nconf.get('message').password;

		request.post('/message')
		.send({'receiver':'Hello Man',
		'botname':botname,
		'message':'Hello World!',
		'password':password,
	  'testMode':true})
		.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 password = nconf.get('message').password;

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

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

			test.done();

		});
	},

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

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

		request.post('/message')
		.send({'receiver':receiver,
		'botname':botname,
		'message':'Hello World!',
		'password':'',
		'testMode':true})
		.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 password = nconf.get('message').password;

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

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

			test.done();

		});
	}
}

@@ -962,32 +962,80 @@
 sage').botname;%0A
+%09%09var username = nconf.get('message').username;%0A
 %09%09var password =
@@ -1058,32 +1058,32 @@
 age').password;%0A
-
 %0A%09%09request.post(
@@ -1137,32 +1137,55 @@
 tname':botname,%0A
+%09%09'username':username,%0A
 %09%09'message':'Hel
@@ -1217,35 +1217,16 @@
 password
-,%0A%09%09'testMode':true
 %7D)%0A%09%09.en
@@ -1703,32 +1703,55 @@
 'Hello World!',%0A
+%09%09'username':username,%0A
 %09%09'password':pas
@@ -1755,36 +1755,16 @@
 password
-,%0A%09  'testMode':true
 %7D)%0A%09%09.en
@@ -2176,32 +2176,55 @@
 'Hello World!',%0A
+%09%09'username':username,%0A
 %09%09'password':pas
@@ -2228,36 +2228,16 @@
 password
-,%0A%09  'testMode':true
 %7D)%0A%09%09.en
@@ -2679,27 +2679,8 @@
 ':''
-,%0A%09%09'testMode':true
 %7D)%0A%09
@@ -3070,24 +3070,47 @@
 e':botname,%0A
+%09%09'username':username,%0A
 %09%09'password'
@@ -3122,28 +3122,8 @@
 word
-,%0A%09  'testMode':true
 %7D)%0A%09

Remove test mode and add username value to post