Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db.post(), db.bulkDocs(): throw INVALID_REV consistently #6

Open
wants to merge 1 commit into
base: more-careful-rev-checks
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading