generated from redhat-developer/new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Denis Golovin <[email protected]>
- Loading branch information
Showing
5 changed files
with
264 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { beforeEach, Mock, test, vi } from 'vitest'; | ||
import * as podmanDesktopApi from '@podman-desktop/api'; | ||
import * as extension from './extension'; | ||
import { URI } from 'vscode-uri'; | ||
|
||
vi.mock('@podman-desktop/api', async () => { | ||
return { | ||
provider: { | ||
createProvider: vi.fn(), | ||
onDidUpdateContainerConnection: vi.fn(), | ||
setKubernetesProviderConnectionFactory: vi.fn(), | ||
}, | ||
|
||
containerEngine: { | ||
listContainers: vi.fn(), | ||
}, | ||
|
||
kubernetes: { | ||
getKubeconfig: vi.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
const getKubeconfigMock = podmanDesktopApi.kubernetes.getKubeconfig as unknown as Mock<any, any>; | ||
getKubeconfigMock.mockReturnValue(URI.parse('file:///usr/home/test')); | ||
|
||
const context: podmanDesktopApi.ExtensionContext = { | ||
subscriptions: [], | ||
storagePath: '', | ||
}; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
test('check we received notifications ', async () => { | ||
const setKubernetesProviderConnectionFactoryMock = vi.fn(); | ||
(podmanDesktopApi.provider as any).setKubernetesProviderConnectionFactory = | ||
setKubernetesProviderConnectionFactoryMock; | ||
await extension.activate(context); | ||
}); |
Empty file.
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,148 @@ | ||
/*----------------------------------------------------------------------------------------------- | ||
* Copyright (c) Red Hat, Inc. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
*-----------------------------------------------------------------------------------------------*/ | ||
|
||
import fetch = require('make-fetch-happen'); | ||
|
||
// eslint-disable-next-line no-shadow | ||
export enum SBAPIEndpoint { | ||
SIGNUP = '/api/v1/signup', | ||
VERIFICATION = '/api/v1/signup/verification', | ||
} | ||
|
||
export interface SBStatus { | ||
ready: boolean; | ||
reason: 'Provisioned' | 'PendingApproval'; | ||
verificationRequired: boolean; | ||
} | ||
|
||
export interface SBSignupResponse { | ||
apiEndpoint: string; | ||
cheDashboardURL: string; | ||
clusterName: string; | ||
company: string; | ||
compliantUsername: string; | ||
consoleURL: string; | ||
familyName: string; | ||
givenName: string; | ||
status: SBStatus; | ||
username: string; | ||
} | ||
|
||
export interface SBResponseData { | ||
status: string; | ||
code: number; | ||
message: string; | ||
details: string; | ||
} | ||
|
||
export interface VerificationCodeResponse { | ||
ok: boolean; | ||
json: SBResponseData; | ||
} | ||
|
||
export const OAUTH_SERVER_INFO_PATH = '.well-known/oauth-authorization-server'; | ||
|
||
export interface OauthServerInfo { | ||
issuer: string; | ||
authorization_endpoint: string; | ||
token_endpoint: string; | ||
scopes_supported: string[]; | ||
response_types_supported: string[]; | ||
grant_types_supported: string[]; | ||
code_challenge_methods_supported: string[]; | ||
} | ||
|
||
export function getSandboxAPIUrl(): string { | ||
return ''; | ||
} | ||
|
||
export function getSandboxAPITimeout(): number { | ||
return 5000; | ||
} | ||
|
||
export interface SandboxAPI { | ||
getSignUpStatus(token: string): Promise<SBSignupResponse | undefined>; | ||
signUp(token: string): Promise<boolean>; | ||
requestVerificationCode(token: string, areaCode: string, phoneNumber: string): Promise<VerificationCodeResponse>; | ||
validateVerificationCode(token: string, code: string): Promise<boolean>; | ||
getOauthServerInfo(apiEndpointUrl: string): Promise<OauthServerInfo>; | ||
} | ||
|
||
export async function getSignUpStatus(token: string): Promise<SBSignupResponse | undefined> { | ||
const signupResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.SIGNUP}`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
cache: 'no-cache', | ||
timeout: getSandboxAPITimeout(), | ||
}); | ||
return signupResponse.ok ? (signupResponse.json() as Promise<SBSignupResponse>) : undefined; | ||
} | ||
|
||
export async function signUp(token: string): Promise<boolean> { | ||
const signupResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.SIGNUP}`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
timeout: getSandboxAPITimeout(), | ||
}); | ||
return signupResponse.ok; | ||
} | ||
|
||
export async function requestVerificationCode( | ||
token: string, | ||
countryCode: string, | ||
phoneNumber: string, | ||
): Promise<VerificationCodeResponse> { | ||
const verificationCodeRequestResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.VERIFICATION}`, { | ||
method: 'PUT', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
timeout: getSandboxAPITimeout(), | ||
body: JSON.stringify({ | ||
country_code: countryCode, | ||
phone_number: phoneNumber, | ||
}), | ||
}); | ||
const responseText = await verificationCodeRequestResponse.text(); | ||
return { | ||
ok: verificationCodeRequestResponse.ok, | ||
json: (responseText ? JSON.parse(responseText) : {}) as SBResponseData, | ||
}; | ||
} | ||
|
||
export async function validateVerificationCode(token: string, code: string): Promise<boolean> { | ||
const validationRequestResponse = await fetch(`${getSandboxAPIUrl()}${SBAPIEndpoint.VERIFICATION}/${code}`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
timeout: getSandboxAPITimeout(), | ||
}); | ||
|
||
return validationRequestResponse.ok; | ||
} | ||
|
||
export async function getOauthServerInfo(apiEndpointUrl: string): Promise<OauthServerInfo> { | ||
const oauthServerInfoResponse = await fetch(`${apiEndpointUrl}/${OAUTH_SERVER_INFO_PATH}`, { | ||
method: 'GET', | ||
timeout: getSandboxAPITimeout(), | ||
}); | ||
const oauthInfoText = await oauthServerInfoResponse.text(); | ||
return (oauthInfoText ? JSON.parse(oauthInfoText) : {}) as OauthServerInfo; | ||
} | ||
|
||
export function createSandboxAPI(): SandboxAPI { | ||
return { | ||
getSignUpStatus, | ||
signUp, | ||
requestVerificationCode, | ||
validateVerificationCode, | ||
getOauthServerInfo, | ||
}; | ||
} |
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,49 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import path from 'node:path'; | ||
|
||
/** | ||
* Config for global end-to-end tests | ||
* placed in project root tests folder | ||
* @type {import('vite').UserConfig} | ||
* @see https://vitest.dev/config/ | ||
*/ | ||
const config = { | ||
test: { | ||
globals: true, | ||
environment: 'jsdom', | ||
/** | ||
* By default, vitest search test files in all packages. | ||
* For e2e tests have sense search only is project root tests folder | ||
*/ | ||
include: ['**/src/**/*.spec.ts'], | ||
/** | ||
* A default timeout of 5000ms is sometimes not enough for playwright. | ||
*/ | ||
testTimeout: 60_000, | ||
hookTimeout: 60_000, | ||
}, | ||
resolve: { | ||
alias: { | ||
'@podman-desktop/api': path.resolve(__dirname, '__mocks__/@podman-desktop/api.js'), | ||
}, | ||
}, | ||
}; | ||
|
||
export default config; |