Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda