In the comments it says that only owner should be able to set the new password but anyone can set the new password instead of owner and this can lead to misleading because when the owner will call getPassword it will return the wrong password.
pragma solidity 0.8.18;
import {Test, console} from "forge-std/Test.sol";
import {PasswordStore} from "../src/PasswordStore.sol";
import {DeployPasswordStore} from "../script/DeployPasswordStore.s.sol";
contract PasswordStoreTest is Test {
PasswordStore public passwordStore;
DeployPasswordStore public deployer;
address public owner;
string password = "myNewPassword";
address anyone = makeAddr("anyone");
function setUp() public {
deployer = new DeployPasswordStore();
passwordStore = deployer.run();
owner = msg.sender;
vm.startPrank(owner);
passwordStore.setPassword(password);
string memory _password = passwordStore.getPassword();
assertEq(_password, password);
}
function test_anyone_can_set_password_not_so_secure_lol() public {
vm.startPrank(anyone);
string memory newPassword = "Haha, I can set the password, lol!";
passwordStore.setPassword(newPassword);
vm.startPrank(owner);
string memory _newPassword = passwordStore.getPassword();
assertEq(_newPassword, newPassword);
}
}
/*
* @notice This function allows only the owner to set a new password.
* @param newPassword The new password to set.
*/
function setPassword(string memory newPassword) external {
+ if (msg.sender != s_owner) {
+ revert PasswordStore__NotOwner();
+ }
s_password = newPassword;
emit SetNetPassword();
}