const chai = require('chai');
const chaiHttp = require('chai-http');
const jwt = require('jsonwebtoken');
const app = require('../../../src/app');
const testObjects = require('../helpers/testObjects')(app);

const accountService = app.service('accounts');

const { logger } = app;
const should = chai.should();

chai.use(chaiHttp);

describe('General login service', () => {
	const testAccount = {
		username: 'poweruser@mail.schul.tech',
		password: 'passwordA',
	};

	before(() => testObjects.createTestUser()
		.then((testUser) => testObjects.createTestAccount(testAccount, null, testUser)));

	it('should get a JWT which includes accountId', () => new Promise((resolve, reject) => {
		chai.request(app)
			.post('/authentication')
			.set('Accept', 'application/json')
			.set('content-type', 'application/x-www-form-urlencoded')
		// send credentials
			.send({
				username: testAccount.username,
				password: testAccount.password,
				strategy: 'local',
			})
			.end((err, res) => {
				if (err) {
					reject(err);
					return;
				}

				const decodedToken = jwt.decode(res.body.accessToken);

				// get the account id from JWT
				decodedToken.should.have.property('accountId');

				accountService.get(decodedToken.accountId)
					.then((account) => {
						account.username.should.equal(testAccount.username);
						resolve();
					})
					.catch((error) => {
						logger.error(`failed to get the account from the service: ${error}`);
						// throw error;
						reject(error);
						// done();
					});

				resolve();
			});
	}));

	it('should not get a JWT with wrong credentials', () => new Promise((resolve, reject) => {
		chai.request(app)
			.post('/authentication')
			.set('Accept', 'application/json')
			.set('content-type', 'application/x-www-form-urlencoded')
		// send credentials
			.send({
				username: testAccount.username,
				password: `${testAccount.password}a`,
				strategy: 'local',
			})
			.end((err, res) => {
				if (err) {
					reject(err);
					return;
				}

				const decodedToken = jwt.decode(res.body.accessToken);

				// JWT should not exist
				should.equal(decodedToken, null);

				resolve();
			});
	}));

	after((done) => {
		testObjects.cleanup()
			.then(() => {
				done();
			})
			.catch((error) => {
				logger.error(`Could not remove test account(s): ${error}`);
				done();
			});
	});
});

@@ -1539,24 +1539,1009 @@
 %09%7D);%0A%09%7D));%0A%0A
+%09it('should get a JWT when credentials are padded in spaces', () =%3E new Promise((resolve, reject) =%3E %7B%0A%09%09chai.request(app)%0A%09%09%09.post('/authentication')%0A%09%09%09.set('Accept', 'application/json')%0A%09%09%09.set('content-type', 'application/x-www-form-urlencoded')%0A%09%09// send credentials%0A%09%09%09.send(%7B%0A%09%09%09%09username: %60     $%7BtestAccount.username%7D %60,%0A%09%09%09%09password: %60  $%7BtestAccount.password%7D %60,%0A%09%09%09%09strategy: 'local',%0A%09%09%09%7D)%0A%09%09%09.end((err, res) =%3E %7B%0A%09%09%09%09if (err) %7B%0A%09%09%09%09%09reject(err);%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%0A%09%09%09%09const decodedToken = jwt.decode(res.body.accessToken);%0A%0A%09%09%09%09// get the account id from JWT%0A%09%09%09%09decodedToken.should.have.property('accountId');%0A%0A%09%09%09%09accountService.get(decodedToken.accountId)%0A%09%09%09%09%09.then((account) =%3E %7B%0A%09%09%09%09%09%09account.username.should.equal(testAccount.username);%0A%09%09%09%09%09%09resolve();%0A%09%09%09%09%09%7D)%0A%09%09%09%09%09.catch((error) =%3E %7B%0A%09%09%09%09%09%09logger.error(%60failed to get the account from the service: $%7Berror%7D%60);%0A%09%09%09%09%09%09// throw error;%0A%09%09%09%09%09%09reject(error);%0A%09%09%09%09%09%09// done();%0A%09%09%09%09%09%7D);%0A%0A%09%09%09%09resolve();%0A%09%09%09%7D);%0A%09%7D));%0A%0A
 %09it('should 

add test to validate trimming