{"size": 4899, "ext": "js", "max_stars_repo_path": "server/test/loans.test.js", "max_stars_repo_name": "JosephNjuguna/QuickCredit", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/test/loans.test.js", "max_issues_repo_name": "JosephNjuguna/QuickCredit", "max_issues_repo_issues_event_min_datetime": "2019-05-21T13:00:58.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T21:06:47.000Z", "max_forks_repo_path": "server/test/loans.test.js", "max_forks_repo_name": "JosephNjuguna/QuickCredit", "max_forks_repo_forks_event_min_datetime": "2019-05-23T16:03:58.000Z", "max_forks_repo_forks_event_max_datetime": "2019-11-12T15:38:24.000Z"}
{"max_stars_count": null, "max_issues_count": 8, "max_forks_count": 2, "avg_line_length": 21.8705357143, "max_line_length": 70, "alphanum_fraction": 0.5478669116}
import jwt from 'jsonwebtoken';
import chai from 'chai';
import chaiHttp from 'chai-http';
import assert, {
	equal,
} from 'assert';
import dotenv from 'dotenv';
import app from '../../app';
import Db from '../db/db';
import total from '../helpers/Totalamount';
import paymentFunction from '../models/Loans';

dotenv.config();

const {
	expect,
} = chai;
chai.should();
chai.use(chaiHttp);

let userToken; let
	adminToken;
const wrongId = 1232;
const loanId = 1;
describe('/LOAN', () => {
	before('generate JWT', (done) => {
		adminToken = jwt.sign({
			email: 'admin123@gmail.com',
			firstname: 'main',
			lastname: 'admin',
			address: 'database',
			admin: true,
		},
		process.env.JWT_KEY, {
			expiresIn: '1h',
		});

		userToken = jwt.sign({
			email: 'test1@mail.com',
			firstname: 'Joseph',
			lastname: 'Njuguna',
			address: 'Kenya',
			admin: false,
		},
		process.env.JWT_KEY, {
			expiresIn: '1h',
		});
		done();
	});

	after('after all test', (done) => {
		Db.query('DELETE FROM loans');
		// Db.query('DELETE FROM payments');
		Db.query('DROP TABLE IF EXISTS loans');
		// Db.query('DROP TABLE IF EXISTS payments');
		done();
	});

	describe('/POST user request loan', () => {
		it('should check loan field is not entered', (done) => {
			chai.request(app)
				.post('/api/v2/requestloan')
				.set('authorization', `Bearer ${userToken}`)
				.send({
					'': 10000,
					tenor: 4,
				})
				.end((err, res) => {
					res.should.have.status(400);
					if (err) return done();
					done();
				});
		});

		it('should check loan amount is not entered', (done) => {
			chai.request(app)
				.post('/api/v2/requestloan')
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: '',
					tenor: 2,
				})
				.end((err, res) => {
					res.should.have.status(400);
					if (err) return done();
					done();
				});
		});

		it('should check successful loan request', (done) => {
			chai.request(app)
				.post('/api/v2/requestloan')
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: 2000,
					tenor: 6,
				})
				.end((err, res) => {
					res.should.have.status(200);
					if (err) return done();
					done();
				});
		});

		it('should check user has loan request available', (done) => {
			chai.request(app)
				.post('/api/v2/requestloan')
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: 10000,
					tenor: 6,
				})
				.end((err, res) => {
					res.should.have.status(409);
					if (err) return done();
					done();
				});
		});
	});

	describe('/PATCH  accept loan application', () => {
		it('should check loan id been updated is no available ', (done) => {
			chai.request(app)
				.patch('/api/v2/loan/100')
				.set('authorization', `Bearer ${adminToken}`)
				.send({
					status: 'accepted',
				})
				.end((err, res) => {
					res.should.have.status(404);
					if (err) return done();
					done();
				});
		});

		it('should update a loan application as accepted', (done) => {
			chai.request(app)
				.patch('/api/v2/loan/1')
				.set('authorization', `Bearer ${adminToken}`)
				.send({
					status: 'accepted',
				})
				.end((err, res) => {
					res.should.have.status(200);
					if (err) return done();
					done();
				});
		});
	});

	describe('/POST user pay loan', () => {
		it('check user has not entered amount to pay', (done) => {
			chai.request(app)
				.post(`/api/v2/payloan/${loanId}`)
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: '',
				})
				.end((err, res) => {
					expect(res.status).equals(400);
					if (err) return done();
					done();
				});
		});

		it('check user loan id doesn`t exist', (done) => {
			chai.request(app)
				.post(`/api/v2/payloan/${wrongId}`)
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: 575,
				})
				.end((err, res) => {
					expect(res.status).equals(404);
					if (err) return done();
					done();
				});
		});

		it('check user has paid loan first time', (done) => {
			chai.request(app)
				.post(`/api/v2/payloan/${loanId}`)
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: 350,
				})
				.end((err, res) => {
					expect(res.status).equals(200);
					if (err) return done();
					done();
				});
		});

		it('check user has paid loan second time', (done) => {
			chai.request(app)
				.post(`/api/v2/payloan/${loanId}`)
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: 350,
				})
				.end((err, res) => {
					expect(res.status).equals(200);
					if (err) return done();
					done();
				});
		});

		it('check user has paid loan third time', (done) => {
			chai.request(app)
				.post(`/api/v2/payloan/${loanId}`)
				.set('authorization', `Bearer ${userToken}`)
				.send({
					amount: 350,
				})
				.end((err, res) => {
					expect(res.status).equals(200);
					if (err) return done();
					done();
				});
		});
	});

});
