Skip to content

Commit

Permalink
setPrimaryTerminalOf permissions tests done
Browse files Browse the repository at this point in the history
  • Loading branch information
odd-amphora committed Nov 27, 2021
1 parent d8b90b4 commit 7b29542
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions test/jb_directory/set_primary_terminal_of.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ import jbOperatoreStore from '../../artifacts/contracts/JBOperatorStore.sol/JBOp
import jbProjects from '../../artifacts/contracts/JBProjects.sol/JBProjects.json';
import jbTerminal from '../../artifacts/contracts/interfaces/IJBTerminal.sol/IJBTerminal.json';

// TODO(odd-amphora): Permissions.
describe('JBDirectory::setPrimaryTerminalOf(...)', function () {
const PROJECT_ID = 13;

let ADD_TERMINALS_PERMISSION_INDEX;

let SET_PRIMARY_TERMINAL_PERMISSION_INDEX;
before(async function () {
let jbOperationsFactory = await ethers.getContractFactory('JBOperations');
let jbOperations = await jbOperationsFactory.deploy();

ADD_TERMINALS_PERMISSION_INDEX = await jbOperations.ADD_TERMINALS();
SET_PRIMARY_TERMINAL_PERMISSION_INDEX = await jbOperations.SET_PRIMARY_TERMINAL();
});

async function setup() {
let [deployer, ...addrs] = await ethers.getSigners();
let caller = addrs[1];
let [deployer, projectOwner, ...addrs] = await ethers.getSigners();

let mockJbOperatorStore = await deployMockContract(deployer, jbOperatoreStore.abi);
let mockJbProjects = await deployMockContract(deployer, jbProjects.abi);
Expand All @@ -33,41 +32,41 @@ describe('JBDirectory::setPrimaryTerminalOf(...)', function () {
mockJbProjects.address,
);

let terminal1 = await deployMockContract(caller, jbTerminal.abi);
let terminal2 = await deployMockContract(caller, jbTerminal.abi);
let terminal1 = await deployMockContract(projectOwner, jbTerminal.abi);
let terminal2 = await deployMockContract(projectOwner, jbTerminal.abi);

await mockJbProjects.mock.ownerOf.withArgs(PROJECT_ID).returns(caller.address);
await mockJbProjects.mock.ownerOf.withArgs(PROJECT_ID).returns(projectOwner.address);
await mockJbOperatorStore.mock.hasPermission
.withArgs(caller.address, caller.address, PROJECT_ID, ADD_TERMINALS_PERMISSION_INDEX)
.withArgs(projectOwner.address, projectOwner.address, PROJECT_ID, ADD_TERMINALS_PERMISSION_INDEX)
.returns(true);

return { caller, deployer, addrs, jbDirectory, terminal1, terminal2 };
return { projectOwner, deployer, addrs, jbDirectory, mockJbOperatorStore, terminal1, terminal2 };
}

it(`Can't set primary terminal with address(0)`, async function () {
const { caller, jbDirectory } = await setup();
const { projectOwner, jbDirectory } = await setup();

await expect(
jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, ethers.constants.AddressZero),
jbDirectory.connect(projectOwner).setPrimaryTerminalOf(PROJECT_ID, ethers.constants.AddressZero),
).to.be.revertedWith('0x2e: ZERO_ADDRESS');
});

it('Setting primary terminal should emit an event and be added to terminals', async function () {
const { caller, jbDirectory, terminal1 } = await setup();
const { projectOwner, jbDirectory, terminal1 } = await setup();

// Initially no terminals should be set.
let initialTerminals = [...(await jbDirectory.connect(caller).terminalsOf(PROJECT_ID))];
let initialTerminals = [...(await jbDirectory.connect(projectOwner).terminalsOf(PROJECT_ID))];
expect(initialTerminals.length).to.equal(0);

const terminal1TokenAddress = ethers.Wallet.createRandom().address;
await terminal1.mock.token.returns(terminal1TokenAddress);

let tx = await jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, terminal1.address);
let tx = await jbDirectory.connect(projectOwner).setPrimaryTerminalOf(PROJECT_ID, terminal1.address);
await expect(tx)
.to.emit(jbDirectory, 'SetPrimaryTerminal')
.withArgs(PROJECT_ID, terminal1TokenAddress, terminal1.address, caller.address);
.withArgs(PROJECT_ID, terminal1TokenAddress, terminal1.address, projectOwner.address);

let resultTerminals = [...(await jbDirectory.connect(caller).terminalsOf(PROJECT_ID))];
let resultTerminals = [...(await jbDirectory.connect(projectOwner).terminalsOf(PROJECT_ID))];
resultTerminals.sort();

// After the primary terminal is set it should be added to the project.
Expand All @@ -77,35 +76,59 @@ describe('JBDirectory::setPrimaryTerminalOf(...)', function () {
expect(resultTerminals).to.eql(expectedTerminals);
});

it('Can set primary terminal if caller is not project owner but has permissions', async function () {
const { projectOwner, addrs, jbDirectory, mockJbOperatorStore, terminal1 } = await setup();
let caller = addrs[1];

await terminal1.mock.token.returns(ethers.Wallet.createRandom().address);
await mockJbOperatorStore.mock.hasPermission
.withArgs(caller.address, projectOwner.address, PROJECT_ID, SET_PRIMARY_TERMINAL_PERMISSION_INDEX)
.returns(true);

await expect(jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, terminal1.address)).to.not.be.reverted;
})

it(`Can't set primary terminal if caller is not project owner and does not have permission`, async function () {
const { projectOwner, addrs, jbDirectory, mockJbOperatorStore, terminal1 } = await setup();
let caller = addrs[1];

await terminal1.mock.token.returns(ethers.Wallet.createRandom().address);
await mockJbOperatorStore.mock.hasPermission
.withArgs(caller.address, projectOwner.address, PROJECT_ID, SET_PRIMARY_TERMINAL_PERMISSION_INDEX)
.returns(false);

await expect(jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, terminal1.address)).to.be.reverted;
})

it(`Can't set the same primary terminal twice in a row`, async function () {
const { caller, jbDirectory, terminal1 } = await setup();
const { projectOwner, jbDirectory, terminal1 } = await setup();

await terminal1.mock.token.returns(ethers.Wallet.createRandom().address);

// Should succeed on the first attempt and then fail on the second.
await jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, terminal1.address);
await jbDirectory.connect(projectOwner).setPrimaryTerminalOf(PROJECT_ID, terminal1.address);
await expect(
jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, terminal1.address),
jbDirectory.connect(projectOwner).setPrimaryTerminalOf(PROJECT_ID, terminal1.address),
).to.be.revertedWith('0x2f: ALREADY_SET');
});

