Skip to content

Commit 516e28d

Browse files
committed
feat(NODE-3728): Allow to pass authorizedCollections option to the db.listCollections method
1 parent 216d194 commit 516e28d

File tree

2 files changed

+101
-7
lines changed

2 files changed

+101
-7
lines changed

src/operations/list_collections.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const LIST_COLLECTIONS_WIRE_VERSION = 3;
1515
export interface ListCollectionsOptions extends CommandOperationOptions {
1616
/** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */
1717
nameOnly?: boolean;
18+
/** 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. */
19+
authorizedCollections?: boolean;
1820
/** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */
1921
batchSize?: number;
2022
}
@@ -25,6 +27,7 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
2527
db: Db;
2628
filter: Document;
2729
nameOnly: boolean;
30+
authorizedCollections: boolean;
2831
batchSize?: number;
2932

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

3842
if (typeof this.options.batchSize === 'number') {
3943
this.batchSize = this.options.batchSize;
@@ -99,7 +103,8 @@ export class ListCollectionsOperation extends CommandOperation<string[]> {
99103
listCollections: 1,
100104
filter: this.filter,
101105
cursor: this.batchSize ? { batchSize: this.batchSize } : {},
102-
nameOnly: this.nameOnly
106+
nameOnly: this.nameOnly,
107+
authorizedCollections: this.authorizedCollections
103108
};
104109
}
105110
}

test/unit/operations/list_collections.test.js

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,41 @@ describe('ListCollectionsOperation', function () {
1212
const operation = new ListCollectionsOperation(db, {}, { nameOnly: true, dbName: db });
1313

1414
it('sets nameOnly to true', function () {
15-
expect(operation.nameOnly).to.be.true;
15+
expect(operation).to.have.property('nameOnly', true);
1616
});
1717
});
1818

1919
context('when nameOnly is false', function () {
2020
const operation = new ListCollectionsOperation(db, {}, { nameOnly: false, dbName: db });
2121

2222
it('sets nameOnly to false', function () {
23-
expect(operation.nameOnly).to.be.false;
23+
expect(operation).to.have.property('nameOnly', false);
24+
});
25+
});
26+
});
27+
28+
context('when authorizedCollections is provided', function () {
29+
context('when authorizedCollections is true', function () {
30+
const operation = new ListCollectionsOperation(
31+
db,
32+
{},
33+
{ authorizedCollections: true, dbName: db }
34+
);
35+
36+
it('sets authorizedCollections to true', function () {
37+
expect(operation).to.have.property('authorizedCollections', true);
38+
});
39+
});
40+
41+
context('when authorizedCollections is false', function () {
42+
const operation = new ListCollectionsOperation(
43+
db,
44+
{},
45+
{ authorizedCollections: false, dbName: db }
46+
);
47+
48+
it('sets authorizedCollections to false', function () {
49+
expect(operation).to.have.property('authorizedCollections', false);
2450
});
2551
});
2652
});
@@ -29,7 +55,15 @@ describe('ListCollectionsOperation', function () {
2955
const operation = new ListCollectionsOperation(db, {}, { dbName: db });
3056

3157
it('sets nameOnly to false', function () {
32-
expect(operation.nameOnly).to.be.false;
58+
expect(operation).to.have.property('nameOnly', false);
59+
});
60+
});
61+
62+
context('when authorizedCollections is not provided', function () {
63+
const operation = new ListCollectionsOperation(db, {}, { dbName: db });
64+
65+
it('sets authorizedCollections to false', function () {
66+
expect(operation).to.have.property('authorizedCollections', false);
3367
});
3468
});
3569
});
@@ -44,7 +78,8 @@ describe('ListCollectionsOperation', function () {
4478
listCollections: 1,
4579
cursor: {},
4680
filter: {},
47-
nameOnly: true
81+
nameOnly: true,
82+
authorizedCollections: false
4883
});
4984
});
5085
});
@@ -57,7 +92,46 @@ describe('ListCollectionsOperation', function () {
5792
listCollections: 1,
5893
cursor: {},
5994
filter: {},
60-
nameOnly: false
95+
nameOnly: false,
96+
authorizedCollections: false
97+
});
98+
});
99+
});
100+
});
101+
102+
context('when authorizedCollections is provided', function () {
103+
context('when authorizedCollections is true', function () {
104+
const operation = new ListCollectionsOperation(
105+
db,
106+
{},
107+
{ authorizedCollections: true, dbName: db }
108+
);
109+
110+
it('sets authorizedCollections to true', function () {
111+
expect(operation.generateCommand()).to.deep.equal({
112+
listCollections: 1,
113+
cursor: {},
114+
filter: {},
115+
nameOnly: false,
116+
authorizedCollections: true
117+
});
118+
});
119+
});
120+
121+
context('when authorizedCollections is false', function () {
122+
const operation = new ListCollectionsOperation(
123+
db,
124+
{},
125+
{ authorizedCollections: false, dbName: db }
126+
);
127+
128+
it('sets authorizedCollections to false', function () {
129+
expect(operation.generateCommand()).to.deep.equal({
130+
listCollections: 1,
131+
cursor: {},
132+
filter: {},
133+
nameOnly: false,
134+
authorizedCollections: false
61135
});
62136
});
63137
});
@@ -71,7 +145,22 @@ describe('ListCollectionsOperation', function () {
71145
listCollections: 1,
72146
cursor: {},
73147
filter: {},
74-
nameOnly: false
148+
nameOnly: false,
149+
authorizedCollections: false
150+
});
151+
});
152+
});
153+
154+
context('when authorizedCollections is not provided', function () {
155+
const operation = new ListCollectionsOperation(db, {}, { dbName: db });
156+
157+
it('sets authorizedCollections to false', function () {
158+
expect(operation.generateCommand()).to.deep.equal({
159+
listCollections: 1,
160+
cursor: {},
161+
filter: {},
162+
nameOnly: false,
163+
authorizedCollections: false
75164
});
76165
});
77166
});

0 commit comments

Comments
 (0)