|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +import * as fakeTimers from '@sinonjs/fake-timers'; |
| 6 | +import { assert } from 'chai'; |
| 7 | +import * as sinon from 'sinon'; |
| 8 | +import { anything, instance, mock, verify, when } from 'ts-mockito'; |
| 9 | +import { Uri } from 'vscode'; |
| 10 | +import { IExtensionSingleActivationService } from '../../../client/activation/types'; |
| 11 | +import { IApplicationShell, ICommandManager } from '../../../client/common/application/types'; |
| 12 | +import { ContextKey } from '../../../client/common/contextKey'; |
| 13 | +import { EnableTrustedNotebooks } from '../../../client/common/experiments/groups'; |
| 14 | +import { IDisposable, IExperimentService } from '../../../client/common/types'; |
| 15 | +import { DataScience } from '../../../client/common/utils/localize'; |
| 16 | +import { Commands } from '../../../client/datascience/constants'; |
| 17 | +import { INotebookStorageProvider } from '../../../client/datascience/interactive-ipynb/notebookStorageProvider'; |
| 18 | +import { TrustCommandHandler } from '../../../client/datascience/interactive-ipynb/trustCommandHandler'; |
| 19 | +import { TrustService } from '../../../client/datascience/interactive-ipynb/trustService'; |
| 20 | +import { INotebookEditorProvider, INotebookModel, ITrustService } from '../../../client/datascience/types'; |
| 21 | +import { noop } from '../../core'; |
| 22 | +import { createNotebookModel, disposeAllDisposables } from '../notebook/helper'; |
| 23 | + |
| 24 | +// tslint:disable: no-any |
| 25 | + |
| 26 | +suite('DataScience - Trust Command Handler', () => { |
| 27 | + let trustCommandHandler: IExtensionSingleActivationService; |
| 28 | + let trustService: ITrustService; |
| 29 | + let editorProvider: INotebookEditorProvider; |
| 30 | + let storageProvider: INotebookStorageProvider; |
| 31 | + let commandManager: ICommandManager; |
| 32 | + let applicationShell: IApplicationShell; |
| 33 | + let disposables: IDisposable[] = []; |
| 34 | + let clock: fakeTimers.InstalledClock; |
| 35 | + let contextKeySet: sinon.SinonStub<[boolean], Promise<void>>; |
| 36 | + let experiments: IExperimentService; |
| 37 | + let model: INotebookModel; |
| 38 | + let trustNotebookCommandCallback: (uri: Uri) => Promise<void>; |
| 39 | + setup(() => { |
| 40 | + trustService = mock<TrustService>(); |
| 41 | + editorProvider = mock<INotebookEditorProvider>(); |
| 42 | + storageProvider = mock<INotebookStorageProvider>(); |
| 43 | + commandManager = mock<ICommandManager>(); |
| 44 | + applicationShell = mock<IApplicationShell>(); |
| 45 | + model = createNotebookModel(false, Uri.file('a')); |
| 46 | + when(storageProvider.get(anything())).thenResolve(model); |
| 47 | + disposables = []; |
| 48 | + |
| 49 | + experiments = mock<IExperimentService>(); |
| 50 | + |
| 51 | + when(trustService.trustNotebook(anything(), anything())).thenResolve(); |
| 52 | + when(experiments.inExperiment(anything())).thenCall((exp) => |
| 53 | + Promise.resolve(exp === EnableTrustedNotebooks.experiment) |
| 54 | + ); |
| 55 | + when(commandManager.registerCommand(anything(), anything(), anything())).thenCall(() => ({ dispose: noop })); |
| 56 | + when(commandManager.registerCommand(Commands.TrustNotebook, anything(), anything())).thenCall((_, cb) => { |
| 57 | + trustNotebookCommandCallback = cb.bind(trustCommandHandler); |
| 58 | + return { dispose: noop }; |
| 59 | + }); |
| 60 | + |
| 61 | + trustCommandHandler = new TrustCommandHandler( |
| 62 | + instance(trustService), |
| 63 | + instance(editorProvider), |
| 64 | + instance(storageProvider), |
| 65 | + instance(commandManager), |
| 66 | + instance(applicationShell), |
| 67 | + disposables, |
| 68 | + instance(experiments) |
| 69 | + ); |
| 70 | + |
| 71 | + clock = fakeTimers.install(); |
| 72 | + |
| 73 | + contextKeySet = sinon.stub(ContextKey.prototype, 'set'); |
| 74 | + contextKeySet.resolves(); |
| 75 | + }); |
| 76 | + teardown(() => { |
| 77 | + sinon.restore(); |
| 78 | + disposeAllDisposables(disposables); |
| 79 | + clock.uninstall(); |
| 80 | + }); |
| 81 | + |
| 82 | + test('Context not set if not in experiment', async () => { |
| 83 | + when(experiments.inExperiment(anything())).thenResolve(false); |
| 84 | + |
| 85 | + await trustCommandHandler.activate(); |
| 86 | + await clock.runAllAsync(); |
| 87 | + |
| 88 | + assert.equal(contextKeySet.callCount, 0); |
| 89 | + }); |
| 90 | + test('Context set if in experiment', async () => { |
| 91 | + when(experiments.inExperiment(anything())).thenCall((exp) => |
| 92 | + Promise.resolve(exp === EnableTrustedNotebooks.experiment) |
| 93 | + ); |
| 94 | + |
| 95 | + await trustCommandHandler.activate(); |
| 96 | + await clock.runAllAsync(); |
| 97 | + |
| 98 | + assert.equal(contextKeySet.callCount, 1); |
| 99 | + }); |
| 100 | + test('Executing command will not update trust after dismissing the prompt', async () => { |
| 101 | + when(applicationShell.showErrorMessage(anything(), anything(), anything())).thenResolve(undefined as any); |
| 102 | + |
| 103 | + await trustCommandHandler.activate(); |
| 104 | + await clock.runAllAsync(); |
| 105 | + await trustNotebookCommandCallback(Uri.file('a')); |
| 106 | + |
| 107 | + verify(applicationShell.showErrorMessage(anything(), anything(), anything())).once(); |
| 108 | + verify(trustService.trustNotebook(anything(), anything())).never(); |
| 109 | + assert.isFalse(model.isTrusted); |
| 110 | + }); |
| 111 | + test('Executing command will update trust', async () => { |
| 112 | + when(applicationShell.showErrorMessage(anything(), anything(), anything())).thenResolve( |
| 113 | + DataScience.trustNotebook() as any |
| 114 | + ); |
| 115 | + |
| 116 | + assert.isFalse(model.isTrusted); |
| 117 | + await trustCommandHandler.activate(); |
| 118 | + await clock.runAllAsync(); |
| 119 | + await trustNotebookCommandCallback(Uri.file('a')); |
| 120 | + |
| 121 | + verify(applicationShell.showErrorMessage(anything(), anything(), anything())).once(); |
| 122 | + verify(trustService.trustNotebook(anything(), anything())).once(); |
| 123 | + assert.isTrue(model.isTrusted); |
| 124 | + }); |
| 125 | +}); |
0 commit comments