forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOwnable.test.js
More file actions
73 lines (59 loc) · 2.86 KB
/
Ownable.test.js
File metadata and controls
73 lines (59 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
contract('Ownable', function (_, signersAsPromise) {
async function fixture() {
const [owner, other] = await signersAsPromise;
const ownable = await ethers.deployContract('$Ownable', [owner]);
return { owner, other, ownable };
}
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
it('rejects zero address for initialOwner', async function () {
await expect(ethers.deployContract('$Ownable', [ethers.ZeroAddress]))
.to.be.revertedWithCustomError({ interface: this.ownable.interface }, 'OwnableInvalidOwner')
.withArgs(ethers.ZeroAddress);
});
it('has an owner', async function () {
expect(await this.ownable.owner()).to.equal(this.owner.address);
});
describe('transfer ownership', function () {
it('changes owner after transfer', async function () {
await expect(this.ownable.connect(this.owner).transferOwnership(this.other))
.to.emit(this.ownable, 'OwnershipTransferred')
.withArgs(this.owner.address, this.other.address);
expect(await this.ownable.owner()).to.equal(this.other.address);
});
it('prevents non-owners from transferring', async function () {
await expect(this.ownable.connect(this.other).transferOwnership(this.other))
.to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
.withArgs(this.other.address);
});
it('guards ownership against stuck state', async function () {
await expect(this.ownable.connect(this.owner).transferOwnership(ethers.ZeroAddress))
.to.be.revertedWithCustomError(this.ownable, 'OwnableInvalidOwner')
.withArgs(ethers.ZeroAddress);
});
});
describe('renounce ownership', function () {
it('loses ownership after renouncement', async function () {
await expect(this.ownable.connect(this.owner).renounceOwnership())
.to.emit(this.ownable, 'OwnershipTransferred')
.withArgs(this.owner.address, ethers.ZeroAddress);
expect(await this.ownable.owner()).to.equal(ethers.ZeroAddress);
});
it('prevents non-owners from renouncement', async function () {
await expect(this.ownable.connect(this.other).renounceOwnership())
.to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
.withArgs(this.other.address);
});
it('allows to recover access using the internal _transferOwnership', async function () {
await this.ownable.connect(this.owner).renounceOwnership();
await expect(this.ownable.$_transferOwnership(this.other))
.to.emit(this.ownable, 'OwnershipTransferred')
.withArgs(ethers.ZeroAddress, this.other.address);
expect(await this.ownable.owner()).to.equal(this.other.address);
});
});
});