-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patha01-e2e.js
More file actions
357 lines (313 loc) · 9.8 KB
/
a01-e2e.js
File metadata and controls
357 lines (313 loc) · 9.8 KB
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
// const rp = require('request-promise')
const assert = require('chai').assert
const ob = require('../../openbazaar.js')
const config = {
apiCredentials: '',
obServer: 'http://localhost',
// obServer: 'http://p2pvps.net',
obPort: 4002
}
const OB_USERNAME = 'yourUsername'
const OB_PASSWORD = 'yourPassword'
describe('Authentication', () => {
describe('getOBAuth()', () => {
it('should generate an authorization header.', async () => {
try {
config.clientId = OB_USERNAME
config.clientSecret = OB_PASSWORD
const apiCredentials = ob.getOBAuth(config)
console.log(`apiCredentials: ${JSON.stringify(apiCredentials, null, 2)}`)
// Save the credentials for use in other tests.
config.apiCredentials = apiCredentials
assert(typeof apiCredentials === 'string', 'Returns a string')
} catch (err) {
console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2))
throw err
}
})
})
})
describe('Notifications', () => {
describe('getNotifications()', () => {
it('should return an array of notifications.', async () => {
const notifications = await ob.getNotifications(config)
// console.log(`test notifications: ${JSON.stringify(notifications, null, 2)}`)
assert.isArray(notifications.notifications, 'Returns an array')
assert.isNumber(notifications.total, 'Returns a total of notifications')
})
})
})
describe('Create Profile', () => {
describe('Generate a new profile for the new store.', () => {
it('should generate a new profile', async () => {
try {
const profileData = {
handle: 'drwasho',
name: 'Washington Sanchez',
location: 'Brisbane',
about: 'The Dude',
shortDescription: 'Yo',
contactInfo: {
website: 'openbazaar.org',
email: 'drwasho@openbazaar.org',
phoneNumber: '12345'
},
nsfw: false,
vendor: true,
moderator: false,
colors: {
primary: '#000000',
secondary: '#FFD700',
text: '#ffffff',
highlight: '#123ABC',
highlightText: '#DEAD00'
}
}
const profile = await ob.createProfile(config, profileData)
// console.log(`profile: ${JSON.stringify(profile, null, 2)}`)
assert(profile.handle === 'drwasho', 'Correct handle returned')
assert(profile.contactInfo.website === 'openbazaar.org', 'Correct contact info returned')
} catch (err) {
if (err.statusCode === 409) {
assert(
err.response.body.reason === 'Profile already exists. Use PUT.',
'Profile already exists. Correct error message returned'
)
} else {
throw err
}
}
})
})
})
describe('Listing', () => {
describe('createListing()', () => {
it('should create a listing.', async () => {
const now = new Date()
const oneDay = 60000 * 60 * 24
const thirtyDaysFromNow = new Date(now.getTime() + oneDay * 30)
const listingData = {
coupons: [
{
discountCode: 'TESTING',
title: 'TESTING',
priceDiscount: 29
}
],
refundPolicy: '',
shippingOptions: [],
termsAndConditions: '',
metadata: {
contractType: 'SERVICE',
expiry: thirtyDaysFromNow.toISOString(),
format: 'FIXED_PRICE',
pricingCurrency: 'USD'
},
item: {
categories: [],
condition: 'NEW',
description: 'test description',
nsfw: false,
options: [],
price: 123,
tags: [],
title: 'test title',
images: [
{
filename: 'p2pvp.org.png',
original: 'zb2rhdwvBAjky685CtiZHbA291rJGGAVpA7RhvY7vRrznX6Ne',
large: 'zb2rhoDG3RLMkFarDGajWALbgeXgGG6xmHCZAqiTYrdrdP8ew',
medium: 'zb2rhmdVqhZjnpw6Pwd4tCimB1L79ABcukcRnqDroP1C5B6GE',
small: 'zb2rhiBc94WnxN3eNtbnBU2CD9sJ1X1QiaemYFpAVFwQVPDsq',
tiny: 'zb2rhYBN6k6udcF86NWaPr1GBB8HpPYLcL1HwFtJZE7gGEpT8'
}
],
skus: [
{
quantity: 1,
productID: '123456789'
}
]
}
}
const result = await ob.createListing(config, listingData)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
config.slug = result.slug
assert.property(result, 'slug', 'Returns object with a property of slug')
})
})
describe('getListings()', () => {
it('should return an array of store listings', async () => {
const listings = await ob.getListings(config)
// console.log(`listings: ${JSON.stringify(listings, null, 2)}`)
assert.isArray(listings, 'Returns an array')
})
})
describe('removeListing()', () => {
it('should return an empty object.', async () => {
const result = await ob.removeListing(config, config.slug)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert.isObject(result, 'Returns an object')
assert.isEmpty(result, 'Object is empty.')
})
})
})
describe('Wallet', () => {
describe('getWalletBalance()', () => {
it('should return the balance of the wallet.', async () => {
const result = await ob.getWalletBalance(config)
// console.log(`getWalletBalance() result: ${JSON.stringify(result, null, 2)}`)
assert.isNumber(result.confirmed, 'Confirmed balance is a number')
assert.isNumber(result.unconfirmed, 'Unconfirmed balance is a number')
})
})
describe('getExchangeRate()', () => {
it('should return the exchange rate.', async () => {
const result = await ob.getExchangeRate(config)
// console.log(`getExchangeRate() result: ${JSON.stringify(result, null, 2)}`)
assert.isNumber(result.USD, 'Get USD exchange rate.')
})
})
describe('getAddress()', () => {
it('should return the public cryptocoin address', async () => {
const result = await ob.getAddress(config)
// console.log(`getAddress() result: ${JSON.stringify(result, null, 2)}`)
assert.isNotEmpty(result.address, 'Address is returned')
})
})
describe('sendMoney()', () => {
it('should send $0.05 in cryptocurrency to self.', async () => {
const walletBalance = await ob.getWalletBalance(config)
const address = await ob.getAddress(config)
const exchangeRate = await ob.getExchangeRate(config)
const usdExchangeRate = exchangeRate.USD
const bchPerDollar = 1 / usdExchangeRate
// Five and Ten cents in Satoshis.
const fiveCents = roundSatoshis(bchPerDollar * 0.05)
const tenCents = fiveCents * 2
if (walletBalance.confirmed < tenCents) {
assert(1, 1, 'Test skipped due to lack of balance.')
return
}
const moneyObj = {
address: address.address,
amount: fiveCents,
feeLevel: 'ECONOMIC',
memo: 'openbazaar-node test'
}
const result = await ob.sendMoney(config, moneyObj)
// console.log(`sendMoney() result: ${JSON.stringify(result, null, 2)}`)
assert.isNumber(result.amount, 'Total amount of Satoshis sent')
assert.isNumber(result.confirmedBalance, 'New wallet balance.')
assert(result.memo, 'openbazaar-node test', 'Memo is included correctly.')
assert.isNotEmpty(result.timestamp, 'Timestamp is included')
assert.isNumber(result.unconfirmedBalance, 'Unconfirmed balance is a number.')
})
})
})
/*
// TODO mock this API
describe('getOrder()', () => {
it('should return information on an order', async () => {
const result = await ob.getOrder(config, 'QmQ1qjhFXvBEtfof346Z9W5Go6K9jojgvvBXquCKQEQQkk')
console.log(`getOrder() result: ${JSON.stringify(result, null, 2)}`)
assert.isNotEmpty(result.address, 'Order information is returned')
})
})
*/
/*
// Example return value from ob.getOrder():
{
contract:{
vendorListings:[
[
Object
],
[
length
]:1
],
buyerOrder:{
refundAddress:'qqstld5hu6kf84arkss7jtdc9kvxm50svqksgk04qy',
refundFee:0,
shipping:[
Object
],
buyerID:[
Object
],
timestamp:'2018-07-28T23:32:04.298412128Z',
items:[
Array
],
payment:[
Object
],
ratingKeys:[
Array
],
alternateContactInfo:'',
version:2
},
vendorOrderConfirmation:{
orderID:'QmcAjF4cjSrG2ChiQY4ByLNciYqCfhsfVYuNPJxQ2C8m1u',
timestamp:'2018-07-28T23:32:04.616538847Z',
paymentAddress:'qz7t6yshmp3vhks3gfnnz0emv2g2nraaeskcag5ell',
requestedAmount:1470,
ratingSignatures:[
Array
]
},
vendorOrderFulfillment:[
[
Object
],
[
length
]:1
],
signatures:[
[
Object
],
[
Object
],
[
Object
],
[
Object
],
[
length
]:4
]
},
state:'FULFILLED',
read:false,
funded:true,
unreadChatMessages:0,
paymentAddressTransactions:[
{
txid:'961988b62b52672385805c9d3d05116cbe0dd766cfa337acd043312c3153c07e',
value:1470,
confirmations:0,
height:0,
timestamp:'2018-07-28T23:32:08.952423586Z'
},
[
length
]:1
]
}
*/
/*
These are support functions.
*/
// Rounds the floating point val to a precise number of satoshis
function roundSatoshis (val) {
const satoshis = Number(val) * 100000000
const roundedSatoshis = Math.round(satoshis)
return roundedSatoshis
}