From 79a52c4d8e9db8906e51faddc372440670e6b87e Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Thu, 9 Apr 2020 16:32:20 -0700 Subject: [PATCH] feat: Check that makeInstance() returns an actual invite Includes a test. closes #820 --- packages/zoe/src/zoe.js | 8 ++++- .../unitTests/contracts/brokenAutoRefund.js | 27 ++++++++++++++++ .../contracts/test-brokenContract.js | 31 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 packages/zoe/test/unitTests/contracts/brokenAutoRefund.js create mode 100644 packages/zoe/test/unitTests/contracts/test-brokenContract.js diff --git a/packages/zoe/src/zoe.js b/packages/zoe/src/zoe.js index e9cfdfdba769..a632a028c833 100644 --- a/packages/zoe/src/zoe.js +++ b/packages/zoe/src/zoe.js @@ -543,7 +543,13 @@ const makeZoe = (additionalEndowments = {}) => { // Once the contract is made, we add the publicAPI to the // contractRecord instanceTable.update(instanceHandle, { publicAPI }); - return invite; + return inviteIssuer.isLive(invite).then(success => { + assert( + success, + details`invites must be issued by the inviteIssuer.`, + ); + return invite; + }); }); }; diff --git a/packages/zoe/test/unitTests/contracts/brokenAutoRefund.js b/packages/zoe/test/unitTests/contracts/brokenAutoRefund.js new file mode 100644 index 000000000000..b37d79d1c95c --- /dev/null +++ b/packages/zoe/test/unitTests/contracts/brokenAutoRefund.js @@ -0,0 +1,27 @@ +// @ts-check +import harden from '@agoric/harden'; + +/** + * This is a a broken contact to test zoe's error handling + * @type {import('@agoric/zoe').MakeContract} zoe - the contract facet of zoe + */ +export const makeContract = harden(zoe => { + const makeSeatInvite = () => { + const seat = harden({ + makeOffer: () => { + // eslint-disable-next-line no-use-before-define + zoe.complete(harden([inviteHandle])); + return `The offer was accepted`; + }, + }); + const { invite, inviteHandle } = zoe.makeInvite(seat, { + seatDesc: 'getRefund', + }); + return invite; + }; + return harden({ + // should be makeSeatInvite(). Intentionally wrong to provoke an error. + invite: makeSeatInvite, + publicAPI: {}, + }); +}); diff --git a/packages/zoe/test/unitTests/contracts/test-brokenContract.js b/packages/zoe/test/unitTests/contracts/test-brokenContract.js new file mode 100644 index 000000000000..193ab73755f4 --- /dev/null +++ b/packages/zoe/test/unitTests/contracts/test-brokenContract.js @@ -0,0 +1,31 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { test } from 'tape-promise/tape'; +// eslint-disable-next-line import/no-extraneous-dependencies +import bundleSource from '@agoric/bundle-source'; + +import harden from '@agoric/harden'; + +import { makeZoe } from '../../../src/zoe'; +// TODO: Remove setupBasicMints and rename setupBasicMints2 +import { setup } from '../setupBasicMints2'; + +const automaticRefundRoot = `${__dirname}/brokenAutoRefund`; + +test.only('zoe - brokenAutomaticRefund', async t => { + t.plan(1); + // Setup zoe and mints + const { moolaR } = setup(); + const zoe = makeZoe({ require }); + // Pack the contract. + const { source, moduleFormat } = await bundleSource(automaticRefundRoot); + const installationHandle = zoe.install(source, moduleFormat); + + const issuerKeywordRecord = harden({ Contribution: moolaR.issuer }); + + // 1: Alice tries to create an instance, but the contract is badly written. + t.rejects( + () => zoe.makeInstance(installationHandle, issuerKeywordRecord), + new Error('invites must be issued by InviteIssuer'), + 'makeInstance should have thrown', + ); +});