I have this situation in my smart contract:
address[] public allowedUsers;
function allowUser(address _newUser) public onlyOwner {
allowedUser.push(_newUser);
}
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:
const MyContract = artifacts.require("../contracts/MyContract.sol");
contract("MyContract", accounts => {
it("should deploy the contract and allow the user", async () => {
const contract = await MyContract.deployed();
const account = accounts[0];
const owner = await contract.owner.call()
await contract.allowUser(account).call({ from: owner });
const allowedUser = contract.allowedUser.call(0);
assert.equal(whitelistedUser, account, 'new user is not allowed');
})
});
Can someone help me? Thank you all!
CodePudding user response:
Assuming that you properly set the owner in the contract, write a getter for the owner in the contract:
function getContractOwner() public view returns (address)
{
return owner;
}
in test.js
contract("MyContract", accounts => {
let _contract = null
let currentOwner=null
before(async () => {
_contract = await MyContract.deployed();
currentOwner = await _contract.getContractOwner()
})
it("should deploy the contract and allow the user", async () => {
const account = accounts[0];
await contract.allowUser(account, {from: currentOwner});
// I assume this is retrieving data from a mapping
const allowedUser = _contract.allowedUser.call(0);
assert.equal(whitelistedUser, account, 'new user is not allowed');
})
});