From 71d98941e1d88112097151fda3cc5be36f1585ff Mon Sep 17 00:00:00 2001 From: weiyu Date: Thu, 27 Jul 2023 14:34:52 -0500 Subject: [PATCH 01/15] fix: rebuild __tests__ file structure, copy original wallet get test --- __tests__/integration/auth.spec.js | 4 +- .../bundle-transfer-decline.spec.js | 8 +- __tests__/integration/wallet-get.spec.js | 159 ++++++++++++++++++ __tests__/mock-data/{ => token}/TokenA.json | 0 __tests__/mock-data/{ => wallet}/Meisze.json | 0 __tests__/mock-data/{ => wallet}/Zaven.json | 0 __tests__/mock-data/wallet/walletA.json | 4 + __tests__/try.js | 24 +++ __tests__/{integration => utils}/testUtils.js | 0 package.json | 2 + 10 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 __tests__/integration/wallet-get.spec.js rename __tests__/mock-data/{ => token}/TokenA.json (100%) rename __tests__/mock-data/{ => wallet}/Meisze.json (100%) rename __tests__/mock-data/{ => wallet}/Zaven.json (100%) create mode 100644 __tests__/mock-data/wallet/walletA.json create mode 100644 __tests__/try.js rename __tests__/{integration => utils}/testUtils.js (100%) diff --git a/__tests__/integration/auth.spec.js b/__tests__/integration/auth.spec.js index 9d5bd046..b67db21d 100644 --- a/__tests__/integration/auth.spec.js +++ b/__tests__/integration/auth.spec.js @@ -4,8 +4,8 @@ const { expect } = require('chai'); const chai = require('chai'); const server = require('../../server/app'); chai.use(require('chai-uuid')); -const Zaven = require('../mock-data/Zaven.json'); -const testUtils = require('./testUtils'); +const Zaven = require('../mock-data/wallet/Zaven.json'); +const testUtils = require('../utils/testUtils'); describe('Authentication', () => { let registeredUser; diff --git a/__tests__/integration/bundle-transfer-decline.spec.js b/__tests__/integration/bundle-transfer-decline.spec.js index cfe936c7..eacbc00b 100644 --- a/__tests__/integration/bundle-transfer-decline.spec.js +++ b/__tests__/integration/bundle-transfer-decline.spec.js @@ -4,10 +4,10 @@ const { expect } = require('chai'); const chai = require('chai'); const server = require('../../server/app'); chai.use(require('chai-uuid')); -const Zaven = require('../mock-data/Zaven.json'); -const Meisze = require('../mock-data/Meisze.json'); -const testUtils = require('./testUtils'); -const TokenA = require('../mock-data/TokenA'); +const Zaven = require('../mock-data/wallet/Zaven.json'); +const Meisze = require('../mock-data/wallet/Meisze.json'); +const testUtils = require('../utils/testUtils'); +const TokenA = require('../mock-data/token/TokenA.json'); const TransferEnums = require('../../server/utils/transfer-enum'); describe('Zaven request to send 1 token to Meisze', () => { diff --git a/__tests__/integration/wallet-get.spec.js b/__tests__/integration/wallet-get.spec.js new file mode 100644 index 00000000..aa3ea975 --- /dev/null +++ b/__tests__/integration/wallet-get.spec.js @@ -0,0 +1,159 @@ +require('dotenv').config(); +const request = require('supertest'); +const {expect} = require('chai'); +const sinon = require('sinon'); +const chai = require('chai'); +const server = require('../../server/app'); +const seed = require('../seed'); +chai.use(require('chai-uuid')); +const WalletService = require('../../server/services/WalletService'); + +const {apiKey} = seed; + +describe('Wallet: Get wallets of an account', () => { + let bearerTokenA; + 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); + bearerTokenA = res.body.token; + expect(bearerTokenA).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 walletService = new WalletService(); + + for (let i = 0; i < 10; i += 1) { + await walletService.createWallet(seed.wallet.id, `test${i}`) + } + + const res = await walletService.getAllWallets(seed.wallet.id); + expect(res.count).to.eq(11); + }); + + beforeEach(async () => { + sinon.restore(); + }); + + it('Get wallets of WalletA without params', async () => { + const res = await request(server) + .get('/wallets') + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenA}`); + + expect(res).property('statusCode').to.eq(200); + expect(res.body.total).to.eq(11); + + const resB = await request(server) + .get('/wallets') + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenB}`); + + expect(resB).property('statusCode').to.eq(200); + expect(resB.body.total).to.eq(2); + }); + + it('Get wallet by wallet name, success', async () => { + const res = await request(server) + .get('/wallets') + .query({name: 'walletB'}) + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenB}`); + + expect(res).property('statusCode').to.eq(200); + expect(res.body.total).to.eq(1); + }) + + it('Get wallet which is managed by other account', async () => { + const res = await request(server) + .get(`/wallets`) + .query({name: 'test0'}) + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenB}`); + + expect(res).property('statusCode').to.eq(200); + expect(res.body.total).to.eq(1); + expect(res.body.wallets[0].name).to.eq(seed.walletB.name); + }) + + it('Get wallet with offset val', async () => { + const res = await request(server) + .get('/wallets') + .query({offset: 0}) + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenA}`); + + expect(res).property('statusCode').to.eq(200); + expect(res.body.total).to.eq(11); + + const resB = await request(server) + .get('/wallets') + .query({limit: 100, offset: 2}) + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenA}`); + + expect(resB).property('statusCode').to.eq(200); + expect(resB.body.total).to.eq(9); + + const resC = await request(server) + .get('/wallets') + .query({limit: 2, offset: 0}) + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenA}`); + expect(resC).property('statusCode').to.eq(200); + expect(resC.body.total).to.eq(2); + }) + + it('Get wallet by valid uuid', async () => { + const res = await request(server) + .get(`/wallets/${seed.walletC.id}`) + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenA}`); + + expect(res).property('statusCode').to.eq(200); + }) + + it('Get wallet by valid uuid which does not exist', async () => { + const res = await request(server) + .get(`/wallets/00a6fa25-df29-4701-9077-557932591766`) + .set('treetracker-api-key', apiKey) + .set('content-type', 'application/json') + .set('Authorization', `Bearer ${bearerTokenA}`); + + expect(res).property('statusCode').to.eq(404); + }) +}) diff --git a/__tests__/mock-data/TokenA.json b/__tests__/mock-data/token/TokenA.json similarity index 100% rename from __tests__/mock-data/TokenA.json rename to __tests__/mock-data/token/TokenA.json diff --git a/__tests__/mock-data/Meisze.json b/__tests__/mock-data/wallet/Meisze.json similarity index 100% rename from __tests__/mock-data/Meisze.json rename to __tests__/mock-data/wallet/Meisze.json diff --git a/__tests__/mock-data/Zaven.json b/__tests__/mock-data/wallet/Zaven.json similarity index 100% rename from __tests__/mock-data/Zaven.json rename to __tests__/mock-data/wallet/Zaven.json diff --git a/__tests__/mock-data/wallet/walletA.json b/__tests__/mock-data/wallet/walletA.json new file mode 100644 index 00000000..f3fc27e8 --- /dev/null +++ b/__tests__/mock-data/wallet/walletA.json @@ -0,0 +1,4 @@ +{ + "name": "walletA", + "password": "test1234" +} \ No newline at end of file diff --git a/__tests__/try.js b/__tests__/try.js new file mode 100644 index 00000000..91842dc2 --- /dev/null +++ b/__tests__/try.js @@ -0,0 +1,24 @@ +// require('dotenv').config(); +// const request = require('supertest'); +// const {expect} = require('chai'); +// const sinon = require('sinon'); +// const chai = require('chai'); +// const server = require('../server/app'); +// const seed = require('./seed'); +// const walletA = require('./mock-data/wallet/Zaven.json'); +// chai.use(require('chai-uuid')); +// +// const { +// register, +// registerAndLogin, +// clear, +// sendAndPend, +// addToken} = require('./utils/testUtils'); +// +// describe('try something I like', () => { +// it('try testUtils', async () => { +// const result = await registerAndLogin(walletA); +// +// console.log(result); +// }) +// }) \ No newline at end of file diff --git a/__tests__/integration/testUtils.js b/__tests__/utils/testUtils.js similarity index 100% rename from __tests__/integration/testUtils.js rename to __tests__/utils/testUtils.js diff --git a/package.json b/package.json index f9cb22fd..0e5b18ac 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "test-seed": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --timeout 10000 './__tests__/seed.spec.js'", "test-seed-ci": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.ci mocha -r dotenv/config --timeout 10000 './__tests__/seed.spec.js'", "test-integration": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --exit --timeout 20000 './__tests__/*.spec.js' './__tests__/integration/*.spec.js'", + "try": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --exit --timeout 20000 './__tests__/try.js'", + "test-integration-ci": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.ci mocha -r dotenv/config --exit --timeout 20000 --parallel=false './__tests__/*.spec.js' './__tests__/integration/*.spec.js'", "test-watch": "NODE_ENV=test NODE_LOG_LEVEL=info mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/supertest.js'", "test-watch-debug": "NODE_ENV=test NODE_LOG_LEVEL=debug DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --timeout 10000 -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/**/*.spec.js'", From b96ca91b0750e4df3d34cf0bc0f0b4f56c0ac8bb Mon Sep 17 00:00:00 2001 From: weiyu Date: Thu, 27 Jul 2023 18:33:10 -0500 Subject: [PATCH 02/15] feat: refactor wallet get, add one function in testUnit, some mock data --- __tests__/integration/wallet-get.spec.js | 117 ++++++-------- __tests__/mock-data/wallet/walletB.json | 4 + __tests__/mock-data/wallet/walletC.json | 4 + __tests__/mock-data/wallet/wallets.json | 42 +++++ __tests__/utils/testUtils.js | 188 +++++++++++++---------- 5 files changed, 206 insertions(+), 149 deletions(-) create mode 100644 __tests__/mock-data/wallet/walletB.json create mode 100644 __tests__/mock-data/wallet/walletC.json create mode 100644 __tests__/mock-data/wallet/wallets.json diff --git a/__tests__/integration/wallet-get.spec.js b/__tests__/integration/wallet-get.spec.js index aa3ea975..23b99d3a 100644 --- a/__tests__/integration/wallet-get.spec.js +++ b/__tests__/integration/wallet-get.spec.js @@ -4,90 +4,69 @@ const {expect} = require('chai'); const sinon = require('sinon'); const chai = require('chai'); const server = require('../../server/app'); -const seed = require('../seed'); chai.use(require('chai-uuid')); -const WalletService = require('../../server/services/WalletService'); +const {registerAndLogin, clear, feedSubWallets} = require('../utils/testUtils'); +const walletAInfo = require('../mock-data/wallet/walletA.json'); +const walletBInfo = require('../mock-data/wallet/walletB.json'); +const wallets = require('../mock-data/wallet/wallets.json'); -const {apiKey} = seed; describe('Wallet: Get wallets of an account', () => { - let bearerTokenA; - let bearerTokenB; + let walletA; + let walletB; 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); - bearerTokenA = res.body.token; - expect(bearerTokenA).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 walletService = new WalletService(); - - for (let i = 0; i < 10; i += 1) { - await walletService.createWallet(seed.wallet.id, `test${i}`) - } - - const res = await walletService.getAllWallets(seed.wallet.id); - expect(res.count).to.eq(11); - }); + await clear(); + }) beforeEach(async () => { - sinon.restore(); + walletA = await registerAndLogin(walletAInfo); + // what registerAndLogin returned? + // walletA = { + // name: , + // password: , + // salt: , + // logo_url: null, + // created_at: