Skip to content

Commit

Permalink
Adding tests for [OZ] Pausable (#572)
Browse files Browse the repository at this point in the history
* Adding tests for Pausable
---------

Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov authored Nov 8, 2023
1 parent 231f85b commit 8b7b6a7
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
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)
})

})

0 comments on commit 8b7b6a7

Please sign in to comment.