Authentication testing
This commit is contained in:
parent
6091138690
commit
8fd0e6a49f
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue