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

Do not show modal for trusted repositories #1172

Merged
merged 9 commits into from
Aug 30, 2024
2 changes: 2 additions & 0 deletions build/dockerfiles/skaffold.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

FROM docker.io/node:18.19.1-alpine3.19

LABEL quay.expires-after=1w
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we use this docker file to build images for testing purposes, so we don't need them to be in the registry forever.


ENV FRONTEND_LIB=../../packages/dashboard-frontend/lib/public
ENV BACKEND_LIB=../../packages/dashboard-backend/lib
ENV BACKEND_NODE_MODULES=../../packages/dashboard-backend/node_modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { FakeStoreBuilder } from '@/store/__mocks__/storeBuilder';

const { createSnapshot, renderComponent } = getComponentRenderer(getComponent);

jest.mock('@/components/UntrustedSourceModal');

const history = createMemoryHistory({
initialEntries: ['/'],
});
Expand Down Expand Up @@ -81,63 +83,6 @@ describe('GitRepoLocationInput', () => {
expect(window.open).not.toHaveBeenCalled();
});

describe('trusted/untrusted source', () => {
jest.mock('@/components/UntrustedSourceModal');

test('untrusted source', () => {
const store = new FakeStoreBuilder()
.withDwServerConfig({
defaults: {
editor: defaultEditorId,
components: [],
plugins: [],
pvcStrategy: 'per-workspace',
},
})
.withWorkspacePreferences({
'trusted-sources': ['repo1', 'repo2'],
})
.build();
renderComponent(store);

const input = screen.getByRole('textbox');
expect(input).toBeValid();

userEvent.paste(input, 'http://test-location');

expect(input).toHaveValue('http://test-location');

const button = screen.getByRole('button', { name: 'Create & Open' });
userEvent.click(button);

const untrustedSourceModal = screen.queryByRole('dialog', {
name: /Do you trust the authors of this repository/i,
});
expect(untrustedSourceModal).not.toBeNull();

expect(window.open).not.toHaveBeenCalled();
});

test('trusted source', () => {
renderComponent(store);

const input = screen.getByRole('textbox');
expect(input).toBeValid();

userEvent.paste(input, 'http://test-location');

expect(input).toHaveValue('http://test-location');

const button = screen.getByRole('button', { name: 'Create & Open' });
userEvent.click(button);

const untrustedSourceModal = screen.queryByRole('dialog', { name: /untrusted source/i });
expect(untrustedSourceModal).toBeNull();

expect(window.open).toHaveBeenCalledTimes(1);
});
});

