-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from opendistro-for-elasticsearch/opendistro-1.4
Specify headers to be stored in session.
- Loading branch information
Showing
5 changed files
with
107 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |