@@ -19,7 +19,7 @@ const docHash = "hello";
1919const empty_hash = "0x0000000000000000000000000000000000000000000000000000000000000000" ;
2020
2121 async function latestTime ( ) {
22- return ( await web3 . eth . getBlock ( "latest" ) ) . timestamp ;
22+ return ( await web3 . eth . getBlock ( "latest" ) ) . timestamp ;
2323 }
2424
2525 before ( async ( ) => {
@@ -33,113 +33,121 @@ const empty_hash = "0x0000000000000000000000000000000000000000000000000000000000
3333
3434 describe ( `Test cases for the ERC1643 contract\n` , async ( ) => {
3535
36- describe ( `Test cases for the setDocument() function of the ERC1643\n` , async ( ) => {
37-
38- it ( "\tShould failed in executing the setDocument() function because msg.sender is not authorised\n" , async ( ) => {
39- await catchRevert (
40- erc1643 . setDocument ( "doc1" , "https://www.gogl.bts.fly" , "0x0" , { from : account1 } )
41- ) ;
42- } ) ;
43-
44- it ( "\tShould failed to set a document details as name is empty\n" , async ( ) => {
45- await catchRevert (
46- erc1643 . setDocument ( "" , "https://www.gogl.bts.fly" , "0x0" , { from : tokenOwner } )
47- ) ;
48- } ) ;
49-
50- it ( "\tShould failed to set a document details as URI is empty\n" , async ( ) => {
51- await catchRevert (
52- erc1643 . setDocument ( "doc1" , "" , "0x0" , { from : tokenOwner } )
53- ) ;
54- } ) ;
55-
56- it ( "\tShould sucessfully add the document details in the `_documents` mapping and change the length of the `_docsNames`\n" , async ( ) => {
57- let tx = await erc1643 . setDocument ( "doc1" , uri , docHash , { from : tokenOwner } ) ;
58- assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc1" ) ;
59- assert . equal ( tx . logs [ 0 ] . args . _uri , uri ) ;
60- assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _documentHash ) , docHash ) ;
61- assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 1 ) ;
62- } ) ;
63-
64- it ( "\tShould successfully add the new document and allow the empty docHash to be added in the `Document` structure\n" , async ( ) => {
65- let tx = await erc1643 . setDocument ( "doc2" , uri , "0x0" , { from : tokenOwner } ) ;
66- assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc2" ) ;
67- assert . equal ( tx . logs [ 0 ] . args . _uri , uri ) ;
68- assert . equal ( tx . logs [ 0 ] . args . _documentHash , empty_hash ) ;
69- assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 2 ) ;
70- } ) ;
71-
72- it ( "\tShould successfully update the existing document and length of `_docsNames` should remain unaffected\n" , async ( ) => {
73- let tx = await erc1643 . setDocument ( "doc2" , "https://www.bts.l" , "0x0" , { from : tokenOwner } ) ;
74- assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc2" ) ;
75- assert . equal ( tx . logs [ 0 ] . args . _uri , "https://www.bts.l" ) ;
76- assert . equal ( tx . logs [ 0 ] . args . _documentHash , empty_hash ) ;
77- assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 2 ) ;
78- } ) ;
79-
80- describe ( "Test cases for the getters functions\n" , async ( ) => {
81-
82- it ( "\tShould get the details of existed document\n" , async ( ) => {
83- let doc1Details = await erc1643 . getDocument . call ( "doc1" ) ;
84- assert . equal ( doc1Details [ 0 ] , uri ) ;
85- assert . equal ( web3 . utils . toUtf8 ( doc1Details [ 1 ] ) , docHash ) ;
86- assert . closeTo ( doc1Details [ 2 ] . toNumber ( ) , await latestTime ( ) , 2 ) ;
87-
88- let doc2Details = await erc1643 . getDocument . call ( "doc2" ) ;
89- assert . equal ( doc2Details [ 0 ] , "https://www.bts.l" ) ;
90- assert . equal ( doc2Details [ 1 ] , empty_hash ) ;
91- assert . closeTo ( doc2Details [ 2 ] . toNumber ( ) , await latestTime ( ) , 2 ) ;
92- } ) ;
93-
94- it ( "\tShould get the details of the non-existed document it means every value should be zero\n" , async ( ) => {
95- let doc3Details = await erc1643 . getDocument . call ( "doc3" ) ;
96- assert . equal ( doc3Details [ 0 ] , "" ) ;
97- assert . equal ( web3 . utils . toUtf8 ( doc3Details [ 1 ] ) , "" ) ;
98- assert . equal ( doc3Details [ 2 ] , 0 ) ;
99- } ) ;
100-
101- it ( "\tShould get all the documents present in the contract\n" , async ( ) => {
102- let allDocs = await erc1643 . getAllDocuments . call ( )
103- assert . equal ( allDocs . length , 2 ) ;
104- assert . equal ( web3 . utils . toUtf8 ( allDocs [ 0 ] ) , "doc1" ) ;
105- assert . equal ( web3 . utils . toUtf8 ( allDocs [ 1 ] ) , "doc2" ) ;
106- } ) ;
107- } )
108- } ) ;
109-
110- describe ( "Test cases for the removeDocument()\n" , async ( ) => {
111-
112- it ( "\tShould failed to remove document because msg.sender is not authorised\n" , async ( ) => {
113- await catchRevert (
114- erc1643 . removeDocument ( "doc2" , { from : account1 } )
115- ) ;
116- } ) ;
117-
118- it ( "\tShould failed to remove the document that is not existed in the contract\n" , async ( ) => {
119- await catchRevert (
120- erc1643 . removeDocument ( "doc3" , { from : tokenOwner } )
121- ) ;
122- } ) ;
123-
124- it ( "\tShould succssfully remove the document from the contract which is present in the last index of the `_docsName` and check the params of the `DocumentRemoved` event\n" , async ( ) => {
125- // first add the new document
126- await erc1643 . setDocument ( "doc3" , "https://www.bts.l" , "0x0" , { from : tokenOwner } ) ;
127- // as this will be last in the array so remove this
128- let tx = await erc1643 . removeDocument ( "doc3" , { from : tokenOwner } ) ;
129- assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc3" ) ;
130- assert . equal ( tx . logs [ 0 ] . args . _uri , "https://www.bts.l" ) ;
131- assert . equal ( tx . logs [ 0 ] . args . _documentHash , empty_hash ) ;
132- assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 2 ) ;
133-
134- // remove the document that is not last in the `docsName` array
135- tx = await erc1643 . removeDocument ( "doc1" , { from : tokenOwner } ) ;
136- assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc1" ) ;
137- assert . equal ( tx . logs [ 0 ] . args . _uri , uri ) ;
138- assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _documentHash ) , docHash ) ;
139- assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 1 ) ;
140- } ) ;
141-
142- describe ( "Test cases for the getters functions\n" , async ( ) => {
36+ describe ( `Test cases for the setDocument() function of the ERC1643\n` , async ( ) => {
37+
38+ it ( "\tShould failed in executing the setDocument() function because msg.sender is not authorised\n" , async ( ) => {
39+ await catchRevert (
40+ erc1643 . setDocument ( "doc1" , "https://www.gogl.bts.fly" , "0x0" , { from : account1 } )
41+ ) ;
42+ } ) ;
43+
44+ it ( "\tShould failed to set a document details as name is empty\n" , async ( ) => {
45+ await catchRevert (
46+ erc1643 . setDocument ( "" , "https://www.gogl.bts.fly" , "0x0" , { from : tokenOwner } )
47+ ) ;
48+ } ) ;
49+
50+ it ( "\tShould failed to set a document details as URI is empty\n" , async ( ) => {
51+ await catchRevert (
52+ erc1643 . setDocument ( "doc1" , "" , "0x0" , { from : tokenOwner } )
53+ ) ;
54+ } ) ;
55+
56+ it ( "\tShould sucessfully add the document details in the `_documents` mapping and change the length of the `_docsNames`\n" , async ( ) => {
57+ let tx = await erc1643 . setDocument ( "doc1" , uri , docHash , { from : tokenOwner } ) ;
58+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc1" ) ;
59+ assert . equal ( tx . logs [ 0 ] . args . _uri , uri ) ;
60+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _documentHash ) , docHash ) ;
61+ assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 1 ) ;
62+ } ) ;
63+
64+ it ( "\tShould successfully add the new document and allow the empty docHash to be added in the `Document` structure\n" , async ( ) => {
65+ let tx = await erc1643 . setDocument ( "doc2" , uri , "0x0" , { from : tokenOwner } ) ;
66+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc2" ) ;
67+ assert . equal ( tx . logs [ 0 ] . args . _uri , uri ) ;
68+ assert . equal ( tx . logs [ 0 ] . args . _documentHash , empty_hash ) ;
69+ assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 2 ) ;
70+ } ) ;
71+
72+ it ( "\tShould successfully update the existing document and length of `_docsNames` should remain unaffected\n" , async ( ) => {
73+ let tx = await erc1643 . setDocument ( "doc2" , "https://www.bts.l" , "0x0" , { from : tokenOwner } ) ;
74+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc2" ) ;
75+ assert . equal ( tx . logs [ 0 ] . args . _uri , "https://www.bts.l" ) ;
76+ assert . equal ( tx . logs [ 0 ] . args . _documentHash , empty_hash ) ;
77+ assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 2 ) ;
78+ } ) ;
79+
80+ describe ( "Test cases for the getters functions\n" , async ( ) => {
81+
82+ it ( "\tShould get the details of existed document\n" , async ( ) => {
83+ let doc1Details = await erc1643 . getDocument . call ( "doc1" ) ;
84+ assert . equal ( doc1Details [ 0 ] , uri ) ;
85+ assert . equal ( web3 . utils . toUtf8 ( doc1Details [ 1 ] ) , docHash ) ;
86+ assert . closeTo ( doc1Details [ 2 ] . toNumber ( ) , await latestTime ( ) , 2 ) ;
87+
88+ let doc2Details = await erc1643 . getDocument . call ( "doc2" ) ;
89+ assert . equal ( doc2Details [ 0 ] , "https://www.bts.l" ) ;
90+ assert . equal ( doc2Details [ 1 ] , empty_hash ) ;
91+ assert . closeTo ( doc2Details [ 2 ] . toNumber ( ) , await latestTime ( ) , 2 ) ;
92+ } ) ;
93+
94+ it ( "\tShould get the details of the non-existed document it means every value should be zero\n" , async ( ) => {
95+ let doc3Details = await erc1643 . getDocument . call ( "doc3" ) ;
96+ assert . equal ( doc3Details [ 0 ] , "" ) ;
97+ assert . equal ( web3 . utils . toUtf8 ( doc3Details [ 1 ] ) , "" ) ;
98+ assert . equal ( doc3Details [ 2 ] , 0 ) ;
99+ } ) ;
100+
101+ it ( "\tShould get all the documents present in the contract\n" , async ( ) => {
102+ let allDocs = await erc1643 . getAllDocuments . call ( )
103+ assert . equal ( allDocs . length , 2 ) ;
104+ assert . equal ( web3 . utils . toUtf8 ( allDocs [ 0 ] ) , "doc1" ) ;
105+ assert . equal ( web3 . utils . toUtf8 ( allDocs [ 1 ] ) , "doc2" ) ;
106+ } ) ;
107+ } )
108+ } ) ;
109+
110+ describe ( "Test cases for the removeDocument()\n" , async ( ) => {
111+
112+ it ( "\tShould failed to remove document because msg.sender is not authorised\n" , async ( ) => {
113+ await catchRevert (
114+ erc1643 . removeDocument ( "doc2" , { from : account1 } )
115+ ) ;
116+ } ) ;
117+
118+ it ( "\tShould failed to remove the document that is not existed in the contract\n" , async ( ) => {
119+ await catchRevert (
120+ erc1643 . removeDocument ( "doc3" , { from : tokenOwner } )
121+ ) ;
122+ } ) ;
123+
124+ it ( "\tShould succssfully remove the document from the contract which is present in the last index of the `_docsName` and check the params of the `DocumentRemoved` event\n" , async ( ) => {
125+ // first add the new document
126+ await erc1643 . setDocument ( "doc3" , "https://www.bts.l" , "0x0" , { from : tokenOwner } ) ;
127+ // as this will be last in the array so remove this
128+ let tx = await erc1643 . removeDocument ( "doc3" , { from : tokenOwner } ) ;
129+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc3" ) ;
130+ assert . equal ( tx . logs [ 0 ] . args . _uri , "https://www.bts.l" ) ;
131+ assert . equal ( tx . logs [ 0 ] . args . _documentHash , empty_hash ) ;
132+ assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 2 ) ;
133+
134+ // remove the document that is not last in the `docsName` array
135+ tx = await erc1643 . removeDocument ( "doc1" , { from : tokenOwner } ) ;
136+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc1" ) ;
137+ assert . equal ( tx . logs [ 0 ] . args . _uri , uri ) ;
138+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _documentHash ) , docHash ) ;
139+ assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 1 ) ;
140+ } ) ;
141+
142+ it ( "\t Should delete the doc to validate the #17 issue problem" , async ( ) => {
143+ let tx = await erc1643 . removeDocument ( "doc2" , { from : tokenOwner } ) ;
144+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _name ) , "doc2" ) ;
145+ assert . equal ( tx . logs [ 0 ] . args . _uri , "https://www.bts.l" ) ;
146+ assert . equal ( web3 . utils . toUtf8 ( tx . logs [ 0 ] . args . _documentHash ) , '' ) ;
147+ assert . equal ( ( await erc1643 . getAllDocuments . call ( ) ) . length , 0 ) ;
148+ } ) ;
149+
150+ describe ( "Test cases for the getters functions\n" , async ( ) => {
143151
144152 it ( "\tShould get the details of the non-existed (earlier was present but get removed ) document it means every value should be zero\n" , async ( ) => {
145153 let doc1Details = await erc1643 . getDocument . call ( "doc1" ) ;
@@ -149,11 +157,13 @@ const empty_hash = "0x0000000000000000000000000000000000000000000000000000000000
149157 } ) ;
150158
151159 it ( "\tShould get all the documents present in the contract which should be 1\n" , async ( ) => {
160+ // add one doc before the getter call
161+ await erc1643 . setDocument ( "doc4" , "https://www.bts.l" , docHash , { from : tokenOwner } )
152162 let allDocs = await erc1643 . getAllDocuments . call ( )
153163 assert . equal ( allDocs . length , 1 ) ;
154- assert . equal ( web3 . utils . toUtf8 ( allDocs [ 0 ] ) , "doc2 " ) ;
164+ assert . equal ( web3 . utils . toUtf8 ( allDocs [ 0 ] ) , "doc4 " ) ;
155165 } ) ;
156- } ) ;
157- } )
158- } ) ;
159- } ) ;
166+ } ) ;
167+ } )
168+ } ) ;
169+ } ) ;
0 commit comments