-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NP Licensing plugin route handler context (#46586)
* introducing a licensing route handler context * extracting route handler context for easier testing * address PR feedback
- Loading branch information
Showing
5 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
x-pack/plugins/licensing/server/licensing_route_handler_context.test.ts
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,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { BehaviorSubject } from 'rxjs'; | ||
import { ILicense } from './types'; | ||
import { setup } from './__fixtures__/setup'; | ||
import { createRouteHandlerContext } from './licensing_route_handler_context'; | ||
|
||
describe('licensingRouteHandlerContext', () => { | ||
it('provides the initial license value', async () => { | ||
const { license$, license } = await setup(); | ||
|
||
const context = createRouteHandlerContext(license$); | ||
|
||
const { license: contextResult } = await context({}); | ||
|
||
expect(contextResult).toBe(license); | ||
}); | ||
|
||
it('provides the latest license value', async () => { | ||
const { license } = await setup(); | ||
const license$ = new BehaviorSubject<ILicense>(license); | ||
|
||
const context = createRouteHandlerContext(license$); | ||
|
||
const latestLicense = (Symbol() as unknown) as ILicense; | ||
license$.next(latestLicense); | ||
|
||
const { license: contextResult } = await context({}); | ||
|
||
expect(contextResult).toBe(latestLicense); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
x-pack/plugins/licensing/server/licensing_route_handler_context.ts
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,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { RequestHandlerContext, IContextProvider } from 'src/core/server'; | ||
import { Observable } from 'rxjs'; | ||
import { take } from 'rxjs/operators'; | ||
import { ILicense } from './types'; | ||
|
||
export function createRouteHandlerContext( | ||
license$: Observable<ILicense> | ||
): IContextProvider<RequestHandlerContext, 'licensing'> { | ||
return async function licensingRouteHandlerContext() { | ||
const license = await license$.pipe(take(1)).toPromise(); | ||
return { license }; | ||
}; | ||
} |
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