packages/core: add tests for google auth scope normalization

This commit is contained in:
Patrik Oldsberg
2020-05-20 17:19:04 +02:00
parent 31202a1e95
commit a928e952cd
2 changed files with 29 additions and 2 deletions
@@ -105,4 +105,31 @@ describe('GoogleAuth', () => {
await expect(promise3).resolves.toBe('token2');
expect(getSession).toBeCalledTimes(4); // De-duping of session requests happens in client
});
it.each([
['email', [`${PREFIX}userinfo.email`]],
['profile', [`${PREFIX}userinfo.profile`]],
['openid', ['openid']],
['userinfo.email', [`${PREFIX}userinfo.email`]],
[
'userinfo.profile email',
[`${PREFIX}userinfo.profile`, `${PREFIX}userinfo.email`],
],
[
`profile ${PREFIX}userinfo.email`,
[`${PREFIX}userinfo.profile`, `${PREFIX}userinfo.email`],
],
[`${PREFIX}userinfo.profile`, [`${PREFIX}userinfo.profile`]],
['a', [`${PREFIX}a`]],
['a b\tc', [`${PREFIX}a`, `${PREFIX}b`, `${PREFIX}c`]],
[`${PREFIX}a b`, [`${PREFIX}a`, `${PREFIX}b`]],
[`${PREFIX}a`, [`${PREFIX}a`]],
// Some incorrect scopes that we don't try to fix
[`${PREFIX}email`, [`${PREFIX}email`]],
[`${PREFIX}profile`, [`${PREFIX}profile`]],
[`${PREFIX}openid`, [`${PREFIX}openid`]],
])(`should normalize scopes correctly - %p`, (scope, scopes) => {
expect(GoogleAuth.normalizeScopes(scope)).toEqual(new Set(scopes));
});
});
@@ -97,14 +97,14 @@ class GoogleAuth implements OAuthApi, OpenIdConnectApi {
await this.sessionManager.removeSession();
}
private static normalizeScopes(scopes?: string | string[]): Set<string> {
static normalizeScopes(scopes?: string | string[]): Set<string> {
if (!scopes) {
return new Set();
}
const scopeList = Array.isArray(scopes)
? scopes
: scopes.split(' ').filter(Boolean);
: scopes.split(/[\s]/).filter(Boolean);
const normalizedScopes = scopeList.map(scope => {
if (scope === 'openid') {