UNPKG

8.27 kBtext/coffeescriptView Raw
1assert = require 'assert'
2async = require 'async'
3_ = require 'underscore'
4google_apis = require "#{__dirname}/../lib/"
5nock = require 'nock'
6util = require 'util'
7
8before ->
9 nock.disableNetConnect()
10
11describe 'Batch Requests', ->
12 before ->
13 @options =
14 token:
15 access: 'fake_access_token'
16
17 @up = new google_apis.UserProvisioning @options
18
19 # filter out random boundary strings and set them to a known value so messages can be diff'd
20 @boundary_filter = (res) ->
21 res.replace(/\n\-\-[0-9a-zA-Z=]+\n/g, '\n--===============1234567890123456789==\n')
22 .replace(/\n\-\-[0-9a-zA-Z=]+\-\-/g, '\n--===============1234567890123456789==--')
23
24 @user_defaults =
25 id: "1234"
26 primaryEmail: "ben.franklin@example.com"
27 name: { givenName: "Ben", familyName: "Franklin", fullName: "Ben Franklin" }
28 lastLoginTime: "2013-09-27T18:56:04.000Z"
29 creationTime: "2013-09-24T22:06:21.000Z"
30 emails: [{ address: "ben.franklin@example.com", primary: true }]
31 kind: "admin#directory#user"
32 isAdmin: false
33 isDelegatedAdmin: false
34 agreedToTerms: true
35 suspended: false
36 changePasswordAtNextLogin: false
37 ipWhitelisted: false
38 customerId: "fake_customer_id"
39 orgUnitPath: "/"
40 isMailboxSetup: true
41 includeInGlobalAddressList: true
42
43 @request_helper = (ids) ->
44 request = "\n--===============1234567890123456789=="
45 for id in ids
46 request += "\nContent-Type: application/http\n\n" +
47 "GET /admin/directory/v1/users/#{id} HTTP/1.1\n" +
48 "--===============1234567890123456789=="
49 request + "--"
50
51 it 'requires options upon construction', ->
52 assert.throws ->
53 new google_apis.Batch()
54 , Error
55
56 it 'can perform multiple get requests', (done) ->
57 #nock.recorder.rec()
58 test_data = [
59 user_id: '1234'
60 expected_data: _.defaults { isDelegatedAdmin: true }, @user_defaults
61 ,
62 user_id: '5678'
63 expected_data: _.defaults
64 id: "5678"
65 primaryEmail: "admin@example.com"
66 name: { givenName: "The", familyName: "Admin", fullName: "The Admin" }
67 isAdmin: true
68 lastLoginTime: "2013-09-27T18:57:15.000Z"
69 creationTime: "2013-09-24T21:41:29.000Z"
70 emails: [{ address: "admin@example.com", primary: true }]
71 orgUnitPath: "/Google Administrators"
72 , @user_defaults
73 ,
74 user_id: '9012'
75 expected_data: _.defaults
76 id: "9012"
77 primaryEmail: "matchmealso@ga4edu.org"
78 name: { givenName: "User2", familyName: "Match", fullName: "User2 Match" }
79 lastLoginTime: "1970-01-01T00:00:00.000Z"
80 creationTime: "2013-08-02T23:31:07.000Z"
81 emails: [{ address: "matchmealso@ga4edu.org", primary: true }]
82 orgUnitPath: "/NotAdmins"
83 , @user_defaults
84 ]
85
86 expected_request = @request_helper ['1234', '5678', '9012']
87
88 expected_reply = "--batch_7av0UPcSyII=_ABKN5zORmiQ=\r\n"
89 _.each test_data, (test) =>
90 expected_reply += "Content-Type: application/http\r\n\r\nHTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
91 expected_reply += JSON.stringify test.expected_data
92 expected_reply += "\r\n--batch_7av0UPcSyII=_ABKN5zORmiQ=\r\n"
93 expected_reply += "--"
94
95 nock('https://www.googleapis.com:443')
96 .get('/oauth2/v1/tokeninfo?access_token=fake_access_token')
97 .reply(200, "OK")
98 .filteringRequestBody(@boundary_filter)
99 .post('/batch', expected_request)
100 .reply 200, expected_reply,
101 { 'content-type': 'multipart/mixed; boundary=batch_7av0UPcSyII=_ABKN5zORmiQ=' }
102
103 queries = _(test_data).map (test) => @up.get test.user_id
104
105 batch = new google_apis.Batch @options
106 batch.go queries, (err, results) ->
107 assert.ifError err
108 _.each (_.zip results, test_data), ([result, test]) ->
109 assert.equal result.statusCode, 200
110 assert.deepEqual test.expected_data, result.body
111 done()
112
113 it 'can send requests with content', (done) ->
114 get_resp_body = _.defaults { isDelegatedAdmin: true }, @user_defaults
115
116 patch_req_body = {name: {familyName: 'Jefferson', givenName: 'Thomas'}}
117 patch_resp_body = _.defaults
118 kind: "admin#directory#user"
119 id: "55555"
120 primaryEmail: "thomas.jefferson@example.com"
121 name: { givenName: "Thomas", familyName: "Jefferson" }
122 lastLoginTime: "1970-01-01T00:00:00.000Z"
123 creationTime: "2013-06-25T19:41:26.000Z"
124 emails: [{ address: "thomas.jefferson@example.com", primary: true }]
125 , @user_defaults
126
127 expected_request =
128 "\n--===============1234567890123456789==\n" +
129 "Content-Type: application/http\n\n" +
130 "GET /admin/directory/v1/users/1234 HTTP/1.1\n" +
131 "--===============1234567890123456789==\n" +
132 "Content-Type: application/http\n\n" +
133 "PATCH /admin/directory/v1/users/55555 HTTP/1.1\n" +
134 "Content-Type: application/json\n" +
135 "content-length: #{(JSON.stringify patch_req_body).length}\n\n" +
136 "#{JSON.stringify patch_req_body}\n" +
137 "--===============1234567890123456789==--"
138
139 expected_reply = "--batch_7av0UPcSyII=_ABKN5zORmiQ=\r\n"
140 expected_reply += "Content-Type: application/http\r\n\r\nHTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
141 expected_reply += JSON.stringify get_resp_body
142 expected_reply += "\r\n--batch_7av0UPcSyII=_ABKN5zORmiQ=\r\n"
143 expected_reply += "Content-Type: application/http\r\n\r\nHTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
144 expected_reply += JSON.stringify patch_resp_body
145 expected_reply += "\r\n--batch_7av0UPcSyII=_ABKN5zORmiQ=--"
146
147 nock('https://www.googleapis.com:443')
148 .get('/oauth2/v1/tokeninfo?access_token=fake_access_token')
149 .reply(200, "OK")
150 .filteringRequestBody(@boundary_filter)
151 .post('/batch', expected_request)
152 .reply 200, expected_reply,
153 { 'content-type': 'multipart/mixed; boundary=batch_7av0UPcSyII=_ABKN5zORmiQ=' }
154
155 queries = [(@up.get '1234'), (@up.patch '55555', patch_req_body)]
156
157 batch = new google_apis.Batch @options
158 batch.go queries, (err, results) ->
159 assert.ifError err
160 _.each (_.zip results, [get_resp_body, patch_resp_body]), ([result, test]) ->
161 assert.equal result.statusCode, 200
162 assert.deepEqual test, result.body
163 done()
164
165 it 'will throw a batch error correctly', (done) ->
166 nock('https://www.googleapis.com:443')
167 .get('/oauth2/v1/tokeninfo?access_token=fake_access_token')
168 .reply(200, "OK")
169 .post('/batch')
170 .reply 404, "We accidently rimraf"
171
172 batch = new google_apis.Batch @options
173
174 batch.go [(@up.get 'bad_id'), (@up.get 'bad_id_2')], (err, results) ->
175 assert err?
176 done()
177
178 it 'will retry slowly if rate-limited', (done) ->
179 #nock.recorder.rec()
180 test_data = [
181 user_id: '1234'
182 expected_data: _.defaults { isDelegatedAdmin: true }, @user_defaults
183 ]
184
185 expected_request = @request_helper ['1234']
186
187 expected_reply = "--batch_7av0UPcSyII=_ABKN5zORmiQ=\r\n"
188 _.each test_data, (test) =>
189 expected_reply += "Content-Type: application/http\r\n\r\nHTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
190 expected_reply += JSON.stringify test.expected_data
191 expected_reply += "\r\n--batch_7av0UPcSyII=_ABKN5zORmiQ=\r\n"
192 expected_reply += "--"
193
194 nock('https://www.googleapis.com:443')
195 .get('/oauth2/v1/tokeninfo?access_token=fake_access_token')
196 .reply(200, "OK")
197 .filteringRequestBody(@boundary_filter)
198 .post('/batch', expected_request)
199 .reply(503, 'Enhance your calm')
200 .post('/batch', expected_request)
201 .reply(503, 'Enhance your calm')
202 .post('/batch', expected_request)
203 .reply 200, expected_reply,
204 { 'content-type': 'multipart/mixed; boundary=batch_7av0UPcSyII=_ABKN5zORmiQ=' }
205
206 queries = _(test_data).map (test) => @up.get test.user_id
207
208 batch = new google_apis.Batch @options
209 batch.go queries, (err, results) ->
210 assert.ifError err
211 _.each (_.zip results, test_data), ([result, test]) ->
212 assert.equal result.statusCode, 200
213 assert.deepEqual test.expected_data, result.body
214 done()