const assert = require('assert');
const SHOP_WASM_PATH = './contracts/shopcontract.wasm';
const SHOP_ABI_PATH = './contracts/shopcontract.abi';
describe('Shop contract', function (eoslime) {
// Increase mocha(testing framework) time, otherwise tests fails
shopAccount = await eoslime.Account.createRandom();
shopContract = await eoslime.Contract.deploy(SHOP_WASM_PATH, SHOP_ABI_PATH);
it('Should have two owners', async () => {
shopAccount = eoslime.Account.load(shopAccount.name, shopAccount.executor.privateKey, 'owner');
const firstOwnerKeys = await eoslime.utils.generateKeys();
const secondOwnerKeys = await eoslime.utils.generateKeys();
await shopAccount.addOnBehalfKey(firstOwnerKeys.publicKey)
await shopAccount.addOnBehalfKey(secondOwnerKeys.publicKey);
authorityInfo = await shopAccount.getAuthorityInfo();
// Check the owner authority has both owner keys
assert(authorityInfo.required_auth.keys.find((keyData) => { return keyData.key == keysPair.publicKey }));
it('Should have a shop manager with the ability to withdraw the daily turnover', async () => {
const shopManagerKeys = await eoslime.utils.generateKeys();
await shopAccount.addAuthority('shopmanager');
await shopAccount.setAuthorityAbilities('shopmanager', [
{ action: 'orderproducts', contract: shopContract.name }
const shopManagerAuthority = eoslime.Account.load(shopAccount.name, shopAccount.privateKey, 'shopmanager');
await shopManagerAuthority.addOnBehalfKey(shopManagerKeys.publicKey);
it('Should have a turnover authority with the ability to withdraw the daily turnover', async () => {
await shopAccount.addAuthority('turnover');
await shopAccount.setAuthorityAbilities('turnover', [
{ action: 'withdraw', contract: shopContract.name }
const turnoverAuthority = eoslime.Account.load(shopAccount.name, shopAccount.privateKey, 'turnover');
await turnoverAuthority.increaseThreshold(2);
const shopManagerWeight = 1;
await turnoverAuthority.addOnBehalfKey(firstOwnerKeys.publicKey, ownerWeight);
await turnoverAuthority.addOnBehalfKey(secondOwnerKeys.publicKey, ownerWeight);
await turnoverAuthority.addOnBehalfKey(shopManagerAuthority.publicKey, shopManagerWeight);
it('Should have a cooker', async () => {
await shopAccount.addAuthority('cooker');
await shopAccount.setAuthorityAbilities('cooker', [
{ action: 'readypizza', contract: shopContract.name }
it('Should be able for a customer to buy a piece of our Pizza', async () => {
const bob = await eoslime.Account.createRandom();
// Buy two pieces of the pizza
await shopContract.actions.buy(bob.name, { from: bob, tokens: "2.0000 EOS" });
await shopContract.actions.buy(bob.name, { from: bob, tokens: "2.0000 EOS", unique: true });
it('Should be able for a customer to order a whole Pizza and receive it once it is ready', async () => {
shopContract.actions.orderpizza.on('processed', (txReceipt, inputParams) => {
await shopContract.actions.readypizza(cooker.name, inputParams[0], { from: cooker });
await shopContract.actions.orderpizza(bob.name, { from: bob, tokens: "10.0000 EOS" });
const orderedPizzas = await shopContract.tables.orderedpizzas.find();
assert(orderedPizzas[0].buyer == bob.name);
it('Should be able for shopper manager to order more cooking products', async () => {
await shopContract.actions.orderproduct('cheese', { from: shopManagerAuthority });
const orderedProducts = await shopContract.tables.coockingproducts.find();
assert(orderedProducts[0].product == 'cheese');
it('Should be able for shopper manager to withdraw the daily turnover', async () => {
const turnoverMultiSigAuthority = eoslime.MultiSigAccount.load(shopAccount.name, shopManagerAuthority.privateKey, 'turnover');
turnoverMultiSigAuthority.loadKeys([firstOwnerKeys.privateKey, secondOwnerKeys.privateKey]);
// Make a withdrawal proposal
const proposalId = await turnoverMultiSigAuthority.propose(shopContract.actions.withdraw, []);
// The first owner approves the withdrawal
await turnoverMultiSigAuthority.approve(firstOwnerKeys.publicKey, proposalId);
// Shop manager is able to process the withdrawal
await turnoverMultiSigAuthority.processProposal(proposalId);
// Check our shop account got the turnover
const shopBalance = await shopAccount.getBalance();
assert(shopBalance[0] == '14.0000 EOS', 'Incorrect tokens amount after send');