Skip to content

Commit

Permalink
fix: an error when passing the editor's devfile link
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <oorel@redhat.com>
  • Loading branch information
olexii4 committed Mar 16, 2023
1 parent 29164d1 commit 1f53637
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { CHE_EDITOR_YAML_PATH } from '../';
import { dump } from 'js-yaml';
import common from '@eclipse-che/common';
import devfileApi from '../../devfileApi';
import mockAxios from 'axios';
import { FakeStoreBuilder } from '../../../store/__mocks__/storeBuilder';

describe('Workspace-client helpers', () => {
describe('checks for HTTP 401 Unauthorized response status code', () => {
Expand Down Expand Up @@ -184,16 +184,28 @@ describe('Workspace-client helpers', () => {
});

it('should return undefined without optionalFilesContent', async () => {
const customEditor = await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
const store = new FakeStoreBuilder().build();
const customEditor = await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);

expect(customEditor).toBeUndefined();
});

describe('inlined editor', () => {
it('should return inlined editor without changes', async () => {
optionalFilesContent[CHE_EDITOR_YAML_PATH] = dump({ inline: editor });
const store = new FakeStoreBuilder().build();

const customEditor = await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
const customEditor = await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);

expect(customEditor).toEqual(dump(editor));
});
Expand All @@ -210,8 +222,14 @@ describe('Workspace-client helpers', () => {
],
},
});
const store = new FakeStoreBuilder().build();

const customEditor = await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
const customEditor = await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);

expect(customEditor).toEqual(expect.stringContaining('memoryLimit: 1234Mi'));
});
Expand All @@ -220,11 +238,17 @@ describe('Workspace-client helpers', () => {
// set an empty value as a name
editor.metadata.name = '';
optionalFilesContent[CHE_EDITOR_YAML_PATH] = dump({ inline: editor });
const store = new FakeStoreBuilder().build();

let errorText: string | undefined = undefined;

try {
await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);
} catch (e) {
errorText = common.helpers.errors.getMessage(e);
}
Expand All @@ -241,19 +265,24 @@ describe('Workspace-client helpers', () => {
optionalFilesContent[CHE_EDITOR_YAML_PATH] = dump({
id: 'che-incubator/che-idea/next',
});
const mockFetch = mockAxios.get as jest.Mock;
mockFetch.mockResolvedValueOnce({
data: dump(editor),
});

const customEditor = await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
const store = new FakeStoreBuilder()
.withDevfileRegistries({
devfiles: {
['https://dummy-plugin-registry/plugins/che-incubator/che-idea/next/devfile.yaml']:
{
content: dump(editor),
},
},
})
.build();

const customEditor = await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);

expect(mockFetch.mock.calls).toEqual([
[
'https://dummy-plugin-registry/plugins/che-incubator/che-idea/next/devfile.yaml',
{ responseType: 'text' },
],
]);
expect(customEditor).toEqual(dump(editor));
});

Expand All @@ -269,19 +298,24 @@ describe('Workspace-client helpers', () => {
],
},
});
const mockFetch = mockAxios.get as jest.Mock;
mockFetch.mockResolvedValueOnce({
data: dump(editor),
});

const customEditor = await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
const store = new FakeStoreBuilder()
.withDevfileRegistries({
devfiles: {
['https://dummy-plugin-registry/plugins/che-incubator/che-idea/next/devfile.yaml']:
{
content: dump(editor),
},
},
})
.build();

const customEditor = await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);

expect(mockFetch.mock.calls).toEqual([
[
'https://dummy-plugin-registry/plugins/che-incubator/che-idea/next/devfile.yaml',
{ responseType: 'text' },
],
]);
expect(customEditor).toEqual(expect.stringContaining('memoryLimit: 1234Mi'));
});

