Skip to content

Commit

Permalink
fix: refactor routers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed May 22, 2022
1 parent 4f1e095 commit a7bea3e
Show file tree
Hide file tree
Showing 42 changed files with 2,305 additions and 2,175 deletions.
80 changes: 45 additions & 35 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
{
"env": {
"node": true,
"mocha": true,
"commonjs": true
},
"plugins": ["prettier", "simple-import-sort", "eslint-plugin-import"],
"extends": [
"airbnb-base",
"prettier"
"env": {
"node": true,
"mocha": true,
"commonjs": true
},
"plugins": ["prettier", "simple-import-sort", "eslint-plugin-import"],
"extends": ["airbnb-base", "prettier"],
"parserOptions": {
"ecmaVersion": 12
},
"overrides": [
{
"files": ["*.test.js", "*.spec.js"],
"rules": {
"no-unused-expressions": "off"
}
}
],
"ignorePatterns": ["database/migrations/*"],
"rules": {
"simple-import-sort/imports": "error",
"no-underscore-dangle": "off",
"camelcase": "off",
"no-unused-vars": [
"error",
{ "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }
],
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
"simple-import-sort/imports": "error",
"no-underscore-dangle": "off",
"camelcase":"off",
"no-unused-vars":"warn",
"func-names": "off",
"no-param-reassign": "warn",
"consistent-return":"warn",
"no-restricted-syntax":"warn",
"global-require":"off",
"radix":"off",
"no-await-in-loop":"off",
"class-methods-use-this":"warn",
"no-return-await":"warn",
"no-new-wrappers": "warn",
"prefer-destructuring": "warn",
"no-shadow":"warn",
"import/order": "warn",
"no-unused-expressions": "warn",
"import/no-extraneous-dependencies": "warn" ,
"import/no-unresolved": "warn",
"no-plusplus":"warn",
"no-continue":"off" }
"func-names": "off",
"no-param-reassign": "error",
"consistent-return": "error",
"no-restricted-syntax": "error",
"global-require": "off",
"radix": "off",
"no-await-in-loop": "off",
"class-methods-use-this": "error",
"no-return-await": "error",
"no-new-wrappers": "error",
"prefer-destructuring": "error",
"no-shadow": "error",
"import/order": "error",
"no-unused-expressions": "error",
"import/no-extraneous-dependencies": "error",
"import/no-unresolved": "error",
"no-plusplus": "error",
"no-continue": "off"
}
}
22 changes: 10 additions & 12 deletions __tests__/auth.spec.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
require('dotenv').config()
require('dotenv').config();
const request = require('supertest');
const { expect } = require('chai');
const log = require('loglevel');
const sinon = require("sinon");
const chai = require("chai");
const server = require("../server/app");
const sinon = require('sinon');
const chai = require('chai');
const server = require('../server/app');
const seed = require('./seed');
const Transfer = require("../server/models/Transfer");
const TrustRelationship = require("../server/models/TrustRelationship");
const Transfer = require('../server/models/Transfer');
const TrustRelationship = require('../server/models/TrustRelationship');
chai.use(require('chai-uuid'));

describe('Authentication', () => {
let bearerToken;
let bearerTokenB;

before( async () => {
before(async () => {
await seed.clear();
await seed.seed();
});

beforeEach(async () => {
sinon.restore();
})
});

// Authorization path
it(`[POST /auth] login with ${seed.wallet.name}`, (done) => {
request(server)
.post('/auth')
.set('treetracker-api-key', seed.apiKey)
.send({wallet: seed.wallet.name, password: seed.wallet.password})
.send({ wallet: seed.wallet.name, password: seed.wallet.password })
.expect('Content-Type', /application\/json/)
.expect(200)
.end((err, res) => {
Expand All @@ -38,12 +38,11 @@ describe('Authentication', () => {
});
});


it(`[POST /auth] login with using wallet id: ${seed.wallet.id}`, (done) => {
request(server)
.post('/auth')
.set('treetracker-api-key', seed.apiKey)
.send({wallet: seed.wallet.id, password: seed.wallet.password})
.send({ wallet: seed.wallet.id, password: seed.wallet.password })
.expect('Content-Type', /application\/json/)
.expect(200)
.end((err, res) => {
Expand All @@ -52,5 +51,4 @@ describe('Authentication', () => {
done();
});
});

});
48 changes: 26 additions & 22 deletions __tests__/bundle-transfer-cancel.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
require('dotenv').config()
require('dotenv').config();
const request = require('supertest');
const { expect } = require('chai');
const log = require('loglevel');
const sinon = require("sinon");
const chai = require("chai");
const server = require("../server/app");
const sinon = require('sinon');
const chai = require('chai');
const server = require('../server/app');
const seed = require('./seed');
const Transfer = require("../server/models/Transfer");
const TrustRelationship = require("../server/models/TrustRelationship");
const Transfer = require('../server/models/Transfer');
const TrustRelationship = require('../server/models/TrustRelationship');
chai.use(require('chai-uuid'));

const {apiKey} = seed;

describe('Create and accept a bundle transfer', () => {
const { apiKey } = seed;

describe('Create and cancel a bundle transfer', () => {
let bearerToken;
let bearerTokenB;
let pendingTransfer;

before( async () => {

before(async () => {
await seed.clear();
await seed.seed();

Expand Down Expand Up @@ -54,12 +52,12 @@ describe('Create and accept a bundle transfer', () => {

beforeEach(async () => {
sinon.restore();
})

await new Promise((resolve) => setTimeout(resolve, 500));
});

it(`create Bundle transfer tokens from ${seed.wallet.name} to ${seed.walletB.name}`, async () => {
const res = await request(server)
.post("/transfers")
.post('/transfers')
.set('treetracker-api-key', apiKey)
.set('Authorization', `Bearer ${bearerToken}`)
.send({
Expand All @@ -70,18 +68,23 @@ describe('Create and accept a bundle transfer', () => {
receiver_wallet: seed.walletB.name,
claim: false,
});
expect(res).property("statusCode").to.eq(202);
pendingTransfer = res.body
expect(res).property("body").property("parameters").property("bundle").property("bundleSize").eq(1);
})
expect(res).property('statusCode').to.eq(202);
pendingTransfer = res.body;
expect(res)
.property('body')
.property('parameters')
.property('bundle')
.property('bundleSize')
.eq(1);
});

it("Delete/cancel the pending transfer", async () => {
it('Delete/cancel the pending transfer', async () => {
const res = await request(server)
.del(`/transfers/${pendingTransfer.id}`)
.set('treetracker-api-key', apiKey)
.set('Authorization', `Bearer ${bearerToken}`);
expect(res).to.have.property('statusCode', 200);
})
});

it(`Wallet:${seed.wallet.name} should be able to find the transfer, it should be cancelled`, async () => {
const res = await request(server)
Expand All @@ -90,7 +93,8 @@ describe('Create and accept a bundle transfer', () => {
.set('Authorization', `Bearer ${bearerToken}`);
expect(res).to.have.property('statusCode', 200);
expect(res.body.transfers).lengthOf(1);
expect(res.body.transfers[0]).property("state").eq(Transfer.STATE.cancelled);
expect(res.body.transfers[0])
.property('state')
.eq(Transfer.STATE.cancelled);
});
});

56 changes: 31 additions & 25 deletions __tests__/bundle-transfer-create-accept.spec.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
require('dotenv').config()
require('dotenv').config();
const request = require('supertest');
const { expect } = require('chai');
const log = require('loglevel');
const sinon = require("sinon");
const chai = require("chai");
const server = require("../server/app");
const sinon = require('sinon');
const chai = require('chai');
const server = require('../server/app');
const seed = require('./seed');
const Transfer = require("../server/models/Transfer");
const TrustRelationship = require("../server/models/TrustRelationship");
const Transfer = require('../server/models/Transfer');
const TrustRelationship = require('../server/models/TrustRelationship');
chai.use(require('chai-uuid'));

const {apiKey} = seed;
const { apiKey } = seed;

describe('Create and accept a bundle transfer', () => {

let bearerToken;
let bearerTokenB;
let pendingTransfer;

before( async () => {

before(async () => {
await seed.clear();
await seed.seed();

Expand Down Expand Up @@ -54,12 +52,12 @@ describe('Create and accept a bundle transfer', () => {

beforeEach(async () => {
sinon.restore();
})

await new Promise((resolve) => setTimeout(resolve, 500));
});

it(`create Bundle transfer tokens from ${seed.wallet.name} to ${seed.walletB.name}`, async () => {
const res = await request(server)
.post("/transfers")
.post('/transfers')
.set('treetracker-api-key', apiKey)
.set('Authorization', `Bearer ${bearerToken}`)
.send({
Expand All @@ -70,40 +68,48 @@ describe('Create and accept a bundle transfer', () => {
receiver_wallet: seed.walletB.name,
claim: false,
});
expect(res).property("statusCode").to.eq(202);
expect(res).property("body").property("parameters").property("bundle").property("bundleSize").eq(1);
})

expect(res).property('statusCode').to.eq(202);
expect(res)
.property('body')
.property('parameters')
.property('bundle')
.property('bundleSize')
.eq(1);
});

it("get all pending transfers belongs to walletB, should have one", async () => {
it('get all pending transfers belongs to walletB, should have one', async () => {
const res = await request(server)
.get('/transfers?state=pending&limit=1000')
.set('treetracker-api-key', apiKey)
.set('Authorization', `Bearer ${bearerTokenB}`);
expect(res).to.have.property('statusCode', 200);
expect(res.body.transfers).lengthOf(1);
pendingTransfer = res.body.transfers[0];
expect(pendingTransfer).property("destination_wallet").eq(seed.walletB.name);
})
expect(pendingTransfer)
.property('destination_wallet')
.eq(seed.walletB.name);
});

it("Accept the pending bundle transfer", async () => {
it('Accept the pending bundle transfer', async () => {
const res = await request(server)
.post(`/transfers/${pendingTransfer.id}/accept`)
.set('Content-Type', "application/json")
.set('Content-Type', 'application/json')
.set('treetracker-api-key', apiKey)
.set('Authorization', `Bearer ${bearerTokenB}`);
expect(res).to.have.property('statusCode', 200);
})
});

it(`Wallet:${seed.wallet.name} should be able to find the transfer, it should be completed 2`, async () => {
const res = await request(server)
.get(`/transfers?limit=1000`)
.set('treetracker-api-key', apiKey)
.set('Authorization', `Bearer ${bearerToken}`);
console.log(res.body)
console.log(res.body);
expect(res).to.have.property('statusCode', 200);
expect(res.body.transfers).lengthOf(1);
expect(res.body.transfers[0]).property("state").eq(Transfer.STATE.completed);
expect(res.body.transfers[0])
.property('state')
.eq(Transfer.STATE.completed);
});

it(`Token:#${seed.token.id} now should belong to ${seed.walletB.name}`, async () => {
Expand Down
Loading

0 comments on commit a7bea3e

Please sign in to comment.