Skip to content

Commit

Permalink
feat: add method to check for collections
Browse files Browse the repository at this point in the history
- calls XML-RPC method 'hasCollection'
- add positive and negative test as well as one to test the behaviour
  for users with insufficient access rights
  • Loading branch information
line-o authored and duncdrum committed Oct 25, 2022
1 parent 77c5bf7 commit b76805e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions components/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,5 +35,6 @@ module.exports = {
remove,
describe,
read,
exists,
existsAndCanOpen
}
35 changes: 35 additions & 0 deletions spec/tests/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit b76805e

Please sign in to comment.