Skip to content

Commit

Permalink
fix: yamlResolver route
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <[email protected]>
  • Loading branch information
olexii4 committed Nov 28, 2023
1 parent 26cde8b commit 4decef6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
2 changes: 0 additions & 2 deletions packages/dashboard-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"https": "^1.0.0",
"js-yaml": "^4.0.0",
"multi-ini": "^2.3.2",
"node-fetch": "^2.6.7",
"pino": "^8.15.1",
"pino-pretty": "^10.2.0",
"querystring": "^0.2.1",
Expand All @@ -57,7 +56,6 @@
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.9",
"@types/node-fetch": "^2.6.4",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"copy-webpack-plugin": "^11.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
*/

import { FastifyInstance } from 'fastify';
import * as mockNodeFetch from 'node-fetch';

import { baseApiPath } from '@/constants/config';
import { axiosInstance } from '@/routes/api/helpers/getCertificateAuthority';
import { setup, teardown } from '@/utils/appBuilder';

const { Response } = jest.requireActual<typeof mockNodeFetch>('node-fetch');

jest.mock('../helpers/getDevWorkspaceClient.ts');
jest.mock('../helpers/getToken.ts');

jest.mock('@/routes/api/helpers/getCertificateAuthority');
const getAxiosInstanceMock = jest.fn();
(axiosInstance.get as jest.Mock).mockImplementation(getAxiosInstanceMock);

describe('Server Config Route', () => {
let app: FastifyInstance;
const namespace = 'user-che';
Expand All @@ -36,9 +38,10 @@ describe('Server Config Route', () => {
describe('POST ${baseApiPath}/namespace/:namespace/yaml/resolver', () => {
test('file exists', async () => {
const devfileContent = 'devfile content';
(mockNodeFetch.default as unknown as jest.Mock).mockReturnValue(
Promise.resolve(new Response(devfileContent)),
);
getAxiosInstanceMock.mockResolvedValue({
status: 200,
data: devfileContent,
});

const res = await app
.inject()
Expand All @@ -51,9 +54,10 @@ describe('Server Config Route', () => {

test('file not found', async () => {
const responseText = 'not found';
(mockNodeFetch.default as unknown as jest.Mock).mockResolvedValue(
new Response(responseText, { status: 404 }),
);
getAxiosInstanceMock.mockResolvedValue({
status: 404,
data: responseText,
});

const res = await app
.inject()
Expand Down
19 changes: 10 additions & 9 deletions packages/dashboard-backend/src/routes/api/yamlResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

import { helpers } from '@eclipse-che/common';
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import fetch from 'node-fetch';

import { baseApiPath } from '@/constants/config';
import { namespacedSchema, yamlResolverSchema } from '@/constants/schemas';
import { restParams } from '@/models';
import { axiosInstance } from '@/routes/api/helpers/getCertificateAuthority';
import { getDevWorkspaceClient } from '@/routes/api/helpers/getDevWorkspaceClient';
import { getToken } from '@/routes/api/helpers/getToken';
import { getSchema } from '@/services/helpers';
Expand All @@ -41,14 +41,15 @@ export function registerYamlResolverRoute(instance: FastifyInstance) {
throw new Error(`User permissions error. ${helpers.errors.getMessage(e)}`);
}

const response = await fetch(url);
if (response.ok) {
return response.text();
} else {
reply.code(response.status);
reply.send(response.body);
return reply;
}
const response = await axiosInstance.get(url, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
},
});
reply.code(response.status);
reply.send(response.data);
return reply;
},
);
});
Expand Down
2 changes: 1 addition & 1 deletion run/local-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@ fi
# relative path from backend package
FRONTEND_RESOURCES=../../../../$DASHBOARD_FRONTEND/lib/public
$PRERUN_COMMAND &
yarn --cwd $DASHBOARD_BACKEND start:debug --publicFolder $FRONTEND_RESOURCES
yarn --cwd $DASHBOARD_BACKEND start:debug --publicFolder $FRONTEND_RESOURCES --loglevel fatal
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1835,14 +1835,6 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.4.tgz#81f886786411c45bba3f33e781ab48bd56bfca2e"
integrity sha512-Kfe/D3hxHTusnPNRbycJE1N77WHDsdS4AjUYIzlDzhDrS47NrwuL3YW4VITxwR7KCVpzwgy4Rbj829KSSQmwXQ==

"@types/node-fetch@^2.6.4":
version "2.6.8"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.8.tgz#9a2993583975849c2e1f360b6ca2f11755b2c504"
integrity sha512-nnH5lV9QCMPsbEVdTb5Y+F3GQxLSw1xQgIydrb2gSfEavRPs50FnMr+KUaa+LoPSqibm2N+ZZxH7lavZlAT4GA==
dependencies:
"@types/node" "*"
form-data "^4.0.0"

"@types/node@*", "@types/node@^20.4.9":
version "20.8.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.8.10.tgz#a5448b895c753ae929c26ce85cab557c6d4a365e"
Expand Down

0 comments on commit 4decef6

Please sign in to comment.