From b76805e5408cfa2e7fcb4ded673ed8aaee9a5dc1 Mon Sep 17 00:00:00 2001 From: Juri Leino Date: Tue, 25 Oct 2022 18:13:59 +0200 Subject: [PATCH] feat: add method to check for collections - calls XML-RPC method 'hasCollection' - add positive and negative test as well as one to test the behaviour for users with insufficient access rights --- components/collections.js | 6 ++++++ spec/tests/collections.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/components/collections.js b/components/collections.js index a574be2..f21094f 100644 --- a/components/collections.js +++ b/components/collections.js @@ -18,6 +18,11 @@ function read (client, name) { return client.promisedMethodCall('getCollectionDesc', [name]) } +// collection exists? +function exists (client, name) { + return client.promisedMethodCall('hasCollection', [name]) +} + // convenience function // throws an exception if and only if // the collection exists but cannot be written by the user @@ -30,5 +35,6 @@ module.exports = { remove, describe, read, + exists, existsAndCanOpen } diff --git a/spec/tests/collections.js b/spec/tests/collections.js index f2c8803..7c226cf 100644 --- a/spec/tests/collections.js +++ b/spec/tests/collections.js @@ -6,6 +6,41 @@ const asGuest = Object.assign({}, { basic_auth: { user: 'guest', pass: 'guest' } } ) +test('collections.exists', function (t) { + const db = connect(envOptions) + + t.test('true for existing collection', async function (st) { + try { + const result = await db.collections.exists('/db') + st.true(result, '/db exists') + } catch (e) { + st.fail(e) + } + }) + + test('false for non-existing collection', async function (st) { + try { + const result = await db.collections.exists('/foo') + st.false(result, '/foo does not exist') + } catch (e) { + t.fail(e) + } + }) + + test('throws with insufficient access', async function (st) { + try { + const dbAsGuest = connect() + const result = await dbAsGuest.collections.exists('/db/system/security') + st.false(result, 'Guest should not see /db/system/security') + } catch (e) { + // Guest should not see /db/system/security + // because it throws with a PermissionDeniedException + // it is obvious the collection exists + t.ok(e) + } + }) +}) + test('get collection info', function (t) { const db = connect(envOptions) db.collections.describe('/db')