@@ -10,22 +10,47 @@ import { AdapterApiMock, DataSourceStorageMock } from './mocks';
1010import { RequestContext } from '../src/interfaces' ;
1111import { generateAuthToken } from './utils' ;
1212
13+ class ApiGatewayOpenAPI extends ApiGateway {
14+ protected isRunning : Promise < void > | null = null ;
15+
16+ public coerceForSqlQuery ( query , context : RequestContext ) {
17+ return super . coerceForSqlQuery ( query , context ) ;
18+ }
19+
20+ public async startSQLServer ( ) : Promise < void > {
21+ if ( this . isRunning ) {
22+ return this . isRunning ;
23+ }
24+
25+ this . isRunning = this . sqlServer . init ( { } ) ;
26+
27+ return this . isRunning ;
28+ }
29+
30+ public async shutdownSQLServer ( ) : Promise < void > {
31+ try {
32+ await this . sqlServer . shutdown ( 'fast' ) ;
33+ } catch ( error ) {
34+ console . log ( `Error while shutting down server: ${ error } ` ) ;
35+ }
36+
37+ this . isRunning = null ;
38+ }
39+ }
40+
1341function createApiGateway ( handler : RequestHandler , logger : ( ) => any , options : Partial < ApiGatewayOptions > ) {
1442 const adapterApi : any = new AdapterApiMock ( ) ;
1543 const dataSourceStorage : any = new DataSourceStorageMock ( ) ;
1644
17- class ApiGatewayFake extends ApiGateway {
18- public coerceForSqlQuery ( query , context : RequestContext ) {
19- return super . coerceForSqlQuery ( query , context ) ;
20- }
21-
45+ class ApiGatewayFake extends ApiGatewayOpenAPI {
2246 public initApp ( app : ExpressApplication ) {
2347 const userMiddlewares : RequestHandler [ ] = [
2448 this . checkAuth ,
2549 this . requestContextMiddleware ,
2650 ] ;
2751
2852 app . get ( '/test-auth-fake' , userMiddlewares , handler ) ;
53+ this . enableNativeApiGateway ( app ) ;
2954
3055 app . use ( this . handleErrorMiddleware ) ;
3156 }
@@ -50,6 +75,61 @@ function createApiGateway(handler: RequestHandler, logger: () => any, options: P
5075 } ;
5176}
5277
78+ describe ( 'test authorization with native gateway' , ( ) => {
79+ const expectSecurityContext = ( securityContext ) => {
80+ expect ( securityContext . uid ) . toEqual ( 5 ) ;
81+ expect ( securityContext . iat ) . toBeDefined ( ) ;
82+ expect ( securityContext . exp ) . toBeDefined ( ) ;
83+ } ;
84+
85+ let app : ExpressApplication ;
86+ let apiGateway : ApiGatewayOpenAPI ;
87+
88+ const handlerMock = jest . fn ( ( req , res ) => {
89+ expectSecurityContext ( req . context . authInfo ) ;
90+ expectSecurityContext ( req . context . securityContext ) ;
91+
92+ res . status ( 200 ) . end ( ) ;
93+ } ) ;
94+ const loggerMock = jest . fn ( ( ) => {
95+ //
96+ } ) ;
97+
98+ beforeAll ( async ( ) => {
99+ const result = createApiGateway ( handlerMock , loggerMock , { } ) ;
100+
101+ app = result . app ;
102+ apiGateway = result . apiGateway ;
103+
104+ await result . apiGateway . startSQLServer ( ) ;
105+ } ) ;
106+
107+ beforeEach ( ( ) => {
108+ handlerMock . mockClear ( ) ;
109+ loggerMock . mockClear ( ) ;
110+ } ) ;
111+
112+ afterAll ( async ( ) => {
113+ await apiGateway . shutdownSQLServer ( ) ;
114+ } ) ;
115+
116+ fit ( 'default authorization' , async ( ) => {
117+ const token = generateAuthToken ( { uid : 5 , } ) ;
118+
119+ await request ( app )
120+ . get ( '/cubejs-api/v2/stream' )
121+ . set ( 'Authorization' , `${ token } ` )
122+ . expect ( 501 ) ;
123+
124+ // No bad logs
125+ expect ( loggerMock . mock . calls . length ) . toEqual ( 0 ) ;
126+ // We should not call js handler, request should go into rust code
127+ expect ( handlerMock . mock . calls . length ) . toEqual ( 0 ) ;
128+
129+ await apiGateway . shutdownSQLServer ( ) ;
130+ } ) ;
131+ } ) ;
132+
53133describe ( 'test authorization' , ( ) => {
54134 test ( 'default authorization' , async ( ) => {
55135 const loggerMock = jest . fn ( ( ) => {
0 commit comments