Skip to content

Commit 66e0eca

Browse files
authored
Fixes to trust service (#14352)
1 parent 842a0b3 commit 66e0eca

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

src/client/datascience/interactive-ipynb/trustService.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ export class TrustService implements ITrustService {
3838
this.computeDigest(this.getFormattedContents(notebookContents))
3939
]);
4040

41-
return this.digestStorage.containsDigest(uri, digest1) || this.digestStorage.containsDigest(uri, digest2);
41+
// return this.digestStorage.containsDigest(uri, digest1) || this.digestStorage.containsDigest(uri, digest2);
42+
const [digest1Valid, digest2Valid] = await Promise.all([
43+
this.digestStorage.containsDigest(uri, digest1),
44+
this.digestStorage.containsDigest(uri, digest2)
45+
]);
46+
47+
return digest1Valid || digest2Valid;
4248
}
4349

4450
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 27,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"1\n"
10+
]
11+
}
12+
],
13+
"metadata": {
14+
"kernelspec": {
15+
"name": ".venvnokernel",
16+
"display_name": ".venvnokernel"
17+
},
18+
"language_info": {
19+
"codemirror_mode": {
20+
"name": "ipython",
21+
"version": 3
22+
},
23+
"file_extension": ".py",
24+
"mimetype": "text/x-python",
25+
"name": "python",
26+
"nbconvert_exporter": "python",
27+
"pygments_lexer": "ipython3",
28+
"version": "3.8.2-final"
29+
},
30+
"orig_nbformat": 2
31+
},
32+
"nbformat": 4,
33+
"nbformat_minor": 2
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)