I have the below given SmartContract and the test. Please note that, when i try to execute the test, only after the second call to the grantAccess
method, I am able to get the mapped value.
pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;
contract AccessControlListContract {
mapping(address => mapping(string=> mapping(address => string[]))) _userAccessGrants;
mapping(address => mapping(address => mapping(string => string[]))) _targetAccessGrants;
/*
* A transaction function does not return any values, it instead returns the transaction object as such for mining.
* Hence, no return statements here....
*/
function grantAccess(address userAddress, string documentHash, address targetUserAddress, string permission) public {
_userAccessGrants[userAddress][documentHash][targetUserAddress].push(permission);
_targetAccessGrants[targetUserAddress][userAddress][documentHash].push(permission);
}
function checkAccess(address requestorAddress, address userAddress, string documentHash) public view returns (string[]) {
return _targetAccessGrants[requestorAddress][userAddress][documentHash];
}
}
Unit Tests
const AccessControlListContract = artifacts.require(
"AccessControlListContract"
);
contract("AccessControlListContract", async (accounts) => {
// let constants = require('./constants.js')(accounts);
let acl;
let constants = {
patient1Address: accounts[0],
patient2Address: accounts[2],
ViewDocumentPermission: "view_document",
doctor1Address: accounts[1],
pharma1Address: accounts[3],
provider1Address: accounts[4],
ECG_DOC_HASH: "ECG-DOC-HASH",
ECHO_DOC_HASH: "ECHO-DOC-HASH",
BLOOD_TEST_DOC_HASH: "BLOOD-TEST-DOC-HASH",
NON_EXISTENT_USER_ADDRESS: 0x13579
};
before(async () => {
acl = await AccessControlListContract.deployed();
});
it("should succeed always", async () => {
assert.equal(true, true);
});
it('should add a new mapping for a patient and a doctor', async () =>{
await acl.grantAccess(constants.patient1Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
// await acl.grantAccess(constants.patient1Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
console.log(await acl.checkAccess(constants.doctor1Address, constants.patient1Address, constants.ECG_DOC_HASH));
assert.equal(true, true);
});
}
Please let me know what is going wrong. I am following the steps given in the truffle suite for performing the testing with the async and await keywords.
Tried to use the old system without the async / await method, still the patient2 permissions are not returned from the smart contract
it('non-async test', () => {
let meta;
return AccessControlListContract.deployed().then(function (instance) {
meta = instance;
return meta.grantAccess(constants.patient1Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
}).then(function () {
return meta.grantAccess(constants.patient2Address, constants.ECG_DOC_HASH, constants.doctor1Address, constants.ViewDocumentPermission);
}).then(function () {
return meta.checkAccess(constants.doctor1Address, constants.patient1Address, constants.ECG_DOC_HASH);
}).then(function(result){
console.log('patient 1 permissions: ',result);
}).then(function(){
return meta.checkAccess(constants.doctor1Address, constants.patient2Address, constants.ECG_DOC_HASH);
}).then(function(result2){
console.log('patient 2 permissions: ',result2);
})
});