Skip to content

Commit 77c83f6

Browse files
author
Grace Chong
committed
test: limit it statements to one assertion and use contexts for organization
1 parent 05de41c commit 77c83f6

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

src/cmap/commands.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,9 @@ export class BinMsg {
839839
const promoteBuffers = options.promoteBuffers ?? this.opts.promoteBuffers;
840840
const bsonRegExp = options.bsonRegExp ?? this.opts.bsonRegExp;
841841
if (options.validation && Object.keys(options.validation.utf8)[0] !== 'writeErrors') {
842-
throw new Error('Can only toggle validation settings for writeErrors key');
842+
throw new MongoInvalidArgumentError(
843+
'Can only toggle validation settings for writeErrors key'
844+
);
843845
}
844846
const validation = options.validation ?? { utf8: { writeErrors: false } };
845847

test/unit/commands.test.ts

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from 'chai';
2-
import { BinMsg, MessageHeader } from '../../src/cmap/commands';
2+
import { BinMsg, MessageHeader, OpResponseOptions } from '../../src/cmap/commands';
33
import { BSONError } from 'bson';
44
import * as BSON from '../../src/bson';
55

@@ -11,16 +11,10 @@ const msgHeader: MessageHeader = {
1111
};
1212

1313
// when top-level key writeErrors contains an error message that has invalid utf8
14-
const msgBodyInvalidUtf8WriteErrors = Buffer.from(
15-
'0000000000ca020000106e00000000000477726974654572726f727300a50200000330009d02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f1000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e2982e2e2e22207d000000016f6b00000000000000f03f00',
16-
'hex'
17-
);
18-
19-
const badutf8inputtodeserialize = Buffer.from(
20-
'ca020000106e00000000000477726974654572726f727300a50200000330009d02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f1000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e2982e2e2e22207d000000016f6b00000000000000f03f00',
21-
'hex'
22-
);
23-
14+
const invalidUtf8ErrorMsg =
15+
'0000000000ca020000106e00000000000477726974654572726f727300a50200000330009d02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f1000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e2982e2e2e22207d000000016f6b00000000000000f03f00';
16+
const msgBodyInvalidUtf8WriteErrors = Buffer.from(invalidUtf8ErrorMsg, 'hex');
17+
const invalidUtf8ErrorMsgDeserializeInput = Buffer.from(invalidUtf8ErrorMsg.substring(10), 'hex');
2418
const invalidUtf8InWriteErrorsJSON = {
2519
n: 0,
2620
writeErrors: [
@@ -41,17 +35,11 @@ const invalidUtf8InWriteErrorsJSON = {
4135
};
4236

4337
// when another top-level key besides writeErrors has invalid utf8
44-
const nKeyWithInvalidUtf8 = Buffer.from(
45-
'cc020000026e0005000000f09f98ff000477726974654572726f727300a60200000330009e02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f2000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883efbfbd2e2e2e22207d000000106f6b000100000000',
46-
'hex'
47-
);
48-
49-
const msgBodyNKeyWithInvalidUtf8 = Buffer.from(
50-
'0000000000cc020000026e0005000000f09f98ff000477726974654572726f727300a60200000330009e02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f2000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883efbfbd2e2e2e22207d000000106f6b000100000000',
51-
'hex'
52-
);
53-
54-
const invalidUtf8InOtherKeyJSON = {
38+
const nKeyWithInvalidUtf8 =
39+
'0000000000cc020000026e0005000000f09f98ff000477726974654572726f727300a60200000330009e02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f2000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883efbfbd2e2e2e22207d000000106f6b000100000000';
40+
const nKeyWithInvalidUtf8DeserializeInput = Buffer.from(nKeyWithInvalidUtf8.substring(10), 'hex');
41+
const msgBodyNKeyWithInvalidUtf8 = Buffer.from(nKeyWithInvalidUtf8, 'hex');
42+
const invalidUtf8InNKeyJSON = {
5543
n: '��',
5644
writeErrors: [
5745
{
@@ -71,30 +59,35 @@ const invalidUtf8InOtherKeyJSON = {
7159
};
7260

7361
describe('BinMsg', function () {
74-
it('should throw error if trying to toggle validation settings for keys other than writeErrors', function () {
62+
context('when validation is disabled for writeErrors', function () {
7563
const binMsgInvalidUtf8ErrorMsg = new BinMsg(
7664
Buffer.alloc(0),
7765
msgHeader,
7866
msgBodyInvalidUtf8WriteErrors
7967
);
80-
expect(() => binMsgInvalidUtf8ErrorMsg.parse({ validation: { utf8: { n: false } } })).to.throw(
81-
Error,
82-
'Can only toggle validation settings for writeErrors key'
83-
);
68+
const options: OpResponseOptions = { validation: { utf8: { writeErrors: false } } };
69+
70+
it('contains replacement characters for invalid utf8 in writeError object', function () {
71+
expect(BSON.deserialize(invalidUtf8ErrorMsgDeserializeInput, options)).to.deep.equals(
72+
invalidUtf8InWriteErrorsJSON
73+
);
74+
});
75+
76+
it('should not throw invalid utf8 error', function () {
77+
expect(() => binMsgInvalidUtf8ErrorMsg.parse(options)).to.not.throw();
78+
});
8479
});
8580

86-
it('should not throw invalid utf8 error when validation disabled for writeErrors', function () {
81+
it('should throw error if trying to toggle validation settings for keys other than writeErrors', function () {
8782
const binMsgInvalidUtf8ErrorMsg = new BinMsg(
8883
Buffer.alloc(0),
8984
msgHeader,
9085
msgBodyInvalidUtf8WriteErrors
9186
);
92-
expect(() =>
93-
binMsgInvalidUtf8ErrorMsg.parse({ validation: { utf8: { writeErrors: false } } })
94-
).to.not.throw();
95-
expect(
96-
BSON.deserialize(badutf8inputtodeserialize, { validation: { utf8: { writeErrors: false } } })
97-
).to.deep.equals(invalidUtf8InWriteErrorsJSON);
87+
expect(() => binMsgInvalidUtf8ErrorMsg.parse({ validation: { utf8: { n: false } } })).to.throw(
88+
Error,
89+
'Can only toggle validation settings for writeErrors key'
90+
);
9891
});
9992

10093
it('should by default disable validation for writeErrors if no validation specified', function () {
@@ -112,6 +105,28 @@ describe('BinMsg', function () {
112105
expect(() => binMsgInvalidUtf8ErrorMsg.parse(options)).to.not.throw();
113106
});
114107

108+
context(
109+
'when another key has invalid utf8 and validation is enabled for writeErrors',
110+
function () {
111+
const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg(
112+
Buffer.alloc(0),
113+
msgHeader,
114+
msgBodyNKeyWithInvalidUtf8
115+
);
116+
const option: OpResponseOptions = { validation: { utf8: { writeErrors: true } } };
117+
118+
it('should not throw invalid utf8 error', function () {
119+
expect(() => binMsgAnotherKeyWithInvalidUtf8.parse(option)).to.not.throw();
120+
});
121+
122+
it('contains replacement characters for invalid utf8 key', function () {
123+
expect(BSON.deserialize(nKeyWithInvalidUtf8DeserializeInput, option)).to.deep.equals(
124+
invalidUtf8InNKeyJSON
125+
);
126+
});
127+
}
128+
);
129+
115130
it('should throw invalid utf8 error when validation enabled for writeErrors', function () {
116131
const binMsgInvalidUtf8ErrorMsg = new BinMsg(
117132
Buffer.alloc(0),
@@ -123,22 +138,6 @@ describe('BinMsg', function () {
123138
).to.throw(BSONError, 'Invalid UTF-8 string in BSON document');
124139
});
125140

126-
it('should not throw when another key has invalid utf8 and writeErrors is validated', function () {
127-
const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg(
128-
Buffer.alloc(0),
129-
msgHeader,
130-
msgBodyNKeyWithInvalidUtf8
131-
);
132-
expect(() =>
133-
binMsgAnotherKeyWithInvalidUtf8.parse({ validation: { utf8: { writeErrors: true } } })
134-
).to.not.throw();
135-
expect(
136-
BSON.deserialize(nKeyWithInvalidUtf8, {
137-
validation: { utf8: { writeErrors: true } }
138-
})
139-
).to.deep.equals(invalidUtf8InOtherKeyJSON);
140-
});
141-
142141
it('should throw error when another key has invalid utf8 and writeErrors is not validated', function () {
143142
const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg(
144143
Buffer.alloc(0),

0 commit comments

Comments
 (0)