Skip to content

Commit

Permalink
feat: tokens list support 'start' parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dadiorchen committed Dec 10, 2020
1 parent 3a7a3c8 commit a46f50d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docs/api/spec/treetracker-token-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ paths:
- type: string
- type: number
example: zaven2
- name: start
in: query
description: 'Where does the list start, 1 means start from the beginning of the list'
required: false
style: form
explode: true
schema:
type: integer
format: int32
example: 10
responses:
'200':
description: ''
Expand Down
5 changes: 3 additions & 2 deletions server/routes/tokenRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ tokenRouter.get('/',
req.query,
Joi.object({
limit: Joi.number().required(),
start: Joi.number().min(1).max(10000).integer(),
wallet: Joi.string(),
})
);
const {limit, wallet} = req.query;
const {limit, wallet, start} = req.query;
const session = new Session();
const tokenService = new TokenService(session);
const walletService = new WalletService(session);
Expand All @@ -64,7 +65,7 @@ tokenRouter.get('/',
}

//filter tokens by query, TODO optimization required
tokens = tokens.slice(0, limit);
tokens = tokens.slice(start? start-1:0, limit);
const tokensJson = [];
for(const token of tokens){
const json = await token.toJSON();
Expand Down
17 changes: 12 additions & 5 deletions server/routes/tokenRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,22 @@ describe("tokenRouter", () => {
entity_id: 1,
tree_id: 1,
});
const token2 = new Token({
id: 2,
token: "test-uuid2",
entity_id: 2,
tree_id: 2,
});
const wallet = new Wallet(1);
sinon.stub(TokenService.prototype, "getByOwner").resolves([token]);
sinon.stub(TokenService.prototype, "getByOwner").resolves([token, token2]);
sinon.stub(WalletService.prototype, "getById").resolves(wallet);
const res = await request(app)
.get("/?limit=10");
.get("/?limit=10&start=2");
expect(res).property("statusCode").eq(200);
expect(res.body.tokens[0]).property("token").eq("test-uuid");
expect(res.body.tokens[0]).property("links").property("capture").eq("/capture/1");
expect(res.body.tokens[0]).property("links").property("tree").eq("/capture/1/tree");
expect(res.body.tokens).lengthOf(1);
expect(res.body.tokens[0]).property("token").eq("test-uuid2");
expect(res.body.tokens[0]).property("links").property("capture").eq("/capture/2");
expect(res.body.tokens[0]).property("links").property("tree").eq("/capture/2/tree");
});

it("successfully, sub wallet", async () => {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/walletRouter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("walletRouter", ()=> {

describe("get /wallets", () => {

it.only("successfully", async () => {
it("successfully", async () => {
sinon.stub(WalletService.prototype, "getById").resolves(new Wallet({id:1}));
sinon.stub(TrustService.prototype, "convertToResponse").resolves({id:1});
sinon.stub(TokenService.prototype, "countTokenByWallet").resolves(10);
Expand Down

0 comments on commit a46f50d

Please sign in to comment.