Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Cleaner tests with await
Browse files Browse the repository at this point in the history
  • Loading branch information
oed committed Jun 19, 2017
1 parent f65a54f commit 132487a
Show file tree
Hide file tree
Showing 9 changed files with 583 additions and 886 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-2", "stage-3"]
}
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -29,6 +29,9 @@
},
"homepage": "https://github.com/uport-project/uport-identity#readme",
"devDependencies": {
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
"bluebird": "^3.5.0",
"eth-signer": "^0.2.2",
"ethereumjs-testrpc": "^3.0.3",
Expand Down
10 changes: 10 additions & 0 deletions test/compareCode.js
@@ -0,0 +1,10 @@
const Promise = require('bluebird')
web3.eth = Promise.promisifyAll(web3.eth)

async function compareCode(addr1, addr2) {
let c1 = await web3.eth.getCodeAsync(addr1)
let c2 = await web3.eth.getCodeAsync(addr2)
assert.equal(c1, c2, 'the deployed contract has incorrect code')
}

module.exports = compareCode
105 changes: 34 additions & 71 deletions test/identityFactory.js
Expand Up @@ -2,22 +2,7 @@ const IdentityFactory = artifacts.require('IdentityFactory')
const Proxy = artifacts.require('Proxy')
const RecoverableController = artifacts.require('RecoverableController')
const RecoveryQuorum = artifacts.require('RecoveryQuorum')
const Promise = require('bluebird')
web3.eth = Promise.promisifyAll(web3.eth)

function compareCode(addr1, addr2) {
let c1, c2
return new Promise((resolve, reject) => {
web3.eth.getCodeAsync(addr1).then(code => {
c1 = code
return web3.eth.getCodeAsync(addr2)
}).then(code => {
c2 = code
assert.equal(c1, c2, 'the deployed contract has incorrect code')
resolve()
})
})
}
const compareCode = require('./compareCode')

contract('IdentityFactory', (accounts) => {
let identityFactory
Expand All @@ -42,7 +27,7 @@ contract('IdentityFactory', (accounts) => {
let shortTimeLock = 2
let longTimeLock = 7

before((done) => {
before(async function() {
// Truffle deploys contracts with accounts[0]
user1 = accounts[0]
nobody = accounts[1] // has no authority
Expand All @@ -52,68 +37,46 @@ contract('IdentityFactory', (accounts) => {
delegate4 = accounts[7]
delegates = [delegate1, delegate2, delegate3, delegate4]

IdentityFactory.deployed().then((instance) => {
identityFactory = instance
return Proxy.new({from: accounts[0]})
}).then((instance) => {
deployedProxy = instance
return RecoverableController.new({from: accounts[0]})
}).then((instance) => {
deployedRecoverableController = instance
return RecoveryQuorum.new({from: accounts[0]})
}).then((instance) => {
deployedRecoveryQuorum = instance
done()
})
identityFactory = await IdentityFactory.deployed()
deployedProxy = await Proxy.new({from: accounts[0]})
deployedRecoverableController = await RecoverableController.new({from: accounts[0]})
deployedRecoveryQuorum = await RecoveryQuorum.new({from: accounts[0]})
})

it('Correctly creates proxy, controller, and recovery contracts', (done) => {
identityFactory.CreateProxyWithControllerAndRecovery(user1, delegates, longTimeLock, shortTimeLock, {from: nobody})
.then( (tx) => {
let log=tx.logs[0];
assert.equal(log.event,"IdentityCreated","wrong event");
proxyAddress = log.args.proxy
recoverableControllerAddress = log.args.controller
recoveryQuorumAddress = log.args.recoveryQuorum
it('Correctly creates proxy, controller, and recovery contracts', async function() {
let tx = await identityFactory.CreateProxyWithControllerAndRecovery(user1, delegates, longTimeLock, shortTimeLock, {from: nobody})
let log=tx.logs[0];
assert.equal(log.event,"IdentityCreated","wrong event");
proxyAddress = log.args.proxy
recoverableControllerAddress = log.args.controller
recoveryQuorumAddress = log.args.recoveryQuorum

proxy = Proxy.at(proxyAddress)
recoverableController = RecoverableController.at(recoverableControllerAddress)
recoveryQuorum = RecoveryQuorum.at(recoveryQuorumAddress)
return compareCode(proxyAddress, deployedProxy.address)
}).then(() => {
return compareCode(recoverableControllerAddress, deployedRecoverableController.address)
}).then(() => {
return compareCode(recoveryQuorumAddress, deployedRecoveryQuorum.address)
}).then(done).catch(done)
proxy = Proxy.at(proxyAddress)
recoverableController = RecoverableController.at(recoverableControllerAddress)
recoveryQuorum = RecoveryQuorum.at(recoveryQuorumAddress)
await compareCode(proxyAddress, deployedProxy.address)
await compareCode(recoverableControllerAddress, deployedRecoverableController.address)
await compareCode(recoveryQuorumAddress, deployedRecoveryQuorum.address)
})

it('Created proxy should have correct state', (done) => {
proxy.owner.call().then((createdControllerAddress) => {
assert.equal(createdControllerAddress, recoverableController.address)
done()
}).catch(done)
it('Created proxy should have correct state', async function() {
let createdControllerAddress = await proxy.owner.call()
assert.equal(createdControllerAddress, recoverableController.address)
})

it('Created controller should have correct state', (done) => {
recoverableController.proxy().then((_proxyAddress) => {
assert.equal(_proxyAddress, proxy.address)
return recoverableController.userKey()
}).then((userKey) => {
assert.equal(userKey, user1)
return recoverableController.recoveryKey()
}).then((recoveryKey) => {
assert.equal(recoveryKey, recoveryQuorumAddress)
done()
}).catch(done)
it('Created controller should have correct state', async function() {
let _proxyAddress = await recoverableController.proxy()
assert.equal(_proxyAddress, proxy.address)
let userKey = await recoverableController.userKey()
assert.equal(userKey, user1)
let recoveryKey = await recoverableController.recoveryKey()
assert.equal(recoveryKey, recoveryQuorumAddress)
})

it('Created recoveryQuorum should have correct state', (done) => {
recoveryQuorum.controller().then(controllerAddress => {
assert.equal(controllerAddress, recoverableController.address)
return recoveryQuorum.getAddresses()
}).then(delegateAddresses => {
assert.deepEqual(delegateAddresses, delegates)
done()
}).catch(done)
it('Created recoveryQuorum should have correct state', async function() {
let controllerAddress = await recoveryQuorum.controller()
assert.equal(controllerAddress, recoverableController.address)
let delegateAddresses = await recoveryQuorum.getAddresses()
assert.deepEqual(delegateAddresses, delegates)
})
})
85 changes: 27 additions & 58 deletions test/identityFactoryWithRecoveryKey.js
@@ -1,22 +1,7 @@
const IdentityFactoryWithRecoveryKey = artifacts.require('IdentityFactoryWithRecoveryKey')
const Proxy = artifacts.require('Proxy')
const RecoverableController = artifacts.require('RecoverableController')
const Promise = require('bluebird')
web3.eth = Promise.promisifyAll(web3.eth)

function compareCode(addr1, addr2) {
let c1, c2
return new Promise((resolve, reject) => {
web3.eth.getCodeAsync(addr1).then(code => {
c1 = code
return web3.eth.getCodeAsync(addr2)
}).then(code => {
c2 = code
assert.equal(c1, c2, 'the deployed contract has incorrect code')
resolve()
})
})
}
const compareCode = require('./compareCode')

contract('IdentityFactoryWithRecoveryKey', (accounts) => {
let proxy
Expand All @@ -34,58 +19,42 @@ contract('IdentityFactoryWithRecoveryKey', (accounts) => {
let shortTimeLock = 2
let longTimeLock = 7

before((done) => {
before(async function() {
// Truffle deploys contracts with accounts[0]
user1 = accounts[0]
nobody = accounts[1] // has no authority
recoveryKey = accounts[4]

IdentityFactoryWithRecoveryKey.deployed().then((instance) => {
identityFactoryWithRecoveryKey = instance
return Proxy.new({from: accounts[0]})
}).then((instance) => {
deployedProxy = instance
return RecoverableController.new({from: accounts[0]})
}).then((instance) => {
deployedRecoverableController = instance
done()
})
identityFactoryWithRecoveryKey = await IdentityFactoryWithRecoveryKey.deployed()
deployedProxy = await Proxy.new({from: accounts[0]})
deployedRecoverableController = await RecoverableController.new({from: accounts[0]})
})

it('Correctly creates proxy and controller', (done) => {
identityFactoryWithRecoveryKey.CreateProxyWithControllerAndRecoveryKey(user1, recoveryKey, longTimeLock, shortTimeLock, {from: nobody})
.then( (tx) => {
let log=tx.logs[0];
assert.equal(log.event,"IdentityCreated","wrong event");
proxyAddress = log.args.proxy
recoverableControllerAddress = log.args.controller
recoveryQuorumAddress = log.args.recoveryQuorum

proxy = Proxy.at(proxyAddress)
recoverableController = RecoverableController.at(recoverableControllerAddress)
return compareCode(proxyAddress, deployedProxy.address)
}).then(() => {
return compareCode(recoverableControllerAddress, deployedRecoverableController.address)
}).then(done).catch(done)
it('Correctly creates proxy and controller', async function() {
let tx = await identityFactoryWithRecoveryKey.CreateProxyWithControllerAndRecoveryKey(user1, recoveryKey, longTimeLock, shortTimeLock, {from: nobody})
let log=tx.logs[0];
assert.equal(log.event,"IdentityCreated","wrong event");
proxyAddress = log.args.proxy
recoverableControllerAddress = log.args.controller
recoveryQuorumAddress = log.args.recoveryQuorum

proxy = Proxy.at(proxyAddress)
recoverableController = RecoverableController.at(recoverableControllerAddress)
await compareCode(proxyAddress, deployedProxy.address)
await compareCode(recoverableControllerAddress, deployedRecoverableController.address)
})

it('Created proxy should have correct state', (done) => {
proxy.owner.call().then((createdControllerAddress) => {
assert.equal(createdControllerAddress, recoverableController.address)
done()
}).catch(done)
it('Created proxy should have correct state', async function() {
let createdControllerAddress = await proxy.owner.call()
assert.equal(createdControllerAddress, recoverableController.address)
})

it('Created controller should have correct state', (done) => {
recoverableController.proxy().then((_proxyAddress) => {
assert.equal(_proxyAddress, proxy.address)
return recoverableController.userKey()
}).then((userKey) => {
assert.equal(userKey, user1)
return recoverableController.recoveryKey()
}).then((rk) => {
assert.equal(rk, recoveryKey)
done()
}).catch(done)
it('Created controller should have correct state', async function() {
let _proxyAddress = await recoverableController.proxy()
assert.equal(_proxyAddress, proxy.address)
let userKey = await recoverableController.userKey()
assert.equal(userKey, user1)
let rk = await recoverableController.recoveryKey()
assert.equal(rk, recoveryKey)
})
})
42 changes: 15 additions & 27 deletions test/owned.js
Expand Up @@ -3,38 +3,26 @@ const Owned = artifacts.require('Owned')
contract('Owned', (accounts) => {
let owned

before((done) => {
Owned.new({from: accounts[0]}).then(instance => {
owned = instance
done();
})
before(async function() {
owned = await Owned.new({from: accounts[0]})
})

it('Is owned by creator', (done) => {
owned.isOwner.call(accounts[0]).then((isOwner) => {
assert.isTrue(isOwner, 'Owner should be owner')
return owned.isOwner.call(accounts[1])
}).then((isOwner) => {
assert.isFalse(isOwner, 'Non-owner should not be owner')
done()
}).catch(done)
it('Is owned by creator', async function() {
let isOwner = await owned.isOwner.call(accounts[0])
assert.isTrue(isOwner, 'Owner should be owner')
isOwner = await owned.isOwner.call(accounts[1])
assert.isFalse(isOwner, 'Non-owner should not be owner')
})

it('Non-owner can not change owner', (done) => {
owned.transfer(accounts[1], {from: accounts[1]}).then(() => {
return owned.isOwner.call(accounts[1])
}).then((isOwner) => {
assert.isFalse(isOwner, 'Owner should not be changed')
done()
}).catch(done)
it('Non-owner can not change owner', async function() {
await owned.transfer(accounts[1], {from: accounts[1]})
let isOwner = await owned.isOwner.call(accounts[1])
assert.isFalse(isOwner, 'Owner should not be changed')
})

it('Owner can change owner', (done) => {
owned.transfer(accounts[1], {from: accounts[0]}).then(() => {
return owned.isOwner.call(accounts[1])
}).then((isOwner) => {
assert.isTrue(isOwner, 'Owner should be changed')
done()
}).catch(done)
it('Owner can change owner', async function() {
await owned.transfer(accounts[1], {from: accounts[0]})
let isOwner = await owned.isOwner.call(accounts[1])
assert.isTrue(isOwner, 'Owner should be changed')
})
})

0 comments on commit 132487a

Please sign in to comment.