UNPKG

9.72 kBtext/coffeescriptView Raw
1expect = require('chai').expect
2fs = require('fs')
3
4OP_GET_GIST = '99d64f8e-eace-417d-8cea-511621aaf57c'
5GIST_ID = '6a06816491046e57e658'
6GIST_RESOURCE = '5cb3434f-0351-461b-9fe0-89d11b2c046c'
7OP_GTABLE_GETTABLE = '7810abb5-1617-452a-a83d-9f0349da460c'
8OP_GTABLE_SQLGET = 'aa746210-294a-4e57-bff0-4ba05d948645'
9TABLE_ID = '1EKWSEI4JtA8xYWou_u_BKmEHjrmXHTguNbct_TI'
10GTABLE_API_KEY = process.env.GTABLE_API_KEY
11OP_SOCRATA_GETRESOURCE='c1b1bf41-a4e6-4562-a880-72d9165a604e'
12SOCRATA_APP_TOKEN = process.env.SOCRATA_APP_TOKEN
13SC_RESOURCE_ID = 'e3fb-j9c6'
14TEST_API = 'fc97d5e9-5671-42c4-97ba-5b11ba8f4c07'
15
16describe 'APIBase library', ->
17 it 'should return an api client', ->
18 APIBase = require '../src'
19 client = new APIBase({
20 baseUrl: 'http://johnsd.cse.unsw.edu.au:3000'
21 })
22 expect(client).to.exist
23
24describe 'APIBase client', ->
25 APIBase = require '../src'
26 client = new APIBase({
27 baseUrl: 'http://johnsd.cse.unsw.edu.au:3000'
28 })
29
30 it 'should get an entity', (done)->
31 client.getEntity OP_GET_GIST, (err, entity)->
32 expect(entity.name).to.equal("GetGist")
33 expect(entity.id).to.equal(OP_GET_GIST)
34 done()
35
36 it 'should return a Gist', (done)->
37 @timeout(20000)
38 client.invokeOperation OP_GET_GIST, {gistId: GIST_ID}, null, null, (err, resp)->
39 resp = JSON.parse resp.body
40 expect(resp).to.exist
41 expect(resp).to.have.property 'url', "https://api.github.com/gists/#{GIST_ID}"
42 expect(resp).to.have.property 'files'
43 expect(resp.files).to.have.property 'route.c'
44 done(err)
45
46 it 'should return a Gist (resource mapping)', (done)->
47 @timeout(20000)
48 client.invokeOperation OP_GET_GIST, {}, {gistIda: GIST_ID}, {"gistId": "$.gistIda"}, (err, resp)->
49 resp = JSON.parse resp.body
50 expect(resp).to.exist
51 expect(resp).to.have.property 'url', "https://api.github.com/gists/#{GIST_ID}"
52 expect(resp).to.have.property 'files'
53 expect(resp.files).to.have.property 'route.c'
54 done(err)
55
56 it 'should return a Gist (resource)', (done)->
57 @timeout(30000)
58 client.invokeOperation OP_GET_GIST, {gistId: "resource://#{GIST_RESOURCE}"}, null, null, (err, resp)->
59 resp = JSON.parse resp.body
60 expect(resp).to.exist
61 expect(resp).to.have.property 'url', "https://api.github.com/gists/6af050afb253caa2cd67"
62 expect(resp).to.have.property 'files'
63 expect(resp.files).to.have.property 'HelloWorld.js'
64 done(err)
65
66describe 'Google Table', ->
67 nock = require 'nock'
68 ft = nock('https://www.googleapis.com')
69 ft.get '/fusiontables/v2/query'
70 .query true
71 .reply 200, {
72 "kind": "fusiontables#sqlresponse"
73 "columns": [
74 "Browser"
75 "Minimum Version"
76 "Maximum Version"
77 "Export to Excel/Word/Mail Merge"
78 "QuickBooks Extension"
79 ]
80 }
81 ft.get("/fusiontables/v2/tables/#{TABLE_ID}")
82 .query(true)
83 .reply 200, {
84 "kind": "fusiontables#table"
85 "name": "Configuring Your Browser Settings"
86 }
87 APIBase = require '../src'
88 client = new APIBase({
89 baseUrl: 'http://johnsd.cse.unsw.edu.au:3000'
90 })
91
92 it 'should return a Google Table Metadata', (done)->
93 @timeout(20000)
94 client.invokeOperation OP_GTABLE_GETTABLE, {tableId: TABLE_ID, key: GTABLE_API_KEY}, null, null, (err, resp)->
95 resp = JSON.parse resp.body
96 expect(resp).to.exist
97 expect(resp).to.have.property 'kind', 'fusiontables#table'
98 expect(resp).to.have.property 'name', 'Configuring Your Browser Settings'
99 done(err)
100
101 it 'should return a Google Table', (done)->
102 @timeout(20000)
103 client.invokeOperation OP_GTABLE_SQLGET, {
104 key: GTABLE_API_KEY,
105 sql: "SELECT * FROM #{TABLE_ID}"
106 }, null, null, (err, resp)->
107 resp = JSON.parse resp.body
108 expect(resp).to.exist
109 expect(resp).to.have.property 'kind', 'fusiontables#sqlresponse'
110 expect(resp).to.have.property('columns')
111 .that.is.an('array')
112 .with.length(5)
113 .and.contains('Browser')
114 done(err)
115
116describe 'Socrata API', ->
117 client = null
118 beforeEach ->
119 APIBase = require '../src'
120 client = new APIBase({
121 baseUrl: 'http://johnsd.cse.unsw.edu.au:3000'
122 })
123
124 it 'should return a Socrata resource', (done)->
125 @timeout(20000)
126 client.invokeOperation OP_SOCRATA_GETRESOURCE, {id: SC_RESOURCE_ID, 'X-App-Token': SOCRATA_APP_TOKEN}, null, null, (err, resp)->
127 resp = JSON.parse resp.body
128 expect(resp).to.exist
129 expect(resp).to.have.length(5)
130 expect(resp[0]).to.have.property 'pcworld', '1.36'
131 done(err)
132
133 it 'should return a Socrata resource', (done)->
134 @timeout(20000)
135 client.invokeOperation OP_SOCRATA_GETRESOURCE, {
136 id: SC_RESOURCE_ID,
137 'X-App-Token': SOCRATA_APP_TOKEN,
138 '$select': 'yahoo,ebay'
139 '$where': 'yahoo>1.3'
140 }, null, null, (err, resp)->
141 resp = JSON.parse resp.body
142 expect(resp).to.exist
143 expect(resp).to.have.length(3)
144 expect(resp[0]).to.have.property 'ebay', '1.13'
145 done(err)
146
147 it 'should return a failure message', (done)->
148 @timeout(20000)
149 client.invokeOperation OP_SOCRATA_GETRESOURCE, {id: SC_RESOURCE_ID, 'X-App-Token': SOCRATA_APP_TOKEN + 'x'}, null, null, (err, resp)->
150 resp = JSON.parse resp.body
151 expect(resp).to.exist
152 expect(resp).to.have.property 'error'
153 done(err)
154
155describe 'caching', ->
156 APIBase = require '../src'
157 client = new APIBase({
158 baseUrl: 'http://johnsd.cse.unsw.edu.au:3000'
159 })
160
161 it 'should get an entity', (done)->
162 client.getEntity OP_GET_GIST, (err, entity)->
163 expect(entity.name).to.equal("GetGist")
164 expect(entity.id).to.equal(OP_GET_GIST)
165 done()
166
167 it 'should get an entity immediately', (done)->
168 @timeout(20)
169 client.getEntity OP_GET_GIST, (err, entity)->
170 expect(entity.name).to.equal("GetGist")
171 expect(entity.id).to.equal(OP_GET_GIST)
172 done()
173
174 it 'should get api of an operation', (done)->
175 @timeout(6000)
176 client._getOperationAPI OP_GET_GIST, (err, api)->
177 expect(api.name).to.equal("Github")
178 done()
179
180 it 'should get api of an operation immediately', (done)->
181 @timeout(20)
182 client._getOperationAPI OP_GET_GIST, (err, api)->
183 expect(api.name).to.equal("Github")
184 done()
185
186describe 'Visual Recognition', ->
187 client = null
188 beforeEach ->
189 APIBase = require '../src'
190 client = new APIBase({
191 baseUrl: 'http://johnsd.cse.unsw.edu.au:3001'
192 })
193
194 it 'should return a recognition results', (done)->
195 @timeout(20000)
196 client.invokeOperation '8a2e6892-8b38-4ce1-9a06-cf639f906045', {
197 image_files: {
198 value: fs.readFileSync(__dirname + '/car-8.jpg').toString('base64')
199 options: {
200 filename: 'car-8.jpg'
201 contentType: 'image/jpg'
202 }
203 }
204 version: '2015-12-02'
205 'content-type': 'multipart/form-data'
206 }, null, null, (err, resp)->
207 resp = JSON.parse resp.body
208 console.log(resp)
209 expect(resp).to.exist
210 expect(resp).to.have.property 'images'
211 expect(resp.images).to.have.length.above(0)
212 expect(resp.images[0]).to.have.property 'scores'
213
214 done(err)
215
216 it 'should use the default version value', (done)->
217 @timeout(20000)
218 client.invokeOperation '8a2e6892-8b38-4ce1-9a06-cf639f906045', {
219 image_files: {
220 value: fs.readFileSync(__dirname + '/car-8.jpg').toString('base64')
221 options: {
222 filename: 'car-8.jpg'
223 contentType: 'image/jpg'
224 }
225 }
226 'content-type': 'multipart/form-data'
227 }, null, null, (err, resp)->
228 resp = JSON.parse resp.body
229 console.log(resp)
230 expect(resp).to.exist
231 expect(resp).to.have.property 'images'
232 expect(resp.images).to.have.length.above(0)
233 expect(resp.images[0]).to.have.property 'scores'
234
235 done(err)
236
237describe 'Recipe', ->
238 client = null
239 beforeEach ->
240 APIBase = require '../src'
241 client = new APIBase({
242 baseUrl: 'http://johnsd.cse.unsw.edu.au:3001'
243 })
244
245 it 'should return first gist of the user', (done)->
246 @timeout(20000)
247 client.runRecipe 'ac4f72b9-259e-4fa7-a5a9-8cd1f191b0bb', {
248 username: "freehaha"
249 }, (err, resp)->
250 if err
251 done err
252 return
253 resp = resp.body
254 results = resp['_results']
255 expect(results['1']).to.exist
256 expect(results['1']).to.have.property 'files'
257
258 done(err)
259
260 it 'should return first gist of the user (via invokeOperation)', (done)->
261 @timeout(20000)
262 client.invokeOperation 'ac4f72b9-259e-4fa7-a5a9-8cd1f191b0bb', {
263 username: "freehaha"
264 }, null, null, (err, resp)->
265 if err
266 done err
267 return
268 resp = resp.body
269 results = resp['_results']
270 expect(results['1']).to.exist
271 expect(results['1']).to.have.property 'files'
272
273 done(err)
274
275describe.only 'Invoking APIs', ->
276 APIBase = require '../src'
277 client = new APIBase({
278 baseUrl: 'http://johnsd.cse.unsw.edu.au:3000'
279 })
280
281 it 'should get the an object', (done)->
282 @timeout(20000)
283 client.invokeOperation TEST_API, {
284 p1: {"a": 1235}
285 }, null, null, (err, resp)->
286 resp = resp.body
287 console.log(resp)
288 expect(resp).to.exist
289 expect(resp).to.have.property 'p1'
290 expect(resp.p1).to.have.property 'a'
291 expect(resp.p1.a).to.equal 1235
292
293 done(err)
294 it 'should send the parameter as object', (done)->
295 @timeout(20000)
296 client.invokeOperation TEST_API, {
297 p1: '{"a": 3456}'
298 }, null, null, (err, resp)->
299 resp = resp.body
300 console.log(resp)
301 expect(resp).to.exist
302 expect(resp).to.have.property 'p1'
303 expect(resp.p1).to.have.property 'a'
304 expect(resp.p1.a).to.equal 3456
305
306 done(err)