I have this situation in my smart contract:
JavaScript
x
6
1
address[] public allowedUsers;
2
3
function allowUser(address _newUser) public onlyOwner {
4
allowedUser.push(_newUser);
5
}
6
I’m using truffle and his test suite and then I wrote this case, that fails maybe because I’m not using the only owner method in the right way:
JavaScript
1
17
17
1
const MyContract = artifacts.require("../contracts/MyContract.sol");
2
3
contract("MyContract", accounts => {
4
it("should deploy the contract and allow the user", async () => {
5
const contract = await MyContract.deployed();
6
7
const account = accounts[0];
8
const owner = await contract.owner.call()
9
10
await contract.allowUser(account).call({ from: owner });
11
12
const allowedUser = contract.allowedUser.call(0);
13
14
assert.equal(whitelistedUser, account, 'new user is not allowed');
15
})
16
});
17
Can someone help me?
Advertisement
Answer
Assuming that you properly set the owner in the contract, write a getter for the owner in the contract:
JavaScript
1
5
1
function getContractOwner() public view returns (address)
2
{
3
return owner;
4
}
5
in test.js
JavaScript
1
17
17
1
contract("MyContract", accounts => {
2
let _contract = null
3
let currentOwner=null
4
5
before(async () => {
6
_contract = await MyContract.deployed();
7
currentOwner = await _contract.getContractOwner()
8
})
9
it("should deploy the contract and allow the user", async () => {
10
const account = accounts[0];
11
await contract.allowUser(account, {from: currentOwner});
12
// I assume this is retrieving data from a mapping
13
const allowedUser = _contract.allowedUser.call(0);
14
assert.equal(whitelistedUser, account, 'new user is not allowed');
15
})
16
});
17