-
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.
chore: show authorized git services based on existed OAuth token secr…
…ets (#986) * chore: show authorized git services based on existed OAuth token secrets Signed-off-by: Anatolii Bazko <[email protected]> * Add test Signed-off-by: Anatolii Bazko <[email protected]> * fix formatting Signed-off-by: Anatolii Bazko <[email protected]> --------- Signed-off-by: Anatolii Bazko <[email protected]>
- Loading branch information
Showing
2 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/index.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,95 @@ | ||
/* | ||
* Copyright (c) 2018-2023 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 { MockStoreEnhanced } from 'redux-mock-store'; | ||
import { ThunkDispatch } from 'redux-thunk'; | ||
|
||
import { AppState } from '@/store'; | ||
import { FakeStoreBuilder } from '@/store/__mocks__/storeBuilder'; | ||
import { IGitOauth } from '@/store/GitOauthConfig/types'; | ||
|
||
import * as TestStore from '..'; | ||
|
||
const gitOauth = [ | ||
{ | ||
name: 'github', | ||
endpointUrl: 'https://github.com', | ||
}, | ||
{ | ||
name: 'gitlab', | ||
endpointUrl: 'https://gitlab.com', | ||
}, | ||
{ | ||
name: 'azure-devops', | ||
endpointUrl: 'https://dev.azure.com', | ||
}, | ||
] as IGitOauth[]; | ||
|
||
const mockGetOAuthProviders = jest.fn().mockResolvedValue(gitOauth); | ||
const mockGetDevWorkspacePreferences = jest.fn().mockResolvedValue({}); | ||
const mockGetOAuthToken = jest.fn().mockImplementation(provider => { | ||
if (provider === 'github') { | ||
return new Promise(resolve => resolve('github-token')); | ||
} | ||
return new Promise((_resolve, reject) => reject(new Error('Token not found'))); | ||
}); | ||
const mockFetchTokens = jest.fn().mockResolvedValue([ | ||
{ | ||
tokenName: 'github-personal-access-token', | ||
gitProvider: 'oauth2-token', | ||
gitProviderEndpoint: 'https://dev.azure.com/', | ||
}, | ||
] as any[]); | ||
|
||
jest.mock('../../../services/backend-client/oAuthApi', () => { | ||
return { | ||
getOAuthProviders: (...args: unknown[]) => mockGetOAuthProviders(...args), | ||
getOAuthToken: (...args: unknown[]) => mockGetOAuthToken(...args), | ||
getDevWorkspacePreferences: (...args: unknown[]) => mockGetDevWorkspacePreferences(...args), | ||
}; | ||
}); | ||
jest.mock('../../../services/backend-client/personalAccessTokenApi', () => { | ||
return { | ||
fetchTokens: (...args: unknown[]) => mockFetchTokens(...args), | ||
}; | ||
}); | ||
|
||
// mute the outputs | ||
console.error = jest.fn(); | ||
|
||
describe('GitOauthConfig store, actions', () => { | ||
let store: MockStoreEnhanced<AppState, ThunkDispatch<AppState, undefined, TestStore.KnownAction>>; | ||
|
||
beforeEach(() => { | ||
store = new FakeStoreBuilder() | ||
.withInfrastructureNamespace([{ name: 'user-che', attributes: { phase: 'Active' } }]) | ||
.build(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should request GitOauthConfig', async () => { | ||
await store.dispatch(TestStore.actionCreators.requestGitOauthConfig()); | ||
|
||
const actions = store.getActions(); | ||
|
||
const expectedAction: TestStore.KnownAction = { | ||
supportedGitOauth: gitOauth, | ||
providersWithToken: ['github', 'azure-devops'], | ||
type: TestStore.Type.RECEIVE_GIT_OAUTH_PROVIDERS, | ||
}; | ||
|
||
expect(actions).toContainEqual(expectedAction); | ||
}); | ||
}); |
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