@@ -17,6 +17,7 @@ import {
1717 DataSourceStorageMock ,
1818 AdapterApiMock
1919} from './mocks' ;
20+ import { ApiScopesTuple } from '../src/types/auth' ;
2021
2122const logger = ( type , message ) => console . log ( { type, ...message } ) ;
2223
@@ -61,6 +62,7 @@ async function createApiGateway(
6162
6263 process . env . NODE_ENV = 'unknown' ;
6364 const app = express ( ) ;
65+ app . use ( express . json ( ) ) ;
6466 apiGateway . initApp ( app ) ;
6567
6668 return {
@@ -982,6 +984,96 @@ describe('API Gateway', () => {
982984 } ) ;
983985 } ) ;
984986
987+ describe ( '/v1/pre-aggregations/jobs' , ( ) => {
988+ const scheduledRefreshContextsFactory = ( ) => ( [
989+ { securityContext : { foo : 'bar' } } ,
990+ { securityContext : { bar : 'foo' } }
991+ ] ) ;
992+
993+ const scheduledRefreshTimeZonesFactory = ( ) => ( [ 'UTC' , 'America/Los_Angeles' ] ) ;
994+
995+ const appPrepareFactory = async ( scope : string [ ] ) => {
996+ const playgroundAuthSecret = 'test12345' ;
997+ const { app } = await createApiGateway (
998+ new AdapterApiMock ( ) ,
999+ new DataSourceStorageMock ( ) ,
1000+ {
1001+ basePath : '/test' ,
1002+ playgroundAuthSecret,
1003+ refreshScheduler : ( ) => new RefreshSchedulerMock ( ) ,
1004+ scheduledRefreshContexts : ( ) => Promise . resolve ( scheduledRefreshContextsFactory ( ) ) ,
1005+ scheduledRefreshTimeZones : scheduledRefreshTimeZonesFactory ,
1006+ contextToApiScopes : ( ) => Promise . resolve ( < ApiScopesTuple > scope )
1007+ }
1008+ ) ;
1009+ const token = generateAuthToken ( { uid : 5 , scope } , { } , playgroundAuthSecret ) ;
1010+ const tokenUser = generateAuthToken ( { uid : 5 , scope } , { } , API_SECRET ) ;
1011+
1012+ return { app, token, tokenUser } ;
1013+ } ;
1014+
1015+ test ( 'no input' , async ( ) => {
1016+ const { app, tokenUser } = await appPrepareFactory ( [ 'graphql' , 'data' , 'meta' , 'jobs' ] ) ;
1017+
1018+ const req = request ( app ) . post ( '/test/v1/pre-aggregations/jobs' )
1019+ . set ( 'Content-type' , 'application/json' )
1020+ . set ( 'Authorization' , `Bearer ${ tokenUser } ` ) ;
1021+
1022+ const res = await req ;
1023+ expect ( res . status ) . toEqual ( 400 ) ;
1024+ expect ( res . body . error ) . toEqual ( 'No job description provided' ) ;
1025+ } ) ;
1026+
1027+ test ( 'invalid input action' , async ( ) => {
1028+ const { app, tokenUser } = await appPrepareFactory ( [ 'graphql' , 'data' , 'meta' , 'jobs' ] ) ;
1029+
1030+ const req = request ( app ) . post ( '/test/v1/pre-aggregations/jobs' )
1031+ . set ( 'Content-type' , 'application/json' )
1032+ . set ( 'Authorization' , `Bearer ${ tokenUser } ` )
1033+ . send ( { action : 'patch' } ) ;
1034+
1035+ const res = await req ;
1036+ expect ( res . status ) . toEqual ( 400 ) ;
1037+ expect ( res . body . error . includes ( 'Invalid Job query format' ) ) . toBeTruthy ( ) ;
1038+ } ) ;
1039+
1040+ test ( 'invalid input date range' , async ( ) => {
1041+ const { app, tokenUser } = await appPrepareFactory ( [ 'graphql' , 'data' , 'meta' , 'jobs' ] ) ;
1042+
1043+ let req = request ( app ) . post ( '/test/v1/pre-aggregations/jobs' )
1044+ . set ( 'Content-type' , 'application/json' )
1045+ . set ( 'Authorization' , `Bearer ${ tokenUser } ` )
1046+ . send ( {
1047+ action : 'post' ,
1048+ selector : {
1049+ contexts : [ { securityContext : { } } ] ,
1050+ timezones : [ 'UTC' , 'America/Los_Angeles' ] ,
1051+ dateRange : [ 'invalid string' , '2020-02-20' ]
1052+ }
1053+ } ) ;
1054+
1055+ let res = await req ;
1056+ expect ( res . status ) . toEqual ( 400 ) ;
1057+ expect ( res . body . error . includes ( 'Cannot parse selector date range' ) ) . toBeTruthy ( ) ;
1058+
1059+ req = request ( app ) . post ( '/test/v1/pre-aggregations/jobs' )
1060+ . set ( 'Content-type' , 'application/json' )
1061+ . set ( 'Authorization' , `Bearer ${ tokenUser } ` )
1062+ . send ( {
1063+ action : 'post' ,
1064+ selector : {
1065+ contexts : [ { securityContext : { } } ] ,
1066+ timezones : [ 'UTC' , 'America/Los_Angeles' ] ,
1067+ dateRange : [ '2020-02-20' , 'invalid string' ]
1068+ }
1069+ } ) ;
1070+
1071+ res = await req ;
1072+ expect ( res . status ) . toEqual ( 400 ) ;
1073+ expect ( res . body . error . includes ( 'Cannot parse selector date range' ) ) . toBeTruthy ( ) ;
1074+ } ) ;
1075+ } ) ;
1076+
9851077 describe ( 'healtchecks' , ( ) => {
9861078 test ( 'readyz (standalone)' , async ( ) => {
9871079 const { app, adapterApi } = await createApiGateway ( ) ;
0 commit comments