Skip to content

Commit 5f0e979

Browse files
authored
feat(compass-settings): add atlas login section to compass settings feature preview tab COMPASS-7098 (#4742)
* feat(compass-settings): add atlas login section to compass settings feature preview tab * chore(atlas-service): ignore network errors on revoke * chore(mocha-config-compass): setup default preferences for compass-plugin test environment * chore(atlas-service): disable atlas service when network traffic is disabled
1 parent 460a436 commit 5f0e979

File tree

24 files changed

+959
-106
lines changed

24 files changed

+959
-106
lines changed

configs/mocha-config-compass/compass-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
require: [
88
...reactConfig.require,
99
path.resolve(__dirname, 'register', 'electron-renderer-register.js'),
10+
path.resolve(__dirname, 'register', 'compass-preferences-register.js'),
1011
],
1112
// electron-mocha config options (ignored when run with just mocha)
1213
// https://github.com/jprichardson/electron-mocha
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
mochaHooks: {
3+
async beforeAll() {
4+
// Only do this setup if we are not in electron environment, electron case
5+
// is handled in main-process.js
6+
if (typeof process.type !== 'undefined') {
7+
return;
8+
}
9+
// For compass-plugin test preset we want to make sure that the
10+
// environment is matching compass environment, including the implicit
11+
// preferences used by a lot of packages in the monorepo
12+
process.env.COMPASS_TEST_USE_PREFERENCES_SANDBOX =
13+
process.env.COMPASS_TEST_USE_PREFERENCES_SANDBOX ?? 'true';
14+
// NB: Not adding this as a dep in package.json to avoid circular dependency
15+
await require('compass-preferences-model').setupPreferences();
16+
},
17+
},
18+
};

package-lock.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/atlas-service/.mocharc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require('@mongodb-js/mocha-config-compass/react');
1+
module.exports = require('@mongodb-js/mocha-config-compass/compass-plugin');

packages/atlas-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"@mongodb-js/compass-utils": "^0.3.3",
7373
"@mongodb-js/devtools-connect": "^2.4.0",
7474
"@mongodb-js/oidc-plugin": "^0.3.0",
75+
"compass-preferences-model": "^2.11.1",
7576
"electron": "^23.3.12",
7677
"keytar": "^7.9.0",
7778
"node-fetch": "^2.6.7",

packages/atlas-service/src/components/ai-signin-modal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ const AISignInModal: React.FunctionComponent<SignInModalProps> = ({
109109
</Button>
110110
<Button
111111
variant="primaryOutline"
112-
onClick={onSignInModalClose}
112+
onClick={() => {
113+
onSignInModalClose?.();
114+
}}
113115
className={cx(buttonStyles, maybeLaterButtonStyles)}
114116
>
115117
Maybe later

packages/atlas-service/src/main.spec.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AtlasService, throwIfNotOk } from './main';
44
import { promisify } from 'util';
55
import type { EventEmitter } from 'events';
66
import { once } from 'events';
7+
import preferencesAccess from 'compass-preferences-model';
78

89
const wait = promisify(setTimeout);
910

@@ -52,6 +53,7 @@ describe('AtlasServiceMain', function () {
5253

5354
const fetch = AtlasService['fetch'];
5455
const ipcMain = AtlasService['ipcMain'];
56+
const createPlugin = AtlasService['createMongoDBOIDCPlugin'];
5557
const apiBaseUrl = process.env.COMPASS_ATLAS_SERVICE_BASE_URL;
5658
const issuer = process.env.COMPASS_OIDC_ISSUER;
5759
const clientId = process.env.COMPASS_CLIENT_ID;
@@ -74,6 +76,7 @@ describe('AtlasServiceMain', function () {
7476
AtlasService['token'] = null;
7577
AtlasService['initPromise'] = null;
7678
AtlasService['oidcPluginSyncedFromLoggerState'] = 'initial';
79+
AtlasService['createMongoDBOIDCPlugin'] = createPlugin;
7780

7881
sandbox.resetHistory();
7982
});
@@ -542,7 +545,8 @@ describe('AtlasServiceMain', function () {
542545
.stub()
543546
.rejects(new Error('Could not retrieve valid access token'));
544547
const createPlugin = () => mockOidcPlugin;
545-
const initPromise = AtlasService.init(createPlugin);
548+
AtlasService['createMongoDBOIDCPlugin'] = createPlugin;
549+
const initPromise = AtlasService.init();
546550
expect(AtlasService).to.have.property(
547551
'oidcPluginSyncedFromLoggerState',
548552
'restoring'
@@ -558,7 +562,8 @@ describe('AtlasServiceMain', function () {
558562
mockOidcPlugin.mongoClientOptions.authMechanismProperties.REQUEST_TOKEN_CALLBACK =
559563
sandbox.stub().resolves({ accessToken: 'token' });
560564
const createPlugin = () => mockOidcPlugin;
561-
const initPromise = AtlasService.init(createPlugin);
565+
AtlasService['createMongoDBOIDCPlugin'] = createPlugin;
566+
const initPromise = AtlasService.init();
562567
expect(AtlasService).to.have.property(
563568
'oidcPluginSyncedFromLoggerState',
564569
'restoring'
@@ -570,4 +575,38 @@ describe('AtlasServiceMain', function () {
570575
);
571576
});
572577
});
578+
579+
describe('with networkTraffic turned off', function () {
580+
const networkTraffic = preferencesAccess.getPreferences().networkTraffic;
581+
582+
before(async function () {
583+
await preferencesAccess.savePreferences({ networkTraffic: false });
584+
});
585+
586+
after(async function () {
587+
await preferencesAccess.savePreferences({ networkTraffic });
588+
});
589+
590+
for (const methodName of [
591+
'requestOAuthToken',
592+
'signIn',
593+
'getUserInfo',
594+
'introspect',
595+
'revoke',
596+
'getAggregationFromUserInput',
597+
'getQueryFromUserInput',
598+
]) {
599+
it(`${methodName} should throw`, async function () {
600+
try {
601+
await (AtlasService as any)[methodName]({});
602+
expect.fail(`Expected ${methodName} to throw`);
603+
} catch (err) {
604+
expect(err).to.have.property(
605+
'message',
606+
'Network traffic is not allowed'
607+
);
608+
}
609+
});
610+
}
611+
});
573612
});

0 commit comments

Comments
 (0)