Skip to content

Commit

Permalink
fix(cli-repl): throw error when changing telemetry setting while `for…
Browse files Browse the repository at this point in the history
…ceDisableTelemetry: true` MONGOSH-1762 (#1955)
  • Loading branch information
addaleax authored Apr 22, 2024
1 parent ced8239 commit 2306b9c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,43 @@ describe('CliRepl', function () {
expect(requests).to.have.lengthOf(0);
});

it('does not let the user modify telemetry settings with global force-disable-telemetry config', async function () {
const globalConfigFile = path.join(tmpdir.path, 'globalconfig.conf');
await fs.writeFile(
globalConfigFile,
'mongosh:\n forceDisableTelemetry: true'
);

cliReplOptions.globalConfigPaths = [globalConfigFile];
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start(await testServer.connectionString(), {});

output = '';
input.write('enableTelemetry()\n');
await waitEval(cliRepl.bus);
expect(output).to.include(
"Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true"
);

output = '';
input.write('disableTelemetry()\n');
await waitEval(cliRepl.bus);
expect(output).to.include(
"Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true"
);

output = '';
input.write('config.set("enableTelemetry", true)\n');
await waitEval(cliRepl.bus);
expect(output).to.include(
"Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true"
);

input.write('exit\n');
await waitBus(cliRepl.bus, 'mongosh:closed');
expect(requests).to.have.lengthOf(0);
});

it('does not send out telemetry if the user only runs a script for disabling telemetry', async function () {
cliReplOptions.shellCliOptions.eval = ['disableTelemetry()'];
cliRepl = new CliRepl(cliReplOptions);
Expand Down
5 changes: 5 additions & 0 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,11 @@ export class CliRepl implements MongoshIOProvider {
}
this.config[key] = value;
if (key === 'enableTelemetry') {
if (this.forceDisableTelemetry) {
throw new MongoshRuntimeError(
"Cannot modify telemetry settings while 'forceDisableTelemetry' is set to true"
);
}
this.setTelemetryEnabled(this.config.enableTelemetry);
this.bus.emit('mongosh:update-user', {
userId: this.config.userId,
Expand Down

0 comments on commit 2306b9c

Please sign in to comment.