Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit a38de10

Browse files
authored
Merge pull request #395 from cloudant/fix-tests-for-fdb
Fix tests for FDB.
2 parents 5d3d66b + ab21b46 commit a38de10

File tree

9 files changed

+712
-1713
lines changed

9 files changed

+712
-1713
lines changed

test/client.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2017, 2018 IBM Corp. All rights reserved.
1+
// Copyright © 2017, 2019 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -227,23 +227,23 @@ describe('CloudantClient', function() {
227227

228228
describe('Error Handling', function() {
229229
it('Propagate request error: Invalid protocol.', function(done) {
230-
var cloudantClient = new Client();
230+
var cloudantClient = new Client({ plugins: [] });
231231
cloudantClient.request('abc://localhost:5984', function(err) {
232232
assert.equal(err.message, 'Invalid protocol: abc:');
233233
done();
234234
});
235235
});
236236

237237
it('Propagate request error: Base URL must be type string.', function(done) {
238-
var cloudantClient = new Client();
238+
var cloudantClient = new Client({ plugins: [] });
239239
cloudantClient.request({ baseUrl: 123, url: '/_all_dbs' }, function(err) {
240240
assert.equal(err.message, 'options.baseUrl must be a string');
241241
done();
242242
});
243243
});
244244

245245
it('Propagate request error: `unix://` URL scheme is no longer supported.', function(done) {
246-
var cloudantClient = new Client();
246+
var cloudantClient = new Client({ plugins: [] });
247247
cloudantClient.request('unix://abc', function(err) {
248248
assert.equal(
249249
err.message,
@@ -294,8 +294,8 @@ describe('CloudantClient', function() {
294294
.get(DBNAME)
295295
.reply(200, {doc_count: 1});
296296

297-
var cloudantClient = new Client();
298-
assert.equal(cloudantClient._plugins.length, 1);
297+
var cloudantClient = new Client({ plugins: [] });
298+
assert.equal(cloudantClient._plugins.length, 0);
299299

300300
var options = {
301301
url: SERVER + DBNAME,

test/legacy/api.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ describe('#db Initialization', function() {
7373

7474
var mocks = nock(SERVER)
7575
.post('/_session')
76+
.times(3)
7677
.replyWithError({code: 'ECONNRESET', message: 'socket hang up'})
77-
.get('/')
78-
.replyWithError({code: 'ECONNRESET', message: 'socket hang up'});
7978

80-
Cloudant({account: ME, username: ME, password: PASSWORD}, function(er, cloudant, body) {
79+
Cloudant({username: ME, password: PASSWORD, url: SERVER}, function(er, cloudant, body) {
8180
er.should.be.an.Object;
8281
mocks.done();
8382
done();
@@ -405,9 +404,9 @@ describe('#db Changes query', function() {
405404
body.should.have.a.property('results').which.is.instanceOf(Object);
406405
body.results.should.have.a.length(2);
407406
body.results[0].should.be.an.Object.and.have.a.property('id').and.match(/^doc[12]$/);
408-
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^1-/);
407+
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^(1-)?[^-]+/);
409408
body.results[1].should.be.an.Object.and.have.a.property('id').and.match(/^doc[12]$/);
410-
body.results[1].should.be.an.Object.and.have.a.property('seq').and.match(/^2-/);
409+
body.results[1].should.be.an.Object.and.have.a.property('seq').and.match(/^(2-)?[^-]+/);
411410

412411
firstChange = body.results[0];
413412

@@ -425,7 +424,7 @@ describe('#db Changes query', function() {
425424
should(er).equal(null);
426425
body.results.should.have.a.length(1);
427426
body.results[0].should.be.an.Object.and.have.a.property('id').and.match(/^doc[12]$/);
428-
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^2-/);
427+
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^(2-)?[^-]+/);
429428

430429
mocks.done();
431430
done();
@@ -527,7 +526,7 @@ describe('#db Changes follower', function() {
527526
change.should.have.a.property('id').and.match(/^doc[12]$/);
528527

529528
// First change should match "1-...", second should match "2-...".
530-
change.should.have.a.property('seq').and.match(new RegExp('^' + iterations + '-'));
529+
change.should.have.a.property('seq').and.match(new RegExp('^(' + iterations + '-)?[^-]+'));
531530

532531
if (iterations == 1) {
533532
firstChange = change;
@@ -552,7 +551,7 @@ describe('#db Changes follower', function() {
552551
should(er).equal(null);
553552
change.should.be.an.Object;
554553
change.should.have.a.property('id').and.match(/^doc[12]$/);
555-
change.should.have.a.property('seq').and.match(/^2-/);
554+
change.should.have.a.property('seq').and.match(/^(2-)?[^-]+/);
556555
feed.stop();
557556

558557
mocks.done();
@@ -579,7 +578,7 @@ describe('#db Changes follower', function() {
579578
function on_change(er, change) {
580579
should(er).equal(null);
581580
change.should.have.a.property('id').and.equal(docId);
582-
change.should.have.a.property('seq').and.match(/^3-/);
581+
change.should.have.a.property('seq').and.match(/^(3-)?[^-]+/);
583582
feed.stop();
584583

585584
mocks.done();

test/legacy/plugin.js

Lines changed: 54 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2015, 2018 IBM Corp. All rights reserved.
1+
// Copyright © 2015, 2019 IBM Corp. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -170,142 +170,6 @@ describe('promises #db', function() {
170170
});
171171
});
172172

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-
309173
describe('custom plugin #db', function() {
310174
before(onBefore);
311175
after(onAfter);
@@ -357,35 +221,71 @@ describe('custom plugin #db', function() {
357221
done();
358222
});
359223
});
224+
});
360225

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);
363229

230+
it('should return a promise', function(done) {
364231
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
373239
mocks.done();
374240
done();
375241
});
242+
assert.equal(p instanceof Promise, true);
376243
});
377244

378-
it('should allow custom plugins using asynchronous instantiation with no credentials', function(done) {
245+
it('should authenticate before attempting API call', function(done) {
379246
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) {
384252
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
387259
mocks.done();
388260
done();
389261
});
390262
});
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+
});
391291
});

0 commit comments

Comments
 (0)