From 44d2f7cf8d3b2391bf121725985082d246496b66 Mon Sep 17 00:00:00 2001 From: Zaven Date: Wed, 17 Feb 2021 13:32:24 -0800 Subject: [PATCH] fix: wip refactor integration tests --- __tests__/auth.spec.js | 56 ++ __tests__/bundle-transfer-cancel.js | 95 +++ __tests__/bundle-transfer-create-accept.js | 116 ++++ __tests__/bundle-transfer-decline.js | 116 ++++ __tests__/fulfill-bundle-transfer-request.js | 116 ++++ .../fulfill-explicity-transfer-request.js | 131 ++++ __tests__/get-tokens.spec.js | 101 +++ __tests__/pending-transfer-cancel.spec.js | 114 ++++ __tests__/pending-transfer-decline.spec.js | 127 ++++ .../pending-transfer-wrong-wallet.spec.js | 101 +++ __tests__/pending-transfer.spec.js | 136 ++++ __tests__/supertest.js | 632 +++++++++--------- __tests__/trust-relationship-api.spec.js | 174 +++++ __tests__/trust-relationship-manage.js | 123 ++++ __tests__/trust-relationship-send-cancel.js | 89 +++ __tests__/trust-relationship-send-decline.js | 111 +++ __tests__/trust-relationship-send.js | 154 +++++ 17 files changed, 2177 insertions(+), 315 deletions(-) create mode 100644 __tests__/auth.spec.js create mode 100644 __tests__/bundle-transfer-cancel.js create mode 100644 __tests__/bundle-transfer-create-accept.js create mode 100644 __tests__/bundle-transfer-decline.js create mode 100644 __tests__/fulfill-bundle-transfer-request.js create mode 100644 __tests__/fulfill-explicity-transfer-request.js create mode 100644 __tests__/get-tokens.spec.js create mode 100644 __tests__/pending-transfer-cancel.spec.js create mode 100644 __tests__/pending-transfer-decline.spec.js create mode 100644 __tests__/pending-transfer-wrong-wallet.spec.js create mode 100644 __tests__/pending-transfer.spec.js create mode 100644 __tests__/trust-relationship-api.spec.js create mode 100644 __tests__/trust-relationship-manage.js create mode 100644 __tests__/trust-relationship-send-cancel.js create mode 100644 __tests__/trust-relationship-send-decline.js create mode 100644 __tests__/trust-relationship-send.js diff --git a/__tests__/auth.spec.js b/__tests__/auth.spec.js new file mode 100644 index 00000000..89b03f33 --- /dev/null +++ b/__tests__/auth.spec.js @@ -0,0 +1,56 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +describe('Authentication', () => { + let bearerToken; + let bearerTokenB; + + 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}) + .expect('Content-Type', /application\/json/) + .expect(200) + .end((err, res) => { + if (err) done(err); + expect(res.body).to.have.property('token'); + done(); + }); + }); + + + it(`[POST /auth] login with using wallet id: ${seed.wallet.id}`, (done) => { + request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({wallet: seed.wallet.id, password: seed.wallet.password}) + .expect('Content-Type', /application\/json/) + .expect(200) + .end((err, res) => { + if (err) done(err); + expect(res.body).to.have.property('token'); + done(); + }); + }); + +}); diff --git a/__tests__/bundle-transfer-cancel.js b/__tests__/bundle-transfer-cancel.js new file mode 100644 index 00000000..15f32988 --- /dev/null +++ b/__tests__/bundle-transfer-cancel.js @@ -0,0 +1,95 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Create and accept a bundle transfer', () => { + + let bearerToken; + let bearerTokenB; + let pendingTransfer; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + + it(`create Bundle transfer tokens from ${seed.wallet.name} to ${seed.walletB.name}`, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + 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 () => { + 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) + .get(`/transfers?limit=1000`) + .set('treetracker-api-key', apiKey) + .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); + }); +}); + diff --git a/__tests__/bundle-transfer-create-accept.js b/__tests__/bundle-transfer-create-accept.js new file mode 100644 index 00000000..16f1de84 --- /dev/null +++ b/__tests__/bundle-transfer-create-accept.js @@ -0,0 +1,116 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Create and accept a bundle transfer', () => { + + let bearerToken; + let bearerTokenB; + let pendingTransfer; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + + it(`create Bundle transfer tokens from ${seed.wallet.name} to ${seed.walletB.name}`, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + 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 () => { + 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); + }) + + it("Accept the pending bundle transfer", async () => { + const res = await request(server) + .post(`/transfers/${pendingTransfer.id}/accept`) + .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) + expect(res).to.have.property('statusCode', 200); + expect(res.body.transfers).lengthOf(1); + expect(res.body.transfers[0]).property("state").eq(Transfer.STATE.completed); + }); + + it(`Token:#${seed.token.id} now should belong to ${seed.walletB.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletB.id); + }); +}); diff --git a/__tests__/bundle-transfer-decline.js b/__tests__/bundle-transfer-decline.js new file mode 100644 index 00000000..885d6d3e --- /dev/null +++ b/__tests__/bundle-transfer-decline.js @@ -0,0 +1,116 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Create and accept a bundle transfer', () => { + + let bearerToken; + let bearerTokenB; + let pendingTransfer; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + + it(`create Bundle transfer tokens from ${seed.wallet.name} to ${seed.walletB.name}`, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + 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 () => { + 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); + }) + + it("Decline the pending transfer", async () => { + const res = await request(server) + .post(`/transfers/${pendingTransfer.id}/decline`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + console.log(res.body) + 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) + .get(`/transfers?limit=1000`) + .set('treetracker-api-key', apiKey) + .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); + }); + + it(`Token:#${seed.token.id} now should still belong to ${seed.wallet.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.wallet.id); + }); +}); diff --git a/__tests__/fulfill-bundle-transfer-request.js b/__tests__/fulfill-bundle-transfer-request.js new file mode 100644 index 00000000..84e5196e --- /dev/null +++ b/__tests__/fulfill-bundle-transfer-request.js @@ -0,0 +1,116 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Request and fulfill a bundle transfer', () => { + + let bearerToken; + let bearerTokenB; + let requestedTransferId; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + it(`WalletB:${seed.walletB.name} request a bundle of token from ${seed.wallet.name}, should get 202`, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(202); + }) + + it(`${seed.wallet.name} should find a requested transfer sent to it`, async () => { + const res = await request(server) + .get("/transfers?state=requested&limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("requested"); + expect(res.body.transfers[0]).property("id").to.be.a.uuid('v4') + requestedTransferId = res.body.transfers[0].id; + }) + + it(`${seed.wallet.name} fulfill this requested transfer with tokens`, async () => { + const res = await request(server) + .post(`/transfers/${requestedTransferId}/fulfill`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [seed.token.id], + }); + expect(res).property("statusCode").to.eq(200); + }) + + it(`${seed.walletB.name} should be able to find requested transfer has been completed`, async () => { + const res = await request(server) + .get("/transfers?state=completed&limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("completed"); + expect(res.body.transfers[0]).property("id").eq(requestedTransferId); + }); + + it(`Token:#${seed.token.id} now should still belong to ${seed.walletB.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletB.id); + }); +}); diff --git a/__tests__/fulfill-explicity-transfer-request.js b/__tests__/fulfill-explicity-transfer-request.js new file mode 100644 index 00000000..f3ca04c4 --- /dev/null +++ b/__tests__/fulfill-explicity-transfer-request.js @@ -0,0 +1,131 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Request and fulfill an explicit transfer', () => { + + let bearerToken; + let bearerTokenB; + let requestedTransferId; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + it(`WalletB:${seed.walletB.name} request a token from ${seed.wallet.name}, should get 202`, async () => { + + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`) + .send({ + tokens: [seed.token.id], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + console.log('AA') + console.log(res.body) + expect(res).property("statusCode").to.eq(202); + + }) + + it(`${seed.wallet.name} should find a requested transfer sent to him`, async () => { + + const res = await request(server) + .get("/transfers?state=requested&limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("requested"); + expect(res.body.transfers[0]).property("id").to.be.a.uuid('v4'); + requestedTransferId = res.body.transfers[0].id; + console.log('JJ') + console.log(requestedTransferId) + + }) + + it(`${seed.wallet.name} fulfill this requested transfer`, async () => { + + console.log(requestedTransferId) + const res = await request(server) + .post(`/transfers/${requestedTransferId}/fulfill`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + implicit: true, + }); + console.log(res.body) + expect(res).property("statusCode").to.eq(200); + + }) + + it(`${seed.walletB.name} should be able to find requested transfer has been completed`, async () => { + + const res = await request(server) + .get("/transfers?state=completed&limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("completed"); + expect(res.body.transfers[0]).property("id").eq(requestedTransferId); + + }); + + it(`Token:#${seed.token.id} now should still belong to ${seed.walletB.name}`, async () => { + + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletB.id); + + }); +}); + diff --git a/__tests__/get-tokens.spec.js b/__tests__/get-tokens.spec.js new file mode 100644 index 00000000..2c322a62 --- /dev/null +++ b/__tests__/get-tokens.spec.js @@ -0,0 +1,101 @@ +/* + * The integration test to test the whole business, with DB + */ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +describe('GET tokens', () => { + let bearerToken; + let bearerTokenB; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', seed.apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', seed.apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + it(`walletA, GET /tokens/${seed.token.id} Should be able to get a token `, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body).to.have.property('id').eq(seed.token.id); + }); + + it(`walletA, GET /tokens/${seed.tokenB.id} Should be forbidden`, async () => { + const res = await request(server) + .get(`/tokens/${seed.tokenB.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 401); + }); + + it(`walletA, GET /tokens Should be able to get a token `, async () => { + const res = await request(server) + .get(`/tokens?limit=10`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.tokens[0]).to.have.property('id').eq(seed.token.id); + }); + + it(`walletB, GET /tokens Should be able to get a token, which actually belongs to walletC`, async () => { + const res = await request(server) + .get(`/tokens?limit=10&wallet=walletC`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.tokens[0]).to.have.property('id').eq(seed.tokenB.id); + }); + + it(`walletB, GET /tokens/${seed.tokenB.id} Should be able to get a token `, async () => { + const res = await request(server) + .get(`/tokens/${seed.tokenB.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body).to.have.property('id').eq(seed.tokenB.id); + }); +}); diff --git a/__tests__/pending-transfer-cancel.spec.js b/__tests__/pending-transfer-cancel.spec.js new file mode 100644 index 00000000..1c67fc9c --- /dev/null +++ b/__tests__/pending-transfer-cancel.spec.js @@ -0,0 +1,114 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Create and cancel a pending transfer', () => { + let bearerToken; + let bearerTokenB; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + let transferId; + let pendingTransfer; + + it(`Creates a pending transaction `, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [seed.token.id], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(202); + expect(res).property("body").property("id").to.be.a.uuid('v4') + expect(res).property("body").property("parameters").property("tokens").lengthOf(1); + expect(res.body.parameters.tokens[0]).eq(seed.token.id); + transferId = res.body.id; + }) + + it("Get all pending transfers belongs to walletB, should have one", async () => { + const res = await request(server) + .get(`/transfers?state=pending&wallet=${seed.wallet.name}&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); + }) + + 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) + .get(`/transfers?limit=1000`) + .set('treetracker-api-key', apiKey) + .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); + }); + + it(`Token:#${seed.token.id} now shouldn't be pending `, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.transfer_pending).eq(false); + }); +}); diff --git a/__tests__/pending-transfer-decline.spec.js b/__tests__/pending-transfer-decline.spec.js new file mode 100644 index 00000000..9cb4203d --- /dev/null +++ b/__tests__/pending-transfer-decline.spec.js @@ -0,0 +1,127 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Create and decline a pending transfer', () => { + let bearerToken; + let bearerTokenB; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + let transferId; + let pendingTransfer; + + it(`Creates a pending transaction `, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [seed.token.id], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(202); + expect(res).property("body").property("id").to.be.a.uuid('v4') + expect(res).property("body").property("parameters").property("tokens").lengthOf(1); + expect(res.body.parameters.tokens[0]).eq(seed.token.id); + transferId = res.body.id; + }) + + it("Get all pending transfers belongs to walletB, should have one", async () => { + const res = await request(server) + .get(`/transfers?state=pending&wallet=${seed.wallet.name}&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); + }) + + it("Decline the pending transfer", async () => { + const res = await request(server) + .post(`/transfers/${pendingTransfer.id}/decline`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + console.log(res.body) + 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) + .get(`/transfers?limit=1000`) + .set('treetracker-api-key', apiKey) + .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); + }); + + it(`Token:#${seed.token.id} now should still belong to ${seed.wallet.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.wallet.id); + }); + + it(`Token:#${seed.token.id} now shouldn't be pending `, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.transfer_pending).eq(false); + }); + +}); + diff --git a/__tests__/pending-transfer-wrong-wallet.spec.js b/__tests__/pending-transfer-wrong-wallet.spec.js new file mode 100644 index 00000000..44677fb5 --- /dev/null +++ b/__tests__/pending-transfer-wrong-wallet.spec.js @@ -0,0 +1,101 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Create and fail to accept a pending transfer with wrong wallet', () => { + let bearerToken; + let bearerTokenB; + let bearerTokenC; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + + { + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletC.name, + password: seed.walletC.password, + }); + expect(res).to.have.property('statusCode', 200); + expect(res).property('body').property("token").a("string"); + bearerTokenC = res.body.token; + } + + }); + + beforeEach(async () => { + sinon.restore(); + }) + + let transferId; + let pendingTransfer; + + it(`Creates a pending transaction `, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [seed.token.id], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(202); + expect(res).property("body").property("id").to.be.a.uuid('v4') + expect(res).property("body").property("parameters").property("tokens").lengthOf(1); + expect(res.body.parameters.tokens[0]).eq(seed.token.id); + transferId = res.body.id; + }) + + it(`${seed.walletC.name} should not be able to accept the transfer (403)`, async () => { + const res = await request(server) + .post(`/transfers/${transferId}/accept`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenC}`); + expect(res).to.have.property('statusCode', 403); + expect(res).property("body").property("message").match(/permission/i); + }); +}); diff --git a/__tests__/pending-transfer.spec.js b/__tests__/pending-transfer.spec.js new file mode 100644 index 00000000..e10ddc4a --- /dev/null +++ b/__tests__/pending-transfer.spec.js @@ -0,0 +1,136 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Create and accept a pending transfer', () => { + let bearerToken; + let bearerTokenB; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + let transferId; + let pendingTransfer; + + it(`Creates a pending transaction `, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [seed.token.id], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(202); + expect(res).property("body").property("id").to.be.a.uuid('v4') + expect(res).property("body").property("parameters").property("tokens").lengthOf(1); + expect(res.body.parameters.tokens[0]).eq(seed.token.id); + transferId = res.body.id; + }) + + it(`Token:#${seed.token.id} now should be pending `, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.transfer_pending).eq(true); + }); + + it("Get all pending transfers belongs to walletB, should have one", async () => { + const res = await request(server) + .get(`/transfers?state=pending&wallet=${seed.wallet.name}&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); + }) + + it("Accept the pending transfer", async () => { + const res = await request(server) + .post(`/transfers/${pendingTransfer.id}/accept`) + .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 1`, async () => { + const res = await request(server) + .get(`/transfers?limit=1000`) + .set('treetracker-api-key', apiKey) + .set('Content-Type', "application/json") + .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.completed); + }); + + it(`Token:#${seed.token.id} now should belong to ${seed.walletB.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletB.id); + }); + + it(`Token:#${seed.token.id} now should have some transaction history`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}/transactions?limit=1000`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.history).lengthOf(1); + expect(res.body.history[0]).property("sender_wallet").eq(seed.wallet.name); + expect(res.body.history[0]).property("receiver_wallet").eq(seed.walletB.name); + }); +}); diff --git a/__tests__/supertest.js b/__tests__/supertest.js index d9dd9866..5ae4b696 100644 --- a/__tests__/supertest.js +++ b/__tests__/supertest.js @@ -28,14 +28,19 @@ describe('Wallet integration tests', () => { let bearerToken; let bearerTokenB; - beforeEach(async () => { - //In case other sinon stub would affect me - sinon.restore(); - //before all, seed data to DB + before( async () => { + + // //before all, seed data to DB await seed.clear(); await seed.seed(); - { + + } + + beforeEach(async () => { + //In case other sinon stub would affect me + sinon.restore(); + { // Authorizes before each of the follow tests const res = await request(server) .post('/auth') @@ -103,6 +108,12 @@ describe('Wallet integration tests', () => { }); describe("Check default tokens", () => { + + beforeEach(async () => { + await seed.clear(); + await seed.seed(); + }) + it(`walletA, GET /tokens/${seed.token.id} Should be able to get a token `, async () => { const res = await request(server) .get(`/tokens/${seed.token.id}`) @@ -140,7 +151,7 @@ describe('Wallet integration tests', () => { expect(res.body.tokens[0]).to.have.property('id').eq(seed.tokenB.id); }); - it(`walletB, GET /tokens/${seed.tokenB.id} Should be able to get a token (tokenB) `, async () => { + it(`walletB, GET /tokens/${seed.tokenB.id} Should be able to get a token `, async () => { const res = await request(server) .get(`/tokens/${seed.tokenB.id}`) .set('treetracker-api-key', apiKey) @@ -151,9 +162,14 @@ describe('Wallet integration tests', () => { }); describe.skip(`Before request trust, try to send token:#${seed.token.id} from ${seed.wallet.name} to ${seed.walletB.name} should be pending (202)`, () => { + + let transferId; beforeEach(async () => { + await seed.clear(); + await seed.seed(); + const res = await request(server) .post("/transfers") .set('treetracker-api-key', apiKey) @@ -319,6 +335,9 @@ describe('Wallet integration tests', () => { let bearerTokenC; beforeEach(async () => { + await seed.clear(); + await seed.seed(); + const res = await request(server) .post('/auth') .set('treetracker-api-key', apiKey) @@ -346,6 +365,9 @@ describe('Wallet integration tests', () => { let trustRelationship; beforeEach(async () => { + await seed.clear(); + await seed.seed(); + const res = await request(server) .post("/trust_relationships") .set('treetracker-api-key', apiKey) @@ -360,95 +382,80 @@ describe('Wallet integration tests', () => { expect(trustRelationship).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested); }); - describe("Login with walletB", () => { - let tokenB; + + describe("Accept this request", () => { beforeEach(async () => { const res = await request(server) - .post('/auth') + .post(`/trust_relationships/${trustRelationship.id}/accept`) + .set('Content-Type', "application/json") .set('treetracker-api-key', apiKey) - .send({ - wallet: seed.walletB.name, - password: seed.walletB.password, - }); - expect(res).to.have.property('statusCode', 200); - tokenB = res.body.token; + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); }) - describe("Accept this request", () => { - - beforeEach(async () => { - const res = await request(server) - .post(`/trust_relationships/${trustRelationship.id}/accept`) - .set('Content-Type', "application/json") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - expect(res).property("statusCode").to.eq(200); - }) - - it("Wallet should be able to find the relationship, and it was approved", async () => { - const res = await request(server) - .get("/trust_relationships?limit=1000") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`); - expect(res).property("statusCode").to.eq(200); - expect(res).property("body").property("trust_relationships").lengthOf(1); - expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') - }); - - it("Try to send a token to walletB again, this time, should success, 201", async () => { - const res = await request(server) - .post("/transfers") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`) - .send({ - tokens: [], - sender_wallet: seed.wallet.name, - receiver_wallet: seed.walletB.name, - }); - expect(res).property("statusCode").to.eq(201); - expect(res).property("body").property("parameters").property("tokens").lengthOf(0); - }); + it("Wallet should be able to find the relationship, and it was approved", async () => { + const res = await request(server) + .get("/trust_relationships?limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res).property("body").property("trust_relationships").lengthOf(1); + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') + }); - it("Try to send bundle token to walletB again, should success, 201", async () => { - const res = await request(server) - .post("/transfers") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`) - .send({ - bundle: { - bundle_size: 1, - }, - sender_wallet: seed.wallet.name, - receiver_wallet: seed.walletB.name, - }); - expect(res).property("statusCode").to.eq(201); - }); + it("Try to send a token to walletB again, this time, should success, 201", async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(201); + expect(res).property("body").property("parameters").property("tokens").lengthOf(0); }); - describe("Decline this request", () => { + it("Try to send bundle token to walletB again, should success, 201", async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(201); + }); + }); - beforeEach(async () => { - const res = await request(server) - .post(`/trust_relationships/${trustRelationship.id}/decline`) - .set('Content-Type', "application/json") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - expect(res).property("statusCode").to.eq(200); - }) + describe("Decline this request", () => { - it("Wallet should be able to find the relationship, and it was cancelled", async () => { - const res = await request(server) - .get("/trust_relationships?limit=1000") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`); - expect(res).property("statusCode").to.eq(200); - expect(res).property("body").property("trust_relationships").lengthOf(1); - expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') - expect(res.body.trust_relationships[0]).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.canceled_by_target); - }); + beforeEach(async () => { + const res = await request(server) + .post(`/trust_relationships/${trustRelationship.id}/decline`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + }) + it("Wallet should be able to find the relationship, and it was cancelled", async () => { + const res = await request(server) + .get("/trust_relationships?limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res).property("body").property("trust_relationships").lengthOf(1); + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') + expect(res.body.trust_relationships[0]).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.canceled_by_target); }); + }); describe(`Cancel this request by ${seed.wallet.name}`, () => { @@ -478,6 +485,11 @@ describe('Wallet integration tests', () => { describe(`Bundle transfer tokens from ${seed.wallet.name} to ${seed.walletB.name}`, () => { + beforeEach(async () => { + await seed.clear(); + await seed.seed(); + }) + // TODO: this should be created in the seed beforeEach(async () => { const res = await request(server) @@ -495,286 +507,285 @@ describe('Wallet integration tests', () => { expect(res).property("body").property("parameters").property("bundle").property("bundleSize").eq(1); }) - describe("Login with ${seed.walletB.name}", () => { - let tokenB; + + describe("get all pending transfers belongs to walletB, should have one", () => { + let pendingTransfer; beforeEach(async () => { const res = await request(server) - .post('/auth') + .get('/transfers?state=pending&limit=1000') .set('treetracker-api-key', apiKey) - .send({ - wallet: seed.walletB.name, - password: seed.walletB.password, - }); + .set('Authorization', `Bearer ${tokenB}`); + console.log('LL1') + console.log(res.body) + console.log(res.statusCode) + console.log('LL2') expect(res).to.have.property('statusCode', 200); - tokenB = res.body.token; + expect(res.body.transfers).lengthOf(1); + pendingTransfer = res.body.transfers[0]; + expect(pendingTransfer).property("destination_wallet").eq(seed.walletB.name); }) - describe("get all pending transfers belongs to walletB, should have one", () => { - let pendingTransfer; + describe("Accept the pending transfer", () => { beforeEach(async () => { const res = await request(server) - .get('/transfers?state=pending&limit=1000') + .post(`/transfers/${pendingTransfer.id}/accept`) + .set('Content-Type', "application/json") .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - console.log('LL1') - console.log(res.body) - console.log(res.statusCode) - console.log('LL2') + .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); }) - describe("Accept the pending transfer", () => { - - beforeEach(async () => { - const res = await request(server) - .post(`/transfers/${pendingTransfer.id}/accept`) - .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) - expect(res).to.have.property('statusCode', 200); - expect(res.body.transfers).lengthOf(1); - expect(res.body.transfers[0]).property("state").eq(Transfer.STATE.completed); - }); + 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) + expect(res).to.have.property('statusCode', 200); + expect(res.body.transfers).lengthOf(1); + expect(res.body.transfers[0]).property("state").eq(Transfer.STATE.completed); + }); - it(`Token:#${seed.token.id} now should belong to ${seed.walletB.name}`, async () => { - const res = await request(server) - .get(`/tokens/${seed.token.id}`) - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerTokenB}`); - expect(res).to.have.property('statusCode', 200); - expect(res.body.wallet_id).eq(seed.walletB.id); - }); + it(`Token:#${seed.token.id} now should belong to ${seed.walletB.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletB.id); }); + }); - describe("Decline the pending transfer", () => { + describe("Decline the pending transfer", () => { - beforeEach(async () => { - const res = await request(server) - .post(`/transfers/${pendingTransfer.id}/decline`) - .set('Content-Type', "application/json") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - console.log(res.body) - expect(res).to.have.property('statusCode', 200); - }) + beforeEach(async () => { + const res = await request(server) + .post(`/transfers/${pendingTransfer.id}/decline`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${tokenB}`); + console.log(res.body) + 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) - .get(`/transfers?limit=1000`) - .set('treetracker-api-key', apiKey) - .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); - }); + it(`Wallet:${seed.wallet.name} should be able to find the transfer, it should be cancelled`, async () => { + const res = await request(server) + .get(`/transfers?limit=1000`) + .set('treetracker-api-key', apiKey) + .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); + }); - it(`Token:#${seed.token.id} now should still belong to ${seed.wallet.name}`, async () => { - const res = await request(server) - .get(`/tokens/${seed.token.id}`) - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`); - expect(res).to.have.property('statusCode', 200); - expect(res.body.wallet_id).eq(seed.wallet.id); - }); + it(`Token:#${seed.token.id} now should still belong to ${seed.wallet.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.wallet.id); }); + }); - describe("Delete/cancel the pending transfer", () => { + describe("Delete/cancel the pending transfer", () => { - beforeEach(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); - }) + beforeEach(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) - .get(`/transfers?limit=1000`) - .set('treetracker-api-key', apiKey) - .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); - }); + it(`Wallet:${seed.wallet.name} should be able to find the transfer, it should be cancelled`, async () => { + const res = await request(server) + .get(`/transfers?limit=1000`) + .set('treetracker-api-key', apiKey) + .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); }); - }); }); + }); - describe("Login with walletB", () => { - describe(`WalletB:${seed.walletB.name} request a token from ${seed.wallet.name}, should get 202`, () => { + describe(`request a transfer and fulfill it`, () => { - it(`WalletB:${seed.walletB.name} request a token from ${seed.wallet.name}, should get 202`, () => { - const res = await request(server) - .post("/transfers") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerTokenB}`) - .send({ - tokens: [seed.token.id], - sender_wallet: seed.wallet.name, - receiver_wallet: seed.walletB.name, - }); - console.log('AA') - console.log(res.body) - expect(res).property("statusCode").to.eq(202); - }) + before(async () => { + await seed.clear(); + await seed.seed(); + }) - describe(`${seed.wallet.name} should find a requested transfer sent to him`, () => { - let requestedTransferId; + let requestedTransferId; - beforeEach(async () => { - const res = await request(server) - .get("/transfers?state=requested&limit=1000") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`); - expect(res).property("statusCode").to.eq(200); - expect(res.body).property("transfers").lengthOf(1); - expect(res.body.transfers[0]).property("state").eq("requested"); - expect(res.body.transfers[0]).property("id").to.be.a.uuid('v4'); - requestedTransferId = res.body.transfers[0].id; - }) + it(`WalletB:${seed.walletB.name} request a token from ${seed.wallet.name}, should get 202`, async () => { - describe(`${seed.wallet.name} fulfill this requested transfer`, () => { - beforeEach(async () => { // TODO: move to seed - const res = await request(server) - .post(`/transfers/${requestedTransferId}/fulfill`) - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`) - .send({ - implicit: true, - }); - console.log(res.body) - expect(res).property("statusCode").to.eq(200); - }) + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`) + .send({ + tokens: [seed.token.id], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + console.log('AA') + console.log(res.body) + expect(res).property("statusCode").to.eq(202); - it(`${seed.walletB.name} should be able to find requested transfer has been completed`, async () => { - const res = await request(server) - .get("/transfers?state=completed&limit=1000") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - expect(res).property("statusCode").to.eq(200); - expect(res.body).property("transfers").lengthOf(1); - expect(res.body.transfers[0]).property("state").eq("completed"); - expect(res.body.transfers[0]).property("id").eq(requestedTransferId); - }); + }) - it(`Token:#${seed.token.id} now should still belong to ${seed.walletB.name}`, async () => { - const res = await request(server) - .get(`/tokens/${seed.token.id}`) - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - expect(res).to.have.property('statusCode', 200); - expect(res.body.wallet_id).eq(seed.walletB.id); - }); + it(`${seed.wallet.name} should find a requested transfer sent to him`, async () => { + + const res = await request(server) + .get("/transfers?state=requested&limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("requested"); + expect(res.body.transfers[0]).property("id").to.be.a.uuid('v4'); + requestedTransferId = res.body.transfers[0].id; + console.log('JJ') + console.log(requestedTransferId) + + }) + + it(`${seed.wallet.name} fulfill this requested transfer`, async () => { + + console.log(requestedTransferId) + const res = await request(server) + .post(`/transfers/${requestedTransferId}/fulfill`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + implicit: true, }); - }); + console.log(res.body) + expect(res).property("statusCode").to.eq(200); + + }) + + it(`${seed.walletB.name} should be able to find requested transfer has been completed`, async () => { + + const res = await request(server) + .get("/transfers?state=completed&limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("completed"); + expect(res.body.transfers[0]).property("id").eq(requestedTransferId); + }); - describe(`WalletB:${seed.walletB.name} request a bundle of token from ${seed.wallet.name}, should get 202`, () => { + it(`Token:#${seed.token.id} now should still belong to ${seed.walletB.name}`, async () => { + + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletB.id); + + }); + }); + + describe(`WalletB:${seed.walletB.name} request a bundle of token from ${seed.wallet.name}, should get 202`, () => { + + beforeEach(async () => { + await seed.clear(); + await seed.seed(); + + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(202); + }) + + describe(`${seed.wallet.name} should find a requested transfer sent to him`, () => { + let requestedTransferId; beforeEach(async () => { const res = await request(server) - .post("/transfers") + .get("/transfers?state=requested&limit=1000") .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`) - .send({ - bundle: { - bundle_size: 1, - }, - sender_wallet: seed.wallet.name, - receiver_wallet: seed.walletB.name, - }); - expect(res).property("statusCode").to.eq(202); + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("requested"); + expect(res.body.transfers[0]).property("id").to.be.a.uuid('v4') + requestedTransferId = res.body.transfers[0].id; }) - describe(`${seed.wallet.name} should find a requested transfer sent to him`, () => { - let requestedTransferId; - + describe(`${seed.wallet.name} fulfill this requested transfer with tokens`, () => { beforeEach(async () => { const res = await request(server) - .get("/transfers?state=requested&limit=1000") + .post(`/transfers/${requestedTransferId}/fulfill`) .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`); + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [seed.token.id], + }); expect(res).property("statusCode").to.eq(200); - expect(res.body).property("transfers").lengthOf(1); - expect(res.body.transfers[0]).property("state").eq("requested"); - expect(res.body.transfers[0]).property("id").to.be.a.uuid('v4') - requestedTransferId = res.body.transfers[0].id; }) - describe(`${seed.wallet.name} fulfill this requested transfer with tokens`, () => { - beforeEach(async () => { - const res = await request(server) - .post(`/transfers/${requestedTransferId}/fulfill`) - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerToken}`) - .send({ - tokens: [seed.token.id], - }); - expect(res).property("statusCode").to.eq(200); - }) - - it(`${seed.walletB.name} should be able to find requested transfer has been completed`, async () => { - const res = await request(server) - .get("/transfers?state=completed&limit=1000") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - expect(res).property("statusCode").to.eq(200); - expect(res.body).property("transfers").lengthOf(1); - expect(res.body.transfers[0]).property("state").eq("completed"); - expect(res.body.transfers[0]).property("id").eq(requestedTransferId); - }); - - it(`Token:#${seed.token.id} now should still belong to ${seed.walletB.name}`, async () => { - const res = await request(server) - .get(`/tokens/${seed.token.id}`) - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); - expect(res).to.have.property('statusCode', 200); - expect(res.body.wallet_id).eq(seed.walletB.id); - }); + it(`${seed.walletB.name} should be able to find requested transfer has been completed`, async () => { + const res = await request(server) + .get("/transfers?state=completed&limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("transfers").lengthOf(1); + expect(res.body.transfers[0]).property("state").eq("completed"); + expect(res.body.transfers[0]).property("id").eq(requestedTransferId); }); + it(`Token:#${seed.token.id} now should still belong to ${seed.walletB.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletB.id); + }); }); - }); - }) - - it(`Should be able to find the trust relationship: 'manage ${seed.walletC.name}`, async () => { - const res = await request(server) - .get("/trust_relationships?limit=1000") - .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${bearerTokenB}`); - expect(res).property("statusCode").to.eq(200); - expect(res).property("body").property("trust_relationships").lengthOf(1); - expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') - expect(res.body.trust_relationships.some(trust => { - return trust.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && - trust.target_wallet === seed.walletC.name; - })).eq(true); }); - it(`Via ${seed.walletB.name}, can transfer token between ${seed.walletC.name} and others`, async () => { + + it(`Should be able to find the trust relationship: 'manage ${seed.walletC.name}`, async () => { + const res = await request(server) + .get("/trust_relationships?limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + expect(res).property("body").property("trust_relationships").lengthOf(1); + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') + expect(res.body.trust_relationships.some(trust => { + return trust.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && + trust.target_wallet === seed.walletC.name; + })).eq(true); + }); + + it(`Via ${seed.walletB.name}, can transfer token between ${seed.walletC.name} and others`, async () => { const res = await request(server) .post("/transfers") .set('treetracker-api-key', apiKey) @@ -816,7 +827,7 @@ describe('Wallet integration tests', () => { .post(`/transfers/${transferId}/accept`) .set('Content-Type', "application/json") .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); + .set('Authorization', `Bearer ${bearerTokenB}`); expect(res).to.have.property('statusCode', 200); }) @@ -824,7 +835,7 @@ describe('Wallet integration tests', () => { const res = await request(server) .get(`/tokens/${seed.token.id}`) .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`); + .set('Authorization', `Bearer ${bearerTokenB}`); expect(res).to.have.property('statusCode', 200); expect(res.body.wallet_id).eq(seed.walletC.id); }); @@ -928,22 +939,13 @@ describe('Wallet integration tests', () => { }); describe(`${seed.walletB.name} try to request "yield" relationship to ${seed.wallet.name}`, () => { - let tokenB; let trustRelationshipId; + beforeEach(async () => { - let res = await request(server) - .post('/auth') - .set('treetracker-api-key', apiKey) - .send({ - wallet: seed.walletB.name, - password: seed.walletB.password, - }); - expect(res).to.have.property('statusCode', 200); - tokenB = res.body.token; res = await request(server) .post("/trust_relationships") .set('treetracker-api-key', apiKey) - .set('Authorization', `Bearer ${tokenB}`) + .set('Authorization', `Bearer ${bearerTokenB}`) .send({ trust_request_type: 'yield', requestee_wallet: seed.wallet.name, diff --git a/__tests__/trust-relationship-api.spec.js b/__tests__/trust-relationship-api.spec.js new file mode 100644 index 00000000..f059667b --- /dev/null +++ b/__tests__/trust-relationship-api.spec.js @@ -0,0 +1,174 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Trust relationship management', () => { + + let bearerToken; + let bearerTokenB; + let trustRelationshipId; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + + it("Creates send relationship", async () => { + const res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + trust_request_type: 'send', + requestee_wallet: seed.walletC.name, + }); + expect(res).property("statusCode").to.eq(200); + }); + + it("GET /trust_relationships", async () => { + const res = await request(server) + .get("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + console.log('got') + console.log(res.body) + expect(res).property("statusCode").to.eq(200); // Integration + expect(res).property("body").property("trust_relationships").lengthOf(1); // Integration + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') // Unit test, or use Joi to evaluate entire payload + }); + + it("POST /trust_relationships with wrong request type", async () => { + const res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + trust_request_type: 'wrongtype', + wallet: 'any', + }); + expect(res).property("statusCode").to.eq(422); + }); + + it("POST /trust_relationships", async () => { + const res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + trust_request_type: 'send', + requestee_wallet: seed.walletB.name, + }); + console.log(res.body) + expect(res).property("statusCode").to.eq(200); + }); + + it(`${seed.walletB.name} try to request "manage" relationship to ${seed.wallet}`, async () => { + beforeEach(async () => { + let res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`) + .send({ + trust_request_type: 'manage', + requestee_wallet: seed.wallet.name, + }); + expect(res).property("statusCode").to.eq(200); + trustRelationship = res.body; + expect(trustRelationship).property("id").to.be.a.uuid('v4') + expect(trustRelationship).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested); + trustRelationshipId = trustRelationship.id; + }) + + it(`${seed.wallet.name} accept this request`, async () => { + const res = await request(server) + .post(`/trust_relationships/${trustRelationshipId}/accept`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("state").eq("trusted"); + expect(res.body).property("type").eq("manage"); + expect(res.body).property("actor_wallet").eq(seed.walletB.name); + expect(res.body).property("target_wallet").eq(seed.wallet.name); + }); + + it(`${seed.walletB.name} try to request "yield" relationship to ${seed.wallet.name}`, async () => { + res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`) + .send({ + trust_request_type: 'yield', + requestee_wallet: seed.wallet.name, + }); + expect(res).property("statusCode").to.eq(200); + trustRelationship = res.body; + expect(trustRelationship).property("id").to.be.a.uuid('v4'); + expect(trustRelationship).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested); + trustRelationshipId = trustRelationship.id; + }) + + it(`${seed.wallet.name} accept yeild request`, async () => { + const res = await request(server) + .post(`/trust_relationships/${trustRelationshipId}/accept`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res.body).property("state").eq("trusted"); + expect(res.body).property("type").eq("manage"); + expect(res.body).property("actor_wallet").eq(seed.walletB.name); + expect(res.body).property("target_wallet").eq(seed.wallet.name); + }); + }); + +}); + + + + diff --git a/__tests__/trust-relationship-manage.js b/__tests__/trust-relationship-manage.js new file mode 100644 index 00000000..bffda97b --- /dev/null +++ b/__tests__/trust-relationship-manage.js @@ -0,0 +1,123 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Trust relationship: manage', () => { + + let bearerToken; + let bearerTokenB; + let requestedTransferId; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + }); + + beforeEach(async () => { + sinon.restore(); + }) + + it(`Should be able to find the trust relationship: manage ${seed.walletC.name}`, async () => { + const res = await request(server) + .get("/trust_relationships?limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + expect(res).property("body").property("trust_relationships").lengthOf(1); + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') + expect(res.body.trust_relationships.some(trust => { + return trust.type === TrustRelationship.ENTITY_TRUST_TYPE.manage && + trust.target_wallet === seed.walletC.name; + })).eq(true); + }); + + it(`Via ${seed.walletB.name}, can transfer token between ${seed.walletC.name} and others`, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletC.name, + }); + expect(res).property("statusCode").to.eq(202); + }); + + let transferId = 0; + it(`Send a token to ${seed.walletC.name}`, async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [seed.token.id], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletC.name, + }); + expect(res).property("statusCode").to.eq(202); + expect(res.body).property("id").to.be.a.uuid('v4') + transferId = res.body.id; + }) + + + it(`${seed.walletB.name} can accept the transfer for ${seed.walletC.name}`, async () => { + const res = await request(server) + .post(`/transfers/${transferId}/accept`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + }) + + it(`Token:#${seed.token.id} now should belong to walletC:${seed.walletC.name}`, async () => { + const res = await request(server) + .get(`/tokens/${seed.token.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).to.have.property('statusCode', 200); + expect(res.body.wallet_id).eq(seed.walletC.id); + }); +}); + diff --git a/__tests__/trust-relationship-send-cancel.js b/__tests__/trust-relationship-send-cancel.js new file mode 100644 index 00000000..cd517302 --- /dev/null +++ b/__tests__/trust-relationship-send-cancel.js @@ -0,0 +1,89 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Trust relationship: decline send', () => { + let bearerToken; + let bearerTokenB; + let trustRelationship; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + + const res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + trust_request_type: 'send', + requestee_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(200); + trustRelationship = res.body; + + }); + + beforeEach(async () => { + sinon.restore(); + }) + + it(`Cancel this request by ${seed.wallet.name}`, async () => { + const res = await request(server) + .del(`/trust_relationships/${trustRelationship.id}`) + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + }) + + it("Wallet should be able to find the relationship, and it was cancelled", async () => { + const res = await request(server) + .get("/trust_relationships?limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res).property("body").property("trust_relationships").lengthOf(1); + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') + expect(res.body.trust_relationships[0]).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.cancelled_by_originator); + }); + +}); diff --git a/__tests__/trust-relationship-send-decline.js b/__tests__/trust-relationship-send-decline.js new file mode 100644 index 00000000..cc43a983 --- /dev/null +++ b/__tests__/trust-relationship-send-decline.js @@ -0,0 +1,111 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Trust relationship: decline send', () => { + let bearerToken; + let bearerTokenB; + let bearerTokenC; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + + { + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletC.name, + password: seed.walletC.password, + }); + expect(res).to.have.property('statusCode', 200); + expect(res).property('body').property("token").a("string"); + bearerTokenC = res.body.token; + } + + }); + + beforeEach(async () => { + sinon.restore(); + }) + + + let trustRelationship; + + it(`${seed.wallet.name} request "send" trust relationship with ${seed.walletB.name} `, async () => { + const res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + trust_request_type: 'send', + requestee_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(200); + trustRelationship = res.body; + expect(trustRelationship).property("id").to.be.a.uuid('v4') + expect(trustRelationship).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested); + }); + + + it("Decline this request", async () => { + const res = await request(server) + .post(`/trust_relationships/${trustRelationship.id}/decline`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + }) + + it("Wallet should be able to find the relationship, and it was cancelled", async () => { + const res = await request(server) + .get("/trust_relationships?limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res).property("body").property("trust_relationships").lengthOf(1); + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') + expect(res.body.trust_relationships[0]).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.canceled_by_target); + }); + +}); diff --git a/__tests__/trust-relationship-send.js b/__tests__/trust-relationship-send.js new file mode 100644 index 00000000..7987495e --- /dev/null +++ b/__tests__/trust-relationship-send.js @@ -0,0 +1,154 @@ +require('dotenv').config() +const request = require('supertest'); +const server = require("../server/app"); +const { expect } = require('chai'); +const seed = require('./seed'); +const log = require('loglevel'); +const Transfer = require("../server/models/Transfer"); +const TrustRelationship = require("../server/models/TrustRelationship"); +const sinon = require("sinon"); +const chai = require("chai"); +chai.use(require('chai-uuid')); + +const apiKey = seed.apiKey; + +describe('Trust relationship: send', () => { + let bearerToken; + let bearerTokenB; + let bearerTokenC; + + before( async () => { + + await seed.clear(); + await seed.seed(); + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.wallet.name, + password: seed.wallet.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerToken = res.body.token; + expect(bearerToken).to.match(/\S+/); + } + + { + // Authorizes before each of the follow tests + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletB.name, + password: seed.walletB.password, + }); + expect(res).to.have.property('statusCode', 200); + bearerTokenB = res.body.token; + expect(bearerTokenB).to.match(/\S+/); + } + + { + const res = await request(server) + .post('/auth') + .set('treetracker-api-key', apiKey) + .send({ + wallet: seed.walletC.name, + password: seed.walletC.password, + }); + expect(res).to.have.property('statusCode', 200); + expect(res).property('body').property("token").a("string"); + bearerTokenC = res.body.token; + } + + }); + + beforeEach(async () => { + sinon.restore(); + }) + + + let trustRelationship; + + it("Try to send a token to walletB again, should fail, no trust", async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + + expect(res).property("statusCode").to.eq(202) + expect(res).property("body").property("state").to.eq("pending") + expect(res).property("body").property("parameters").property("tokens").lengthOf(0); + }); + + it(`${seed.wallet.name} request "send" trust relationship with ${seed.walletB.name} `, async () => { + const res = await request(server) + .post("/trust_relationships") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + trust_request_type: 'send', + requestee_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(200); + trustRelationship = res.body; + expect(trustRelationship).property("id").to.be.a.uuid('v4') + expect(trustRelationship).property("state").eq(TrustRelationship.ENTITY_TRUST_STATE_TYPE.requested); + }); + + + it("Accept this request", async () => { + const res = await request(server) + .post(`/trust_relationships/${trustRelationship.id}/accept`) + .set('Content-Type', "application/json") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerTokenB}`); + expect(res).property("statusCode").to.eq(200); + }) + + it("Wallet should be able to find the relationship, and it was approved", async () => { + const res = await request(server) + .get("/trust_relationships?limit=1000") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`); + expect(res).property("statusCode").to.eq(200); + expect(res).property("body").property("trust_relationships").lengthOf(1); + expect(res.body.trust_relationships[0]).property("id").to.be.a.uuid('v4') + }); + + it("Try to send a token to walletB again, this time, should success, 201", async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + tokens: [], + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(201); + expect(res).property("body").property("parameters").property("tokens").lengthOf(0); + }); + + it("Try to send bundle token to walletB again, should success, 201", async () => { + const res = await request(server) + .post("/transfers") + .set('treetracker-api-key', apiKey) + .set('Authorization', `Bearer ${bearerToken}`) + .send({ + bundle: { + bundle_size: 1, + }, + sender_wallet: seed.wallet.name, + receiver_wallet: seed.walletB.name, + }); + expect(res).property("statusCode").to.eq(201); + }); +});