-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ID-1150 Hide GitHub If Not Available (#4717)
- Loading branch information
Showing
8 changed files
with
152 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { act } from '@testing-library/react'; | ||
import { userHasAccessToEnterpriseFeature } from 'src/enterprise-features/features'; | ||
import { TerraUserState, userStore } from 'src/libs/state'; | ||
|
||
describe('features', () => { | ||
describe('when the user has access to an enterprise feature', () => { | ||
it('says the user has access to that feature', async () => { | ||
// Arrange | ||
const userEnterpriseFeatures = ['github-account-linking']; | ||
await act(async () => { | ||
userStore.update((state: TerraUserState) => ({ ...state, enterpriseFeatures: userEnterpriseFeatures })); | ||
}); | ||
|
||
// Act | ||
const hasAccess = userHasAccessToEnterpriseFeature('github-account-linking'); | ||
// Assert | ||
expect(hasAccess).toBe(true); | ||
}); | ||
}); | ||
describe('when the user does not have access to an enterprise feature', () => { | ||
it('says the user does not have access to that feature', async () => { | ||
// Arrange | ||
const userEnterpriseFeatures = []; | ||
await act(async () => { | ||
userStore.update((state: TerraUserState) => ({ ...state, enterpriseFeatures: userEnterpriseFeatures })); | ||
}); | ||
|
||
// Act | ||
const hasAccess = userHasAccessToEnterpriseFeature('github-account-linking'); | ||
// Assert | ||
expect(hasAccess).toBe(false); | ||
}); | ||
}); | ||
it('it raises an error if the feature is not found', async () => { | ||
// Arrange | ||
const userEnterpriseFeatures = []; | ||
await act(async () => { | ||
userStore.update((state: TerraUserState) => ({ ...state, enterpriseFeatures: userEnterpriseFeatures })); | ||
}); | ||
|
||
// Act | ||
const throws = () => userHasAccessToEnterpriseFeature('feature-not-found'); | ||
// Assert | ||
expect(throws).toThrow(Error); | ||
}); | ||
}); |
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,17 @@ | ||
import { userStore } from 'src/libs/state'; | ||
|
||
type EnterpriseFeature = { | ||
id: string; | ||
name: string; | ||
}; | ||
|
||
const allFeatures: EnterpriseFeature[] = [{ id: 'github-account-linking', name: 'GitHub Account Linking' }]; | ||
|
||
export const userHasAccessToEnterpriseFeature = (feature: string): boolean => { | ||
const matchingFeature = allFeatures.find((f) => f.id === feature); | ||
if (!matchingFeature) { | ||
throw new Error(`Feature ${feature} not found`); | ||
} | ||
const userEnterpriseFeatures = userStore.get().enterpriseFeatures; | ||
return userEnterpriseFeatures?.includes(matchingFeature.id) ?? false; | ||
}; |
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
65 changes: 65 additions & 0 deletions
65
src/profile/external-identities/ExternalIdentities.test.tsx
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,65 @@ | ||
import { asMockedFn } from '@terra-ui-packages/test-utils'; | ||
import { act, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { getConfig } from 'src/libs/config'; | ||
import { TerraUserState, userStore } from 'src/libs/state'; | ||
import { ExternalIdentities } from 'src/profile/external-identities/ExternalIdentities'; | ||
import { renderWithAppContexts as render } from 'src/testing/test-utils'; | ||
|
||
jest.mock('src/libs/config', () => ({ | ||
...jest.requireActual('src/libs/config'), | ||
getConfig: jest.fn().mockReturnValue({}), | ||
})); | ||
|
||
jest.mock('src/profile/external-identities/OAuth2Link', () => ({ | ||
...jest.requireActual('src/profile/external-identities/OAuth2Link'), | ||
OAuth2Link: jest.fn((props) => <div>{props.provider.name}</div>), | ||
})); | ||
|
||
jest.mock('src/profile/external-identities/OAuth2Link', () => ({ | ||
...jest.requireActual('src/profile/external-identities/OAuth2Link'), | ||
OAuth2Link: jest.fn((props) => <div>{props.provider.name}</div>), | ||
})); | ||
|
||
jest.mock('src/profile/external-identities/FenceAccount', () => ({ | ||
...jest.requireActual('src/profile/external-identities/FenceAccount'), | ||
FenceAccount: jest.fn((props) => <div>{props.provider.name}</div>), | ||
})); | ||
|
||
jest.mock('src/profile/external-identities/NihAccount', () => ({ | ||
...jest.requireActual('src/profile/external-identities/NihAccount'), | ||
NihAccount: jest.fn(() => <div>Nih Account</div>), | ||
})); | ||
describe('ExternalIdentities', () => { | ||
beforeEach(() => | ||
asMockedFn(getConfig).mockReturnValue({ externalCreds: { providers: ['github'], urlRoot: 'https/foo.bar.com' } }) | ||
); | ||
describe('when the user has access to GitHub Account Linking', () => { | ||
it('shows the GitHub Account Linking card', async () => { | ||
// Arrange | ||
await act(async () => { | ||
userStore.update((state: TerraUserState) => ({ ...state, enterpriseFeatures: ['github-account-linking'] })); | ||
}); | ||
|
||
// Act | ||
render(<ExternalIdentities queryParams={{}} />); | ||
|
||
// Assert | ||
screen.getByText('GitHub'); | ||
}); | ||
}); | ||
describe('when the user does not have access to GitHub Account Linking', () => { | ||
it('hides the GitHub Account Linking card', async () => { | ||
// Arrange | ||
await act(async () => { | ||
userStore.update((state: TerraUserState) => ({ ...state, enterpriseFeatures: [] })); | ||
}); | ||
|
||
// Act | ||
render(<ExternalIdentities queryParams={{}} />); | ||
|
||
// Assert | ||
expect(screen.queryByText('GitHub')).toBeNull(); | ||
}); | ||
}); | ||
}); |
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