describe('valid HTTP location', () => {
describe('factory URL w/o other parameters', () => {
test('trim spaces from the input value', () => {
Expand All @@ -156,16 +101,19 @@ describe('GitRepoLocationInput', () => {
expect(button).toBeEnabled();

userEvent.click(button);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

// the selected editor ID should be added to the URL
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#http://test-location/',
'_blank',
);
expect(window.open).toHaveBeenCalledTimes(1);

userEvent.type(input, '{enter}');
expect(window.open).toHaveBeenCalledTimes(2);
});

test('editor definition and image are empty', () => {
renderComponent(store);

Expand All @@ -181,15 +129,17 @@ describe('GitRepoLocationInput', () => {
expect(button).toBeEnabled();

userEvent.click(button);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

// the selected editor ID should be added to the URL
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#http://test-location/',
'_blank',
);
expect(window.open).toHaveBeenCalledTimes(1);

userEvent.type(input, '{enter}');
expect(window.open).toHaveBeenCalledTimes(2);
});

test('editor definition is defined, editor image is empty', () => {
Expand All @@ -207,15 +157,17 @@ describe('GitRepoLocationInput', () => {
expect(button).toBeEnabled();

userEvent.click(button);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

// the selected editor ID should be added to the URL
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#http://test-location/?che-editor=che-incubator%2Fche-code%2Finsiders',
'_blank',
);
expect(window.open).toHaveBeenCalledTimes(1);

userEvent.type(input, '{enter}');
expect(window.open).toHaveBeenCalledTimes(2);
});

test('editor definition is empty, editor image is defined', () => {
Expand All @@ -233,15 +185,17 @@ describe('GitRepoLocationInput', () => {
expect(button).toBeEnabled();

userEvent.click(button);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

// the selected editor ID should be added to the URL
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#http://test-location/?editor-image=custom-editor-image',
'_blank',
);
expect(window.open).toHaveBeenCalledTimes(1);

userEvent.type(input, '{enter}');
expect(window.open).toHaveBeenCalledTimes(2);
});

test('editor definition and editor image are defined', () => {
Expand All @@ -259,15 +213,17 @@ describe('GitRepoLocationInput', () => {
expect(button).toBeEnabled();

userEvent.click(button);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

// the selected editor ID should be added to the URL
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#http://test-location/?che-editor=che-incubator%2Fche-code%2Finsiders&editor-image=custom-editor-image',
'_blank',
);
expect(window.open).toHaveBeenCalledTimes(1);

userEvent.type(input, '{enter}');
expect(window.open).toHaveBeenCalledTimes(2);
});
});

Expand All @@ -287,15 +243,17 @@ describe('GitRepoLocationInput', () => {
expect(button).toBeEnabled();

userEvent.click(button);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

// the selected editor ID should NOT be added to the URL, as the URL parameter has higher priority
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#http://test-location/?che-editor=other-editor-id',
'_blank',
);
expect(window.open).toHaveBeenCalledTimes(1);

userEvent.type(input, '{enter}');
expect(window.open).toHaveBeenCalledTimes(2);
});

test('editor definition and editor image are defined, and `editor-image` is provided', () => {
Expand All @@ -313,6 +271,11 @@ describe('GitRepoLocationInput', () => {
expect(button).toBeEnabled();

userEvent.click(button);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

// the selected editor ID should be added to the URL
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#http://test-location/?editor-image=custom-editor-image',
Expand Down Expand Up @@ -369,6 +332,10 @@ describe('GitRepoLocationInput', () => {

userEvent.click(buttonCreate);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

expect(window.open).toHaveBeenCalledTimes(1);
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#[email protected]:user/repo.git?che-editor=che-incubator%2Fche-code%2Finsiders&editor-image=custom-editor-image',
Expand Down Expand Up @@ -398,6 +365,10 @@ describe('GitRepoLocationInput', () => {

userEvent.click(buttonCreate);

// trust the resource
const continueButton = screen.getByRole('button', { name: 'Continue' });
userEvent.click(continueButton);

expect(window.open).toHaveBeenCalledTimes(1);
expect(window.open).toHaveBeenLastCalledWith(
'http://localhost/#[email protected]:user/repo.git?che-editor=other-editor-id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Provider } from 'react-redux';
import { Action, Store } from 'redux';

import UntrustedSourceModal from '@/components/UntrustedSourceModal';
import getComponentRenderer, { screen, waitFor } from '@/services/__mocks__/getComponentRenderer';
import getComponentRenderer, { screen } from '@/services/__mocks__/getComponentRenderer';
import { AppThunk } from '@/store';
import { FakeStoreBuilder } from '@/store/__mocks__/storeBuilder';
import { WorkspacePreferencesActionCreators } from '@/store/Workspaces/Preferences';
Expand Down Expand Up @@ -108,12 +108,14 @@ describe('Untrusted Repo Warning Modal', () => {
});

test('click the continue button', async () => {
jest.useFakeTimers();

const store = storeBuilder
.withWorkspacePreferences({
'trusted-sources': ['repo1', 'repo2'],
})
.build();
renderComponent(store, 'source-location');
const { reRenderComponent } = renderComponent(store, 'source-location');

const continueButton = screen.getByRole('button', { name: 'Continue' });

Expand All @@ -122,10 +124,20 @@ describe('Untrusted Repo Warning Modal', () => {

continueButton.click();

await waitFor(() => expect(mockOnContinue).toHaveBeenCalled());
const nextStore = new FakeStoreBuilder()
.withWorkspacePreferences({
'trusted-sources': ['repo1', 'repo2', 'source-location'],
})
.build();
reRenderComponent(nextStore, 'source-location');

await jest.advanceTimersByTimeAsync(5000);

expect(mockAddTrustedSource).toHaveBeenCalledTimes(1);
expect(mockAddTrustedSource).toHaveBeenCalledWith('source-location');
expect(mockOnContinue).toHaveBeenCalledTimes(1);

jest.useRealTimers();
});

test('trust all checkbox is clicked', () => {
Expand Down Expand Up @@ -163,6 +175,28 @@ describe('Untrusted Repo Warning Modal', () => {

expect(mockOnContinue).toHaveBeenCalledTimes(1);
});

test('re-check if source is trusted', () => {
const store = storeBuilder
.withWorkspacePreferences({
'trusted-sources': ['source-location'],
})
.build();
const { reRenderComponent } = renderComponent(store, 'source-location', false);

// no warning window
expect(screen.queryByRole('dialog')).toBeNull();

// should not call onContinue
expect(mockOnContinue).not.toHaveBeenCalled();

// open the modal
reRenderComponent(store, 'source-location', true);

// should call mockOnContinue
expect(mockOnContinue).toHaveBeenCalledTimes(1);
expect(screen.queryByRole('dialog')).toBeNull();
});
});

function getComponent(store: Store, location: string, isOpen = true): React.ReactElement {
Expand Down
Loading
Loading