test(FrontendConfig): Move getAuthProviders to own test

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-05-22 17:21:23 +02:00
parent e7a6472059
commit f3cf4f4b1f

View file

@ -61,348 +61,375 @@ describe('FrontendConfigService', () => {
saml: [], saml: [],
oauth2: [], oauth2: [],
}; };
const facebook: AuthConfig['facebook'] = {
clientID: 'facebookTestId', describe('getAuthProviders', () => {
clientSecret: 'facebookTestSecret', const facebook: AuthConfig['facebook'] = {
}; clientID: 'facebookTestId',
const twitter: AuthConfig['twitter'] = { clientSecret: 'facebookTestSecret',
consumerKey: 'twitterTestId', };
consumerSecret: 'twitterTestSecret', const twitter: AuthConfig['twitter'] = {
}; consumerKey: 'twitterTestId',
const github: AuthConfig['github'] = { consumerSecret: 'twitterTestSecret',
clientID: 'githubTestId', };
clientSecret: 'githubTestSecret', const github: AuthConfig['github'] = {
}; clientID: 'githubTestId',
const dropbox: AuthConfig['dropbox'] = { clientSecret: 'githubTestSecret',
clientID: 'dropboxTestId', };
clientSecret: 'dropboxTestSecret', const dropbox: AuthConfig['dropbox'] = {
appKey: 'dropboxTestKey', clientID: 'dropboxTestId',
}; clientSecret: 'dropboxTestSecret',
const google: AuthConfig['google'] = { appKey: 'dropboxTestKey',
clientID: 'googleTestId', };
clientSecret: 'googleTestSecret', const google: AuthConfig['google'] = {
apiKey: 'googleTestKey', clientID: 'googleTestId',
}; clientSecret: 'googleTestSecret',
const gitlab: AuthConfig['gitlab'] = [ apiKey: 'googleTestKey',
{ };
identifier: 'gitlabTestIdentifier', const gitlab: AuthConfig['gitlab'] = [
providerName: 'gitlabTestName', {
baseURL: 'gitlabTestUrl', identifier: 'gitlabTestIdentifier',
clientID: 'gitlabTestId', providerName: 'gitlabTestName',
clientSecret: 'gitlabTestSecret', baseURL: 'gitlabTestUrl',
scope: GitlabScope.API, clientID: 'gitlabTestId',
version: GitlabVersion.V4, clientSecret: 'gitlabTestSecret',
}, scope: GitlabScope.API,
]; version: GitlabVersion.V4,
const ldap: AuthConfig['ldap'] = [
{
identifier: 'ldapTestIdentifier',
providerName: 'ldapTestName',
url: 'ldapTestUrl',
bindDn: 'ldapTestBindDn',
bindCredentials: 'ldapTestBindCredentials',
searchBase: 'ldapTestSearchBase',
searchFilter: 'ldapTestSearchFilter',
searchAttributes: ['ldapTestSearchAttribute'],
userIdField: 'ldapTestUserId',
displayNameField: 'ldapTestDisplayName',
profilePictureField: 'ldapTestProfilePicture',
tlsCaCerts: ['ldapTestTlsCa'],
},
];
const saml: AuthConfig['saml'] = [
{
identifier: 'samlTestIdentifier',
providerName: 'samlTestName',
idpSsoUrl: 'samlTestUrl',
idpCert: 'samlTestCert',
clientCert: 'samlTestClientCert',
issuer: 'samlTestIssuer',
identifierFormat: 'samlTestUrl',
disableRequestedAuthnContext: 'samlTestUrl',
groupAttribute: 'samlTestUrl',
requiredGroups: ['samlTestUrl'],
externalGroups: ['samlTestUrl'],
attribute: {
id: 'samlTestUrl',
username: 'samlTestUrl',
email: 'samlTestUrl',
}, },
}, ];
]; const ldap: AuthConfig['ldap'] = [
const oauth2: AuthConfig['oauth2'] = [ {
{ identifier: 'ldapTestIdentifier',
identifier: 'oauth2Testidentifier', providerName: 'ldapTestName',
providerName: 'oauth2TestName', url: 'ldapTestUrl',
baseURL: 'oauth2TestUrl', bindDn: 'ldapTestBindDn',
userProfileURL: 'oauth2TestProfileUrl', bindCredentials: 'ldapTestBindCredentials',
userProfileIdAttr: 'oauth2TestProfileId', searchBase: 'ldapTestSearchBase',
userProfileUsernameAttr: 'oauth2TestProfileUsername', searchFilter: 'ldapTestSearchFilter',
userProfileDisplayNameAttr: 'oauth2TestProfileDisplay', searchAttributes: ['ldapTestSearchAttribute'],
userProfileEmailAttr: 'oauth2TestProfileEmail', userIdField: 'ldapTestUserId',
tokenURL: 'oauth2TestTokenUrl', displayNameField: 'ldapTestDisplayName',
authorizationURL: 'oauth2TestAuthUrl', profilePictureField: 'ldapTestProfilePicture',
clientID: 'oauth2TestId', tlsCaCerts: ['ldapTestTlsCa'],
clientSecret: 'oauth2TestSecret', },
scope: 'oauth2TestScope', ];
rolesClaim: 'oauth2TestRoles', const saml: AuthConfig['saml'] = [
accessRole: 'oauth2TestAccess', {
}, identifier: 'samlTestIdentifier',
]; providerName: 'samlTestName',
idpSsoUrl: 'samlTestUrl',
idpCert: 'samlTestCert',
clientCert: 'samlTestClientCert',
issuer: 'samlTestIssuer',
identifierFormat: 'samlTestUrl',
disableRequestedAuthnContext: 'samlTestUrl',
groupAttribute: 'samlTestUrl',
requiredGroups: ['samlTestUrl'],
externalGroups: ['samlTestUrl'],
attribute: {
id: 'samlTestUrl',
username: 'samlTestUrl',
email: 'samlTestUrl',
},
},
];
const oauth2: AuthConfig['oauth2'] = [
{
identifier: 'oauth2Testidentifier',
providerName: 'oauth2TestName',
baseURL: 'oauth2TestUrl',
userProfileURL: 'oauth2TestProfileUrl',
userProfileIdAttr: 'oauth2TestProfileId',
userProfileUsernameAttr: 'oauth2TestProfileUsername',
userProfileDisplayNameAttr: 'oauth2TestProfileDisplay',
userProfileEmailAttr: 'oauth2TestProfileEmail',
tokenURL: 'oauth2TestTokenUrl',
authorizationURL: 'oauth2TestAuthUrl',
clientID: 'oauth2TestId',
clientSecret: 'oauth2TestSecret',
scope: 'oauth2TestScope',
rolesClaim: 'oauth2TestRoles',
accessRole: 'oauth2TestAccess',
},
];
for (const authConfigConfigured of [
facebook,
twitter,
github,
dropbox,
google,
gitlab,
ldap,
saml,
oauth2,
]) {
it(`works with ${JSON.stringify(authConfigConfigured)}`, async () => {
const appConfig: AppConfig = {
domain: domain,
rendererOrigin: domain,
port: 3000,
loglevel: Loglevel.ERROR,
};
const authConfig: AuthConfig = {
...emptyAuthConfig,
...authConfigConfigured,
};
const module: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [
registerAs('appConfig', () => appConfig),
registerAs('authConfig', () => authConfig),
registerAs('customizationConfig', () => {
return { branding: {}, specialUrls: {} };
}),
registerAs('externalServicesConfig', () => {
return {};
}),
registerAs('noteConfig', () => {
return {
forbiddenNoteIds: [],
maxDocumentLength: 200,
};
}),
],
}),
LoggerModule,
],
providers: [FrontendConfigService],
}).compile();
const service = module.get(FrontendConfigService);
const config = await service.getFrontendConfig();
if (authConfig.dropbox.clientID) {
expect(config.authProviders).toContainEqual({
type: AuthProviderType.DROPBOX,
});
}
if (authConfig.facebook.clientID) {
expect(config.authProviders).toContainEqual({
type: AuthProviderType.FACEBOOK,
});
}
if (authConfig.google.clientID) {
expect(config.authProviders).toContainEqual({
type: AuthProviderType.GOOGLE,
});
}
if (authConfig.github.clientID) {
expect(config.authProviders).toContainEqual({
type: AuthProviderType.GITHUB,
});
}
if (authConfig.local.enableLogin) {
expect(config.authProviders).toContainEqual({
type: AuthProviderType.LOCAL,
});
}
if (authConfig.twitter.consumerKey) {
expect(config.authProviders).toContainEqual({
type: AuthProviderType.TWITTER,
});
}
expect(
config.authProviders.filter(
(provider) => provider.type === AuthProviderType.GITLAB,
).length,
).toEqual(authConfig.gitlab.length);
expect(
config.authProviders.filter(
(provider) => provider.type === AuthProviderType.LDAP,
).length,
).toEqual(authConfig.ldap.length);
expect(
config.authProviders.filter(
(provider) => provider.type === AuthProviderType.SAML,
).length,
).toEqual(authConfig.saml.length);
expect(
config.authProviders.filter(
(provider) => provider.type === AuthProviderType.OAUTH2,
).length,
).toEqual(authConfig.oauth2.length);
if (authConfig.gitlab.length > 0) {
expect(
config.authProviders.find(
(provider) => provider.type === AuthProviderType.GITLAB,
),
).toEqual({
type: AuthProviderType.GITLAB,
providerName: authConfig.gitlab[0].providerName,
identifier: authConfig.gitlab[0].identifier,
});
}
if (authConfig.ldap.length > 0) {
expect(
config.authProviders.find(
(provider) => provider.type === AuthProviderType.LDAP,
),
).toEqual({
type: AuthProviderType.LDAP,
providerName: authConfig.ldap[0].providerName,
identifier: authConfig.ldap[0].identifier,
});
}
if (authConfig.saml.length > 0) {
expect(
config.authProviders.find(
(provider) => provider.type === AuthProviderType.SAML,
),
).toEqual({
type: AuthProviderType.SAML,
providerName: authConfig.saml[0].providerName,
identifier: authConfig.saml[0].identifier,
});
}
if (authConfig.oauth2.length > 0) {
expect(
config.authProviders.find(
(provider) => provider.type === AuthProviderType.OAUTH2,
),
).toEqual({
type: AuthProviderType.OAUTH2,
providerName: authConfig.oauth2[0].providerName,
identifier: authConfig.oauth2[0].identifier,
});
}
});
}
});
let index = 1; let index = 1;
for (const renderOrigin of [undefined, 'http://md-renderer.example.com']) { for (const renderOrigin of [undefined, 'http://md-renderer.example.com']) {
for (const maxDocumentLength of [100000, 900]) { for (const maxDocumentLength of [100000, 900]) {
for (const enableLogin of [true, false]) { for (const enableLogin of [true, false]) {
for (const enableRegister of [true, false]) { for (const enableRegister of [true, false]) {
for (const authConfigConfigured of [ for (const customName of [undefined, 'Test Branding Name']) {
facebook, for (const customLogo of [
twitter, undefined,
github, 'https://example.com/logo.png',
dropbox, ]) {
google, for (const privacyLink of [
gitlab,
ldap,
saml,
oauth2,
]) {
for (const customName of [undefined, 'Test Branding Name']) {
for (const customLogo of [
undefined, undefined,
'https://example.com/logo.png', 'https://example.com/privacy',
]) { ]) {
for (const privacyLink of [ for (const termsOfUseLink of [
undefined, undefined,
'https://example.com/privacy', 'https://example.com/terms',
]) { ]) {
for (const termsOfUseLink of [ for (const imprintLink of [
undefined, undefined,
'https://example.com/terms', 'https://example.com/imprint',
]) { ]) {
for (const imprintLink of [ for (const plantUmlServer of [
undefined, undefined,
'https://example.com/imprint', 'https://plantuml.example.com',
]) { ]) {
for (const plantUmlServer of [ for (const imageProxy of [
undefined, undefined,
'https://plantuml.example.com', 'https://imageProxy.example.com',
]) { ]) {
for (const imageProxy of [ it(`combination #${index} works`, async () => {
undefined, const appConfig: AppConfig = {
'https://imageProxy.example.com', domain: domain,
]) { rendererOrigin: renderOrigin ?? domain,
it(`combination #${index} works`, async () => { port: 3000,
const appConfig: AppConfig = { loglevel: Loglevel.ERROR,
domain: domain, };
rendererOrigin: renderOrigin ?? domain, const authConfig: AuthConfig = {
port: 3000, ...emptyAuthConfig,
loglevel: Loglevel.ERROR, local: {
}; enableLogin,
const authConfig: AuthConfig = {
...emptyAuthConfig,
local: {
enableLogin,
enableRegister,
},
...authConfigConfigured,
};
const customizationConfig: CustomizationConfig = {
branding: {
customName: customName,
customLogo: customLogo,
},
specialUrls: {
privacy: privacyLink,
termsOfUse: termsOfUseLink,
imprint: imprintLink,
},
};
const externalServicesConfig: ExternalServicesConfig =
{
plantUmlServer: plantUmlServer,
imageProxy: imageProxy,
};
const noteConfig: NoteConfig = {
forbiddenNoteIds: [],
maxDocumentLength: maxDocumentLength,
};
const module: TestingModule =
await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [
registerAs('appConfig', () => appConfig),
registerAs(
'authConfig',
() => authConfig,
),
registerAs(
'customizationConfig',
() => customizationConfig,
),
registerAs(
'externalServicesConfig',
() => externalServicesConfig,
),
registerAs(
'noteConfig',
() => noteConfig,
),
],
}),
LoggerModule,
],
providers: [FrontendConfigService],
}).compile();
const service = module.get(FrontendConfigService);
const config = await service.getFrontendConfig();
expect(config.allowRegister).toEqual(
enableRegister, enableRegister,
); },
if (authConfig.dropbox.clientID) { };
expect(config.authProviders).toContainEqual({ const customizationConfig: CustomizationConfig = {
type: AuthProviderType.DROPBOX, branding: {
}); customName: customName,
} customLogo: customLogo,
if (authConfig.facebook.clientID) { },
expect(config.authProviders).toContainEqual({ specialUrls: {
type: AuthProviderType.FACEBOOK, privacy: privacyLink,
}); termsOfUse: termsOfUseLink,
} imprint: imprintLink,
if (authConfig.google.clientID) { },
expect(config.authProviders).toContainEqual({ };
type: AuthProviderType.GOOGLE, const externalServicesConfig: ExternalServicesConfig =
}); {
} plantUmlServer: plantUmlServer,
if (authConfig.github.clientID) { imageProxy: imageProxy,
expect(config.authProviders).toContainEqual({ };
type: AuthProviderType.GITHUB, const noteConfig: NoteConfig = {
}); forbiddenNoteIds: [],
} maxDocumentLength: maxDocumentLength,
if (authConfig.local.enableLogin) { };
expect(config.authProviders).toContainEqual({ const module: TestingModule =
type: AuthProviderType.LOCAL, await Test.createTestingModule({
}); imports: [
} ConfigModule.forRoot({
if (authConfig.twitter.consumerKey) { isGlobal: true,
expect(config.authProviders).toContainEqual({ load: [
type: AuthProviderType.TWITTER, registerAs('appConfig', () => appConfig),
}); registerAs('authConfig', () => authConfig),
} registerAs(
expect( 'customizationConfig',
config.authProviders.filter( () => customizationConfig,
(provider) => ),
provider.type === AuthProviderType.GITLAB, registerAs(
).length, 'externalServicesConfig',
).toEqual(authConfig.gitlab.length); () => externalServicesConfig,
expect( ),
config.authProviders.filter( registerAs('noteConfig', () => noteConfig),
(provider) => ],
provider.type === AuthProviderType.LDAP, }),
).length, LoggerModule,
).toEqual(authConfig.ldap.length); ],
expect( providers: [FrontendConfigService],
config.authProviders.filter( }).compile();
(provider) =>
provider.type === AuthProviderType.SAML, const service = module.get(FrontendConfigService);
).length, const config = await service.getFrontendConfig();
).toEqual(authConfig.saml.length); expect(config.allowRegister).toEqual(enableRegister);
expect(
config.authProviders.filter( expect(config.allowAnonymous).toEqual(false);
(provider) => expect(config.branding.name).toEqual(customName);
provider.type === AuthProviderType.OAUTH2, expect(config.branding.logo).toEqual(
).length, customLogo ? new URL(customLogo) : undefined,
).toEqual(authConfig.oauth2.length); );
if (authConfig.gitlab.length > 0) { expect(
expect( config.iframeCommunication.editorOrigin,
config.authProviders.find( ).toEqual(new URL(appConfig.domain));
(provider) => expect(
provider.type === AuthProviderType.GITLAB, config.iframeCommunication.rendererOrigin,
), ).toEqual(
).toEqual({ appConfig.rendererOrigin
type: AuthProviderType.GITLAB, ? new URL(appConfig.rendererOrigin)
providerName: authConfig.gitlab[0].providerName, : new URL(appConfig.domain),
identifier: authConfig.gitlab[0].identifier, );
}); expect(config.maxDocumentLength).toEqual(
} maxDocumentLength,
if (authConfig.ldap.length > 0) { );
expect( expect(config.plantUmlServer).toEqual(
config.authProviders.find( plantUmlServer
(provider) => ? new URL(plantUmlServer)
provider.type === AuthProviderType.LDAP, : undefined,
), );
).toEqual({ expect(config.specialUrls.imprint).toEqual(
type: AuthProviderType.LDAP, imprintLink ? new URL(imprintLink) : undefined,
providerName: authConfig.ldap[0].providerName, );
identifier: authConfig.ldap[0].identifier, expect(config.specialUrls.privacy).toEqual(
}); privacyLink ? new URL(privacyLink) : undefined,
} );
if (authConfig.saml.length > 0) { expect(config.specialUrls.termsOfUse).toEqual(
expect( termsOfUseLink
config.authProviders.find( ? new URL(termsOfUseLink)
(provider) => : undefined,
provider.type === AuthProviderType.SAML, );
), expect(config.useImageProxy).toEqual(!!imageProxy);
).toEqual({ expect(config.version).toEqual(
type: AuthProviderType.SAML, await getServerVersionFromPackageJson(),
providerName: authConfig.saml[0].providerName, );
identifier: authConfig.saml[0].identifier, });
}); index += 1;
}
if (authConfig.oauth2.length > 0) {
expect(
config.authProviders.find(
(provider) =>
provider.type === AuthProviderType.OAUTH2,
),
).toEqual({
type: AuthProviderType.OAUTH2,
providerName: authConfig.oauth2[0].providerName,
identifier: authConfig.oauth2[0].identifier,
});
}
expect(config.allowAnonymous).toEqual(false);
expect(config.branding.name).toEqual(customName);
expect(config.branding.logo).toEqual(
customLogo ? new URL(customLogo) : undefined,
);
expect(
config.iframeCommunication.editorOrigin,
).toEqual(new URL(appConfig.domain));
expect(
config.iframeCommunication.rendererOrigin,
).toEqual(
appConfig.rendererOrigin
? new URL(appConfig.rendererOrigin)
: new URL(appConfig.domain),
);
expect(config.maxDocumentLength).toEqual(
maxDocumentLength,
);
expect(config.plantUmlServer).toEqual(
plantUmlServer
? new URL(plantUmlServer)
: undefined,
);
expect(config.specialUrls.imprint).toEqual(
imprintLink ? new URL(imprintLink) : undefined,
);
expect(config.specialUrls.privacy).toEqual(
privacyLink ? new URL(privacyLink) : undefined,
);
expect(config.specialUrls.termsOfUse).toEqual(
termsOfUseLink
? new URL(termsOfUseLink)
: undefined,
);
expect(config.useImageProxy).toEqual(!!imageProxy);
expect(config.version).toEqual(
await getServerVersionFromPackageJson(),
);
});
index += 1;
}
} }
} }
} }