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

Commit f6e7104

Browse files
committed
Use URL option in client construction for tests.
This change allows tests to be executed against a database with a non-"cloudant.com" server URL.
1 parent 0129298 commit f6e7104

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

test/legacy/api.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('#db Initialization', function() {
7777
.get('/')
7878
.replyWithError({code: 'ECONNRESET', message: 'socket hang up'});
7979

80-
Cloudant({account: ME, username: ME, password: PASSWORD}, function(er, cloudant, body) {
80+
Cloudant({username: ME, password: PASSWORD, url: SERVER}, function(er, cloudant, body) {
8181
er.should.be.an.Object;
8282
mocks.done();
8383
done();
@@ -405,9 +405,9 @@ describe('#db Changes query', function() {
405405
body.should.have.a.property('results').which.is.instanceOf(Object);
406406
body.results.should.have.a.length(2);
407407
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-/);
408+
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^(1-)?[^-]+/);
409409
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-/);
410+
body.results[1].should.be.an.Object.and.have.a.property('seq').and.match(/^(2-)?[^-]+/);
411411

412412
firstChange = body.results[0];
413413

@@ -425,7 +425,7 @@ describe('#db Changes query', function() {
425425
should(er).equal(null);
426426
body.results.should.have.a.length(1);
427427
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-/);
428+
body.results[0].should.be.an.Object.and.have.a.property('seq').and.match(/^(2-)?[^-]+/);
429429

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

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

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

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

585585
mocks.done();

test/plugins/iamauth.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ describe('#db IAMAuth Plugin', function() {
115115
});
116116

117117
it('performs request and returns 200 response', function(done) {
118+
if (!process.env.NOCK_OFF || !process.env.cloudant_iam_api_key) {
119+
this.skip();
120+
}
121+
118122
// NOTE: Use NOCK_OFF=true to test using a real CouchDB instance.
119123
var iamMocks = nock(TOKEN_SERVER)
120124
.post('/identity/token', {
@@ -185,6 +189,10 @@ describe('#db IAMAuth Plugin', function() {
185189
});
186190

187191
it('performs multiple requests that return 200 responses with only a single session request', function(done) {
192+
if (!process.env.NOCK_OFF || !process.env.cloudant_iam_api_key) {
193+
this.skip();
194+
}
195+
188196
// NOTE: Use NOCK_OFF=true to test using a real CouchDB instance.
189197
var iamMocks = nock(TOKEN_SERVER)
190198
.post('/identity/token', {
@@ -478,6 +486,10 @@ describe('#db IAMAuth Plugin', function() {
478486
});
479487

480488
it('supports using vcap with the promise plugin', function(done) {
489+
if (!process.env.NOCK_OFF || !process.env.cloudant_iam_api_key) {
490+
this.skip();
491+
}
492+
481493
// NOTE: Use NOCK_OFF=true to test using a real CouchDB instance.
482494
var iamMocks = nock(TOKEN_SERVER)
483495
.post('/identity/token', {

test/readmeexamples.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const assert = require('assert');
1919
const Client = require('../lib/client.js');
2020
const Cloudant = require('../cloudant.js');
2121
const nock = require('./nock.js');
22+
const u = require('url');
2223
const uuidv4 = require('uuid/v4'); // random
2324

2425
const ME = process.env.cloudant_username || 'nodejs';
@@ -53,7 +54,7 @@ describe('#db README Examples', function() {
5354
.put(`/${DBNAME}`)
5455
.reply(201, { ok: true });
5556

56-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
57+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
5758
cloudant.db.create(DBNAME).then(() => {
5859
mocks.done();
5960
done();
@@ -67,7 +68,7 @@ describe('#db README Examples', function() {
6768
.put(`/${DBNAME}/rabbit`, { happy: true })
6869
.reply(200, { ok: true, _id: 'rabbit' });
6970

70-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
71+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
7172

7273
async function asyncCall() {
7374
await cloudant.db.create(DBNAME);
@@ -88,7 +89,7 @@ describe('#db README Examples', function() {
8889
.put(`/${DBNAME}/rabbit`, { happy: true })
8990
.reply(200, { ok: true, _id: 'rabbit' });
9091

91-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
92+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
9293

9394
cloudant.db.create(DBNAME).then(() => {
9495
cloudant.use(DBNAME).insert({ happy: true }, 'rabbit').then((data) => {
@@ -106,7 +107,7 @@ describe('#db README Examples', function() {
106107
.put(`/${DBNAME}/rabbit`, { happy: true })
107108
.reply(200, { ok: true, _id: 'rabbit' });
108109

109-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
110+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
110111

111112
cloudant.db.create(DBNAME, (err) => {
112113
assert.ifError(err);
@@ -126,7 +127,7 @@ describe('#db README Examples', function() {
126127
.get('/_session')
127128
.reply(200, { userCtx: { name: null } });
128129

129-
var cloudant = Cloudant(`https://${ME}.cloudant.com`);
130+
var cloudant = Cloudant(SERVER);
130131
cloudant.session().then((session) => {
131132
assert.equal(session.userCtx.name, null);
132133
mocks.done();
@@ -139,10 +140,13 @@ describe('#db README Examples', function() {
139140
.get('/_session')
140141
.reply(200, { userCtx: { name: ME } });
141142

142-
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: `https://${ME}.cloudant.com` });
143+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
143144
cloudant.session().then((session) => {
144145
assert.equal(session.userCtx.name, ME);
145-
assert.equal(cloudant.config.url, `https://${ME}:${PASSWORD}@${ME}.cloudant.com`);
146+
let actual = new u.URL(cloudant.config.url);
147+
assert.equal(actual.username, ME);
148+
assert.equal(actual.password, PASSWORD);
149+
assert.equal(actual.origin, SERVER);
146150
mocks.done();
147151
done();
148152
}).catch(done);
@@ -153,10 +157,13 @@ describe('#db README Examples', function() {
153157
.get('/_session')
154158
.reply(200, { userCtx: { name: ME } });
155159

156-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
160+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
157161
cloudant.session().then((session) => {
158162
assert.equal(session.userCtx.name, ME);
159-
assert.equal(cloudant.config.url, `https://${ME}:${PASSWORD}@${ME}.cloudant.com`);
163+
let actual = new u.URL(cloudant.config.url);
164+
assert.equal(actual.username, ME);
165+
assert.equal(actual.password, PASSWORD);
166+
assert.equal(actual.origin, SERVER);
160167
mocks.done();
161168
done();
162169
}).catch(done);
@@ -193,7 +200,7 @@ describe('#db README Examples', function() {
193200
.get('/_all_dbs')
194201
.reply(200, [ 'animaldb' ]);
195202

196-
Cloudant({ account: ME, password: PASSWORD }, function(err, cloudant, pong) {
203+
Cloudant({ username: ME, password: PASSWORD, url: SERVER }, function(err, cloudant, pong) {
197204
assert.ifError(err);
198205
assert.equal(pong.couchdb, 'Welcome');
199206

@@ -212,7 +219,7 @@ describe('#db README Examples', function() {
212219
.get('/animaldb/non-existent-doc')
213220
.reply(404, { error: 'not_found', reason: 'missing' });
214221

215-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
222+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
216223
var db = cloudant.db.use('animaldb');
217224
db.get('non-existent-doc', function(err, data) {
218225
assert.equal(err.name, 'Error');
@@ -229,7 +236,7 @@ describe('#db README Examples', function() {
229236
.get('/animaldb/panda')
230237
.reply(200, { _id: 'panda', 'max_weight': 115 });
231238

232-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
239+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
233240
var db = cloudant.db.use('animaldb');
234241
db.get('panda', function(err, data) {
235242
assert.ifError(err);
@@ -244,7 +251,7 @@ describe('#db README Examples', function() {
244251
.get('/animaldb/panda')
245252
.reply(200, { _id: 'panda' });
246253

247-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
254+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
248255
var db = cloudant.db.use('animaldb');
249256
db.get('panda', function(err, data, headers) {
250257
assert.ifError(err);
@@ -329,7 +336,7 @@ describe('#db README Examples', function() {
329336
.get('/_api/v2/db/animaldb/_security')
330337
.reply(200, { ok: true });
331338

332-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
339+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
333340
cloudant.generate_api_key(function(err, api) {
334341
assert.ifError(err);
335342
assert.equal(api.key, 'foo');
@@ -368,7 +375,7 @@ describe('#db README Examples', function() {
368375
})
369376
.reply(200, { ok: true });
370377

371-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
378+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
372379
cloudant.set_cors({
373380
enable_cors: true,
374381
allow_credentials: true,
@@ -393,7 +400,7 @@ describe('#db README Examples', function() {
393400
})
394401
.reply(200, { ok: true });
395402

396-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
403+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
397404
cloudant.set_cors({
398405
enable_cors: true,
399406
allow_credentials: true,
@@ -416,7 +423,7 @@ describe('#db README Examples', function() {
416423
})
417424
.reply(200, { ok: true });
418425

419-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
426+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
420427
cloudant.set_cors({
421428
enable_cors: true,
422429
origins: []
@@ -440,7 +447,7 @@ describe('#db README Examples', function() {
440447
origins: [ 'https://example.com' ]
441448
});
442449

443-
var cloudant = Cloudant({ account: ME, password: PASSWORD });
450+
var cloudant = Cloudant({ username: ME, password: PASSWORD, url: SERVER });
444451
cloudant.get_cors().then((data) => {
445452
assert.ok(data.enable_cors);
446453
assert.ok(data.allow_credentials);

0 commit comments

Comments
 (0)