Skip to content

Commit

Permalink
Update comment and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithvm committed Mar 9, 2020
1 parent 09e12ca commit 85b59e1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/session/sessionPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const register = function (server, options) {
throw new MissingRoleError('No roles available for this user, please contact your system administrator.');
}

// If we used any additional auth headers when authenticating, we need to store them in the session
// Store only specified auth headers in the session.
// Headers stored in the session are added to every request coming in at AuthType#addAdditionalAuthHeaders .
authResponse.session.additionalAuthHeaders = null;
if (Object.keys(additionalAuthHeaders).length) {
authResponse.session.additionalAuthHeaders = filterAuthHeaders(additionalAuthHeaders, settings.headersToStoreInSession);
Expand Down
14 changes: 1 addition & 13 deletions tests/AuthType.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import AuthType from "../lib/auth/types/AuthType";

class MockServer {
config() {
return {
get: () => {
return null;
}
}
}
register(args) {
this.registerArgs = args;
}
}
import { MockServer } from './Mocks'

describe('AuthType tests', () => {
it('should contain only security_impersonate_as when no additional headers are passed', () => {
Expand Down
44 changes: 44 additions & 0 deletions tests/Mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class MockServer {
config() {
return {
get: () => {
return null;
}
}
}
ext(_, preAuthFunc) {
this.preAuthFunc = preAuthFunc;
}
register(args) {
this.registerArgs = args;
}
}

class MockRequest {
constructor() {
this.auth = {};
this.state = {};
this.cookieAuth = {
set(_) { }
};
}
}

class MockAuthResponse {
constructor() {
this.user = { roles: [""] };
this.session = {};
}
}

class MockHapi {
state(storageCookieName, storage) {
}
}

export {
MockServer,
MockRequest,
MockAuthResponse,
MockHapi
}
57 changes: 57 additions & 0 deletions tests/SessionPlugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { plugin } from "../lib/session/sessionPlugin";
import { MockServer, MockRequest, MockAuthResponse, MockHapi } from './Mocks'

describe('Session Plugin tests', () => {
var mockServer = new MockServer();
var request = new MockRequest();
var h = new MockHapi();
var authResponse = new MockAuthResponse();
const testHeaderKey1 = "test-header-key-1", testHeaderValue1 = "test-header-value-1";
const testHeaderKey2 = "test-header-key-2", testHeaderValue2 = "test-header-value-2";
var additionalAuthHeaders = {
[testHeaderKey1]: testHeaderValue1,
[testHeaderKey2]: testHeaderValue2
};

it('should store only 1 specified header in the session', () => {
// arrange
plugin.register(mockServer, { headersToStoreInSession:[testHeaderKey1] })
mockServer.preAuthFunc(request, h)

// act
request.auth.securitySessionStorage._handleAuthResponse({}, authResponse, additionalAuthHeaders)

// assert
const storedHeaders = authResponse.session.additionalAuthHeaders;
expect(storedHeaders).toHaveProperty(testHeaderKey1, testHeaderValue1);
expect(storedHeaders).not.toHaveProperty(testHeaderKey2);
});

it('should store 2 specified headers in the session', () => {
// arrange
plugin.register(mockServer, { headersToStoreInSession:[testHeaderKey1, testHeaderKey2] })
mockServer.preAuthFunc(request, h)

// act
request.auth.securitySessionStorage._handleAuthResponse({}, authResponse, additionalAuthHeaders)

// assert
const storedHeaders = authResponse.session.additionalAuthHeaders;
expect(storedHeaders).toHaveProperty(testHeaderKey1, testHeaderValue1);
expect(storedHeaders).toHaveProperty(testHeaderKey2, testHeaderValue2);
});

it('should store no headers in the session', () => {
// arrange
plugin.register(mockServer, { headersToStoreInSession:[] })
mockServer.preAuthFunc(request, h)

// act
request.auth.securitySessionStorage._handleAuthResponse({}, authResponse, additionalAuthHeaders)

// assert
const storedHeaders = authResponse.session.additionalAuthHeaders;
expect(storedHeaders).not.toHaveProperty(testHeaderKey1, testHeaderValue1);
expect(storedHeaders).not.toHaveProperty(testHeaderKey2, testHeaderValue2);
});
});

0 comments on commit 85b59e1

Please sign in to comment.