Skip to content

Fixes to trust service #14352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/client/datascience/interactive-ipynb/trustService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export class TrustService implements ITrustService {
this.computeDigest(this.getFormattedContents(notebookContents))
]);

return this.digestStorage.containsDigest(uri, digest1) || this.digestStorage.containsDigest(uri, digest2);
// return this.digestStorage.containsDigest(uri, digest1) || this.digestStorage.containsDigest(uri, digest2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull the old commented out code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove in next PR, as CI is slow.

const [digest1Valid, digest2Valid] = await Promise.all([
this.digestStorage.containsDigest(uri, digest1),
this.digestStorage.containsDigest(uri, digest2)
]);

return digest1Valid || digest2Valid;
}

/**
Expand Down
34 changes: 34 additions & 0 deletions src/test/datascience/interactive-common/nbToTrust.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"1\n"
]
}
],
"metadata": {
"kernelspec": {
"name": ".venvnokernel",
"display_name": ".venvnokernel"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2-final"
},
"orig_nbformat": 2
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';

import { assert } from 'chai';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as uuid from 'uuid/v4';
import { Uri } from 'vscode';
import { ITrustService } from '../../../client/datascience/types';
import { IExtensionTestApi } from '../../common';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../constants';
import { initialize } from '../../initialize';

suite('DataScience - TrustService', () => {
let api: IExtensionTestApi;
let trustService: ITrustService;
const templateIpynb = Uri.file(
path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src/test/datascience/interactive-common/nbToTrust.ipynb')
);
suiteSetup(async () => {
api = await initialize();
trustService = api.serviceContainer.get<ITrustService>(ITrustService);
});

test('Trusting a notebook', async () => {
const uri = Uri.file(path.join(templateIpynb.fsPath, uuid()));

const contents = await fs.readFile(templateIpynb.fsPath, { encoding: 'utf8' });
assert.isFalse(await trustService.isNotebookTrusted(uri, contents), 'Notebook is not trusted');

await trustService.trustNotebook(uri, contents);
assert.isTrue(await trustService.isNotebookTrusted(uri, contents), 'Notebook is not trusted');
});
test('Trusting a notebook (json saved with different formats)', async () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I should have added in the previous PR.

const uri = Uri.file(path.join(templateIpynb.fsPath, uuid()));
const contents = await fs.readFile(templateIpynb.fsPath, { encoding: 'utf8' });
const contentsWithNoIndentation = JSON.stringify(JSON.parse(contents), undefined, '');

await trustService.trustNotebook(uri, contentsWithNoIndentation);
assert.isTrue(await trustService.isNotebookTrusted(uri, contentsWithNoIndentation), 'Notebook is not trusted');

// Confirm the same json formatted with 2 & 4 spaces are considered trusted.
const contentsWith2Indentation = JSON.stringify(JSON.parse(contents), undefined, 2);
const contentsWith4Indentation = JSON.stringify(JSON.parse(contents), undefined, 4);
assert.isTrue(
await trustService.isNotebookTrusted(uri, contentsWith2Indentation),
'Not trusted with 2 indents'
);
assert.isTrue(
await trustService.isNotebookTrusted(uri, contentsWith4Indentation),
'Not trusted with 4 indents'
);
});
});