-
Notifications
You must be signed in to change notification settings - Fork 20
/
ValidationModule.test.js
81 lines (72 loc) · 2.95 KB
/
ValidationModule.test.js
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
74
75
76
77
78
79
80
81
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers')
const { DEFAULT_ADMIN_ROLE } = require('../utils')
const { should } = require('chai').should()
const CMTAT = artifacts.require('CMTAT')
const RuleEngineMock = artifacts.require('RuleEngineMock')
contract(
'ValidationModule',
function ([_, owner, address1, address2, address3, fakeRuleEngine]) {
beforeEach(async function () {
this.cmtat = await CMTAT.new(_, { from: owner })
await this.cmtat.initialize(owner, 'CMTA Token', 'CMTAT', 'CMTAT_ISIN', 'https://cmta.ch', { from: owner })
})
context('Rule Engine', function () {
it('can be changed by the owner', async function () {
({ logs: this.logs } = await this.cmtat.setRuleEngine(fakeRuleEngine, {
from: owner
}))
})
it('emits a RuleEngineSet event', function () {
expectEvent.inLogs(this.logs, 'RuleEngineSet', {
newRuleEngine: fakeRuleEngine
})
})
it('reverts when calling from non-owner', async function () {
await expectRevert(
this.cmtat.setRuleEngine(fakeRuleEngine, { from: address1 }),
'AccessControl: account ' +
address1.toLowerCase() +
' is missing role ' +
DEFAULT_ADMIN_ROLE
)
})
})
context('Transferring with Rule Engine set', function () {
beforeEach(async function () {
this.ruleEngineMock = await RuleEngineMock.new({ from: owner })
await this.cmtat.mint(address1, 31, { from: owner })
await this.cmtat.mint(address2, 32, { from: owner })
await this.cmtat.mint(address3, 33, { from: owner })
await this.cmtat.setRuleEngine(this.ruleEngineMock.address, {
from: owner
})
})
it('can check if transfer is valid', async function () {
(
await this.cmtat.detectTransferRestriction(address1, address2, 11)
).should.be.bignumber.equal('0');
(await this.cmtat.messageForTransferRestriction(0)).should.equal(
'No restriction'
);
(
await this.cmtat.detectTransferRestriction(address1, address2, 21)
).should.be.bignumber.equal('10');
(await this.cmtat.messageForTransferRestriction(10)).should.equal(
'Amount too high'
)
})
it('allows address1 to transfer tokens to address2', async function () {
await this.cmtat.transfer(address2, 11, { from: address1 });
(await this.cmtat.balanceOf(address1)).should.be.bignumber.equal('20');
(await this.cmtat.balanceOf(address2)).should.be.bignumber.equal('43');
(await this.cmtat.balanceOf(address3)).should.be.bignumber.equal('33')
})
it('reverts if address1 transfers more tokens than rule allows', async function () {
await expectRevert(
this.cmtat.transfer(address2, 21, { from: address1 }),
'CMTAT: transfer rejected by validation module'
)
})
})
}
)