Skip to content

Commit

Permalink
feat: improve configuration in strategy test
Browse files Browse the repository at this point in the history
  • Loading branch information
brakmic committed Dec 30, 2024
1 parent e0124a6 commit 2f1cb82
Showing 1 changed file with 71 additions and 59 deletions.
130 changes: 71 additions & 59 deletions test/strategy.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
const { Strategy: KeycloakStrategy } = require('../lib/index.js');

const config = {
baseAuthServerURL: 'http://localhost:8080',
realm: 'myKeycloakRealm',
clientID: 'ABC123',
clientSecret: 'mySecret',
callbackURL: 'http://www.example.com',
sslRequired: 'none',
scopes: ['openid', 'profile', 'email'],
};

const urls = {
authorizationURL: `${config.baseAuthServerURL}/realms/${config.realm}/protocol/openid-connect/auth`,
tokenURL: `${config.baseAuthServerURL}/realms/${config.realm}/protocol/openid-connect/token`,
userProfileURL: `${config.baseAuthServerURL}/realms/${config.realm}/protocol/openid-connect/userinfo`,
};

describe('KeycloakStrategy', () => {
describe('constructed with undefined options', () => {
it('should throw error', () => {
Expand All @@ -12,12 +28,12 @@ describe('KeycloakStrategy', () => {
describe('constructed with publicClient=true', () => {
const strategy = new KeycloakStrategy(
{
realm: 'myKeycloakRealm',
authServerURL: 'http://localhost:8080', // Updated for newer Keycloak
clientID: 'ABC123',
realm: config.realm,
authServerURL: config.baseAuthServerURL,
clientID: config.clientID,
publicClient: true,
callbackURL: 'http://www.example.com',
sslRequired: 'none',
callbackURL: config.callbackURL,
sslRequired: config.sslRequired,
},
function () {}
);
Expand All @@ -26,43 +42,38 @@ describe('KeycloakStrategy', () => {
expect(strategy.name).toBe('keycloak');
});

it('realm should be set to myKeycloakRealm', () => {
expect(strategy.options.realm).toBe('myKeycloakRealm');
it('realm should be set correctly', () => {
expect(strategy.options.realm).toBe(config.realm);
});

it('publicClient should be set to true', () => {
expect(strategy.options.publicClient).toBe(true);
});

it('sslRequired should be set to none', () => {
expect(strategy.options.sslRequired).toBe('none');
it('sslRequired should be set correctly', () => {
expect(strategy.options.sslRequired).toBe(config.sslRequired);
});

it('authorizationURL should be set', () => {
expect(strategy.options.authorizationURL).toBe(
'http://localhost:8080/realms/myKeycloakRealm/protocol/openid-connect/auth'
);
it('authorizationURL should be set correctly', () => {
expect(strategy.options.authorizationURL).toBe(urls.authorizationURL);
});

it('tokenURL should be set', () => {
expect(strategy.options.tokenURL).toBe(
'http://localhost:8080/realms/myKeycloakRealm/protocol/openid-connect/token'
);
it('tokenURL should be set correctly', () => {
expect(strategy.options.tokenURL).toBe(urls.tokenURL);
});

it('_userProfileURL should be set', () => {
expect(strategy._userProfileURL).toBe(
'http://localhost:8080/realms/myKeycloakRealm/protocol/openid-connect/userinfo'
);
it('_userProfileURL should be set correctly', () => {
expect(strategy._userProfileURL).toBe(urls.userProfileURL);
});

it('should include "openid" in the scope', () => {
expect(strategy.options.scope).toContain('openid');
});

it('should include other specified scopes', () => {
expect(strategy.options.scope).toContain('profile');
expect(strategy.options.scope).toContain('email');
config.scopes.slice(1).forEach((scope) => {
expect(strategy.options.scope).toContain(scope);
});
});
});

Expand All @@ -71,12 +82,12 @@ describe('KeycloakStrategy', () => {
expect(() => {
new KeycloakStrategy(
{
realm: 'myKeycloakRealm',
authServerURL: 'http://localhost:8080',
clientID: 'ABC123',
realm: config.realm,
authServerURL: config.baseAuthServerURL,
clientID: config.clientID,
publicClient: false,
callbackURL: 'http://www.example.com',
sslRequired: 'none',
callbackURL: config.callbackURL,
sslRequired: config.sslRequired,
},
function () {}
);
Expand All @@ -87,32 +98,31 @@ describe('KeycloakStrategy', () => {
describe('constructed with publicClient=false and clientSecret', () => {
const strategy = new KeycloakStrategy(
{
realm: 'myKeycloakRealm',
authServerURL: 'http://localhost:8080',
clientID: 'ABC123',
realm: config.realm,
authServerURL: config.baseAuthServerURL,
clientID: config.clientID,
publicClient: false,
clientSecret: 'mySecret',
callbackURL: 'http://www.example.com',
sslRequired: 'none',
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
sslRequired: config.sslRequired,
},
function () {}
);

it('clientSecret should be set to mySecret', () => {
expect(strategy.options.clientSecret).toBe('mySecret');
it('clientSecret should be set correctly', () => {
expect(strategy.options.clientSecret).toBe(config.clientSecret);
});

it('should include "openid" in the scope even if not explicitly set', () => {
const strategyWithoutScope = new KeycloakStrategy(
{
realm: 'myKeycloakRealm',
authServerURL: 'http://localhost:8080',
clientID: 'ABC123',
realm: config.realm,
authServerURL: config.baseAuthServerURL,
clientID: config.clientID,
publicClient: false,
clientSecret: 'mySecret',
callbackURL: 'http://www.example.com',
sslRequired: 'none',
// scope is not set
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
sslRequired: config.sslRequired,
},
function () {}
);
Expand All @@ -121,39 +131,41 @@ describe('KeycloakStrategy', () => {
});

it('should prepend "openid" to existing scopes if missing', () => {
const partialScopes = 'profile email';
const strategyWithPartialScope = new KeycloakStrategy(
{
realm: 'myKeycloakRealm',
authServerURL: 'http://localhost:8080',
clientID: 'ABC123',
realm: config.realm,
authServerURL: config.baseAuthServerURL,
clientID: config.clientID,
publicClient: false,
clientSecret: 'mySecret',
callbackURL: 'http://www.example.com',
sslRequired: 'none',
scope: 'profile email',
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
sslRequired: config.sslRequired,
scope: partialScopes,
},
function () {}
);

expect(strategyWithPartialScope.options.scope).toBe('openid profile email');
expect(strategyWithPartialScope.options.scope).toBe(`openid ${partialScopes}`);
});

it('should not duplicate "openid" if already present', () => {
const fullScopes = config.scopes.join(' ');
const strategyWithOpenid = new KeycloakStrategy(
{
realm: 'myKeycloakRealm',
authServerURL: 'http://localhost:8080',
clientID: 'ABC123',
realm: config.realm,
authServerURL: config.baseAuthServerURL,
clientID: config.clientID,
publicClient: false,
clientSecret: 'mySecret',
callbackURL: 'http://www.example.com',
sslRequired: 'none',
scope: 'openid profile email',
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
sslRequired: config.sslRequired,
scope: fullScopes,
},
function () {}
);

expect(strategyWithOpenid.options.scope).toBe('openid profile email');
expect(strategyWithOpenid.options.scope).toBe(fullScopes);
});
});
});

0 comments on commit 2f1cb82

Please sign in to comment.