Skip to content

Commit

Permalink
Add keep alive test
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <[email protected]>
  • Loading branch information
derek-ho committed Feb 13, 2024
1 parent b621d10 commit 1da9739
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions server/auth/types/authentication_type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@
import { SecurityPluginConfigType } from '../..';

Check failure on line 16 in server/auth/types/authentication_type.test.ts

View workflow job for this annotation

GitHub Actions / Run unit tests (ubuntu-latest)

File has too many classes (3). Maximum allowed is 1

Check failure on line 16 in server/auth/types/authentication_type.test.ts

View workflow job for this annotation

GitHub Actions / Run unit tests (macos-latest)

File has too many classes (3). Maximum allowed is 1

Check failure on line 16 in server/auth/types/authentication_type.test.ts

View workflow job for this annotation

GitHub Actions / Run unit tests (windows-latest)

File has too many classes (3). Maximum allowed is 1
import { AuthenticationType } from './authentication_type';
import { httpServerMock } from '../../../../../src/core/server/mocks';
import {
SessionStorageFactory,
SessionStorage,
OpenSearchDashboardsRequest,
} from '../../../../../src/core/server';
import { SecuritySessionCookie } from '../../session/security_cookie';

const mockedNow = 0;
Date.now = jest.fn(() => mockedNow);

class DummyAuthType extends AuthenticationType {
authNotRequired(request: OpenSearchDashboardsRequest): boolean {
return false;
}
buildAuthHeaderFromCookie() {}
getAdditionalAuthHeader() {}
handleUnauthedRequest() {}
Expand All @@ -35,6 +47,43 @@ class DummyAuthType extends AuthenticationType {
}
}

// Implementation of SessionStorage using browser's sessionStorage
class BrowserSessionStorage<T> implements SessionStorage<T> {
private readonly storageKey: string;

constructor(storageKey: string) {
this.storageKey = storageKey;
}

async get(): Promise<T | null> {
const storedValue = sessionStorage.getItem(this.storageKey);
return storedValue ? JSON.parse(storedValue) : null;
}

set(sessionValue: T): void {
const serializedValue = JSON.stringify(sessionValue);
sessionStorage.setItem(this.storageKey, serializedValue);
}

clear(): void {
sessionStorage.removeItem(this.storageKey);
}
}

// Implementation of SessionStorageFactory using the browser's sessionStorage
class BrowserSessionStorageFactory<T> implements SessionStorageFactory<T> {
private readonly storageKey: string;

constructor(storageKey: string) {
this.storageKey = storageKey;
}

// This method returns a new instance of the browser's SessionStorage for each request
asScoped(request: OpenSearchDashboardsRequest): SessionStorage<T> {
return new BrowserSessionStorage<T>(this.storageKey);
}
}

describe('test tenant header', () => {
const config = {
multitenancy: {
Expand Down Expand Up @@ -106,4 +155,45 @@ describe('test tenant header', () => {
const result = await dummyAuthType.authHandler(request, response, toolkit);
expect(result.requestHeaders.securitytenant).toEqual('dummy_tenant');
});

it(`keepalive should not shorten the cookie expiry`, async () => {
const keepAliveConfig = {
multitenancy: {
enabled: true,
},
auth: {
unauthenticated_routes: [] as string[],
},
session: {
keepalive: true,
ttl: 1000,
},
} as SecurityPluginConfigType;
const keepAliveDummyAuth = new DummyAuthType(
keepAliveConfig,
new BrowserSessionStorageFactory('security_cookie'),
router,
esClient,
coreSetup,
logger
);
const testCookie: SecuritySessionCookie = {
credentials: {
authHeaderValueExtra: true,
},
expiryTime: 2000,
};
// Set cookie
sessionStorage.setItem('security_cookie', JSON.stringify(testCookie));
const request = httpServerMock.createOpenSearchDashboardsRequest({
path: '/internal/v1',
});
const response = jest.fn();
const toolkit = {
authenticated: jest.fn((value) => value),
};
const _ = await keepAliveDummyAuth.authHandler(request, response, toolkit);
const cookieAfterRequest = sessionStorage.getItem('security_cookie');
expect(JSON.parse(cookieAfterRequest!).expiryTime).toBe(2000);
});
});

0 comments on commit 1da9739

Please sign in to comment.