|
| 1 | +import 'should'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import * as express from 'express'; |
| 4 | +import { ApiResponseError, BitGo } from 'bitgo'; |
| 5 | +import { promiseWrapper, redirectRequest } from '../../../src/clientRoutes'; |
| 6 | + |
| 7 | +describe('common methods', () => { |
| 8 | + describe('redirectRequest', () => { |
| 9 | + let bitgo: BitGo; |
| 10 | + let req: express.Request; |
| 11 | + let next: express.NextFunction; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + bitgo = new BitGo({ env: 'test' }); |
| 15 | + req = { |
| 16 | + body: {}, |
| 17 | + params: {}, |
| 18 | + bitgo, |
| 19 | + } as express.Request; |
| 20 | + next = () => undefined; |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + sinon.restore(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should handle GET request and return status and body', async () => { |
| 28 | + const url = 'https://example.com/api'; |
| 29 | + const response = { res: { statusCode: 200 }, result: async () => ({ success: true }) }; |
| 30 | + sinon |
| 31 | + .stub(bitgo, 'get') |
| 32 | + .withArgs(url) |
| 33 | + .returns(response as any); |
| 34 | + |
| 35 | + const result = await redirectRequest(bitgo, 'GET', url, req, next); |
| 36 | + result.status.should.equal(200); |
| 37 | + result.body.should.deepEqual({ success: true }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should handle POST request and return status and body', async () => { |
| 41 | + const url = 'https://example.com/api'; |
| 42 | + req.body = { data: 'test' }; |
| 43 | + const response = { res: { statusCode: 201 }, result: async () => ({ success: true }) }; |
| 44 | + sinon |
| 45 | + .stub(bitgo, 'post') |
| 46 | + .withArgs(url) |
| 47 | + .returns({ send: () => response } as any); |
| 48 | + |
| 49 | + const result = await redirectRequest(bitgo, 'POST', url, req, next); |
| 50 | + result.status.should.equal(201); |
| 51 | + result.body.should.deepEqual({ success: true }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle error response and return status and body', async () => { |
| 55 | + const url = 'https://example.com/api'; |
| 56 | + const response = { |
| 57 | + result: async () => { |
| 58 | + throw new ApiResponseError('Bad Request', 400); |
| 59 | + }, |
| 60 | + }; |
| 61 | + sinon |
| 62 | + .stub(bitgo, 'get') |
| 63 | + .withArgs(url) |
| 64 | + .returns(response as any); |
| 65 | + |
| 66 | + await redirectRequest(bitgo, 'GET', url, req, next).should.be.rejectedWith(ApiResponseError, { |
| 67 | + message: 'Bad Request', |
| 68 | + status: 400, |
| 69 | + }); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('promiseWrapper', () => { |
| 74 | + afterEach(() => { |
| 75 | + sinon.restore(); |
| 76 | + }); |
| 77 | + it('should handle successful request', async () => { |
| 78 | + const handler = sinon.stub().resolves({ status: 200, body: { success: true } }); |
| 79 | + const req: any = {}; |
| 80 | + const res: any = { |
| 81 | + status: sinon.stub().returnsThis(), |
| 82 | + send: sinon.stub().returnsThis(), |
| 83 | + }; |
| 84 | + const next = sinon.stub(); |
| 85 | + |
| 86 | + await promiseWrapper(handler)(req, res, next); |
| 87 | + |
| 88 | + res.status.calledWith(200).should.be.true(); |
| 89 | + res.send.calledWith({ success: true }).should.be.true(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle successful request with status 202', async () => { |
| 93 | + const handler = sinon.stub().resolves({ status: 202, body: { success: true } }); |
| 94 | + const req: any = {}; |
| 95 | + const res: any = { |
| 96 | + status: sinon.stub().returnsThis(), |
| 97 | + send: sinon.stub().returnsThis(), |
| 98 | + }; |
| 99 | + const next = sinon.stub(); |
| 100 | + |
| 101 | + await promiseWrapper(handler)(req, res, next); |
| 102 | + |
| 103 | + res.status.calledWith(202).should.be.true(); |
| 104 | + res.send.calledWith({ success: true }).should.be.true(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle successful request with default status', async () => { |
| 108 | + const handler = sinon.stub().resolves({ success: true }); |
| 109 | + const req: any = {}; |
| 110 | + const res: any = { |
| 111 | + status: sinon.stub().returnsThis(), |
| 112 | + send: sinon.stub().returnsThis(), |
| 113 | + }; |
| 114 | + const next = sinon.stub(); |
| 115 | + |
| 116 | + await promiseWrapper(handler)(req, res, next); |
| 117 | + |
| 118 | + res.status.calledWith(200).should.be.true(); |
| 119 | + res.send.calledWith({ success: true }).should.be.true(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should handle error request', async () => { |
| 123 | + const handler = sinon.stub().rejects(new Error('Test error')); |
| 124 | + const req: any = {}; |
| 125 | + const res: any = { |
| 126 | + status: sinon.stub().returnsThis(), |
| 127 | + send: sinon.stub().returnsThis(), |
| 128 | + }; |
| 129 | + const next = sinon.stub(); |
| 130 | + |
| 131 | + await promiseWrapper(handler)(req, res, next); |
| 132 | + |
| 133 | + res.status.calledWith(500).should.be.true(); |
| 134 | + res.send.calledWithMatch((result: any) => result.message === 'Test error').should.be.true(); |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments