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

Commit

Permalink
Merge pull request #395 from cloudant/fix-tests-for-fdb
Browse files Browse the repository at this point in the history
Fix tests for FDB.
  • Loading branch information
smithsz committed Sep 24, 2019
2 parents 5d3d66b + ab21b46 commit a38de10
Show file tree
Hide file tree
Showing 9 changed files with 712 additions and 1,713 deletions.
12 changes: 6 additions & 6 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017, 2018 IBM Corp. All rights reserved.
// Copyright © 2017, 2019 IBM Corp. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -227,23 +227,23 @@ describe('CloudantClient', function() {

describe('Error Handling', function() {
it('Propagate request error: Invalid protocol.', function(done) {
var cloudantClient = new Client();
var cloudantClient = new Client({ plugins: [] });
cloudantClient.request('abc://localhost:5984', function(err) {
assert.equal(err.message, 'Invalid protocol: abc:');
done();
});
});

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

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

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

var options = {
url: SERVER + DBNAME,
Expand Down
17 changes: 8 additions & 9 deletions test/legacy/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ describe('#db Initialization', function() {

var mocks = nock(SERVER)
.post('/_session')
.times(3)
.replyWithError({code: 'ECONNRESET', message: 'socket hang up'})
.get('/')
.replyWithError({code: 'ECONNRESET', message: 'socket hang up'});

Cloudant({account: ME, username: ME, password: PASSWORD}, function(er, cloudant, body) {
Cloudant({username: ME, password: PASSWORD, url: SERVER}, function(er, cloudant, body) {
er.should.be.an.Object;
mocks.done();
done();
Expand Down Expand Up @@ -405,9 +404,9 @@ describe('#db Changes query', function() {
body.should.have.a.property('results').which.is.instanceOf(Object);
body.results.should.have.a.length(2);
body.results[0].should.be.an.Object.and.have.a.property('id').and.match(/^doc[12]$/);
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^1-/);
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^(1-)?[^-]+/);
body.results[1].should.be.an.Object.and.have.a.property('id').and.match(/^doc[12]$/);
body.results[1].should.be.an.Object.and.have.a.property('seq').and.match(/^2-/);
body.results[1].should.be.an.Object.and.have.a.property('seq').and.match(/^(2-)?[^-]+/);

firstChange = body.results[0];

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

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

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

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

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

mocks.done();
Expand Down
208 changes: 54 additions & 154 deletions test/legacy/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2015, 2018 IBM Corp. All rights reserved.
// Copyright © 2015, 2019 IBM Corp. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -170,142 +170,6 @@ describe('promises #db', function() {
});
});

describe('cookieauth plugin #db', function() {
before(onBefore);
after(onAfter);

it('should return a promise', function(done) {
var mocks = nock(SERVER)
.post('/_session').reply(200, { ok: true })
.get('/' + dbName).reply(200, { ok: true });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD});
var db = cloudant.db.use(dbName);
var p = db.info().then(function(data) {
data.should.be.an.Object;
// check that we use all the nocked API calls
mocks.done();
done();
});
assert.equal(p instanceof Promise, true);
});

it('should authenticate before attempting API call', function(done) {
var mocks = nock(SERVER)
.post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } })
.get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD});
var db = cloudant.db.use(dbName);
var p = db.get('mydoc', function(err, data) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.have.property._id;
data.should.have.property._rev;
data.should.have.property.ok;

// check that we use all the nocked API calls
mocks.done();
done();
});
});

it('should fail with incorrect authentication', function(done) {
var mocks = nock(SERVER)
.post('/_session', {name: ME, password: 'wrongpassword'})
.reply(401, {error: 'unauthorized', reason: 'Name or password is incorrect.'})
.get('/' + dbName + '/mydoc')
.reply(401, {error: 'unauthorized', reason: 'Name or password is incorrect.'});
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: 'wrongpassword'});
var db = cloudant.db.use(dbName);
var p = db.get('mydoc', function(err, data) {
assert.equal(data, null);
err.should.be.an.Object;
err.should.have.property.error;
err.should.have.property.reason;

// check that we use all the nocked API calls
mocks.done();
done();
});
});

it('should only authenticate once', function(done) {
var mocks = nock(SERVER)
.post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } }, { 'Set-Cookie': 'AuthSession=xyz; Version=1; Path=/; HttpOnly' })
.get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true })
.get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD});
var db = cloudant.db.use(dbName);
var p = db.get('mydoc', function(err, data) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.have.property._id;
data.should.have.property._rev;
data.should.have.property.ok;

db.get('mydoc', function(err, data) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.have.property._id;
data.should.have.property._rev;
data.should.have.property.ok;

// check that we use all the nocked API calls
mocks.done();
done();
});
});
});

it('should not authenticate without credentials', function(done) {
var mocks = nock(SERVER)
.get('/' + dbName + '/mydoc').reply(401, { error: 'unauthorized', reason: '_reader access is required for this request' });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER});
var db = cloudant.db.use(dbName);
var p = db.get('mydoc', function(err, data) {
assert.equal(data, null);
err.should.be.an.Object;
err.should.have.property.error;
err.should.have.property.reason;

// check that we use all the nocked API calls
mocks.done();
done();
});
});

it('should work with asynchronous instantiation', function(done) {
if (process.env.NOCK_OFF) {
this.skip();
}
var mocks = nock(SERVER)
.post('/_session', {name: ME, password: PASSWORD})
.reply(401, {error: 'unauthorized', reason: 'Name or password is incorrect.'})
.get('/')
.reply(200, {couchdb: 'Welcome', version: '1.0.2', cloudant_build: '2488'});
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD}, function(err, cloudant, data) {
cloudant.should.be.an.Object;
data.should.be.an.Object.have.a.property('couchdb');
mocks.done();
done();
});
});

it('should work with asynchronous instantiation with no credentials', function(done) {
if (process.env.NOCK_OFF) {
this.skip();
}
var mocks = nock(SERVER)
.get('/')
.reply(200, { couchdb: 'Welcome', version: '1.0.2', cloudant_build: '2488' });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER}, function(err, cloudant, data) {
cloudant.should.be.an.Object;
data.should.be.an.Object.have.a.property('couchdb');
mocks.done();
done();
});
});
});

describe('custom plugin #db', function() {
before(onBefore);
after(onAfter);
Expand Down Expand Up @@ -357,35 +221,71 @@ describe('custom plugin #db', function() {
done();
});
});
});

it('should allow custom plugins using asynchronous instantiation with invalid credentials', function(done) {
const badPass = 'bAD%%Pa$$w0rd123';
describe('cookieauth plugin #db', function() {
before(onBefore);
after(onAfter);

it('should return a promise', function(done) {
var mocks = nock(SERVER)
.post('/_session', { name: ME, password: badPass })
.reply(401, { error: 'unauthorized', reason: 'Name or password is incorrect.' })
.get('/')
.reply(401, { error: 'unauthorized', reason: 'Name or password is incorrect.' });

Cloudant({ plugins: defaultPlugin, url: SERVER, username: ME, password: badPass }, function(err, nano) {
assert.equal(err.error, 'unauthorized');
assert.equal(nano, null);
.post('/_session').reply(200, { ok: true })
.get('/' + dbName).reply(200, { ok: true });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD});
var db = cloudant.db.use(dbName);
var p = db.info().then(function(data) {
data.should.be.an.Object;
// check that we use all the nocked API calls
mocks.done();
done();
});
assert.equal(p instanceof Promise, true);
});

it('should allow custom plugins using asynchronous instantiation with no credentials', function(done) {
it('should authenticate before attempting API call', function(done) {
var mocks = nock(SERVER)
.get('/')
.reply(200, { couchdb: 'Welcome' });

Cloudant({ plugins: defaultPlugin, url: SERVER }, function(err, nano, pong) {
.post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } })
.get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD});
var db = cloudant.db.use(dbName);
var p = db.get('mydoc', function(err, data) {
assert.equal(err, null);
assert.notEqual(nano, null);
assert.equal(pong.couchdb, 'Welcome');
data.should.be.an.Object;
data.should.have.property._id;
data.should.have.property._rev;
data.should.have.property.ok;

// check that we use all the nocked API calls
mocks.done();
done();
});
});

it('should only authenticate once', function(done) {
var mocks = nock(SERVER)
.post('/_session', {name: ME, password: PASSWORD}).reply(200, { ok: true, info: {}, userCtx: { name: ME, roles: ['_admin'] } }, { 'Set-Cookie': 'AuthSession=xyz; Version=1; Path=/; HttpOnly' })
.get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true })
.get('/' + dbName + '/mydoc').reply(200, { _id: 'mydoc', _rev: '1-123', ok: true });
var cloudant = Cloudant({plugins: 'cookieauth', url: SERVER, username: ME, password: PASSWORD});
var db = cloudant.db.use(dbName);
var p = db.get('mydoc', function(err, data) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.have.property._id;
data.should.have.property._rev;
data.should.have.property.ok;

db.get('mydoc', function(err, data) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.have.property._id;
data.should.have.property._rev;
data.should.have.property.ok;

// check that we use all the nocked API calls
mocks.done();
done();
});
});
});
});
Loading

0 comments on commit a38de10

Please sign in to comment.