Skip to content
Permalink
Browse files

Pull in marketplace tests (#364)

  • Loading branch information...
tyleryasaka authored and micahalcorn committed Aug 23, 2018
1 parent dd63999 commit 0c09e12a823ae049906c84f66a8181b26ad96215
@@ -1,3 +1,4 @@
{
"presets": [ "babel-preset-env" ]
"presets": ["babel-preset-env"],
"plugins": ["transform-object-rest-spread"]
}
@@ -0,0 +1,51 @@
import assert from 'assert'
import helper from './_helper'

// Account 0: Token owner. Marketplace owner
// Account 1: Seller
// Account 2: Buyer
// Account 3: Dispute resolver

describe('ArbitrableExample.sol', async function() {
let accounts, deploy, web3
let Arbitrator, Arbitrable

before(async function() {
({ deploy, accounts, web3 } = await helper(`${__dirname}/..`))

Arbitrator = await deploy('CentralizedArbitrator', {
from: accounts[0],
path: `${__dirname}/contracts/arbitration/`,
args: [0]
})

Arbitrable = await deploy('ArbitrableExample', {
from: accounts[0],
path: `${__dirname}/contracts/arbitration/`,
args: [Arbitrator._address]
})
})

it('should allow a dispute to be created', async function() {
const result = await Arbitrable.methods.startDispute().send({
value: web3.utils.toWei('0.1', 'ether')
})
assert(result.events.Dispute)
})

it('should allow a ruling to be given', async function() {
const result = await Arbitrator.methods.giveRuling(0, 1).send()
const { data, topics } = result.events['0'].raw
const ruling = Arbitrable._jsonInterface.find(i => {
return i.signature === topics[0]
})
const decoded = web3.eth.abi.decodeLog(ruling.inputs, data, topics)
assert.equal(ruling.name, 'Ruling')
assert.equal(decoded._ruling, '1')
})

it('should allow evidence to be submitted', async function() {
const result = await Arbitrable.methods.submitEvidence(0, 'evidence').send()
assert(result.events.Evidence)
})
})
@@ -0,0 +1,71 @@
import assert from 'assert'
import helper from './_helper'

describe('ClaimVerifier.sol', async function() {
let web3, accounts, deploy, prvSigner, pubSigner
let UserIdentity, ClaimIssuer, ClaimVerifier

before(async function() {
({ deploy, accounts, web3 } = await helper(`${__dirname}/..`))

prvSigner = web3.utils.randomHex(32)
pubSigner = web3.eth.accounts.privateKeyToAccount(prvSigner).address

UserIdentity = await deploy('ClaimHolder', {
from: accounts[0],
path: `${__dirname}/../contracts/identity/`
})
ClaimIssuer = await deploy('ClaimHolder', {
from: accounts[1],
path: `${__dirname}/../contracts/identity/`
})
ClaimVerifier = await deploy('ClaimVerifier', {
from: accounts[2],
args: [ClaimIssuer._address],
path: `${__dirname}/../contracts/identity/`
})
})

it('should allow verifier owner to addKey', async function() {
const key = web3.utils.sha3(pubSigner)
const result = await ClaimIssuer.methods
.addKey(key, 3, 1)
.send({ from: accounts[1] })

assert(result)
})

it('should not allow new listing without identity claim', async function() {
const res = await ClaimVerifier.methods
.checkClaim(UserIdentity._address, 3)
.send({ from: accounts[0] })
assert(res.events.ClaimInvalid)
})

it('should allow identity owner to addClaim', async function() {
const data = web3.utils.asciiToHex('Verified OK')
const claimType = 3
const hashed = web3.utils.soliditySha3(UserIdentity._address, claimType, data)
const signed = await web3.eth.accounts.sign(hashed, prvSigner)

const claimRes = await UserIdentity.methods
.addClaim(
claimType,
2,
ClaimIssuer._address,
signed.signature,
data,
'abc.com'
)
.send({ from: accounts[0] })

assert(claimRes.events.ClaimAdded)
})

it('should not allow new listing without identity claim', async function() {
const res = await ClaimVerifier.methods
.checkClaim(UserIdentity._address, 3)
.send({ from: accounts[0] })
assert(res.events.ClaimValid)
})
})
@@ -0,0 +1,209 @@
import assert from 'assert'
import helper from './_helper'

describe('Identity', async function() {
let web3, accounts, deploy, acctSha3, randomHex
let UserIdentity

before(async function() {
({
web3,
deploy,
accounts,
web3: {
utils: { randomHex }
}
} = await helper(`${__dirname}/..`))

UserIdentity = await deploy('ClaimHolder', {
from: accounts[0],
path: `${__dirname}/../contracts/identity/`
})
acctSha3 = web3.utils.keccak256(accounts[0])
})

describe.skip('Pre-Auth Identity', async function() {
it('should deploy successfully', async function() {
const sig = randomHex(10)
const data = randomHex(10)
const url = '1234567890'
await deploy('Identity', {
from: accounts[0],
args: [
// [1], [3], [accounts[0]], sig, data, url, [sig.length-2], [data.length-2], [url.length]
[1],
[3],
[accounts[0]],
sig,
data,
url,
[10],
[10],
[10]
],
path: `${__dirname}/contracts/identity/`,
})
})
})

describe('Keys', async function() {
it('should set a default MANAGEMENT_KEY', async function() {
const res = await UserIdentity.methods.getKey(acctSha3).call()
assert.equal(res.purpose, '1')
assert.equal(res.keyType, '1')
assert.equal(res.key, acctSha3)
})

it('should respond to getKeyPurpose', async function() {
const res = await UserIdentity.methods.getKeyPurpose(acctSha3).call()
assert.equal(res, '1')
})

it('should respond to getKeysByPurpose', async function() {
const res = await UserIdentity.methods.getKeysByPurpose(1).call()
assert.deepEqual(res, [acctSha3])
})

it('should implement addKey', async function() {
const newKey = web3.utils.randomHex(32)
const res = await UserIdentity.methods.addKey(newKey, 1, 1).send()
assert(res.events.KeyAdded)

const getKey = await UserIdentity.methods.getKey(newKey).call()
assert.equal(getKey.key, newKey)
})

it('should not allow an existing key to be added', async function() {
try {
await UserIdentity.methods.addKey(acctSha3, 1, 1).send()
assert(false)
} catch (e) {
assert(e.message.match(/revert/))
}
})

it('should not allow sender without MANAGEMENT_KEY to addKey', async function() {
try {
await UserIdentity.methods.addKey(web3.utils.randomHex(32), 1, 1).send({
from: accounts[1]
})
assert(false)
} catch (e) {
assert(e.message.match(/revert/))
}
})
})

describe('Claims', async function() {
it('should allow a claim to be added by management account', async function() {
const response = await UserIdentity.methods
.addClaim(1, 2, accounts[0], randomHex(32), randomHex(32), 'abc.com')
.send()
assert(response.events.ClaimAdded)
})

it('should disallow new claims from unrecognized accounts', async function() {
try {
await UserIdentity.methods
.addClaim(1, 2, accounts[0], randomHex(32), randomHex(32), 'abc.com')
.send({ from: accounts[2] })
assert(false)
} catch (e) {
assert(e.message.match(/revert/))
}
})

it('should have 1 claim by type', async function() {
const byTypeRes = await UserIdentity.methods.getClaimIdsByType(1).call()
assert.equal(byTypeRes.length, 1)
})

it('should respond to getClaim', async function() {
const claimId = web3.utils.soliditySha3(accounts[0], 1)
const claim = await UserIdentity.methods.getClaim(claimId).call()
assert.equal(claim.claimType, '1')
})

// it('should respond to isClaimValid', async function() {
// const claimId = web3.utils.soliditySha3(accounts[0], 1)
// const valid = await UserIdentity.methods.isClaimValid(claimId).call()
// assert(valid)
// })

it('should allow claim to be removed', async function() {
const claimId = web3.utils.soliditySha3(accounts[0], 1)
const response = await UserIdentity.methods
.removeClaim(claimId)
.send({ from: accounts[0] })
assert(response.events.ClaimRemoved)

const claim = await UserIdentity.methods.getClaim(claimId).call()
assert.equal(claim.claimType, '0')
})
})

describe('Executions', async function() {
it('should allow any account to execute actions', async function() {
const addClaimAbi = await UserIdentity.methods
.addClaim(1, 2, accounts[0], randomHex(32), randomHex(32), 'abc.com')
.encodeABI()

const response = await UserIdentity.methods
.execute(UserIdentity.options.address, 0, addClaimAbi)
.send({
from: accounts[2]
})

assert(response.events.ExecutionRequested)
assert(!response.events.Approved)
assert(!response.events.Executed)
})

it('should auto-approve executions from MANAGEMENT_KEYs', async function() {
const addClaimAbi = await UserIdentity.methods
.addClaim(1, 2, accounts[0], randomHex(32), randomHex(32), 'abc.com')
.encodeABI()

const response = await UserIdentity.methods
.execute(UserIdentity.options.address, 0, addClaimAbi)
.send({
from: accounts[0]
})

assert(response.events.ExecutionRequested)
assert(response.events.Approved)
assert(response.events.ClaimAdded)
assert(response.events.Executed)
})
})

describe('Approvals', async function() {
it('should allow MANAGEMENT_KEYs to approve executions', async function() {
const addClaimAbi = await UserIdentity.methods
.addClaim(1, 2, accounts[2], randomHex(32), randomHex(32), 'abc.com')
.encodeABI()

const response = await UserIdentity.methods
.execute(UserIdentity.options.address, 0, addClaimAbi)
.send({ from: accounts[2] })

assert(response.events.ExecutionRequested)
assert(!response.events.Approved)

const id = response.events.ExecutionRequested.returnValues.executionId

const approval = await UserIdentity.methods
.approve(id, true)
.send({ from: accounts[0] })

assert(approval.events.Approved)
assert(approval.events.ClaimAdded)
assert(approval.events.Executed)
})

it('should allow ACTION_KEYs to approve executions')
it('should not allow CLAIM_SIGNER_KEYs to approve executions')
it('should not be able to approve an already executed execution')
it('should not be able to approve a non-existant execution')
})
})

0 comments on commit 0c09e12

Please sign in to comment.
You can’t perform that action at this time.