Skip to content
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

Data url links will now prompt to save to disk. #1315

Merged
merged 1 commit into from
Feb 13, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [client] fix link to manage qna service, in PR [#1301](https://github.com/Microsoft/BotFramework-Emulator/pull/1301)
- [client] Fixed resources pane styling issues, and added scrollbars, in PR [#1303](https://github.com/Microsoft/BotFramework-Emulator/pull/1303)
- [client] Fixed issue where transcript tab name was being overwritten after opening the transcript a second time, in PR [#1304](https://github.com/Microsoft/BotFramework-Emulator/pull/1304)
- [client] Fixed issue where links pointing to data urls were opening the Windows store, in PR [#1315](https://github.com/Microsoft/BotFramework-Emulator/pull/1315)
130 changes: 130 additions & 0 deletions packages/app/client/src/hyperlinkHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Framework Emulator Github:
// https://github.com/Microsoft/BotFramwork-Emulator
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

import { SharedConstants } from '@bfemulator/app-shared';

import { navigate } from './hyperlinkHandler';

let mockParse;
jest.mock('url', () => ({
get parse() {
return mockParse;
},
}));

const mockUniqueId = 'id1234';
jest.mock('@bfemulator/sdk-shared', () => ({
uniqueId: () => mockUniqueId,
}));

let mockRemoteCallsMade;
let mockRemoteCall;
jest.mock('./platform/commands/commandServiceImpl', () => ({
CommandServiceImpl: {
get remoteCall() {
return mockRemoteCall;
},
},
}));

describe('hyperlinkHandler', () => {
beforeEach(() => {
mockParse = jest.fn(url => {
if (url) {
const parts = url.split(':');
return { protocol: parts[0] + ':' || '' };
} else {
return '';
}
});
mockRemoteCallsMade = [];
(window as any)._openExternal.mockClear();
mockRemoteCall = jest.fn((commandName: string, ...args: any[]) => {
mockRemoteCallsMade.push({ commandName, args });
return Promise.resolve(true);
});
});

it('should navigate to an emulated ouath url', async () => {
const url = 'oauth://someoauthurl.com/auth&&&ending';
await navigate(url);

expect(mockRemoteCallsMade).toHaveLength(1);
expect(mockRemoteCallsMade[0].commandName).toBe(SharedConstants.Commands.OAuth.SendTokenResponse);
expect(mockRemoteCallsMade[0].args).toEqual(['someoauthurl.com/auth', 'ending', 'emulatedToken_' + mockUniqueId]);
});

it('should navigate to an ouath url', async () => {
const url = 'oauthlink://someoauthurl.com/auth&&&ending';
await navigate(url);

expect(mockRemoteCallsMade).toHaveLength(1);
expect(mockRemoteCallsMade[0].commandName).toBe(SharedConstants.Commands.OAuth.CreateOAuthWindow);
expect(mockRemoteCallsMade[0].args).toEqual(['someoauthurl.com/auth', 'ending']);
});

it('should open a data url', async () => {
const url = 'data:image/png;base64;somedata';
await navigate(url);

expect(mockRemoteCallsMade).toHaveLength(1);
expect(mockRemoteCallsMade[0].commandName).toBe(SharedConstants.Commands.Telemetry.TrackEvent);
expect(mockRemoteCallsMade[0].args).toEqual(['app_openLink', { url }]);
expect((window as any)._openExternal).not.toHaveBeenCalled();
});

it('should open a normal url', async () => {
const url = 'https://aka.ms/bot-framework-emulator';
await navigate(url);

expect(mockRemoteCallsMade).toHaveLength(1);
expect(mockRemoteCallsMade[0].commandName).toBe(SharedConstants.Commands.Telemetry.TrackEvent);
expect(mockRemoteCallsMade[0].args).toEqual(['app_openLink', { url }]);
expect((window as any)._openExternal).toHaveBeenCalled();
expect((window as any)._openExternal).toHaveBeenCalledWith(url, { activate: true });
});

it('should track opening a url on a throw', async () => {
mockParse = jest.fn(() => {
throw new Error();
});
const url = '';
await navigate(url);

expect(mockRemoteCallsMade).toHaveLength(1);
expect(mockRemoteCallsMade[0].commandName).toBe(SharedConstants.Commands.Telemetry.TrackEvent);
expect(mockRemoteCallsMade[0].args).toEqual(['app_openLink', { url }]);
expect((window as any)._openExternal).toHaveBeenCalled();
expect((window as any)._openExternal).toHaveBeenCalledWith(url, { activate: true });
});
});
13 changes: 11 additions & 2 deletions packages/app/client/src/hyperlinkHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { CommandServiceImpl } from './platform/commands/commandServiceImpl';
const Electron = (window as any).require('electron');
const { shell } = Electron;

export function navigate(url: string) {
export function navigate(url: string = '') {
const { TrackEvent } = SharedConstants.Commands.Telemetry;
try {
const parsed = URL.parse(url) || { protocol: '' };
Expand All @@ -50,7 +50,16 @@ export function navigate(url: string) {
navigateOAuthUrl(url.substring(12));
} else {
CommandServiceImpl.remoteCall(TrackEvent, 'app_openLink', { url }).catch(_e => void 0);
shell.openExternal(url, { activate: true });
// manually create and click a download link for data url's
if (url.startsWith('data:')) {
const a = document.createElement('a');
a.href = url;
a.download = '';
a.click();
a.remove();
Copy link
Contributor

Choose a reason for hiding this comment

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

This is inconsequential since the element is never being added to the dom. It can probably be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove the remove?!?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll change it when I inevitably have to update the branch once the azure gov fix goes into master.

} else {
shell.openExternal(url, { activate: true });
}
}
} catch (e) {
CommandServiceImpl.remoteCall(TrackEvent, 'app_openLink', { url }).catch(_e => void 0);
Expand Down
18 changes: 12 additions & 6 deletions testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');

Enzyme.configure({ adapter: new Adapter() });
window.require = function () {
window.require = function() {
return {
ipcRenderer: {
on() {
return null;
},
send() {
return null;
}
}
},
},
shell: {
openExternal: window._openExternal,
},
};
};
window.define = function () {

window._openExternal = jest.fn(() => null);

window.define = function() {
return null;
};

Expand All @@ -32,6 +38,6 @@ window.TextDecoder = class {

window.crypto = {
subtle: {
digest: async () => Promise.resolve('Hi! I am in your digest')
}
digest: async () => Promise.resolve('Hi! I am in your digest'),
},
};