Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests for [OZ] Pausable #572

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contracts/solidity/errors/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ contract Errors {
function tryCatchWithErrorMessageRevert(string memory message) external returns (int value, bool success) {
try errorsExternal.revertWithErrorMessage(message) returns (bool v) {
return (1, v);
} catch Error(string memory message) {
emit Result(0, message);
} catch Error(string memory _message) {
emit Result(0, _message);
}
}

Expand Down
24 changes: 24 additions & 0 deletions contracts/solidity/oz/Pausable/Pausable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/utils/Pausable.sol";

contract PausableTest is Pausable {
string public message;

function setPausedMessage(string calldata _message) external whenNotPaused {
message = _message;
}

function getPausedMessage() external view whenPaused returns (string memory) {
return message;
}

function pause() external {
_pause();
}

function unpause() external {
_unpause();
}
}
101 changes: 101 additions & 0 deletions test/solidity/oz/pausable/pausable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

const { expect } = require('chai')
const { ethers } = require('hardhat')

describe('@solidityequiv3 Pausable', function () {
let signers,wallet
let contract
const CALL_EXCEPTION = 'CALL_EXCEPTION'

before(async function () {
signers = await ethers.getSigners()
wallet = signers[0]

const factory = await ethers.getContractFactory('PausableTest')
contract = await factory.deploy()
await contract.deployed()
})

it('should BE able to call function "setPausedMessage" with "whenNotPaused" modifier when unpaused', async function () {
const tx = await contract.setPausedMessage("Hello World")
await tx.wait()
const message = await contract.message()

expect(message).to.equal("Hello World")
})

it('should NOT be able to call function "setPausedMessage" with "whenNotPaused" modifier when paused', async function () {
await contract.pause()
const tx = await contract.setPausedMessage("Hello World")

expect(tx.wait()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

it('should BE able to call function "getPausedMessage" with "whenNotPaused" modifier when unpaused', async function () {
expect(await contract.getPausedMessage()).to.be.equal("Hello World")
})

it('should NOT be able to call function "getPausedMessage" with "whenNotPaused" modifier when paused', async function () {
await contract.unpause()

expect(contract.getPausedMessage()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

it('should fire event when Paused', async function () {
const tx = await contract.pause()
const rec = await tx.wait()
const event = rec.events[0]
const account = event.args.account


expect(event.event).to.be.equal("Paused")
expect(account).to.be.equal(wallet.address)
})

it('should fire event when Unpaused', async function () {
const tx = await contract.unpause()
const rec = await tx.wait()
const event = rec.events[0]
const account = event.args.account


expect(event.event).to.be.equal("Unpaused")
expect(account).to.be.equal(wallet.address)
})

it('should Not be able to pause when paused', async function () {
const tx = await contract.pause()
await tx.wait()
const tx2 = await contract.pause()

expect(tx2.wait()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

it('should Not be able to Unpause when Unpaused', async function () {
const tx = await contract.unpause()
await tx.wait()
const tx2 = await contract.unpause()

expect(tx2.wait()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

})