'use strict';

const assert = require('assert');
const config = require('../../built/helpers/loadConfig')();
const authorizeRequestModelAsync = require('../../built/models/authorizeRequest');
const routeRequest = require('../../built/routes/ice_auth/request');
const routeVerificationCode = require('../../built/routes/ice_auth/verification_code');
const routeAuthorize = require('../../built/routes/ice_auth/authorize');
const dbAsync = require('../../built/helpers/db');

describe('IceAuth API', () => {
	describe('/ice_auth', () => {
		// load collections
		let db, authorizeRequestModel;
		before(done => {
			(async () => {
				try {
					config.api.database = config.api.testDatabase;
					db = await dbAsync(config);
					authorizeRequestModel = await authorizeRequestModelAsync(db, config);

					await db.users.removeAsync();
					await db.applications.removeAsync();

					done();
				}
				catch(e) {
					done(e);
				}
			})();
		});

		// add general user, general application
		let user, app;
		beforeEach(done => {
			(async () => {
				try {
					let res = await db.users.createAsync({
						screenName: 'generaluser',
						passwordHash: 'abcdefg',
						name: 'froster',
						description: 'this is generaluser.'
					});
					user = res;

					res = await db.applications.createAsync({
						name: 'generalapp',
						description: 'this is generalapp.',
						permissions: []
					});
					app = res;

					done();
				}
				catch(e) {
					done(e);
				}
			})();
		});

		// remove all users, all applications
		afterEach(done => {
			(async () => {
				try {
					await db.users.removeAsync({});
					await db.applications.removeAsync({});

					done();
				}
				catch(e) {
					done(e);
				}
			})();
		});

		describe('/request', () => {
			describe('[POST]', () => {
				it('正しくリクエストされた場合は成功する', done => {
					(async () => {
						try {
							const applicationKey = await app.generateApplicationKeyAsync();

							let res = await routeRequest.post({
								body: {
									application_key: applicationKey
								}
							}, null, db, config);
							assert.equal(res.message, 'success');
							assert(await authorizeRequestModel.verifyKeyAsync(res.data.request_key));

							done();
						}
						catch(e) {
							done(e);
						}
					})();
				});
			});
		});

		describe('/verification_code', () => {
			describe('[GET]', () => {
				it('正しくリクエストされた場合は成功する', done => {
					(async () => {
						try {
							let res = await routeVerificationCode.get({
								body: {
									application_key: 'application_key_hoge',
									request_key: 'request_key_hoge'
								}
							}, null, db, config);

							assert.equal(res.message, 'success');

							assert.deepEqual(res.data, {
								verification_code: 'verification_code_hoge'
							});

							done();
						}
						catch(e) {
							done(e);
						}
					})();
				});
			});
		});

		describe('/authorize', () => {
			describe('[POST]', () => {
				it('正しくリクエストされた場合は成功する', done => {
					(async () => {
						try {
							let res = await routeAuthorize.post({
								body: {
									application_key: 'application_key_hoge',
									request_key: 'request_key_hoge',
									verification_code: 'verification_code_hoge'
								}
							}, null, db, config);

							assert.equal(res.message, 'success');

							assert.deepEqual(res.data, {
								access_key: 'access_key_hoge'
							});

							done();
						}
						catch(e) {
							done(e);
						}
					})();
				});
			});
		});
	});
});

@@ -2434,36 +2434,289 @@
 %09%09%09try %7B%0A%09%09%09%09%09%09%09
-let 
+const applicationKey = await app.generateApplicationKeyAsync();%0A%0A%09%09%09%09%09%09%09let res = await routeRequest.post(%7B%0A%09%09%09%09%09%09%09%09body: %7B%0A%09%09%09%09%09%09%09%09%09application_key: applicationKey%0A%09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%7D, null, db, config);%0A%09%09%09%09%09%09%09assert.equal(res.message, 'success');%0A%0A%09%09%09%09%09%09%09
 res = await rout
@@ -2773,33 +2773,32 @@
 pplication_key: 
-'
 application_key_
@@ -2788,34 +2788,27 @@
  application
-_key_hoge'
+Key
 ,%0A%09%09%09%09%09%09%09%09%09r
@@ -2811,33 +2811,41 @@
 %09%09%09request_key: 
-'
+res.data.
 request_key_hoge
@@ -2835,30 +2835,24 @@
 .request_key
-_hoge'
 %0A%09%09%09%09%09%09%09%09%7D%0A%09
@@ -2871,33 +2871,32 @@
 l, db, config);%0A
-%0A
 %09%09%09%09%09%09%09assert.eq
@@ -2916,33 +2916,32 @@
 ge, 'success');%0A
-%0A
 %09%09%09%09%09%09%09assert.de
@@ -2930,36 +2930,35 @@
 ;%0A%09%09%09%09%09%09%09assert.
-deep
+not
 Equal(res.data, 
@@ -2955,28 +2955,17 @@
 res.data
-, %7B%0A%09%09%09%09%09%09%09%09
+.
 verifica
@@ -2977,43 +2977,14 @@
 code
-: 'verification_code_hoge'%0A%09%09%09%09%09%09%09%7D
+, null
 );%0A%0A

update test