Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task(settings,react): Add feature flag component that supports list of oauth client ids #15637

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/fxa-content-server/server/lib/beta-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ const settingsConfig = {
isPromptNoneEnabledClientIds: config.get(
'oauth.prompt_none.enabled_client_ids'
),
reactClientIdsEnabled: config.get(
'oauth.react_feature_flags.enabled_client_ids'
),
},
recoveryCodes: {
count: config.get('recovery_codes.count'),
Expand Down
9 changes: 9 additions & 0 deletions packages/fxa-content-server/server/lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,15 @@ const conf = (module.exports = convict({
format: Array,
},
},
react_feature_flags: {
enabled_client_ids: {
// 123done enabled for functional tests, 321done is not.
default: ['dcdb5ae7add825d2', '7f368c6886429f19'],
doc: 'client_ids for which feature flags in react are supported',
env: 'OAUTH_REACT_FEATURE_FLAGS_ENABLED_CLIENT_IDS',
format: Array,
},
},
},
openid_configuration: {
claims_supported: ['aud', 'exp', 'iat', 'iss', 'sub'],
Expand Down
45 changes: 45 additions & 0 deletions packages/fxa-react/components/FeatureFlag/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import React from 'react';
import { render, screen } from '@testing-library/react';
import { FeatureFlag } from '.';

describe('Feature Flag', () => {
const enabled = (feature: string) => {
return feature === 'foo';
};

it('enables', async () => {
render(
<FeatureFlag
{...{
feature: 'foo',
enabled,
}}
>
<h1>foo</h1>
<p>bar</p>
</FeatureFlag>
);

expect(screen.queryByText('foo')).toBeDefined();
expect(screen.queryByText('bar')).toBeDefined();
});

it('disables', async () => {
<FeatureFlag
{...{
feature: 'bar',
enabled,
}}
>
<h1>foo</h1>
<p>bar</p>
</FeatureFlag>;

expect(screen.queryByText('foo')).toBeNull();
expect(screen.queryByText('bar')).toBeNull();
});
});
17 changes: 17 additions & 0 deletions packages/fxa-react/components/FeatureFlag/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import React, { ReactElement } from 'react';

export const FeatureFlag = ({
feature,
enabled,
children,
}: {
feature: string;
enabled: (feature: string) => boolean;
children: ReactElement | ReactElement[];
}) => {
return enabled(feature) ? <>{children}</> : <></>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import React from 'react';
import { screen, render } from '@testing-library/react';
import { OAuthClientFeatureFlag } from '.';

jest.mock('../../lib/config', () => ({
oauth: {
reactClientIdsEnabled: ['foo123'],
},
}));

describe('OAuthClientFeatureFlag component', () => {
it('shows for a supported clientId', () => {
render(
<OAuthClientFeatureFlag
{...{
clientId: 'foo123',
}}
>
<h1>foo</h1>
</OAuthClientFeatureFlag>
);
expect(screen.queryByText('foo')).toBeDefined();
});

it('hides for an unsupported clientId', () => {
render(
<OAuthClientFeatureFlag
{...{
clientId: 'bar123',
}}
>
<h1>foo</h1>
</OAuthClientFeatureFlag>
);
expect(screen.queryByText('foo')).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import config from '../../lib/config';
import { ReactElement } from 'react-markdown/lib/react-markdown';
import { FeatureFlag } from 'fxa-react/components/FeatureFlag';

export type OAuthClientFeatureFlagConfig = {
oauth: {
reactClientIdsEnabled: string[];
};
};

export const OAuthClientFeatureFlag = ({
clientId,
children,
}: {
clientId: string;
children: ReactElement | ReactElement[];
}) => {
return (
<FeatureFlag
{...{
feature: clientId,
enabled: (feature: string) => {
return config.oauth.reactClientIdsEnabled.some(
(id: string) => id === feature
);
},
}}
>
{children}
</FeatureFlag>
);
};
2 changes: 2 additions & 0 deletions packages/fxa-settings/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface Config {
scopedKeysValidation: Record<string, any>;
isPromptNoneEnabled: boolean;
isPromptNoneEnabledClientIds: string[];
reactClientIdsEnabled: string[];
};
recoveryCodes: {
count: number;
Expand Down Expand Up @@ -104,6 +105,7 @@ export function getDefault() {
scopedKeysEnabled: false,
isPromptNoneEnabled: false,
isPromptNoneEnabledClientIds: new Array<string>(),
reactClientIdsEnabled: new Array<string>(),
},
recoveryCodes: {
count: 8,
Expand Down