UNPKG

machinomy

Version:

Micropayments powered by Ethereum

149 lines 7.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const sinon = require("sinon"); const BigNumber = require("bignumber.js"); const client_1 = require("./client"); const expects_rejection_1 = require("./util/expects_rejection"); const payment_1 = require("./payment"); const accept_payment_request_1 = require("./accept_payment_request"); const accept_payment_response_1 = require("./accept_payment_response"); const accept_token_response_1 = require("./accept_token_response"); const PaymentRequiredResponse_1 = require("./PaymentRequiredResponse"); const Signature_1 = require("./Signature"); const expect = require('expect'); describe('ClientImpl', () => { let transport; let channelManager; let client; beforeEach(() => { transport = {}; channelManager = {}; client = new client_1.ClientImpl(transport, channelManager); }); describe('doPreflight', () => { it('returns payment required when a payment required or OK response comes back', () => { transport.paymentRequired = sinon.stub().resolves(PaymentRequiredResponse_1.PaymentRequiredResponseSerializer.instance.deserialize({ 'paywall-version': PaymentRequiredResponse_1.TRANSPORT_VERSION, 'paywall-address': '0x1234', 'paywall-price': '1000', 'paywall-gateway': 'http://honkhost:8080/machinomy', 'paywall-meta': 'hello', 'paywall-token-contract': '0xbeef', 'paywall-channels': '[{"channelId": "0x111", "spent": "10", "sign": "0xbabe"}]' })); return client.doPreflight('0xcafe', 'http://honkhost:1234/site').then((res) => { expect(res.receiver).toBe('0x1234'); expect(res.price).toEqual(new BigNumber.BigNumber(1000)); expect(res.gateway).toBe('http://honkhost:8080/machinomy'); expect(res.meta).toBe('hello'); expect(res.tokenContract).toBe('0xbeef'); expect(res.remoteChannelInfo.channels.length).toBe(1); expect(res.remoteChannelInfo.channels[0].channelId).toBe('0x111'); expect(res.remoteChannelInfo.channels[0].spent).toEqual(new BigNumber.BigNumber(10)); expect(res.remoteChannelInfo.channels[0].sign).toEqual(Signature_1.default.fromRpcSig('0xbabe')); }); }); it('throws an error for any other status code', () => { transport.paymentRequired = sinon.stub().rejects(); return expects_rejection_1.default(client.doPreflight('0xcafe', 'http://honkhost:1234/site')); }); }); describe('doPayment', () => { let paymentJson; beforeEach(() => { transport.doPayment = sinon.stub().resolves(new accept_payment_response_1.AcceptPaymentResponse('beep')); paymentJson = { channelId: '0x1234', value: '1000', sender: '0xbeef', receiver: '0xdead', price: '100', channelValue: '1000', v: 27, r: '0x000000000000000000000000000000000000000000000000000000000000000a', s: '0x000000000000000000000000000000000000000000000000000000000000000a', contractAddress: '0xab', token: '0x123' }; }); it('returns an AcceptPaymentResponse on success', () => { const payment = payment_1.PaymentSerde.instance.deserialize(paymentJson); return client.doPayment(payment, 'gateway').then((res) => { expect(res.token).toBe('beep'); }); }); it('emits willSendPayment and didSendPayment', () => { const payment = payment_1.PaymentSerde.instance.deserialize(paymentJson); const will = sinon.stub(); const did = sinon.stub(); client.addListener('willSendPayment', will); client.addListener('didSendPayment', did); return client.doPayment(payment, 'gateway').then((res) => { expect(will.called).toBe(true); expect(did.called).toBe(true); }); }); it('throws an error if transport reject', () => { const payment = payment_1.PaymentSerde.instance.deserialize(paymentJson); transport.doPayment = sinon.stub().rejects(); return expects_rejection_1.default(client.doPayment(payment, 'gateway')); }); it('throws an error if transport throw', () => { const payment = payment_1.PaymentSerde.instance.deserialize(paymentJson); transport.doPayment = sinon.stub().throws('any'); return expects_rejection_1.default(client.doPayment(payment, 'gateway')); }); }); describe('acceptPayment', () => { it('returns an AcceptPaymentResponse from the channel manager', () => { const req = accept_payment_request_1.AcceptPaymentRequestSerde.instance.deserialize({ payment: { channelId: '0x1234', value: '1000', sender: '0xbeef', receiver: '0xdead', price: '100', channelValue: '1000', v: 27, r: '0xa', s: '0xb', contractAddress: '0xab', token: '0x123' } }); channelManager.acceptPayment = sinon.stub().withArgs(req.payment).resolves('token'); return client.acceptPayment(req).then((res) => { expect(res.token).toBe('token'); }); }); }); describe('doVerify', () => { it('returns an AcceptTokenResponse if the token is accepted', () => { transport.doVerify = sinon.stub().resolves(new accept_token_response_1.AcceptTokenResponse(true)); return client.doVerify('token', 'gateway').then((res) => { expect(res.status).toBe(true); }); }); it('returns an AcceptTokenResponse if the token is rejected', () => { transport.doVerify = sinon.stub().resolves(new accept_token_response_1.AcceptTokenResponse(false)); return client.doVerify('token', 'gateway').then((res) => { expect(res.status).toBe(false); }); }); it('returns a false AcceptTokenResponse if an error occurs', () => { transport.doVerify = sinon.stub().throws(); return client.doVerify('token', 'gateway').then((res) => { expect(res.status).toBe(false); }); }); }); describe('acceptVerify', () => { it('returns an AcceptTokenResponse based on the request', () => { channelManager.verifyToken = sinon.stub().withArgs('token').resolves(true); return client.acceptVerify({ token: 'token' }).then((res) => { expect(res.status).toBe(true); }); }); }); }); //# sourceMappingURL=client.test.js.map