Allow custom label for auth provider (#15673)

* Allow to pass custom label for auth provider

* Add tests

* Update api/src/cli/utils/create-env/env-stub.liquid

* Fix tests

Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>
This commit is contained in:
José Varela
2022-10-15 04:14:00 +01:00
committed by GitHub
parent ef895fb784
commit da2998a7e7
6 changed files with 91 additions and 2 deletions

View File

@@ -0,0 +1,83 @@
let factoryEnv: { [k: string]: any } = {};
jest.mock(
'../../src/env',
() =>
new Proxy(
{},
{
get(target, prop) {
return factoryEnv[prop as string];
},
}
)
);
import { getAuthProviders } from '../../src/utils/get-auth-providers';
const scenarios = [
{
name: 'when no providers configured',
input: {},
output: [],
},
{
name: 'when no driver configured',
input: {
AUTH_PROVIDERS: 'directus',
},
output: [],
},
{
name: 'when single provider and driver are properly configured',
input: {
AUTH_PROVIDERS: 'directus',
AUTH_DIRECTUS_DRIVER: 'openid',
AUTH_DIRECTUS_LABEL: 'Directus',
AUTH_DIRECTUS_ICON: 'hare',
},
output: [
{
name: 'directus',
driver: 'openid',
label: 'Directus',
icon: 'hare',
},
],
},
{
name: 'when multiple provider and driver are properly configured',
input: {
AUTH_PROVIDERS: 'directus,custom',
AUTH_DIRECTUS_DRIVER: 'openid',
AUTH_DIRECTUS_LABEL: 'Directus',
AUTH_DIRECTUS_ICON: 'hare',
AUTH_CUSTOM_DRIVER: 'openid',
AUTH_CUSTOM_ICON: 'lock',
},
output: [
{
name: 'directus',
driver: 'openid',
label: 'Directus',
icon: 'hare',
},
{
name: 'custom',
driver: 'openid',
icon: 'lock',
},
],
},
];
describe('get auth providers', () => {
for (const scenario of scenarios) {
test(scenario.name, () => {
factoryEnv = scenario.input;
expect(getAuthProviders()).toEqual(scenario.output);
});
}
});

View File

@@ -2,6 +2,7 @@ import { toArray } from '@directus/shared/utils';
import env from '../env';
interface AuthProvider {
label: string;
name: string;
driver: string;
icon?: string;
@@ -12,6 +13,7 @@ export function getAuthProviders(): AuthProvider[] {
.filter((provider) => provider && env[`AUTH_${provider.toUpperCase()}_DRIVER`])
.map((provider) => ({
name: provider,
label: env[`AUTH_${provider.toUpperCase()}_LABEL`],
driver: env[`AUTH_${provider.toUpperCase()}_DRIVER`],
icon: env[`AUTH_${provider.toUpperCase()}_ICON`],
}));