Skip to content

Commit

Permalink
Add final tests for TicketManager.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
mcgrathcoutinho committed Oct 17, 2023
1 parent e43dcdc commit a9d1af9
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion test/testTicketManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ contract testTicketManager is Test {
// Sample URI - this is our ticketex URI
string constant URI = "https://storage.fleek-internal.com/513c5496-b170-498d-a846-123191d5e84f-bucket/";

// Sample addresses
address alice = makeAddr("alice");
address bob = makeAddr("bob");

function setUp() public {
ticketManager = new TicketManager(URI);
}
Expand Down Expand Up @@ -65,8 +69,56 @@ contract testTicketManager is Test {
function testRevertOnTransferToERC1155ReceiverNonImplementer() public {
testCreateEventHelper();
bytes memory data = abi.encodeWithSignature("purchaseTickets(uint256,uint256)", 1, 10);
vm.txGasPrice(0);
vm.txGasPrice(0);
vm.expectRevert();
(bool success,) = address(ticketManager).call{value: 10*(10 ** 17), gas: 2000000}(data);
}

function testSuccessOnTicketPurchase() public {
testCreateEventHelper();
bytes memory data = abi.encodeWithSignature("purchaseTickets(uint256,uint256)", 1, 10);
vm.deal(alice, 1 ether);
vm.prank(alice);
(bool success,) = address(ticketManager).call{value: 10*(10 ** 17), gas: 2000000}(data);
assertEq(ticketManager.balanceOf(alice, 1), 10);
}

function testSuccessOnTicketTransfer() public {
testSuccessOnTicketPurchase();
vm.prank(alice);
ticketManager.transferTicket(bob, 1, 5); //Transfers 5 tickets to bob
assertEq(ticketManager.balanceOf(bob, 1), 5);
assertEq(ticketManager.balanceOf(alice, 1), 5);
}

function testRevertWhenOwnerWithdrawsForInvalidId() public {
testCreateEventHelper();
vm.expectRevert();
ticketManager.withdraw(2);
}

function testRevertWhenOwnerWithdrawsForOngoingEvent() public {
testCreateEventHelper();
vm.expectRevert();
ticketManager.withdraw(1);
}

function testRevertWhenNonOwnerTriesToWithdraw() public {
testCreateEventHelper();
vm.warp(1 days);
vm.prank(alice);
vm.expectRevert();
ticketManager.withdraw(1);
}

function testSuccessOnWithdraw() public {
uint256 preBalance = address(this).balance;
testSuccessOnTicketPurchase(); //alice buys tickets
vm.warp(1 days); // Event is now over
ticketManager.withdraw(1); //owner calls withdraw
uint256 postBalance = address(this).balance;
assertEq(postBalance - preBalance, 1e18); //alice paid 1 ether for tickets so owner receives 1 ether
}

receive() external payable {} // Helper function to allow receiving native tokens possible during withdrawals
}

0 comments on commit a9d1af9

Please sign in to comment.