1
1
'use strict' ;
2
2
3
3
const _ = require ( 'lodash' ) ;
4
+ const itParam = require ( 'mocha-param' ) ;
4
5
const expect = require ( 'chai' ) . expect ;
5
6
const sinon = require ( 'sinon' ) ;
6
7
const Serverless = require ( 'serverless/lib/Serverless' ) ;
7
8
const AwsProvider = require ( 'serverless/lib/plugins/aws/provider' ) ;
8
9
const ServerlessStepFunctions = require ( './../../index' ) ;
9
10
11
+ function getParamsOrArgs ( queryLanguage , params , args ) {
12
+ return queryLanguage === 'JSONPath'
13
+ ? { Parameters : params }
14
+ : { Arguments : args === undefined ? params : args } ;
15
+ }
16
+
10
17
describe ( '#compileIamRole' , ( ) => {
11
18
let serverless ;
12
19
let serverlessStepFunctions ;
@@ -536,7 +543,7 @@ describe('#compileIamRole', () => {
536
543
expect ( policy . PolicyDocument . Statement [ 0 ] . Resource ) . to . have . lengthOf ( 0 ) ;
537
544
} ) ;
538
545
539
- it ( 'should give dynamodb permission for only tables referenced by state machine' , ( ) => {
546
+ itParam ( 'should give dynamodb permission for only tables referenced by state machine: ${value} ' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
540
547
const helloTable = 'hello' ;
541
548
const helloTableArn = {
542
549
'Fn::Join' : [
@@ -554,50 +561,42 @@ describe('#compileIamRole', () => {
554
561
id,
555
562
definition : {
556
563
StartAt : 'A' ,
564
+ QueryLanguage : queryLanguage ,
557
565
States : {
558
566
A : {
559
567
Type : 'Task' ,
560
568
Resource : resources [ 0 ] ,
561
- Parameters : {
562
- TableName : tableName ,
563
- } ,
569
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
564
570
Next : 'B' ,
565
571
} ,
566
572
B : {
567
573
Type : 'Task' ,
568
574
Resource : resources [ 1 ] ,
569
- Parameters : {
570
- TableName : tableName ,
571
- } ,
575
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
572
576
Next : 'C' ,
573
577
} ,
574
578
C : {
575
579
Type : 'Task' ,
576
580
Resource : 'arn:aws:states:::dynamodb:getItem' ,
577
- Parameters : {
578
- TableName : tableName ,
579
- } ,
581
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
580
582
Next : 'D' ,
581
583
} ,
582
584
D : {
583
585
Type : 'Task' ,
584
586
Resource : 'arn:aws:states:::dynamodb:deleteItem' ,
585
- Parameters : {
586
- TableName : tableName ,
587
- } ,
587
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
588
588
End : true ,
589
589
} ,
590
590
E : {
591
591
Type : 'Task' ,
592
592
Resource : 'arn:aws:states:::aws-sdk:dynamodb:updateTable' ,
593
- Parameters : {
594
- TableName : tableName ,
595
- } ,
593
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
596
594
End : true ,
597
595
} ,
598
596
} ,
599
597
} ,
600
598
} ) ;
599
+
601
600
serverless . service . stepFunctions = {
602
601
stateMachines : {
603
602
myStateMachine1 : genStateMachine ( 'StateMachine1' , helloTable , [ 'arn:aws:states:::dynamodb:updateItem' , 'arn:aws:states:::dynamodb:putItem' ] ) ,
@@ -636,7 +635,7 @@ describe('#compileIamRole', () => {
636
635
. to . be . deep . equal ( [ worldTableArn ] ) ;
637
636
} ) ;
638
637
639
- it ( 'should give dynamodb permission for table name imported from external stack' , ( ) => {
638
+ itParam ( 'should give dynamodb permission for table name imported from external stack' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
640
639
// Necessary to convince the region is in the gov cloud infrastructure.
641
640
const externalHelloTable = { 'Fn::ImportValue' : 'HelloStack:Table:Name' } ;
642
641
const helloTableArn = {
@@ -660,33 +659,25 @@ describe('#compileIamRole', () => {
660
659
A : {
661
660
Type : 'Task' ,
662
661
Resource : resources [ 0 ] ,
663
- Parameters : {
664
- TableName : tableName ,
665
- } ,
662
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
666
663
Next : 'B' ,
667
664
} ,
668
665
B : {
669
666
Type : 'Task' ,
670
667
Resource : resources [ 1 ] ,
671
- Parameters : {
672
- TableName : tableName ,
673
- } ,
668
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
674
669
Next : 'C' ,
675
670
} ,
676
671
C : {
677
672
Type : 'Task' ,
678
673
Resource : 'arn:aws:states:::dynamodb:getItem' ,
679
- Parameters : {
680
- TableName : tableName ,
681
- } ,
674
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
682
675
Next : 'D' ,
683
676
} ,
684
677
D : {
685
678
Type : 'Task' ,
686
679
Resource : 'arn:aws:states:::dynamodb:deleteItem' ,
687
- Parameters : {
688
- TableName : tableName ,
689
- } ,
680
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
690
681
End : true ,
691
682
} ,
692
683
} ,
@@ -731,7 +722,7 @@ describe('#compileIamRole', () => {
731
722
. to . be . deep . equal ( [ worldTableArn ] ) ;
732
723
} ) ;
733
724
734
- it ( 'should give dynamodb permission to index table whenever IndexName is provided' , ( ) => {
725
+ itParam ( 'should give dynamodb permission to index table whenever IndexName is provided: ${value} ' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
735
726
const helloTable = 'hello' ;
736
727
737
728
const genStateMachine = ( id , tableName ) => ( {
@@ -742,18 +733,13 @@ describe('#compileIamRole', () => {
742
733
A : {
743
734
Type : 'Task' ,
744
735
Resource : 'arn:aws:states:::aws-sdk:dynamodb:query' ,
745
- Parameters : {
746
- TableName : tableName ,
747
- } ,
736
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
748
737
Next : 'B' ,
749
738
} ,
750
739
B : {
751
740
Type : 'Task' ,
752
741
Resource : 'arn:aws:states:::aws-sdk:dynamodb:query' ,
753
- Parameters : {
754
- TableName : tableName ,
755
- IndexName : 'GSI1' ,
756
- } ,
742
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName , IndexName : 'GSI1' } ) ,
757
743
End : true ,
758
744
} ,
759
745
} ,
@@ -783,7 +769,7 @@ describe('#compileIamRole', () => {
783
769
} ) ;
784
770
} ) ;
785
771
786
- it ( 'should give dynamodb permission to * whenever TableName.$ is seen' , ( ) => {
772
+ itParam ( 'should give dynamodb permission to * whenever TableName.$ is seen: ${value} ' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
787
773
const helloTable = 'hello' ;
788
774
789
775
const genStateMachine = ( id , tableName ) => ( {
@@ -794,17 +780,13 @@ describe('#compileIamRole', () => {
794
780
A : {
795
781
Type : 'Task' ,
796
782
Resource : 'arn:aws:states:::dynamodb:updateItem' ,
797
- Parameters : {
798
- TableName : tableName ,
799
- } ,
783
+ ...getParamsOrArgs ( queryLanguage , { TableName : tableName } ) ,
800
784
Next : 'B' ,
801
785
} ,
802
786
B : {
803
787
Type : 'Task' ,
804
788
Resource : 'arn:aws:states:::dynamodb:updateItem' ,
805
- Parameters : {
806
- 'TableName.$' : '$.tableName' ,
807
- } ,
789
+ ...getParamsOrArgs ( queryLanguage , { 'TableName.$' : '$.tableName' } , { TableName : '{% $tableName %}' } ) ,
808
790
End : true ,
809
791
} ,
810
792
} ,
@@ -830,7 +812,7 @@ describe('#compileIamRole', () => {
830
812
expect ( policy . PolicyDocument . Statement [ 0 ] . Resource ) . to . equal ( '*' ) ;
831
813
} ) ;
832
814
833
- it ( 'should give dynamodb permission to table/TableName/index/* when IndexName.$ is seen' , ( ) => {
815
+ itParam ( 'should give dynamodb permission to table/TableName/index/* when IndexName.$ is seen: ${value} ' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
834
816
const helloTable = 'hello' ;
835
817
836
818
const genStateMachine = ( id , tableName ) => ( {
@@ -841,10 +823,10 @@ describe('#compileIamRole', () => {
841
823
A : {
842
824
Type : 'Task' ,
843
825
Resource : 'arn:aws:states:::aws-sdk:dynamodb:query' ,
844
- Parameters : {
826
+ ... getParamsOrArgs ( queryLanguage , {
845
827
TableName : tableName ,
846
828
'IndexName.$' : '$.myDynamicIndexName' ,
847
- } ,
829
+ } , { TableName : tableName , IndexName : '{% $myDynamicIndexName %}' } ) ,
848
830
End : true ,
849
831
} ,
850
832
} ,
@@ -870,7 +852,7 @@ describe('#compileIamRole', () => {
870
852
expect ( policy . PolicyDocument . Statement [ 0 ] . Resource [ 0 ] [ 'Fn::Join' ] [ 1 ] [ 5 ] ) . to . equal ( 'table/hello/index/*' ) ;
871
853
} ) ;
872
854
873
- it ( 'should give dynamodb permission to table/* whenever TableName.$ and IndexName.$ are seen' , ( ) => {
855
+ itParam ( 'should give dynamodb permission to table/* whenever TableName.$ and IndexName.$ are seen: ${value} ' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
874
856
const genStateMachine = id => ( {
875
857
id,
876
858
definition : {
@@ -879,10 +861,10 @@ describe('#compileIamRole', () => {
879
861
A : {
880
862
Type : 'Task' ,
881
863
Resource : 'arn:aws:states:::aws-sdk:dynamodb:query' ,
882
- Parameters : {
864
+ ... getParamsOrArgs ( queryLanguage , {
883
865
'TableName.$' : '$.myDynamicTableName' ,
884
866
'IndexName.$' : '$.myDynamicIndexName' ,
885
- } ,
867
+ } , { TableName : '{% $myDynamicTableName %}' , IndexName : '{% $myDynamicIndexName %}' } ) ,
886
868
End : true ,
887
869
} ,
888
870
} ,
@@ -908,7 +890,7 @@ describe('#compileIamRole', () => {
908
890
expect ( policy . PolicyDocument . Statement [ 0 ] . Resource [ 0 ] ) . to . equal ( '*' ) ;
909
891
} ) ;
910
892
911
- it ( 'should give batch dynamodb permission for only tables referenced by state machine' , ( ) => {
893
+ itParam ( 'should give batch dynamodb permission for only tables referenced by state machine: ${value} ' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
912
894
const helloTable = 'hello' ;
913
895
const helloTableArn = {
914
896
'Fn::Join' : [
@@ -930,21 +912,13 @@ describe('#compileIamRole', () => {
930
912
A : {
931
913
Type : 'Task' ,
932
914
Resource : 'arn:aws:states:::aws-sdk:dynamodb:batchWriteItem' ,
933
- Parameters : {
934
- RequestItems : {
935
- [ tableName ] : [ ] ,
936
- } ,
937
- } ,
915
+ ...getParamsOrArgs ( queryLanguage , { RequestItems : { [ tableName ] : [ ] } } ) ,
938
916
Next : 'B' ,
939
917
} ,
940
918
B : {
941
919
Type : 'Task' ,
942
920
Resource : 'arn:aws:states:::aws-sdk:dynamodb:batchGetItem' ,
943
- Parameters : {
944
- RequestItems : {
945
- [ tableName ] : { } ,
946
- } ,
947
- } ,
921
+ ...getParamsOrArgs ( queryLanguage , { RequestItems : { [ tableName ] : { } } } ) ,
948
922
End : true ,
949
923
} ,
950
924
} ,
@@ -977,7 +951,7 @@ describe('#compileIamRole', () => {
977
951
. to . be . deep . equal ( [ worldTableArn ] ) ;
978
952
} ) ;
979
953
980
- it ( 'should give batch dynamodb permission to * whenever RequestItems.$ is seen' , ( ) => {
954
+ itParam ( 'should give batch dynamodb permission to * whenever RequestItems.$ is seen: ${value} ' , [ 'JSONPath' , 'JSONata' ] , ( queryLanguage ) => {
981
955
const genStateMachine = id => ( {
982
956
id,
983
957
definition : {
@@ -986,19 +960,13 @@ describe('#compileIamRole', () => {
986
960
A : {
987
961
Type : 'Task' ,
988
962
Resource : 'arn:aws:states:::aws-sdk:dynamodb:batchWriteItem' ,
989
- Parameters : {
990
- RequestItems : {
991
- tableName : [ ] ,
992
- } ,
993
- } ,
963
+ ...getParamsOrArgs ( queryLanguage , { RequestItems : { tableName : [ ] } } ) ,
994
964
Next : 'B' ,
995
965
} ,
996
966
B : {
997
967
Type : 'Task' ,
998
968
Resource : 'arn:aws:states:::aws-sdk:dynamodb:batchWriteItem' ,
999
- Parameters : {
1000
- 'RequestItems.$' : '$.requestItems' ,
1001
- } ,
969
+ ...getParamsOrArgs ( queryLanguage , { 'RequestItems.$' : '$.requestItems' } , { RequestItems : '{% $requestItems %}' } ) ,
1002
970
End : true ,
1003
971
} ,
1004
972
} ,
0 commit comments