Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Test an onlyOwner function in Javascript

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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');
    })
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!