@@ -182,7 +182,7 @@ describe('parse_max_items', () => {
182182describe ( 'validate_user_input_iam' , ( ) => {
183183 describe ( 'validate_iam_path' , ( ) => {
184184 const max_length = 512 ;
185- it ( 'should return true when path is undefined' , ( ) => {
185+ it ( 'should return void when path is undefined' , ( ) => {
186186 let dummy_path ;
187187 const res = iam_utils . validate_iam_path ( dummy_path ) ;
188188 expect ( res ) . toBeUndefined ( ) ;
@@ -313,7 +313,7 @@ describe('validate_user_input_iam', () => {
313313
314314 describe ( 'validate_marker' , ( ) => {
315315 const min_length = 1 ;
316- it ( 'should return true when marker is undefined' , ( ) => {
316+ it ( 'should return void when marker is undefined' , ( ) => {
317317 let dummy_marker ;
318318 const res = iam_utils . validate_marker ( dummy_marker ) ;
319319 expect ( res ) . toBeUndefined ( ) ;
@@ -407,7 +407,7 @@ describe('validate_user_input_iam', () => {
407407 describe ( 'validate_access_key_id' , ( ) => {
408408 const min_length = 16 ;
409409 const max_length = 128 ;
410- it ( 'should return true when access_key is undefined' , ( ) => {
410+ it ( 'should return void when access_key is undefined' , ( ) => {
411411 let dummy_access_key ;
412412 const res = iam_utils . validate_access_key_id ( dummy_access_key ) ;
413413 expect ( res ) . toBeUndefined ( ) ;
@@ -511,7 +511,7 @@ describe('validate_user_input_iam', () => {
511511 } ) ;
512512
513513 describe ( 'validate_status' , ( ) => {
514- it ( 'should return true when status is undefined' , ( ) => {
514+ it ( 'should return void when status is undefined' , ( ) => {
515515 let dummy_status ;
516516 const res = iam_utils . validate_status ( dummy_status ) ;
517517 expect ( res ) . toBeUndefined ( ) ;
@@ -547,4 +547,197 @@ describe('validate_user_input_iam', () => {
547547 } ) ;
548548
549549 } ) ;
550+
551+ describe ( 'validate_policy_name' , ( ) => {
552+ const min_length = 1 ;
553+ const max_length = 128 ;
554+ it ( 'should return void when policy_name is undefined' , ( ) => {
555+ let dummy_policy_name ;
556+ const res = iam_utils . validate_policy_name ( dummy_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
557+ expect ( res ) . toBeUndefined ( ) ;
558+ } ) ;
559+
560+ it ( 'should return true when policy_name is at the min or max length' , ( ) => {
561+ expect ( iam_utils . validate_policy_name ( 'a' , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ) . toBe ( true ) ;
562+ expect ( iam_utils . validate_policy_name ( 'a' . repeat ( max_length ) , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ) . toBe ( true ) ;
563+ } ) ;
564+
565+ it ( 'should return true when policy_name is within the length constraint' , ( ) => {
566+ expect ( iam_utils . validate_policy_name ( 'a' . repeat ( min_length + 1 ) , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ) . toBe ( true ) ;
567+ expect ( iam_utils . validate_policy_name ( 'a' . repeat ( max_length - 1 ) , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ) . toBe ( true ) ;
568+ } ) ;
569+
570+ it ( 'should return true when policy_name is valid CamelCase' , ( ) => {
571+ const dummy_policy_name = 'CreateBucketPolicy' ;
572+ const res = iam_utils . validate_policy_name ( dummy_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
573+ expect ( res ) . toBe ( true ) ;
574+ } ) ;
575+
576+ it ( 'should return true when policy_name is valid SnakeCase' , ( ) => {
577+ const dummy_policy_name = 'create_bucket_policy' ;
578+ const res = iam_utils . validate_policy_name ( dummy_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
579+ expect ( res ) . toBe ( true ) ;
580+ } ) ;
581+
582+ it ( 'should throw error when policy_name is invalid - contains invalid character' , ( ) => {
583+ try {
584+ iam_utils . validate_policy_name ( '{}' , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
585+ throw new NoErrorThrownError ( ) ;
586+ } catch ( err ) {
587+ expect ( err ) . toBeInstanceOf ( IamError ) ;
588+ expect ( err ) . toHaveProperty ( 'code' , IamError . ValidationError . code ) ;
589+ }
590+ } ) ;
591+
592+ it ( 'should throw error when policy_name is too short' , ( ) => {
593+ try {
594+ const dummy_policy_name = '' ;
595+ iam_utils . validate_policy_name ( dummy_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
596+ throw new NoErrorThrownError ( ) ;
597+ } catch ( err ) {
598+ expect ( err ) . toBeInstanceOf ( IamError ) ;
599+ expect ( err ) . toHaveProperty ( 'code' , IamError . ValidationError . code ) ;
600+ }
601+ } ) ;
602+
603+ it ( 'should throw error when dummy_policy_name is too long' , ( ) => {
604+ try {
605+ const dummy_policy_name = 'A' . repeat ( max_length + 1 ) ;
606+ iam_utils . validate_policy_name ( dummy_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
607+ throw new NoErrorThrownError ( ) ;
608+ } catch ( err ) {
609+ expect ( err ) . toBeInstanceOf ( IamError ) ;
610+ expect ( err ) . toHaveProperty ( 'code' , IamError . ValidationError . code ) ;
611+ }
612+ } ) ;
613+
614+ it ( 'should throw error for invalid input types (null) in policy_name' , ( ) => {
615+ try {
616+ // @ts -ignore
617+ const invalid_policy_name = null ;
618+ iam_utils . validate_policy_name ( invalid_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
619+ throw new NoErrorThrownError ( ) ;
620+ } catch ( err ) {
621+ expect ( err ) . toBeInstanceOf ( IamError ) ;
622+ expect ( err ) . toHaveProperty ( 'code' , IamError . ValidationError . code ) ;
623+ }
624+ } ) ;
625+
626+ it ( 'should throw error for invalid input types (number) in policy_name' , ( ) => {
627+ try {
628+ const invalid_policy_name = 1 ;
629+ // @ts -ignore
630+ iam_utils . validate_policy_name ( invalid_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
631+ throw new NoErrorThrownError ( ) ;
632+ } catch ( err ) {
633+ expect ( err ) . toBeInstanceOf ( IamError ) ;
634+ expect ( err ) . toHaveProperty ( 'code' , IamError . ValidationError . code ) ;
635+ }
636+ } ) ;
637+
638+ it ( 'should throw error for invalid input types (object) in policy_name' , ( ) => {
639+ try {
640+ const invalid_policy_name = { } ;
641+ // @ts -ignore
642+ iam_utils . validate_policy_name ( invalid_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
643+ throw new NoErrorThrownError ( ) ;
644+ } catch ( err ) {
645+ expect ( err ) . toBeInstanceOf ( IamError ) ;
646+ expect ( err ) . toHaveProperty ( 'code' , IamError . ValidationError . code ) ;
647+ }
648+ } ) ;
649+
650+ it ( 'should throw error for invalid input types (boolean) in policy_name' , ( ) => {
651+ try {
652+ const invalid_policy_name = false ;
653+ // @ts -ignore
654+ iam_utils . validate_policy_name ( invalid_policy_name , iam_constants . IAM_PARAMETER_NAME . POLICY_NAME ) ;
655+ throw new NoErrorThrownError ( ) ;
656+ } catch ( err ) {
657+ expect ( err ) . toBeInstanceOf ( IamError ) ;
658+ expect ( err ) . toHaveProperty ( 'code' , IamError . ValidationError . code ) ;
659+ }
660+ } ) ;
661+ } ) ;
662+
663+ describe ( 'validate_policy_document' , ( ) => {
664+ it ( 'should return void when policy_document is undefined' , ( ) => {
665+ let dummy_policy_document ;
666+ const res = iam_utils . validate_policy_document ( dummy_policy_document , iam_constants . IAM_PARAMETER_NAME . POLICY_DOCUMENT ) ;
667+ expect ( res ) . toBeUndefined ( ) ;
668+ } ) ;
669+
670+ it ( 'should return true when policy_document is valid JSON string' , ( ) => {
671+ const dummy_policy_document = JSON . stringify ( {
672+ Version : '2012-10-17' ,
673+ Statement : [ {
674+ Effect : 'Allow' ,
675+ Action : 's3:GetBucketLocation' ,
676+ Resource : '*'
677+ } ]
678+ } ) ;
679+ const res = iam_utils . validate_policy_document ( dummy_policy_document , iam_constants . IAM_PARAMETER_NAME . POLICY_DOCUMENT ) ;
680+ expect ( res ) . toBe ( true ) ;
681+ } ) ;
682+
683+ it ( 'should throw error when policy_document is invalid JSON string' , ( ) => {
684+ try {
685+ const invalid_policy_document = 'hello' ;
686+ iam_utils . validate_policy_document ( invalid_policy_document , iam_constants . IAM_PARAMETER_NAME . POLICY_DOCUMENT ) ;
687+ throw new NoErrorThrownError ( ) ;
688+ } catch ( err ) {
689+ expect ( err ) . toBeInstanceOf ( IamError ) ;
690+ expect ( err ) . toHaveProperty ( 'code' , IamError . MalformedPolicyDocument . code ) ;
691+ }
692+ } ) ;
693+
694+ it ( 'should throw error when policy_document is empty' , ( ) => {
695+ try {
696+ const invalid_policy_document = '' ;
697+ iam_utils . validate_policy_document ( invalid_policy_document , iam_constants . IAM_PARAMETER_NAME . POLICY_DOCUMENT ) ;
698+ throw new NoErrorThrownError ( ) ;
699+ } catch ( err ) {
700+ expect ( err ) . toBeInstanceOf ( IamError ) ;
701+ expect ( err ) . toHaveProperty ( 'code' , IamError . MalformedPolicyDocument . code ) ;
702+ }
703+ } ) ;
704+
705+ it ( 'should throw error when policy_document has Principal field' , ( ) => {
706+ try {
707+ const dummy_policy_document = JSON . stringify ( {
708+ Version : '2012-10-17' ,
709+ Statement : [ {
710+ Effect : 'Allow' ,
711+ Action : 's3:GetBucketLocation' ,
712+ Resource : '*' ,
713+ Principal : '*' // in IAM user inline policies we don't have the principal (user policy is embed to user)
714+ } ]
715+ } ) ;
716+ iam_utils . validate_policy_document ( dummy_policy_document , iam_constants . IAM_PARAMETER_NAME . POLICY_DOCUMENT ) ;
717+ throw new NoErrorThrownError ( ) ;
718+ } catch ( err ) {
719+ expect ( err ) . toBeInstanceOf ( IamError ) ;
720+ expect ( err ) . toHaveProperty ( 'code' , IamError . MalformedPolicyDocument . code ) ;
721+ }
722+ } ) ;
723+
724+ it ( 'should throw error when policy_document has NotPrincipal field' , ( ) => {
725+ try {
726+ const dummy_policy_document = JSON . stringify ( {
727+ Version : '2012-10-17' ,
728+ Statement : [ {
729+ Effect : 'Allow' ,
730+ Action : 's3:GetBucketLocation' ,
731+ Resource : '*' ,
732+ NotPrincipal : '*' // in IAM user inline policies we don't have the principal (user policy is embed to user)
733+ } ]
734+ } ) ;
735+ iam_utils . validate_policy_document ( dummy_policy_document , iam_constants . IAM_PARAMETER_NAME . POLICY_DOCUMENT ) ;
736+ throw new NoErrorThrownError ( ) ;
737+ } catch ( err ) {
738+ expect ( err ) . toBeInstanceOf ( IamError ) ;
739+ expect ( err ) . toHaveProperty ( 'code' , IamError . MalformedPolicyDocument . code ) ;
740+ }
741+ } ) ;
742+ } ) ;
550743} ) ;
0 commit comments