-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtwitter_spec.js
99 lines (74 loc) · 3.13 KB
/
dtwitter_spec.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// our DTwitter contract object to test
const DTwitter = require('Embark/contracts/DTwitter');
// contract methods we'll be testing
const { createAccount, users, owners, userExists, editAccount, tweet } = DTwitter.methods;
// variables that will be updated in the tests
let accounts;
// set up our config test parameters
config({
contracts: {
DTwitter: {
// would pass constructor args here if needed
}
}
}, (err, theAccounts) => {
// this is the list of accounts our node / wallet controls.
accounts = theAccounts;
});
// other test parameters
const username = 'testhandle';
const description = 'test description';
const tweetContent = 'test tweet';
// Embark exposes a global contract method as an alias
// for Mocha.describe
contract("DTwitter contract", function () {
this.timeout(0);
it("transaction to create a dtwitter user 'testhandle' with description 'test description' should be successful", async function () {
// do the create account
const createAccountTx = await createAccount(username, description).send();
// assert that the transaction was successful
assert.equal(createAccountTx.status, true);
});
it("should have created a user 'testhandle'", async function () {
// get user details from contract
const user = await users(web3.utils.keccak256(username)).call();
assert.equal(user.username, username);
assert.equal(user.description, description);
});
it("should have created an owner for our defaultAccount", async function () {
// read from the owners mapping the value associated with the defaultAccount
const usernameHash = await owners(web3.eth.defaultAccount).call();
// check the return value from owners mapping matches
assert.equal(usernameHash, web3.utils.keccak256(username));
});
it("should know 'testhandle' exists", async function () {
const usernameHash = web3.utils.keccak256(username);
// check the usernamehash exists
const exists = await userExists(usernameHash).call();
assert.equal(exists, true);
});
it("should be able to edit 'testhandle' user details", async function () {
const usernameHash = web3.utils.keccak256(username);
const updatedDescription = description + ' edited';
const updatedImageHash = 'QmWvPtv2xVGgdV12cezG7iCQ4hQ52e4ptmFFnBK3gTjnec';
// call edit account
await editAccount(usernameHash, updatedDescription, updatedImageHash).send();
// then fetch the user details with the usernamehash
const updatedUserDetails = await users(usernameHash).call();
assert.equal(updatedUserDetails.description, updatedDescription);
assert.equal(updatedUserDetails.picture, updatedImageHash);
});
it("should be able to add a tweet as 'testhandle' and receive it via contract event", async function () {
const usernameHash = web3.utils.keccak256(username);
// send the tweet
await tweet(tweetContent).send();
// subscribe to new tweet events
DTwitter.events.NewTweet({
filter: { _from: usernameHash },
fromBlock: 1 // must be > 0!
})
.on('data', (event) => {
assert.equal(event.returnValues.tweet, tweetContent);
});
});
});