Skip to content

Commit

Permalink
100 percent testcoverage for tests
Browse files Browse the repository at this point in the history
Co-authored-by: IosifKoen <[email protected]>
  • Loading branch information
cetceeve and sifisKoen committed Dec 16, 2023
1 parent 6f7e721 commit 347f502
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 23 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/codecov.yml

This file was deleted.

5 changes: 2 additions & 3 deletions contract/src/ItemBlocks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract ItemBlocks is ERC721, Ownable {
}

function updatePassport(uint tokenId, string calldata name, string calldata desc, string calldata family, string calldata url, string calldata img) public returns(uint256) {
require( isEligible(tokenId, msg.sender) == true, "Must be the owner of the item or the creator of it" );
require( isEligible(tokenId, msg.sender), "Must be the owner of the item or the creator of it" );

itemPassports[tokenId] = Passport ({
name: name,
Expand All @@ -67,8 +67,7 @@ contract ItemBlocks is ERC721, Ownable {
// isEligible is a funtion that it takes the tokenId and userAddress and checks
// if the user is eligible user for this item (tokenId)
function isEligible(uint256 tokenId, address userAddress) public view returns(bool){
if (userAddress == ownerOf(tokenId) || userAddress == allItemOwners[tokenId][0]) return true;
return false;
return (userAddress == ownerOf(tokenId) || userAddress == allItemOwners[tokenId][0]);
}

function setCreator(address creatorAddress, uint256 tokenId) internal {
Expand Down
18 changes: 13 additions & 5 deletions contract/test/ItemBlocksTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ contract ItemBlocksTest is Test{
assertTrue(itemBlocks.isEligible(12, address(420)));
}

// TESTS FOR updateOwnership
function testOwnershipShouldChange() public {

}

// TESTS FOR setCreator
// should place owner into the allItemOwners array
function testSetCreator() public {
Expand All @@ -74,6 +69,12 @@ contract ItemBlocksTest is Test{
itemBlocks.exposed_setCreator(address(0), 12);
}

// should fail because zero address should not be allowed
function testUpdateSetCreatorZeroAddress() public {
vm.expectRevert();
itemBlocks.exposed_setCreator(address(0), 12);
}

// should fail because token 13 should not be in the mapping
function testFailUpdateSetCreatorNoToken() public {
itemBlocks.exposed_setCreator(address(42), 12);
Expand All @@ -86,6 +87,13 @@ contract ItemBlocksTest is Test{
itemBlocks.exposed_setCreator(address(44), 12);
}

// should fail because token is already owned
function testSetCreatorAlreadyOwner() public {
itemBlocks.exposed_setCreator(address(42), 12);
vm.expectRevert();
itemBlocks.exposed_setCreator(address(44), 12);
}

//TESTS for updateOwnership
// should not allow transfer from zero address
function testFailUpdateOwnershipFromZeroAddress() public {
Expand Down
56 changes: 55 additions & 1 deletion contract/test/TestCreatePassport.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ contract ItemBlocksTest is Test{
itemBlocks.createPassport(7, testPassport.name, testPassport.desc, testPassport.family, testPassport.url, testPassport.img);
}

function testUpdatePassportEligibility() public {
function testUpdatePassport() public {
ItemBlocks.Passport memory testPassport = ItemBlocks.Passport({
name: "TestItemName",
desc: "TestItemDesc",
Expand Down Expand Up @@ -108,4 +108,58 @@ contract ItemBlocksTest is Test{
itemBlocks.updatePassport(tokenId, updatedPassport.name, updatedPassport.desc, updatedPassport.family, updatedPassport.url, updatedPassport.img);
}

function testUpdatePassportNotEligibility() public {
ItemBlocks.Passport memory testPassport = ItemBlocks.Passport({
name: "TestItemName",
desc: "TestItemDesc",
family: "TestItemFamily",
url: "TestItemUrl",
img: "TestItemImg"
});

vm.prank(address(42));
uint256 tokenId = itemBlocks.createPassport(7, testPassport.name, testPassport.desc, testPassport.family, testPassport.url, testPassport.img);

ItemBlocks.Passport memory updatedPassport = ItemBlocks.Passport({
name: "UpdatedItemName",
desc: "UpdatedItemDesc",
family: "UpdatedItemFamily",
url: "UpdatedItemUrl",
img: "UpdatedItemImg"
});

//Not eligible user for passport updates.
vm.prank(address(40));
vm.expectRevert();
itemBlocks.updatePassport(tokenId, updatedPassport.name, updatedPassport.desc, updatedPassport.family, updatedPassport.url, updatedPassport.img);
}

function testUpdatePassportEligibileCreator() public {
ItemBlocks.Passport memory testPassport = ItemBlocks.Passport({
name: "TestItemName",
desc: "TestItemDesc",
family: "TestItemFamily",
url: "TestItemUrl",
img: "TestItemImg"
});

vm.prank(address(42));
uint256 tokenId = itemBlocks.createPassport(7, testPassport.name, testPassport.desc, testPassport.family, testPassport.url, testPassport.img);

vm.prank(address(42));
itemBlocks.updateOwnership(address(42), address(43), 7);

ItemBlocks.Passport memory updatedPassport = ItemBlocks.Passport({
name: "UpdatedItemName",
desc: "UpdatedItemDesc",
family: "UpdatedItemFamily",
url: "UpdatedItemUrl",
img: "UpdatedItemImg"
});

//Eligible as the creator
vm.prank(address(42));
itemBlocks.updatePassport(tokenId, updatedPassport.name, updatedPassport.desc, updatedPassport.family, updatedPassport.url, updatedPassport.img);
}

}
17 changes: 16 additions & 1 deletion contract/test/TestGetters.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ contract ItemBlocksTest is Test {
function testFailGetCreatedItemTokensZeroAddress() public view{
itemBlocks.getCreatedItemTokens(address(0));
}


// should fail for null address
function testGetCreatedItemTokensZeroAddress() public {
vm.expectRevert();
itemBlocks.getCreatedItemTokens(address(0));
}
// should return list of users who owned this token
function testGetUserHistory() public {
assertEq(itemBlocks.getUserHistory(7)[0], address(42));
Expand All @@ -51,6 +56,11 @@ contract ItemBlocksTest is Test {
itemBlocks.getUserHistory(1234);
}

// should fail if token doesn't exits
function testUserHistoryZeroAddress() public {
vm.expectRevert();
itemBlocks.getUserHistory(1234);
}
// should return passport
function testGetPassport() public {
ItemBlocks.Passport memory p = itemBlocks.getPassport(7);
Expand All @@ -66,4 +76,9 @@ contract ItemBlocksTest is Test {
itemBlocks.getPassport(1234);
}

// should fail if token doesn't exits
function testGetPassportNotToken() public {
vm.expectRevert();
itemBlocks.getPassport(1234);
}
}

0 comments on commit 347f502

Please sign in to comment.