|
1 | | -// Copyright © 2015, 2018 IBM Corp. All rights reserved. |
| 1 | +// Copyright © 2015, 2019 IBM Corp. All rights reserved. |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
@@ -170,142 +170,6 @@ describe('promises #db', function() { |
170 | 170 | }); |
171 | 171 | }); |
172 | 172 |
|
173 | | -describe('cookieauth plugin #db', function() { |
174 | | - before(onBefore); |
175 | | - after(onAfter); |
176 | | - |
177 | | - it('should return a promise', function(done) { |
178 | | - var mocks = nock(SERVER) |
179 | | - .post('/_session').reply(200, { ok: true }) |
180 | | - .get('/' + dbName).reply(200, { ok: true }); |
181 | | - var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}); |
182 | | - var db = cloudant.db.use(dbName); |
183 | | - var p = db.info().then(function(data) { |
184 | | - data.should.be.an.Object; |
185 | | - // check that we use all the nocked API calls |
186 | | - mocks.done(); |
187 | | - done(); |
188 | | - }); |
189 | | - assert.equal(p instanceof Promise, true); |
190 | | - }); |
191 | | - |
192 | | - it('should authenticate before attempting API call', function(done) { |
193 | | - var mocks = nock(SERVER) |
194 | | - .post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } }) |
195 | | - .get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true }); |
196 | | - var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}); |
197 | | - var db = cloudant.db.use(dbName); |
198 | | - var p = db.get('mydoc', function(err, data) { |
199 | | - assert.equal(err, null); |
200 | | - data.should.be.an.Object; |
201 | | - data.should.have.property._id; |
202 | | - data.should.have.property._rev; |
203 | | - data.should.have.property.ok; |
204 | | - |
205 | | - // check that we use all the nocked API calls |
206 | | - mocks.done(); |
207 | | - done(); |
208 | | - }); |
209 | | - }); |
210 | | - |
211 | | - it('should fail with incorrect authentication', function(done) { |
212 | | - var mocks = nock(SERVER) |
213 | | - .post('/_session', {name: ME, password: 'wrongpassword'}) |
214 | | - .reply(401, {error: 'unauthorized', reason: 'Name or password is incorrect.'}) |
215 | | - .get('/' + dbName + '/mydoc') |
216 | | - .reply(401, {error: 'unauthorized', reason: 'Name or password is incorrect.'}); |
217 | | - var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: 'wrongpassword'}); |
218 | | - var db = cloudant.db.use(dbName); |
219 | | - var p = db.get('mydoc', function(err, data) { |
220 | | - assert.equal(data, null); |
221 | | - err.should.be.an.Object; |
222 | | - err.should.have.property.error; |
223 | | - err.should.have.property.reason; |
224 | | - |
225 | | - // check that we use all the nocked API calls |
226 | | - mocks.done(); |
227 | | - done(); |
228 | | - }); |
229 | | - }); |
230 | | - |
231 | | - it('should only authenticate once', function(done) { |
232 | | - var mocks = nock(SERVER) |
233 | | - .post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } }, { 'Set-Cookie': 'AuthSession=xyz; Version=1; Path=/; HttpOnly' }) |
234 | | - .get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true }) |
235 | | - .get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true }); |
236 | | - var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}); |
237 | | - var db = cloudant.db.use(dbName); |
238 | | - var p = db.get('mydoc', function(err, data) { |
239 | | - assert.equal(err, null); |
240 | | - data.should.be.an.Object; |
241 | | - data.should.have.property._id; |
242 | | - data.should.have.property._rev; |
243 | | - data.should.have.property.ok; |
244 | | - |
245 | | - db.get('mydoc', function(err, data) { |
246 | | - assert.equal(err, null); |
247 | | - data.should.be.an.Object; |
248 | | - data.should.have.property._id; |
249 | | - data.should.have.property._rev; |
250 | | - data.should.have.property.ok; |
251 | | - |
252 | | - // check that we use all the nocked API calls |
253 | | - mocks.done(); |
254 | | - done(); |
255 | | - }); |
256 | | - }); |
257 | | - }); |
258 | | - |
259 | | - it('should not authenticate without credentials', function(done) { |
260 | | - var mocks = nock(SERVER) |
261 | | - .get('/' + dbName + '/mydoc').reply(401, { error: 'unauthorized', reason: '_reader access is required for this request' }); |
262 | | - var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER}); |
263 | | - var db = cloudant.db.use(dbName); |
264 | | - var p = db.get('mydoc', function(err, data) { |
265 | | - assert.equal(data, null); |
266 | | - err.should.be.an.Object; |
267 | | - err.should.have.property.error; |
268 | | - err.should.have.property.reason; |
269 | | - |
270 | | - // check that we use all the nocked API calls |
271 | | - mocks.done(); |
272 | | - done(); |
273 | | - }); |
274 | | - }); |
275 | | - |
276 | | - it('should work with asynchronous instantiation', function(done) { |
277 | | - if (process.env.NOCK_OFF) { |
278 | | - this.skip(); |
279 | | - } |
280 | | - var mocks = nock(SERVER) |
281 | | - .post('/_session', {name: ME, password: PASSWORD}) |
282 | | - .reply(401, {error: 'unauthorized', reason: 'Name or password is incorrect.'}) |
283 | | - .get('/') |
284 | | - .reply(200, {couchdb: 'Welcome', version: '1.0.2', cloudant_build: '2488'}); |
285 | | - var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}, function(err, cloudant, data) { |
286 | | - cloudant.should.be.an.Object; |
287 | | - data.should.be.an.Object.have.a.property('couchdb'); |
288 | | - mocks.done(); |
289 | | - done(); |
290 | | - }); |
291 | | - }); |
292 | | - |
293 | | - it('should work with asynchronous instantiation with no credentials', function(done) { |
294 | | - if (process.env.NOCK_OFF) { |
295 | | - this.skip(); |
296 | | - } |
297 | | - var mocks = nock(SERVER) |
298 | | - .get('/') |
299 | | - .reply(200, { couchdb: 'Welcome', version: '1.0.2', cloudant_build: '2488' }); |
300 | | - var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER}, function(err, cloudant, data) { |
301 | | - cloudant.should.be.an.Object; |
302 | | - data.should.be.an.Object.have.a.property('couchdb'); |
303 | | - mocks.done(); |
304 | | - done(); |
305 | | - }); |
306 | | - }); |
307 | | -}); |
308 | | - |
309 | 173 | describe('custom plugin #db', function() { |
310 | 174 | before(onBefore); |
311 | 175 | after(onAfter); |
@@ -357,35 +221,71 @@ describe('custom plugin #db', function() { |
357 | 221 | done(); |
358 | 222 | }); |
359 | 223 | }); |
| 224 | +}); |
360 | 225 |
|
361 | | - it('should allow custom plugins using asynchronous instantiation with invalid credentials', function(done) { |
362 | | - const badPass = 'bAD%%Pa$$w0rd123'; |
| 226 | +describe('cookieauth plugin #db', function() { |
| 227 | + before(onBefore); |
| 228 | + after(onAfter); |
363 | 229 |
|
| 230 | + it('should return a promise', function(done) { |
364 | 231 | var mocks = nock(SERVER) |
365 | | - .post('/_session', { name: ME, password: badPass }) |
366 | | - .reply(401, { error: 'unauthorized', reason: 'Name or password is incorrect.' }) |
367 | | - .get('/') |
368 | | - .reply(401, { error: 'unauthorized', reason: 'Name or password is incorrect.' }); |
369 | | - |
370 | | - Cloudant({ plugins: defaultPlugin, url: SERVER, username: ME, password: badPass }, function(err, nano) { |
371 | | - assert.equal(err.error, 'unauthorized'); |
372 | | - assert.equal(nano, null); |
| 232 | + .post('/_session').reply(200, { ok: true }) |
| 233 | + .get('/' + dbName).reply(200, { ok: true }); |
| 234 | + var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}); |
| 235 | + var db = cloudant.db.use(dbName); |
| 236 | + var p = db.info().then(function(data) { |
| 237 | + data.should.be.an.Object; |
| 238 | + // check that we use all the nocked API calls |
373 | 239 | mocks.done(); |
374 | 240 | done(); |
375 | 241 | }); |
| 242 | + assert.equal(p instanceof Promise, true); |
376 | 243 | }); |
377 | 244 |
|
378 | | - it('should allow custom plugins using asynchronous instantiation with no credentials', function(done) { |
| 245 | + it('should authenticate before attempting API call', function(done) { |
379 | 246 | var mocks = nock(SERVER) |
380 | | - .get('/') |
381 | | - .reply(200, { couchdb: 'Welcome' }); |
382 | | - |
383 | | - Cloudant({ plugins: defaultPlugin, url: SERVER }, function(err, nano, pong) { |
| 247 | + .post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } }) |
| 248 | + .get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true }); |
| 249 | + var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}); |
| 250 | + var db = cloudant.db.use(dbName); |
| 251 | + var p = db.get('mydoc', function(err, data) { |
384 | 252 | assert.equal(err, null); |
385 | | - assert.notEqual(nano, null); |
386 | | - assert.equal(pong.couchdb, 'Welcome'); |
| 253 | + data.should.be.an.Object; |
| 254 | + data.should.have.property._id; |
| 255 | + data.should.have.property._rev; |
| 256 | + data.should.have.property.ok; |
| 257 | + |
| 258 | + // check that we use all the nocked API calls |
387 | 259 | mocks.done(); |
388 | 260 | done(); |
389 | 261 | }); |
390 | 262 | }); |
| 263 | + |
| 264 | + it('should only authenticate once', function(done) { |
| 265 | + var mocks = nock(SERVER) |
| 266 | + .post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } }, { 'Set-Cookie': 'AuthSession=xyz; Version=1; Path=/; HttpOnly' }) |
| 267 | + .get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true }) |
| 268 | + .get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true }); |
| 269 | + var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}); |
| 270 | + var db = cloudant.db.use(dbName); |
| 271 | + var p = db.get('mydoc', function(err, data) { |
| 272 | + assert.equal(err, null); |
| 273 | + data.should.be.an.Object; |
| 274 | + data.should.have.property._id; |
| 275 | + data.should.have.property._rev; |
| 276 | + data.should.have.property.ok; |
| 277 | + |
| 278 | + db.get('mydoc', function(err, data) { |
| 279 | + assert.equal(err, null); |
| 280 | + data.should.be.an.Object; |
| 281 | + data.should.have.property._id; |
| 282 | + data.should.have.property._rev; |
| 283 | + data.should.have.property.ok; |
| 284 | + |
| 285 | + // check that we use all the nocked API calls |
| 286 | + mocks.done(); |
| 287 | + done(); |
| 288 | + }); |
| 289 | + }); |
| 290 | + }); |
391 | 291 | }); |
0 commit comments