|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +import { assert } from 'chai'; |
| 6 | +import * as fs from 'fs-extra'; |
| 7 | +import * as path from 'path'; |
| 8 | +import * as uuid from 'uuid/v4'; |
| 9 | +import { Uri } from 'vscode'; |
| 10 | +import { ITrustService } from '../../../client/datascience/types'; |
| 11 | +import { IExtensionTestApi } from '../../common'; |
| 12 | +import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../constants'; |
| 13 | +import { initialize } from '../../initialize'; |
| 14 | + |
| 15 | +suite('DataScience - TrustService', () => { |
| 16 | + let api: IExtensionTestApi; |
| 17 | + let trustService: ITrustService; |
| 18 | + const templateIpynb = Uri.file( |
| 19 | + path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src/test/datascience/interactive-common/nbToTrust.ipynb') |
| 20 | + ); |
| 21 | + suiteSetup(async () => { |
| 22 | + api = await initialize(); |
| 23 | + trustService = api.serviceContainer.get<ITrustService>(ITrustService); |
| 24 | + }); |
| 25 | + |
| 26 | + test('Trusting a notebook', async () => { |
| 27 | + const uri = Uri.file(path.join(templateIpynb.fsPath, uuid())); |
| 28 | + |
| 29 | + const contents = await fs.readFile(templateIpynb.fsPath, { encoding: 'utf8' }); |
| 30 | + assert.isFalse(await trustService.isNotebookTrusted(uri, contents), 'Notebook is not trusted'); |
| 31 | + |
| 32 | + await trustService.trustNotebook(uri, contents); |
| 33 | + assert.isTrue(await trustService.isNotebookTrusted(uri, contents), 'Notebook is not trusted'); |
| 34 | + }); |
| 35 | + test('Trusting a notebook (json saved with different formats)', async () => { |
| 36 | + const uri = Uri.file(path.join(templateIpynb.fsPath, uuid())); |
| 37 | + const contents = await fs.readFile(templateIpynb.fsPath, { encoding: 'utf8' }); |
| 38 | + const contentsWithNoIndentation = JSON.stringify(JSON.parse(contents), undefined, ''); |
| 39 | + |
| 40 | + await trustService.trustNotebook(uri, contentsWithNoIndentation); |
| 41 | + assert.isTrue(await trustService.isNotebookTrusted(uri, contentsWithNoIndentation), 'Notebook is not trusted'); |
| 42 | + |
| 43 | + // Confirm the same json formatted with 2 & 4 spaces are considered trusted. |
| 44 | + const contentsWith2Indentation = JSON.stringify(JSON.parse(contents), undefined, 2); |
| 45 | + const contentsWith4Indentation = JSON.stringify(JSON.parse(contents), undefined, 4); |
| 46 | + assert.isTrue( |
| 47 | + await trustService.isNotebookTrusted(uri, contentsWith2Indentation), |
| 48 | + 'Not trusted with 2 indents' |
| 49 | + ); |
| 50 | + assert.isTrue( |
| 51 | + await trustService.isNotebookTrusted(uri, contentsWith4Indentation), |
| 52 | + 'Not trusted with 4 indents' |
| 53 | + ); |
| 54 | + }); |
| 55 | +}); |
0 commit comments