@@ -16,16 +16,28 @@ const stepFunctions = new AWS.StepFunctions(options);
1616const cloudwatchevents = new AWS . CloudWatchEvents ( options ) ;
1717const cloudformation = new AWS . CloudFormation ( options ) ;
1818
19+ const StatusCodes = {
20+ OK : 200 ,
21+ BAD_REQUEST : 400 ,
22+ FORBIDDEN : 403 ,
23+ NOT_FOUND : 404 ,
24+ NOT_ALLOWED : 405 ,
25+ REQUEST_TOO_LONG : 413 ,
26+ INTERNAL_SERVER_ERROR : 500 ,
27+ TIMEOUT : 503 ,
28+ } ;
29+
1930/**
2031 * Class to throw errors
2132 * @param {string } code
2233 * @param {string } errMsg
2334 */
2435class ErrorException extends Error {
25- constructor ( code , errMsg ) {
26- super ( errMsg ) ;
36+ constructor ( code , errMsg , statusCode = StatusCodes . BAD_REQUEST ) {
37+ super ( statusCode , code , errMsg ) ;
2738 this . code = code ;
2839 this . message = errMsg ;
40+ this . statusCode = statusCode ;
2941 }
3042 toString ( ) {
3143 return `${ this . code } : ${ this . message } ` ;
@@ -143,6 +155,7 @@ const getRegionInfraConfigs = async (testRegion) => {
143155const getTestAndRegionConfigs = async ( testId ) => {
144156 try {
145157 const testEntry = await getTestEntry ( testId ) ;
158+ if ( ! testEntry ) throw new ErrorException ( "TEST_NOT_FOUND" , `testId '${ testId } ' not found` , StatusCodes . NOT_FOUND ) ;
146159 if ( testEntry . testTaskConfigs ) {
147160 for ( let testRegionSettings of testEntry . testTaskConfigs ) {
148161 const regionInfraConfig = await getRegionInfraConfigs ( testRegionSettings . region ) ;
@@ -272,6 +285,8 @@ const convertLinuxCronToAwsCron = (linuxCron, cronExpiryDate) => {
272285const checkEnoughIntervalDiff = ( cronValue , cronExpiryDate , holdFor , rampUp , testTaskConfigs ) => {
273286 if ( ! holdFor || ! rampUp ) return "" ;
274287 let cronExpiry = new Date ( cronExpiryDate ) ;
288+ const parts = cronValue . trim ( ) . split ( " " ) ;
289+ if ( parts . length !== 5 ) throw new ErrorException ( "Invalid Linux cron expression" , "Expected format: 0 * * * *" ) ;
275290
276291 let cronInterval ;
277292 try {
@@ -396,6 +411,26 @@ const removeRules = async (testId, functionName, recurrence) => {
396411 }
397412} ;
398413
414+ const isValidTimeString = ( timeString ) => {
415+ const timeRegex = / ^ ( [ 0 1 ] \d | 2 [ 0 - 3 ] ) : ( [ 0 - 5 ] \d ) $ / ;
416+ if ( ! timeRegex . test ( timeString ) )
417+ throw new ErrorException ( "InvalidParameter" , "Invalid time format. Expected format: HH:MM" ) ;
418+ } ;
419+
420+ const isValidDateString = ( dateString ) => {
421+ // Check if the dateString is in the format YYYY-MM-DD
422+ const dateRegex = / ^ ( \d { 4 } ) - ( 0 [ 1 - 9 ] | 1 [ 0 - 2 ] ) - ( 0 [ 1 - 9 ] | [ 1 2 ] \d | 3 [ 0 1 ] ) $ / ;
423+
424+ if ( ! dateRegex . test ( dateString ) )
425+ throw new ErrorException ( "InvalidParameter" , "Invalid date format. Expected format: YYYY-MM-DD" ) ;
426+ } ;
427+
428+ const isValidDate = ( date ) => {
429+ const today = new Date ( ) ;
430+ today . setHours ( 0 , 0 , 0 , 0 ) ;
431+
432+ if ( date < today ) throw new ErrorException ( "InvalidParameter" , "Date cannot be in the past" ) ;
433+ } ;
399434/**
400435 * Schedules test and returns a consolidated list of test scenarios
401436 * @param {object } event test event information
@@ -459,7 +494,10 @@ const scheduleTest = async (event, context) => {
459494 config . scheduleTime = scheduleTime ;
460495 config . scheduleDate = scheduleDate ;
461496 } else {
497+ isValidTimeString ( scheduleTime ) ;
498+ isValidDateString ( scheduleDate ) ;
462499 createRun = new Date ( year , parseInt ( month , 10 ) - 1 , day , hour , minute ) ;
500+ isValidDate ( createRun ) ;
463501 } // Schedule for 1 min prior to account for time it takes to create rule
464502 // getMonth() returns Jan with index Zero that is why months need a +1
465503 // refrence https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth
@@ -1377,6 +1415,15 @@ const cancelTest = async (testId) => {
13771415
13781416 try {
13791417 // Get test and regional infrastructure configuration
1418+ const listTestsRes = await listTests ( ) ;
1419+ const allTests = listTestsRes . Items ;
1420+
1421+ // Check if the testId exists in the list of tests
1422+ const testExists = allTests . some ( ( test ) => test . testId === testId ) ;
1423+ if ( ! testExists ) {
1424+ throw new ErrorException ( "TEST_NOT_FOUND" , `testId '${ testId } ' not found` , StatusCodes . NOT_FOUND ) ;
1425+ }
1426+
13801427 const testAndRegionalInfraConfigs = await getTestAndRegionConfigs ( testId ) ;
13811428 if ( testAndRegionalInfraConfigs . testTaskConfigs ) {
13821429 for ( const regionalConfig of testAndRegionalInfraConfigs . testTaskConfigs ) {
@@ -1689,4 +1736,6 @@ module.exports = {
16891736 getCFUrl : getCFUrl ,
16901737 getAccountFargatevCPUDetails : getAccountFargatevCPUDetails ,
16911738 getTestDurationSeconds : getTestDurationSeconds ,
1739+ ErrorException : ErrorException ,
1740+ StatusCodes : StatusCodes ,
16921741} ;
0 commit comments