Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda