This repository was archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathipfs_store.test.js
412 lines (335 loc) · 14.2 KB
/
ipfs_store.test.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinon from 'sinon'
import {
IpfsDataStore,
BASE_SCHEMA_ID,
LISTING_DATA_TYPE,
LISTING_WITHDRAW_DATA_TYPE,
OFFER_DATA_TYPE,
OFFER_ACCEPT_DATA_TYPE,
PROFILE_DATA_TYPE,
REVIEW_DATA_TYPE
} from '../src/ipfsInterface/store'
import validListing from './fixtures/listing-valid.json'
import validOffer from './fixtures/offer-valid.json'
import validProfile from './fixtures/profile-valid.json'
import validReview from './fixtures/review-valid.json'
chai.use(chaiAsPromised)
const expect = chai.expect
describe('IpfsDataStore', () => {
it(`Should parse a valid schemaId`, () => {
const { dataType, schemaVersion } = IpfsDataStore.parseSchemaId(
BASE_SCHEMA_ID+'my-data-type_v1.0.0')
expect(dataType).to.equal('my-data-type')
expect(schemaVersion).to.equal('1.0.0')
})
it(`Should generate a valid schemaId`, () => {
const { schemaId, schemaVersion } = IpfsDataStore.generateSchemaId('my-data-type')
expect(schemaId).to.equal(BASE_SCHEMA_ID+'my-data-type_v1.0.0')
expect(schemaVersion).to.equal('1.0.0')
})
})
describe('Listing IpfsDataStore load', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should load a valid object`, async () => {
mockIpfsService.loadObjFromFile = sinon.stub().resolves(validListing)
mockIpfsService.rewriteUrl = sinon.stub().returns('http://test-gateway')
const listing = await store.load(LISTING_DATA_TYPE, 'TestHash')
expect(listing.type).to.equal('unit')
expect(listing.category).to.equal('schema.forSale')
expect(listing.subCategory).to.equal('schema.forSale.mushrooms')
expect(listing.language).to.equal('en-US')
expect(listing.title).to.equal('my listing')
expect(listing.description).to.equal('my description')
expect(listing.expiry).to.equal('1996-12-19T16:39:57-08:00')
expect(listing.media.length).to.equal(2)
expect(listing.media[0].url).to.equal('http://test-gateway')
expect(listing.unitsTotal).to.equal(1)
expect(listing.price).to.deep.equal({ amount: '0.033', currency: 'ETH' })
expect(listing.commission).to.deep.equal({ amount: '0', currency: 'OGN' })
expect(listing.ipfs.hash).to.equal('TestHash')
expect(listing.ipfs.data).to.deep.equal(validListing)
})
it(`Should throw an exception on listing using invalid schema Id`, () => {
const listingInvalidSchemaId = Object.assign({}, validListing, {
schemaId: 'badSchemaId'
})
mockIpfsService.loadObjFromFile = sinon
.stub()
.resolves(listingInvalidSchemaId)
return expect(store.load(LISTING_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
it(`Should throw an exception on listing data with missing fields`, () => {
const badListing = { title: 'bad listing' }
mockIpfsService.loadObjFromFile = sinon.stub().resolves(badListing)
return expect(store.load(LISTING_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
})
describe('Listing IpfsDataStore save', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should save a valid object with IPFS URLs`, () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('ListingHash')
mockIpfsService.saveDataURIAsFile = sinon.stub().returns('DataHash')
mockIpfsService.gatewayUrlForHash = sinon
.stub()
.returns('http://test-gateway')
// Clone validListing, because IpfsDataStore.save() filters out the test
// media URLs, which appear to be invalid.
const validListingCopy = JSON.parse(JSON.stringify(validListing))
return expect(store.save(LISTING_DATA_TYPE, validListingCopy)).to.eventually.equal('ListingHash')
.then(() => expect(mockIpfsService.saveDataURIAsFile.callCount).to.equal(0))
.then(() => expect(mockIpfsService.gatewayUrlForHash.callCount).to.equal(0))
.then(() => expect(mockIpfsService.saveObjAsFile.firstCall.args[0]).to.have.property('schemaId'))
})
it(`Should save a valid listing with data`, async () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('ListingHash')
mockIpfsService.saveDataURIAsFile = sinon.stub().returns('DataHash')
mockIpfsService.gatewayUrlForHash = sinon
.stub()
.returns('http://test-gateway')
const media = {
media: [
{ url: 'data:image/jpeg;name=test1.jpg;base64,/AA/BB' },
{ url: 'data:image/jpeg;name=test2.jpg;base64,/CC/DD' }
]
}
const listing = Object.assign({}, validListing, media)
const ipfsHash = await store.save(LISTING_DATA_TYPE, listing)
expect(ipfsHash).to.equal('ListingHash')
// Check the media content was saved as separate IPFS files.
expect(mockIpfsService.saveDataURIAsFile.callCount).to.equal(2)
// Check the URL for media content is an IPFS URL.
const ipfsData = mockIpfsService.saveObjAsFile.firstCall.args[0]
expect(ipfsData.media[0].url.substring(0, 7)).to.equal('ipfs://')
expect(ipfsData.media[1].url.substring(0, 7)).to.equal('ipfs://')
})
it(`Should filter out invalid media`, async () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('ListingHash')
const media = {
media: [
{ url: 'bogus://' }, // Invalid data field.
{ url: 'http://notallowed' } // Only ipfs and dwed URL are allowed.
]
}
const listing = Object.assign({}, validListing, media)
await store.save(LISTING_DATA_TYPE, listing)
// Check all the entries were filtered out in the listing data saved to IPFS.
const ipfsData = mockIpfsService.saveObjAsFile.firstCall.args[0]
expect(ipfsData.media.length).to.equal(0)
})
it(`Should throw an exception on invalid listing`, async () => {
const badListing = { title: 'bad listing' }
return expect(store.save(
LISTING_DATA_TYPE,
badListing)).to.eventually.be.rejectedWith(Error)
})
})
describe('ListingWithdraw IpfsDataStore load', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should load a valid object`, async () => {
// Empty besides schemaId since withdrawal does not have any data yet.
const validWithdrawal = { schemaId: BASE_SCHEMA_ID+'listing-withdraw_v1.0.0' }
mockIpfsService.loadObjFromFile = sinon.stub().resolves(validWithdrawal)
const withdraw = await store.load(LISTING_WITHDRAW_DATA_TYPE, 'WithdrawalHash')
expect(withdraw.ipfs.hash).to.equal('WithdrawalHash')
expect(withdraw.ipfs.data).to.deep.equal(validWithdrawal)
})
})
describe('ListingWithdraw IpfsDataStore save', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should save a valid withdrawal`, () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('WithdrawHash')
const validWithdrawal = {}
return expect(
store.save(LISTING_WITHDRAW_DATA_TYPE, validWithdrawal)).to.eventually.equal('WithdrawHash')
.then(() => expect(mockIpfsService.saveObjAsFile.firstCall.args[0]).to.have.property('schemaId'))
})
})
describe('Offer IpfsStore load', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should load a valid object`, async () => {
mockIpfsService.loadObjFromFile = sinon.stub().resolves(validOffer)
mockIpfsService.rewriteUrl = sinon.stub().returns('http://test-gateway')
const offer = await store.load(OFFER_DATA_TYPE, 'TestHash')
expect(offer.listingType).to.equal('unit')
expect(offer.unitsPurchased).to.equal(1)
expect(offer.totalPrice).to.deep.equal({ amount: '0.033', currency: 'ETH' })
expect(offer.ipfs.hash).to.equal('TestHash')
expect(offer.ipfs.data).to.deep.equal(validOffer)
})
it(`Should throw an exception on offer using invalid schema Id`, () => {
const offerInvalidSchemaId = Object.assign({}, validReview, {
schemaId: 'badSchemaId'
})
mockIpfsService.loadObjFromFile = sinon
.stub()
.resolves(offerInvalidSchemaId)
return expect(store.load(OFFER_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
it(`Should throw an exception on offer data with missing fields`, () => {
const badOffer = { title: 'bad offer' }
mockIpfsService.loadObjFromFile = sinon.stub().resolves(badOffer)
return expect(store.load(OFFER_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
})
describe('Offer IpfsStore save', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should save a valid offer`, () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('OfferHash')
return expect(
store.save(OFFER_DATA_TYPE, validOffer)).to.eventually.equal('OfferHash')
.then(() => expect(mockIpfsService.saveObjAsFile.firstCall.args[0]).to.have.property('schemaId'))
})
it(`Should throw an exception on invalid offer`, async () => {
const badOffer = { title: 'bad offer' }
return expect(store.save(
OFFER_DATA_TYPE,
badOffer)).to.eventually.be.rejectedWith(Error)
})
})
describe('OfferAccept IpfsDataStore load', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should load a valid accept`, async () => {
// Empty besides schemaId since accept does not have any data yet.
const validAccept = { schemaId: BASE_SCHEMA_ID+'offer-accept_v1.0.0' }
mockIpfsService.loadObjFromFile = sinon.stub().resolves(validAccept)
const accept = await store.load(OFFER_ACCEPT_DATA_TYPE, 'AcceptHash')
expect(accept.ipfs.hash).to.equal('AcceptHash')
expect(accept.ipfs.data).to.deep.equal(validAccept)
})
})
describe('OfferAccept IpfsDataStore save', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should save a valid accept`, () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('AcceptHash')
const validAccept = {}
return expect(
store.save(OFFER_ACCEPT_DATA_TYPE, validAccept)).to.eventually.equal('AcceptHash')
.then(() => expect(mockIpfsService.saveObjAsFile.firstCall.args[0]).to.have.property('schemaId'))
})
})
describe('Review IpfsDataStore load', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should load a valid withdrawal`, async () => {
mockIpfsService.loadObjFromFile = sinon.stub().resolves(validReview)
const review = await store.load(REVIEW_DATA_TYPE, 'ReviewHash')
expect(review.rating).to.equal(3)
expect(review.text).to.equal('Good stuff')
expect(review.ipfs.hash).to.equal('ReviewHash')
expect(review.ipfs.data).to.deep.equal(validReview)
})
it(`Should throw an exception on review using invalid schema Id`, () => {
const reviewInvalidSchemaId = Object.assign({}, validReview, {
schemaId: 'badSchemaId'
})
mockIpfsService.loadObjFromFile = sinon
.stub()
.resolves(reviewInvalidSchemaId)
return expect(store.load(REVIEW_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
it(`Should throw an exception on review data with missing fields`, () => {
const badReview = { title: 'bad review' }
mockIpfsService.loadObjFromFile = sinon.stub().resolves(badReview)
return expect(store.load(REVIEW_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
})
describe('Review IpfsDataStore save', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should save a valid review`, () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('ReviewHash')
return expect(
store.save(REVIEW_DATA_TYPE, validReview)).to.eventually.equal('ReviewHash')
.then(() => expect(mockIpfsService.saveObjAsFile.firstCall.args[0]).to.have.property('schemaId'))
})
it(`Should throw an exception on invalid review`, async () => {
const badReview = { title: 'bad review' }
return expect(store.save(
REVIEW_DATA_TYPE,
badReview)).to.eventually.be.rejectedWith(Error)
})
})
describe('Profile IpfsDataStore load', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should load a valid profile`, async () => {
mockIpfsService.loadObjFromFile = sinon.stub().resolves(validProfile)
const profile = await store.load(PROFILE_DATA_TYPE, 'ProfileHash')
expect(profile.firstName).to.equal('originus')
expect(profile.lastName).to.equal('protocolus')
expect(profile.description).to.equal('Semper Fidelis')
expect(profile.avatar).to.equal('data:,Avatar')
expect(profile.ipfs.hash).to.equal('ProfileHash')
expect(profile.ipfs.data).to.deep.equal(validProfile)
})
it(`Should throw an exception on profile with invalid schema Id`, () => {
const profileInvalidSchemaId = Object.assign({}, validProfile, {
schemaId: 'badSchemaId'
})
mockIpfsService.loadObjFromFile = sinon
.stub()
.resolves(profileInvalidSchemaId)
return expect(store.load(PROFILE_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
it(`Should throw an exception on profile data with missing schemaId`, () => {
const badProfile = { }
mockIpfsService.loadObjFromFile = sinon.stub().resolves(badProfile)
return expect(store.load(PROFILE_DATA_TYPE, 'TestHash')).to.eventually.be.rejectedWith(Error)
})
})
describe('Profile IpfsDataStore save', () => {
let mockIpfsService, store
before(() => {
mockIpfsService = new Object()
store = new IpfsDataStore(mockIpfsService)
})
it(`Should save a valid profile`, () => {
mockIpfsService.saveObjAsFile = sinon.stub().returns('ProfileHash')
return expect(
store.save(PROFILE_DATA_TYPE, validProfile)).to.eventually.equal('ProfileHash')
.then(() => expect(mockIpfsService.saveObjAsFile.firstCall.args[0]).to.have.property('schemaId'))
})
})