-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.js
277 lines (236 loc) · 7.75 KB
/
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
import test from "ava"
const Piranhax = require("./index.js")
const _ = require("lodash")
const getCredentials = function() {
let env = process.env
return {
AccessKeyId: env.AccessKeyId,
SecretKey: env.SecretKey,
AssociateTag: env.AssociateTag
}
}
// Check whether Sign method is works or not
test.cb("Check Signed URL value", t => {
let client = new Piranhax("AKIAIOSFODNN7EXAMPLE",
"1234567890", "mytag-20")
let expected = "https://webservices.amazon.com/onca/xml?AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&AssociateTag=mytag-20&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews&Service=AWSECommerceService&Timestamp=2014-08-18T12%3A00%3A00Z&Version=2013-08-01&Signature=j7bZM0LXZ9eXeZruTqWm2DIvDYVUU3wxPPpp%2BiXxzQc%3D"
client.ItemLookup("0679722769", {
Timestamp: "2014-08-18T12:00:00Z",
ResponseGroup: ["Images", "ItemAttributes", "Offers", "Reviews"]
}).then(() => {
t.pass()
t.end()
}).catch(e => {
t.not(e, null)
let url = e.message.split("\n")
url = url[1].split(": ")
url = url[1]
t.not(url, null)
t.is(url, expected)
t.pass()
t.end()
})
})
//
// test ItemLookup
test.cb("ItemLookup", t => {
// here is the real test
let c = getCredentials()
let client = new Piranhax(c.AccessKeyId, c.SecretKey, c.AssociateTag)
var ASIN = "0679722769"
client.ItemLookup(ASIN, {
ResponseGroup: ["Images", "ItemAttributes", "Offers", "Reviews"]
}).then(result => {
// console.log(JSON.stringify(result.data()))
t.truthy(result.data())
t.is(result.get("Item.ASIN"), ASIN)
t.pass()
t.end()
}).catch(e => {
t.fail(e)
t.end()
})
})
// test ItemSearch
test.cb("ItemSearch", t => {
// here is the real test
let c = getCredentials()
let client = new Piranhax(c.AccessKeyId, c.SecretKey, c.AssociateTag)
client.ItemSearch("Books", {
Keywords: "Universe"
}).then(result => {
t.truthy(result.data())
t.is(result.get("Item").length > 0, true)
t.pass()
t.end()
}).catch(e => {
t.fail(e)
t.end()
})
})
// test BrowseNodeLookup
test.cb("BrowseNodeLookup", t => {
// here is the real test
let c = getCredentials()
let client = new Piranhax(c.AccessKeyId, c.SecretKey, c.AssociateTag)
// set BrowseNodeId to 1000 which is books
let BrowseNodeId = 1000
// call BrowseNodeLookup operation
client.BrowseNodeLookup(BrowseNodeId).then(result => {
t.truthy(result.data())
t.is(result.get("BrowseNode.BrowseNodeId"), BrowseNodeId.toString())
t.pass()
t.end()
}).catch(e => {
t.fail(e)
t.end()
})
})
// test SimilarityLookup
test.cb("SimilarityLookup", t => {
// here is the real test
let c = getCredentials()
let client = new Piranhax(c.AccessKeyId, c.SecretKey, c.AssociateTag)
client.ItemSearch("Books", {
Keywords: "Universe"
}).then(results => {
t.truthy(results)
// get ASIN item in items
let ASIN = results.get("Item[0].ASIN", 0)
// validate that itemId is not 0
t.not(ASIN, 0)
// get similarity
return client.SimilarityLookup(ASIN)
}).then(result => {
t.truthy(result)
// check existence of ASIN
let ASIN = result.get("Item[0].ASIN", 0)
t.truthy(ASIN)
// pass
t.pass()
t.end()
}).catch(e => {
t.fail(e)
t.end()
})
})
// test Cart Operation
test.cb("All cart operation", t => {
let c = getCredentials()
let client = new Piranhax(c.AccessKeyId, c.SecretKey, c.AssociateTag)
// create global var here
var CartId = ""
var HMAC = ""
var count = 0
var amount = 0
client.ItemSearch("Books", {
Keywords: "Calculus",
ResponseGroup: ["Large"]
})
.then(results => {
let Item = results.get("Item")
let OfferListingIds = _.map(Item, i =>
_.get(i, "Offers.Offer.OfferListing.OfferListingId"))
.filter(i => i !== undefined)
t.is(Item.length > 0, true)
// map OfferListingId
let CheckoutItems = _.map(OfferListingIds, id => {
let item = client.CreateCartItem("OfferListingId", id, 1)
return item
})
// create request
return client.CartCreate(CheckoutItems)
})
.then(result => {
// update global CartId to Cart.CartId found on result
CartId = result.get("CartId")
// check whether CartId is exist and truthy or not
t.truthy(CartId)
// update global HMAC to HMAC in Cart result
HMAC = result.get("HMAC")
// check whether HMAC is truthy, exist or not
t.truthy(HMAC)
// update global count to items in cart
count = result.get("CartItems.CartItem").length
// get CartItemId
let CartItemId = result.get("CartItems.CartItem[0].CartItemId")
t.truthy(CartItemId)
// store amount to global
amount = result.get("CartItems.CartItem[0]")
// CartGet Operation
return client.CartGet(CartId, CartItemId, HMAC)
})
.then(result => {
// get CartId from result
let cartId = result.get("CartId")
// check whether CartId is equal to global CartId or not
t.is(cartId, CartId)
// check HMAC from result
let hmac = result.get("HMAC")
// check whether HMAC is equal to its global or not
t.is(hmac, HMAC)
// get CartItems length from result
let c = result.get("CartItems.CartItem").length
// check whether count is still the same or not
t.is(c, count)
// find more items with different keywords
return client.ItemSearch("Books", {
Keywords: "Topology",
ResponseGroup: ["Large"]
})
})
.then(result => {
let Item = result.get("Item")
let OfferListingIds = _.map(Item,
i => _.get(i, "Offers.Offer.OfferListing.OfferListingId"))
.filter(i => i !== undefined)
// check is item.length more than 0
t.is(Item.length > 0, true)
// map OfferListingId
let AddItems = _.map(OfferListingIds, id => {
let item = client.CreateCartItem("OfferListingId", id, 1)
return item
})
// just select 2 of them
AddItems = AddItems.slice(0, 2)
// CartAdd operation
return client.CartAdd(AddItems, CartId, HMAC)
})
.then(result => {
// get CartItems size
let c = result.get("CartItems.CartItem").length
// check for the count, if 12 then pass
t.is(c, 11)
// set amount to global variable
amount = result.get("SubTotal.Amount")
let CartItem = client.CreateCartItem("CartItemId",
result.get("CartItems.CartItem[0].CartItemId"), 3)
//t.pass()1
//t.end()
// do CartModify operation
return client.CartModify([CartItem], CartId, HMAC)
})
.then(result => {
let CartItemId = result.get("Request.CartModifyRequest.Items.Item.CartItemId")
t.truthy(CartItemId)
t.is(result.get("HMAC"), HMAC)
t.is(result.get("CartId"), CartId)
return client.CartGet(CartId, CartItemId, HMAC)
})
.then(result => {
let $amount = result.get("SubTotal.Amount")
t.not($amount, amount)
return client.CartClear(CartId, HMAC)
})
.then(result => {
t.is(result.get("CartId"), CartId)
t.is(result.get("HMAC"), HMAC)
t.not(count, result.get("CartItems.Item"))
t.pass()
t.end()
})
.catch(e => {
t.fail(e)
t.end()
})
})