Skip to content

Commit

Permalink
feat(NODE-3728): Allow to pass authorizedCollections option to the db…
Browse files Browse the repository at this point in the history
….listCollections method
  • Loading branch information
gribnoysup committed Nov 1, 2021
1 parent fa2f8d0 commit 76e51b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/operations/list_collections.ts
Expand Up @@ -15,6 +15,8 @@ const LIST_COLLECTIONS_WIRE_VERSION = 3;
export interface ListCollectionsOptions extends CommandOperationOptions {
/** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */
nameOnly?: boolean;
/** Since 4.0: If true and nameOnly is true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. */
authorizedCollections?: boolean;
/** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */
batchSize?: number;
}
Expand All @@ -25,6 +27,7 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
db: Db;
filter: Document;
nameOnly: boolean;
authorizedCollections: boolean;
batchSize?: number;

constructor(db: Db, filter: Document, options?: ListCollectionsOptions) {
Expand All @@ -34,6 +37,7 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
this.db = db;
this.filter = filter;
this.nameOnly = !!this.options.nameOnly;
this.authorizedCollections = !!this.options.authorizedCollections;

if (typeof this.options.batchSize === 'number') {
this.batchSize = this.options.batchSize;
Expand Down Expand Up @@ -94,7 +98,8 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
listCollections: 1,
filter: this.filter,
cursor: this.batchSize ? { batchSize: this.batchSize } : {},
nameOnly: this.nameOnly
nameOnly: this.nameOnly,
authorizedCollections: this.authorizedCollections
};

return super.executeCommand(server, session, command, callback);
Expand Down
20 changes: 16 additions & 4 deletions test/functional/unit_db_list_collections.test.js
Expand Up @@ -34,17 +34,27 @@ describe('db.listCollections', function () {
{
description: 'should always send nameOnly option, defaulting to false',
command: db => db.listCollections().toArray(() => {}),
listCollectionsValue: false
listCollectionsOptions: { nameOnly: false }
},
{
description: 'should propagate the nameOnly option',
command: db => db.listCollections({}, { nameOnly: true }).toArray(() => {}),
listCollectionsValue: true
listCollectionsOptions: { nameOnly: true }
},
{
description: 'should always send authorizedCollections option, defaulting to false',
command: db => db.listCollections().toArray(() => {}),
listCollectionsOptions: { authorizedCollections: false }
},
{
description: 'should propagate the authorizedCollections option',
command: db => db.listCollections({}, { authorizedCollections: true }).toArray(() => {}),
listCollectionsOptions: { authorizedCollections: true }
},
{
description: 'should send nameOnly: true for db.collections',
command: db => db.collections(() => {}),
listCollectionsValue: true
listCollectionsOptions: { nameOnly: true }
}
].forEach(config => {
function testFn(done) {
Expand All @@ -59,7 +69,9 @@ describe('db.listCollections', function () {
client.on('commandStarted', e => {
if (e.commandName === 'listCollections') {
try {
expect(e).to.have.nested.property('command.nameOnly', config.listCollectionsValue);
for (const [name, value] of Object.entries(config.listCollectionsOptions)) {
expect(e).to.have.nested.property(`command.${name}`, value);
}
client.close(done);
} catch (err) {
client.close(() => done(err));
Expand Down

0 comments on commit 76e51b2

Please sign in to comment.