Expand All @@ -291,24 +325,29 @@ describe('Workspace-client helpers', () => {
optionalFilesContent[CHE_EDITOR_YAML_PATH] = dump({
id: 'che-incubator/che-idea/next',
});
const mockFetch = mockAxios.get as jest.Mock;
mockFetch.mockResolvedValueOnce({
data: dump(editor),
});
const store = new FakeStoreBuilder()
.withDevfileRegistries({
devfiles: {
['https://dummy-plugin-registry/plugins/che-incubator/che-idea/next/devfile.yaml']:
{
content: dump(editor),
},
},
})
.build();

let errorText: string | undefined = undefined;
try {
await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);
} catch (e) {
errorText = common.helpers.errors.getMessage(e);
}

expect(mockFetch.mock.calls).toEqual([
[
'https://dummy-plugin-registry/plugins/che-incubator/che-idea/next/devfile.yaml',
{ responseType: 'text' },
],
]);
expect(errorText).toEqual(
'Failed to analyze the editor devfile, reason: Missing metadata.name attribute in the editor yaml file.',
);
Expand All @@ -318,30 +357,33 @@ describe('Workspace-client helpers', () => {
it('should return an editor without changes', async () => {
optionalFilesContent[CHE_EDITOR_YAML_PATH] = dump({
id: 'che-incubator/che-idea/next',
registryUrl: 'https://eclipse-che.github.io/che-plugin-registry/main/v3',
registryUrl: 'https://dummy/che-plugin-registry/main/v3',
});
const store = new FakeStoreBuilder()
.withDevfileRegistries({
devfiles: {
['https://dummy/che-plugin-registry/main/v3/plugins/che-incubator/che-idea/next/devfile.yaml']:
{
content: dump(editor),
},
},
})
.build();

const customEditor = await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);

const mockFetch = mockAxios.get as jest.Mock;

mockFetch.mockResolvedValueOnce({
data: dump(editor),
});

const customEditor = await getCustomEditor(pluginRegistryUrl, optionalFilesContent);

expect(mockFetch.mock.calls).toEqual([
[
'https://eclipse-che.github.io/che-plugin-registry/main/v3/plugins/che-incubator/che-idea/next/devfile.yaml',
{ responseType: 'text' },
],
]);
expect(customEditor).toEqual(dump(editor));
});

it('should return an overridden devfile', async () => {
optionalFilesContent[CHE_EDITOR_YAML_PATH] = dump({
id: 'che-incubator/che-idea/next',
registryUrl: 'https://eclipse-che.github.io/che-plugin-registry/main/v3',
registryUrl: 'https://dummy/che-plugin-registry/main/v3',
override: {
containers: [
{
Expand All @@ -351,19 +393,24 @@ describe('Workspace-client helpers', () => {
],
},
});
const mockFetch = mockAxios.get as jest.Mock;
mockFetch.mockResolvedValueOnce({
data: dump(editor),
});

const customEditor = await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
const store = new FakeStoreBuilder()
.withDevfileRegistries({
devfiles: {
['https://dummy/che-plugin-registry/main/v3/plugins/che-incubator/che-idea/next/devfile.yaml']:
{
content: dump(editor),
},
},
})
.build();

const customEditor = await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);

expect(mockFetch.mock.calls).toEqual([
[
'https://eclipse-che.github.io/che-plugin-registry/main/v3/plugins/che-incubator/che-idea/next/devfile.yaml',
{ responseType: 'text' },
],
]);
expect(customEditor).toEqual(expect.stringContaining('memoryLimit: 1234Mi'));
});

Expand All @@ -372,26 +419,31 @@ describe('Workspace-client helpers', () => {
editor.metadata.name = '';
optionalFilesContent[CHE_EDITOR_YAML_PATH] = dump({
id: 'che-incubator/che-idea/next',
registryUrl: 'https://eclipse-che.github.io/che-plugin-registry/main/v3',
});
const mockFetch = mockAxios.get as jest.Mock;
mockFetch.mockResolvedValueOnce({
data: dump(editor),
registryUrl: 'https://dummy/che-plugin-registry/main/v3',
});
const store = new FakeStoreBuilder()
.withDevfileRegistries({
devfiles: {
['https://dummy/che-plugin-registry/main/v3/plugins/che-incubator/che-idea/next/devfile.yaml']:
{
content: dump(editor),
},
},
})
.build();

let errorText: string | undefined = undefined;
try {
await getCustomEditor(pluginRegistryUrl, optionalFilesContent);
await getCustomEditor(
pluginRegistryUrl,
optionalFilesContent,
store.dispatch,
store.getState,
);
} catch (e) {
errorText = common.helpers.errors.getMessage(e);
}

expect(mockFetch.mock.calls).toEqual([
[
'https://eclipse-che.github.io/che-plugin-registry/main/v3/plugins/che-incubator/che-idea/next/devfile.yaml',
{ responseType: 'text' },
],
]);
expect(errorText).toEqual(
'Failed to analyze the editor devfile, reason: Missing metadata.name attribute in the editor yaml file.',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import devfileApi from '../devfileApi';
import { load, dump } from 'js-yaml';
import { ICheEditorYaml } from './devworkspace/devWorkspaceClient';
import { CHE_EDITOR_YAML_PATH } from './';
import { EDITOR_ATTR } from '../../containers/Loader/const';
import { AppState } from '../../store';
import { ThunkDispatch } from 'redux-thunk';
import { KnownAction } from '../../store/DevfileRegistries';
import { getEditor } from '../../store/DevfileRegistries/getDevfileByUrl';

/**
* Checks for HTTP 401 Unauthorized response status code
Expand Down Expand Up @@ -76,6 +81,8 @@ function hasStatus(response: unknown, _status: number): boolean {
export async function getCustomEditor(
pluginRegistryUrl: string | undefined,
optionalFilesContent: { [fileName: string]: string },
dispatch: ThunkDispatch<AppState, unknown, KnownAction>,
getState: () => AppState,
): Promise<string | undefined> {
let editorsDevfile: devfileApi.Devfile | undefined = undefined;

Expand Down Expand Up @@ -110,11 +117,11 @@ export async function getCustomEditor(
repositoryEditorYamlUrl = cheEditorYaml.reference;
}
if (repositoryEditorYamlUrl) {
const response = await axios.get<string>(repositoryEditorYamlUrl, {
responseType: 'text',
});
if (response.data) {
repositoryEditorYaml = load(response.data);
const response = await getEditor(repositoryEditorYamlUrl, dispatch, getState);
if (response.content) {
repositoryEditorYaml = load(response.content);
} else {
throw new Error(response.error);
}
}

Expand Down
Loading

0 comments on commit 1f53637

Please sign in to comment.