Authentication testing

This commit is contained in:
David E. Perez Negron R 2023-08-25 18:34:16 -06:00
parent 6091138690
commit 8fd0e6a49f
2 changed files with 65 additions and 0 deletions

21
src/Wallet.sol Normal file
View File

@ -0,0 +1,21 @@
pragma solidity ^0.8.20;
contract Wallet {
address payable public owner;
constructor () payable {
owner = payable(msg.sender);
}
receive() external payable {}
function withdraw(uint256 _amount) external {
require(msg.sender == owner, "caller is not owner");
payable(msg.sender).transfer(_amount);
}
function setOwner(address _owner) external {
require(msg.sender == owner, "caller is not owner");
owner = payable(_owner);
}
}

44
test/Auth.t.sol Normal file
View File

@ -0,0 +1,44 @@
pragma solidity ^0.8.20;
import "forge-std/Test.sol";
import {Wallet} from "../src/Wallet.sol";
contract AuthTest is Test {
Wallet public wallet;
function setUp() public {
wallet = new Wallet();
}
function testSetOwner() public {
console.log("wallet Owner Before:", wallet.owner());
wallet.setOwner(address(1));
console.log("wallet Owner After:", wallet.owner());
assertEq(wallet.owner(),address(1));
}
function testFailNotOwner() public {
console.log("wallet Owner Before:", wallet.owner());
vm.prank(address(1));
wallet.setOwner(address(1));
}
function testFailSetOwnerAgain() public {
// msg.sender = address(this)
wallet.setOwner(address(1));
vm.startPrank(address(1));
// msg.sender = address(1)
wallet.setOwner(address(1));
wallet.setOwner(address(1));
wallet.setOwner(address(1));
vm.stopPrank();
// msg.sender = address(this)
wallet.setOwner(address(1));
}
}