2018-06-26 13:34:24 -04:00
const utils = require ( '../utils/testUtils' )
const assert = require ( 'assert' ) ;
const BN = web3 . utils . BN ;
var _ = require ( 'lodash' ) ;
var rlp = require ( 'rlp' ) ;
2018-10-01 08:45:27 -04:00
const PollManager = embark . require ( 'Embark/contracts/PollManager' ) ;
const SNT = embark . require ( 'Embark/contracts/SNT' ) ;
2018-09-19 14:35:14 -04:00
const decimals = ( amount ) => web3 . utils . toWei ( amount . toString ( ) , "ether" ) ;
2018-06-26 13:34:24 -04:00
config ( {
contracts : {
"MiniMeTokenFactory" : {
"gasLimit" : 4000000
} ,
"MiniMeToken" : {
"deploy" : false ,
} ,
"SNT" : {
"instanceOf" : "MiniMeToken" ,
"args" : [
"$MiniMeTokenFactory" ,
utils . zeroAddress ,
0 ,
"TestMiniMeToken" ,
18 ,
"TST" ,
true
] ,
"gasLimit" : 4000000
} ,
"PollManager" : {
"deploy" : true ,
2018-09-18 11:46:30 -04:00
"args" : [ "$SNT" ]
2018-06-26 13:34:24 -04:00
}
}
} ) ;
2018-09-19 14:35:14 -04:00
2018-06-26 13:34:24 -04:00
describe ( "VotingDapp" , function ( ) {
this . timeout ( 0 ) ;
let accounts ;
2018-09-19 14:35:14 -04:00
let blockNumber ;
2018-06-26 13:34:24 -04:00
before ( function ( done ) {
web3 . eth . getAccounts ( ) . then ( ( acc ) => {
accounts = acc ;
2018-09-19 14:35:14 -04:00
return SNT . methods . generateTokens ( accounts [ 0 ] , decimals ( 12 ) ) . send ( ) ;
} ) . then ( ( ) => {
return SNT . methods . generateTokens ( accounts [ 1 ] , decimals ( 10 ) ) . send ( ) ;
} ) . then ( ( ) => {
return SNT . methods . generateTokens ( accounts [ 2 ] , decimals ( 12 ) ) . send ( ) ;
} ) . then ( ( ) => {
return SNT . methods . generateTokens ( accounts [ 3 ] , decimals ( 7 ) ) . send ( ) ;
} ) . then ( ( ) => {
return web3 . eth . getBlockNumber ( ) ;
} ) . then ( ( block ) => {
blockNumber = block ;
2018-06-26 13:34:24 -04:00
done ( ) ;
} ) ;
} ) ;
2018-09-19 14:35:14 -04:00
const description = [ "Should we ditch Slack for Status.im Desktop?" , [ "What are we waiting for?" , "Let's wait for Windows version first" , "Yes, as long as _____ feature is implemented" ] ] ;
const numBallots = 3 ;
const encodedDesc = "0x" + rlp . encode ( description ) . toString ( 'hex' ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:35:14 -04:00
let pollId ;
2018-09-18 13:47:39 -04:00
2018-09-19 14:35:14 -04:00
it ( "Creating a proposal without holding SNT should fail" , async ( ) => {
2018-06-26 13:34:24 -04:00
try {
2018-09-19 14:35:14 -04:00
const receipt = await PollManager . methods . addPoll (
2018-09-20 10:24:51 -04:00
blockNumber ,
2018-06-26 13:34:24 -04:00
blockNumber + 10 ,
2018-09-18 13:47:39 -04:00
encodedDesc ,
2018-09-18 11:46:30 -04:00
numBallots )
2018-06-26 13:34:24 -04:00
. send ( { from : accounts [ 8 ] } ) ;
assert . fail ( 'should have reverted before' ) ;
} catch ( error ) {
utils . assertJump ( error ) ;
2018-09-19 14:35:14 -04:00
}
} ) ;
it ( "A SNT holder can create polls" , async ( ) => {
2018-09-20 10:24:51 -04:00
const blockNumber = ( await web3 . eth . getBlockNumber ( ) ) + 1 ;
2018-09-19 14:35:14 -04:00
const receipt = await PollManager . methods . addPoll (
2018-09-20 10:24:51 -04:00
blockNumber ,
2018-09-18 11:46:30 -04:00
blockNumber + 10 ,
2018-09-18 13:47:39 -04:00
encodedDesc ,
2018-09-18 11:46:30 -04:00
numBallots )
2018-06-26 13:34:24 -04:00
. send ( { from : accounts [ 0 ] } ) ;
2018-09-18 11:46:30 -04:00
assert ( ! ! receipt . events . PollCreated , "PollCreated not triggered" ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:35:14 -04:00
pollId = receipt . events . PollCreated . returnValues . idPoll ;
2018-09-18 11:46:30 -04:00
2018-09-19 14:35:14 -04:00
console . log ( " - Gas used during poll creation: " + receipt . gasUsed ) ;
} ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:35:14 -04:00
it ( "A user should be able to determine if he can vote" , async ( ) => {
const canVote = await PollManager . methods . canVote ( pollId ) . call ( { from : accounts [ 0 ] } ) ;
2018-06-26 13:34:24 -04:00
assert . equal ( canVote , true , "User should be able to vote" ) ;
2018-09-19 14:35:14 -04:00
} ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:35:14 -04:00
it ( "A SNT holder cannot vote if ballots total is greater than current balance" , async ( ) => {
try {
const receipt = await PollManager . methods . vote ( pollId , [ decimals ( 11 ) , decimals ( 12 ) , decimals ( 12 ) ] ) . send ( { from : accounts [ 0 ] } ) ;
assert . fail ( 'should have reverted before' ) ;
} catch ( error ) {
utils . assertJump ( error ) ;
}
} ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:39:18 -04:00
it ( "Should be able to vote if the ballots contain the correct amount" , async ( ) => {
2018-09-19 14:35:14 -04:00
const ballots = [ decimals ( 9 ) , decimals ( 0 ) , decimals ( 1 ) ] ;
const receipt = await PollManager . methods . vote ( pollId , ballots ) . send ( { from : accounts [ 0 ] } ) ;
2018-09-18 11:46:30 -04:00
assert ( ! ! receipt . events . Vote , "Vote not triggered" ) ;
2018-09-19 14:35:14 -04:00
console . log ( " - Gas used during voting: " + receipt . gasUsed ) ;
} ) ;
2018-09-18 11:46:30 -04:00
2018-09-19 14:35:14 -04:00
it ( "The option the voter selected must be stored correctly" , async ( ) => {
const ballots = [ decimals ( 9 ) , decimals ( 0 ) , decimals ( 1 ) ] ;
2018-06-26 13:34:24 -04:00
2018-09-26 09:41:06 -04:00
const contractBallots = await PollManager . methods . getVote ( pollId , accounts [ 0 ] ) . call ( ) ;
assert . equal ( contractBallots [ 0 ] , ballots [ 0 ] , "Ballot1 value is incorrect" ) ;
assert . equal ( contractBallots [ 1 ] , ballots [ 1 ] , "Ballot2 value is incorrect" ) ;
assert . equal ( contractBallots [ 2 ] , ballots [ 2 ] , "Ballot3 value is incorrect" ) ;
2018-09-19 14:35:14 -04:00
} ) ;
it ( "More than 1 user should be able to vote" , async ( ) => {
const receipt = await PollManager . methods . vote ( pollId , [ decimals ( 4 ) , decimals ( 4 ) , decimals ( 1 ) ] ) . send ( { from : accounts [ 2 ] } ) ;
assert ( ! ! receipt . events . Vote , "Vote not triggered" ) ;
2018-10-01 08:58:40 -04:00
const poll = await PollManager . methods . poll ( pollId ) . call ( ) ;
assert . equal ( poll . _voters , 2 , "Invalid number of voters" ) ;
2018-09-19 14:35:14 -04:00
} ) ;
it ( "Voting when you're not a SNT holder SHOULD FAIL!" , async ( ) => {
2018-06-26 13:34:24 -04:00
try {
2018-09-19 14:35:14 -04:00
const receipt = await PollManager . methods . vote ( pollId , [ decimals ( 1 ) , decimals ( 2 ) , decimals ( 3 ) ] )
2018-06-26 13:34:24 -04:00
. send ( { from : accounts [ 8 ] } ) ;
assert . fail ( 'should have reverted before' ) ;
} catch ( error ) {
utils . assertJump ( error ) ;
}
2018-09-19 14:35:14 -04:00
} ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:35:14 -04:00
it ( "Getting poll information" , async ( ) => {
console . log ( " - Decoding poll title: " + ( await PollManager . methods . pollTitle ( pollId ) . call ( ) ) ) ;
console . log ( " - Decoding poll ballot 1: " + ( await PollManager . methods . pollBallot ( pollId , 0 ) . call ( ) ) ) ;
console . log ( " - Decoding poll ballot 2: " + ( await PollManager . methods . pollBallot ( pollId , 1 ) . call ( ) ) ) ;
console . log ( " - Decoding poll ballot 3: " + ( await PollManager . methods . pollBallot ( pollId , 2 ) . call ( ) ) ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:35:14 -04:00
const poll = await PollManager . methods . poll ( pollId ) . call ( ) ;
2018-06-26 16:34:28 -04:00
2018-09-19 14:35:14 -04:00
const voters = poll . _voters ;
2018-09-26 09:41:06 -04:00
2018-06-26 13:34:24 -04:00
2018-09-26 09:41:06 -04:00
// TODO: add tests with voters, and results*
2018-09-19 14:35:14 -04:00
} ) ;
2018-06-26 16:34:28 -04:00
2018-09-19 14:35:14 -04:00
it ( "User can unvote" , async ( ) => {
const poll = await PollManager . methods . poll ( pollId ) . call ( ) ;
2018-06-26 13:34:24 -04:00
2018-09-19 14:35:14 -04:00
const receipt = await PollManager . methods . unvote ( pollId ) . send ( { from : accounts [ 0 ] } ) ;
assert ( ! ! receipt . events . Unvote , "Unvote not triggered" ) ;
2018-09-18 11:46:30 -04:00
2018-09-26 09:41:06 -04:00
const contractBallots = await PollManager . methods . getVote ( pollId , accounts [ 0 ] ) . call ( ) ;
2018-09-18 11:46:30 -04:00
2018-09-26 09:41:06 -04:00
assert . equal ( contractBallots [ 0 ] , 0 , "Ballot1 value is incorrect" ) ;
assert . equal ( contractBallots [ 1 ] , 0 , "Ballot2 value is incorrect" ) ;
assert . equal ( contractBallots [ 2 ] , 0 , "Ballot3 value is incorrect" ) ;
2018-09-18 11:46:30 -04:00
2018-09-19 14:35:14 -04:00
const updatedPoll = await PollManager . methods . poll ( pollId ) . call ( ) ;
assert . equal ( updatedPoll . _voters , poll . _voters - 1 , "Number of voters is incorrect" )
} ) ;
2018-09-18 11:46:30 -04:00
2018-09-19 14:35:14 -04:00
it ( "User can vote again" , async ( ) => {
const ballots = [ 9 , 2 , 1 ] ;
const receipt = await PollManager . methods . vote ( pollId , ballots ) . send ( { from : accounts [ 0 ] } ) ;
assert ( ! ! receipt . events . Vote , "Vote not triggered" ) ;
2018-06-26 13:34:24 -04:00
} ) ;
} ) ;