Skip to content

Commit

Permalink
Merge pull request #11 from rpenn/test-suite
Browse files Browse the repository at this point in the history
Test suite
  • Loading branch information
mcgrathcoutinho authored Sep 17, 2023
2 parents d63f540 + e43dcdc commit 4811002
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
6 changes: 4 additions & 2 deletions contracts/TicketManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.19;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "./EventManager.sol";

Expand All @@ -26,15 +27,16 @@ contract TicketManager is ERC1155, ReentrancyGuard, EventManager {
constructor(string memory uri_) ERC1155(uri_) {}

function getUri(uint256 eventId) external view returns(string memory) {
string memory eventID = Strings.toString(eventId);
string memory baseUri = uri(eventId);
return string(abi.encodePacked(baseUri, eventId));
return string(abi.encodePacked(baseUri, eventID));
}

function purchaseTickets(uint256 eventId, uint256 numOfTickets) external payable checkId(eventId) {
uint256 remainingTickets = events[eventId].remainingTickets;

if(numOfTickets > remainingTickets) revert ExceededTicketQtyLeft();
if(msg.value < events[eventId].price * numOfTickets) revert InsufficientETHSent();
if(msg.value != events[eventId].price * numOfTickets) revert InsufficientETHSent();

events[eventId].remainingTickets = remainingTickets - numOfTickets;

Expand Down
72 changes: 72 additions & 0 deletions test/testTicketManager.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import {Test, console} from "forge-std/Test.sol";
import {TicketManager} from "contracts/TicketManager.sol";
import {testEventManager} from "./testEventManager.t.sol";

contract testTicketManager is Test {

// Stores the ticket manager instance
TicketManager ticketManager;

//Sample addresses
address constant USER1 = address(1);

// Sample URI - this is our ticketex URI
string constant URI = "https://storage.fleek-internal.com/513c5496-b170-498d-a846-123191d5e84f-bucket/";

function setUp() public {
ticketManager = new TicketManager(URI);
}

function testCorrectURISet() public {
assertEq(ticketManager.uri(0), URI);
}

function testCorrectUriReturnedForAnEvent() public {
assertEq(ticketManager.getUri(1), string(abi.encodePacked(URI, "1")));
}

function testCreateEventHelper() public {
ticketManager.createEvent({
name: "F1",
location: "Japan Suzuka",
date: 100,
startTime: 200,
endTime: 1000,
description: "Max wins the championship",
eventType: 1,
price: 1e17, //corresponds to 0.1 ether
ticketQuantity: 50000
});
}

function testRevertOnPurchasingTicketsForInvalidId() public {
testCreateEventHelper();
vm.expectRevert();
ticketManager.purchaseTickets(2, 10);
}

function testRevertWhenExceededTicketQtyLeftOnPurchase() public {
testCreateEventHelper();
vm.expectRevert();
ticketManager.purchaseTickets(1, 50001);
}

function testRevertWhenInsufficientEthSentOnPurchase() public {
testCreateEventHelper();
bytes memory data = abi.encodeWithSignature("purchaseTickets(uint256,uint256)", 1, 10);
vm.txGasPrice(0);
vm.expectRevert();
(bool success,) = address(ticketManager).call{value: 9*(10 ** 17), gas: 2000000}(data);
}

function testRevertOnTransferToERC1155ReceiverNonImplementer() public {
testCreateEventHelper();
bytes memory data = abi.encodeWithSignature("purchaseTickets(uint256,uint256)", 1, 10);
vm.txGasPrice(0);
vm.expectRevert();
(bool success,) = address(ticketManager).call{value: 10*(10 ** 17), gas: 2000000}(data);
}
}

0 comments on commit 4811002

Please sign in to comment.