-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependency on /user and /profile API (#657)
- Loading branch information
Showing
38 changed files
with
630 additions
and
633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/dashboard-backend/src/devworkspaceClient/services/__tests__/userProfileApi.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2018-2021 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
import * as mockClient from '@kubernetes/client-node'; | ||
import { CoreV1Api, V1Secret } from '@kubernetes/client-node'; | ||
import { UserProfileApiService } from '../userProfileApi'; | ||
|
||
jest.mock('../../../helpers/getUserName.ts'); | ||
|
||
const userNamespace = 'user1-che'; | ||
|
||
describe('UserProfile API Service', () => { | ||
let userProfileService: UserProfileApiService; | ||
|
||
beforeEach(() => { | ||
const { KubeConfig } = mockClient; | ||
const kubeConfig = new KubeConfig(); | ||
|
||
kubeConfig.makeApiClient = jest.fn().mockImplementation(_api => { | ||
return { | ||
readNamespacedSecret: (_name, _namespace) => { | ||
return Promise.resolve(buildSecret()); | ||
}, | ||
} as CoreV1Api; | ||
}); | ||
|
||
userProfileService = new UserProfileApiService(kubeConfig); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('user profile object', async () => { | ||
const res = await userProfileService.getUserProfile(userNamespace); | ||
expect(res).toEqual({ email: 'user1@che', username: 'user1' }); | ||
}); | ||
}); | ||
|
||
function buildSecret(): { body: V1Secret } { | ||
return { | ||
body: { | ||
apiVersion: 'v1', | ||
data: { | ||
email: 'dXNlcjFAY2hl', | ||
id: 'Q2dFeEVnVnNiMk5oYkE=', | ||
name: 'dXNlcjE=', | ||
}, | ||
kind: 'Secret', | ||
}, | ||
}; | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/dashboard-backend/src/devworkspaceClient/services/userProfileApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2018-2021 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { api } from '@eclipse-che/common'; | ||
import * as k8s from '@kubernetes/client-node'; | ||
import { IUserProfileApi } from '../types'; | ||
import { createError } from './helpers/createError'; | ||
|
||
const ERROR_LABEL = 'CORE_V1_API_ERROR'; | ||
const USER_PROFILE_SECRET_NAME = 'user-profile'; | ||
|
||
export class UserProfileApiService implements IUserProfileApi { | ||
private readonly coreV1API: k8s.CoreV1Api; | ||
|
||
constructor(kc: k8s.KubeConfig) { | ||
this.coreV1API = kc.makeApiClient(k8s.CoreV1Api); | ||
} | ||
|
||
async getUserProfile(namespace: string): Promise<api.IUserProfile | undefined> { | ||
try { | ||
const result = await this.coreV1API.readNamespacedSecret(USER_PROFILE_SECRET_NAME, namespace); | ||
const data = result.body.data; | ||
if (data === undefined) { | ||
throw new Error('Data is empty'); | ||
} | ||
return { | ||
username: Buffer.from(data.name, 'base64').toString(), | ||
email: Buffer.from(data.email, 'base64').toString(), | ||
}; | ||
} catch (e) { | ||
console.error('Unable to get user profile data:', e); | ||
throw createError(e, ERROR_LABEL, 'Unable to get user profile data'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/dashboard-backend/src/routes/api/__tests__/userProfile.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2018-2021 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { FastifyInstance } from 'fastify'; | ||
import { baseApiPath } from '../../../constants/config'; | ||
import { setup, teardown } from '../../../helpers/tests/appBuilder'; | ||
import { stubUserProfile } from '../helpers/__mocks__/getDevWorkspaceClient'; | ||
|
||
jest.mock('../helpers/getDevWorkspaceClient.ts'); | ||
jest.mock('../helpers/getToken.ts'); | ||
jest.mock('../helpers/getServiceAccountToken.ts'); | ||
|
||
describe('UserProfile Route', () => { | ||
let app: FastifyInstance; | ||
const namespace = 'user-che'; | ||
|
||
beforeEach(async () => { | ||
app = await setup(); | ||
}); | ||
|
||
afterEach(() => { | ||
teardown(app); | ||
}); | ||
|
||
test('GET ${baseApiPath}/namespaces', async () => { | ||
const res = await app.inject().get(`${baseApiPath}/userprofile/${namespace}`); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.json()).toEqual(stubUserProfile); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2018-2021 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { FastifyInstance, FastifyRequest } from 'fastify'; | ||
import { baseApiPath } from '../../constants/config'; | ||
import { namespacedSchema } from '../../constants/schemas'; | ||
import { getSchema } from '../../services/helpers'; | ||
import { restParams } from '../../typings/models'; | ||
import { getDevWorkspaceClient } from './helpers/getDevWorkspaceClient'; | ||
import { getToken } from './helpers/getToken'; | ||
|
||
const tags = ['UserProfile']; | ||
|
||
export function registerUserProfileRoute(server: FastifyInstance) { | ||
server.get( | ||
`${baseApiPath}/userprofile/:namespace`, | ||
getSchema({ tags, params: namespacedSchema }), | ||
async function (request: FastifyRequest) { | ||
const { namespace } = request.params as restParams.INamespacedParam; | ||
const token = getToken(request); | ||
const { userProfileApi } = getDevWorkspaceClient(token); | ||
return userProfileApi.getUserProfile(namespace); | ||
}, | ||
); | ||
} |
Oops, something went wrong.