I’m trying this smart contract which has functions to set and get a user. I’m also testing it on JavaScript with mocha and chai tools and for all of this I use Truffle. But the thing is that for the test case “should Add New User”, I’m not getting this test case to pass and I realized that when I’m calling this.storageContract.getUser
function and storaging its result on user constant, if I print its content I’m not getting the data I stored when calling this.storageContract.setUser
, so when I use checkUserData function, it does not pass.
I wonder how can I make this test case to pass, because I can’t find the error for which it currently fails the test. I deployed this smart contract on Remix IDE and it works well, so the problem must be on the test script.
This is Solidity Smart Contract:
contract SupplyChainStorage is SupplyChainStorageOwnable {
address public lastAccess;
constructor() {
authorizedCaller[msg.sender] = 1;
emit AuthorizedCaller(msg.sender);
}
event AuthorizedCaller(address caller);
event DeAuthorizedCaller(address caller);
event UserUpdate(address userAddress);
event UserRoleUpdate(address userAddress);
modifier onlyAuthCaller() {
lastAccess = msg.sender;
require(authorizedCaller[msg.sender] == 1);
_;
}
struct User {
string name;
string contactNo;
bool isActive;
string profileHash;
}
mapping(address => User) userDetails;
mapping(address => string) userRole;
mapping(address => uint8) authorizedCaller;
function authorizeCaller(address _caller) public onlyOwner returns (bool) {
authorizedCaller[_caller] = 1;
emit AuthorizedCaller(_caller);
return true;
}
function deAuthorizeCaller(address _caller)
public
onlyOwner
returns (bool)
{
authorizedCaller[_caller] = 0;
emit DeAuthorizedCaller(_caller);
return true;
}
User userData;
function setUser(
address _userAddress,
string memory _name,
string memory _contactNo,
string memory _role,
bool _isActive,
string memory _profileHash
) public onlyAuthCaller returns (bool) {
userData.name = _name;
userData.contactNo = _contactNo;
userData.isActive = _isActive;
userData.profileHash = _profileHash;
userDetails[_userAddress] = userData;
userRole[_userAddress] = _role;
emit UserUpdate(_userAddress);
emit UserRoleUpdate(_userAddress);
return true;
}
function getUserRole(address _userAddress)
public
onlyAuthCaller
returns (string memory)
{
return userRole[_userAddress];
}
function getUser(address _userAddress)
public
onlyAuthCaller
returns (
string memory name,
string memory contactNo,
string memory role,
bool isActive,
string memory profileHash
)
{
User memory tmpData = userDetails[_userAddress];
return (
tmpData.name,
tmpData.contactNo,
userRole[_userAddress],
tmpData.isActive,
tmpData.profileHash
);
}
}
This is JavaScript test:
const SupplyChainStorage = artifacts.require('SupplyChainStorage');
const _name = 'Mat';
const _contactNo = '0979081091';
const _role = 'PROCESSOR';
const _isActive = true;
const _profileHash = 'Qmadp4L61MaQPX5NFfjqaihnY8r7PmogqZL6wvX1HqvL';
contract('SupplyChainStorage', function(accounts){
const spenderAddress = accounts[0];
const authorizedCaller = accounts[1];
const userAddress = accounts[2];
beforeEach(async() => {
this.storageContract = await SupplyChainStorage.new({from:spenderAddress, gas: 6000000});
});
it('should Authorize', async() => {
const {logs} = await this.storageContract.authorizeCaller(authorizedCaller,{from: spenderAddress});
const authorizedCallerEvent = logs.find(e => e.event === 'AuthorizedCaller');
assert.exists(authorizedCallerEvent, "AuthorizedCaller does not exists");
});
it('should DeAuthorize', async() => {
const {logs} = await this.storageContract.deAuthorizeCaller(authorizedCaller, {from: spenderAddress});
const deAuthorizeCallerEvent = logs.find(e => e.event === 'DeAuthorizedCaller');
assert.exists(deAuthorizeCallerEvent, "DeAuthorizedCaller does not exists");
});
it('should Add New User', async() => {
const {logs} = await this.storageContract.setUser(userAddress,_name, _contactNo, _role, _isActive, _profileHash, {from: spenderAddress});
checkUserExists(logs, function(result){
console.log(result);
});
const user = await this.storageContract.getUser(userAddress, {from: spenderAddress});
// console.log(user[0]); it returns undefined value
checkUserData(user, function(result){
console.log("checkUserData");
});
});
})
function checkUserExists(logs, callback) {
const updateUserEvent = logs.find(e => e.event === 'UserUpdate');
assert.exists(updateUserEvent, "UserUpdate does not exists");
const updateUserRoleEvent = logs.find(e => e.event === 'UserRoleUpdate');
assert.exists(updateUserRoleEvent, "UserRoleUpdate does not exists");
callback(true);
}
function checkUserData(user, callback){
assert.equal(user[0],_name,"Name checked:");
assert.equal(user[1],_contactNo,"Contact No checked:");
assert.equal(user[2],_role,"Role checked:");
assert.equal(user[3],_isActive,"isActive checked:");
assert.equal(user[4],_profileHash,"Profile Hash checked:");
assert.isTrue(true);
callback(true);
}
This is the log I get when running truffle test --network ganache
command:
2 passing (2s)
1 failing
1) Contract: SupplyChainStorage
should Add New User:
AssertionError: Name checked:: expected undefined to equal 'Mat'
at checkUserData (test/SupplyChainStorage.js:66:12)
at Context.<anonymous> (test/SupplyChainStorage.js:49:9)
at processTicksAndRejections (internal/process/task_queues.js:95:5)