Skip to content

Commit

Permalink
db.post(), db.bulkDocs(): throw INVALID_REV consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Apr 12, 2024
1 parent 569e28b commit aac0dc4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/node_modules/pouchdb-core/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,13 @@ class AbstractPouchDB extends EventEmitter {
}

for (var i = 0; i < req.docs.length; ++i) {
if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) {
const doc = req.docs[i];
if (typeof doc !== 'object' || Array.isArray(doc)) {
return callback(createError(NOT_AN_OBJECT));
}
if ('_rev' in doc && !isValidRev(doc._rev)) {
return callback(createError(INVALID_REV));
}
}

var attachmentError;
Expand Down
65 changes: 65 additions & 0 deletions tests/integration/test.basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ adapters.forEach(function (adapter) {
({ rev }) => ({ toString:() => rev, indexOf:() => 12, substring:'hi' }),
({ rev }) => ({ toString:() => rev, indexOf:() => 12, substring:() => 'hi' }),
].forEach((generateRev, idx) => {
it(`post doc with illegal rev value #${idx}`, async () => {
const db = new PouchDB(dbs.name);

let threw;
try {
await db.post({
_rev: generateRev({ rev:'1-valid' }),
another: 'test'
});
} catch (err) {
threw = true;
err.message.should.equal('Invalid rev format'); // TODO should be err.reason?
}

if (!threw) {
throw new Error('db.put() should have thrown.');
}
});

it(`Modify a doc with illegal rev value #${idx}`, async () => {
const db = new PouchDB(dbs.name);

Expand All @@ -314,6 +333,52 @@ adapters.forEach(function (adapter) {
throw new Error('db.put() should have thrown.');
}
});

it(`bulkDocs with illegal rev value #${idx} (existing doc)`, async () => {
const db = new PouchDB(dbs.name);

const info = await db.post({ test: 'somestuff' });

let threw;
try {
await db.bulkDocs({
docs: [ {
_id: info.id,
_rev: generateRev(info),
another: 'test'
} ],
});
} catch (err) {
threw = true;
err.message.should.equal('Invalid rev format'); // TODO should be err.reason?
}

if (!threw) {
throw new Error('db.put() should have thrown.');
}
});

it(`bulkDocs with illegal rev value #${idx} (new doc)`, async () => {
const db = new PouchDB(dbs.name);

let threw;
try {
await db.bulkDocs({
docs: [ {
_id: '1',
_rev: generateRev({ rev:'1_valid' }),
another: 'test'
} ],
});
} catch (err) {
threw = true;
err.message.should.equal('Invalid rev format'); // TODO should be err.reason?
}

if (!threw) {
throw new Error('db.put() should have thrown.');
}
});
});

it('Remove doc', function (done) {
Expand Down

0 comments on commit aac0dc4

Please sign in to comment.