it('Multiple terminals for the same project with the same token', async function () {
const { caller, jbDirectory, terminal1, terminal2 } = await setup();
const { projectOwner, jbDirectory, terminal1, terminal2 } = await setup();

let token = ethers.Wallet.createRandom().address;
await terminal1.mock.token.returns(token);
await terminal2.mock.token.returns(token);

let terminals = [terminal1.address, terminal2.address];
await jbDirectory.connect(caller).addTerminalsOf(PROJECT_ID, terminals);
await jbDirectory.connect(projectOwner).addTerminalsOf(PROJECT_ID, terminals);

await jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, terminal1.address);
expect(await jbDirectory.connect(caller).primaryTerminalOf(PROJECT_ID, token)).to.equal(
await jbDirectory.connect(projectOwner).setPrimaryTerminalOf(PROJECT_ID, terminal1.address);
expect(await jbDirectory.connect(projectOwner).primaryTerminalOf(PROJECT_ID, token)).to.equal(
terminal1.address,
);

await jbDirectory.connect(caller).setPrimaryTerminalOf(PROJECT_ID, terminal2.address);
expect(await jbDirectory.connect(caller).primaryTerminalOf(PROJECT_ID, token)).to.equal(
await jbDirectory.connect(projectOwner).setPrimaryTerminalOf(PROJECT_ID, terminal2.address);
expect(await jbDirectory.connect(projectOwner).primaryTerminalOf(PROJECT_ID, token)).to.equal(
terminal2.address,
);
});
Expand Down

0 comments on commit 7b29542

Please sign in to comment.