@@ -441,4 +441,136 @@ describe('plugin tests', function(this: any) {
441441 } ) ;
442442 } ) ;
443443
444+ describe ( 'support new provider.iam property' , ( ) => {
445+ const getLambdaTestStatements = ( ) : any [ ] => {
446+ const plugin = new Plugin ( serverless ) ;
447+
448+ const compiledResources = serverless . service . provider . compiledCloudFormationTemplate . Resources ;
449+ plugin . createRolesPerFunction ( ) ;
450+ const helloInherit = compiledResources . HelloInheritIamRoleLambdaExecution ;
451+ assert . isNotEmpty ( helloInherit ) ;
452+
453+ return helloInherit . Properties . Policies [ 0 ] . PolicyDocument . Statement ;
454+ }
455+
456+ it ( 'no global iam and iamRoleStatements properties' , ( ) => {
457+ _ . set ( serverless . service , 'provider.iam' , undefined ) ;
458+ _ . set ( serverless . service , 'provider.iamRoleStatements' , undefined ) ;
459+
460+ const statements = getLambdaTestStatements ( ) ;
461+
462+ assert . isTrue ( statements . find ( ( s ) => s . Action [ 0 ] === 'xray:PutTelemetryRecords' ) === undefined ,
463+ 'provider.iamRoleStatements values shouldn\'t exists' ) ;
464+ assert . isObject (
465+ statements . find ( ( s ) => s . Action [ 0 ] === 'dynamodb:GetItem' ) ,
466+ 'per function statements imported upon inherit' ,
467+ ) ;
468+ } ) ;
469+
470+ describe ( 'new iam property takes precedence over old iamRoleStatements property' , ( ) => {
471+ it ( 'empty iam object' , ( ) => {
472+ _ . set ( serverless . service , 'provider.iam' , { } ) ;
473+
474+ const statements = getLambdaTestStatements ( ) ;
475+
476+ assert . isTrue ( statements . find ( ( s ) => s . Action [ 0 ] === 'xray:PutTelemetryRecords' ) === undefined ,
477+ 'provider.iamRoleStatements values shouldn\'t exists' ) ;
478+ assert . isObject (
479+ statements . find ( ( s ) => s . Action [ 0 ] === 'dynamodb:GetItem' ) ,
480+ 'per function statements imported upon inherit' ,
481+ ) ;
482+ } ) ;
483+
484+ it ( 'no role property' , ( ) => {
485+ _ . set ( serverless . service , 'provider.iam' , {
486+ deploymentRole : 'arn:aws:iam::123456789012:role/deploy-role'
487+ } ) ;
488+
489+ const statements = getLambdaTestStatements ( ) ;
490+
491+ assert . isTrue ( statements . find ( ( s ) => s . Action [ 0 ] === 'xray:PutTelemetryRecords' ) === undefined ,
492+ 'provider.iamRoleStatements values shouldn\'t exists' ) ;
493+ assert . isObject (
494+ statements . find ( ( s ) => s . Action [ 0 ] === 'dynamodb:GetItem' ) ,
495+ 'per function statements imported upon inherit' ,
496+ ) ;
497+ } ) ;
498+
499+ it ( 'role property set to role ARN' , ( ) => {
500+ _ . set ( serverless . service , 'provider.iam' , {
501+ role : 'arn:aws:iam::0123456789:role//my/default/path/roleInMyAccount'
502+ } ) ;
503+
504+ const statements = getLambdaTestStatements ( ) ;
505+
506+ assert . isTrue ( statements . find ( ( s ) => s . Action [ 0 ] === 'xray:PutTelemetryRecords' ) === undefined ,
507+ 'provider.iamRoleStatements values shouldn\'t exists' ) ;
508+ assert . isObject (
509+ statements . find ( ( s ) => s . Action [ 0 ] === 'dynamodb:GetItem' ) ,
510+ 'per function statements imported upon inherit' ,
511+ ) ;
512+ } ) ;
513+
514+ it ( 'role is set without statements' , ( ) => {
515+ _ . set ( serverless . service , 'provider.iam' , {
516+ role : {
517+ managedPolicies : [ 'arn:aws:iam::123456789012:user/*' ]
518+ }
519+ } ) ;
520+
521+ const statements = getLambdaTestStatements ( ) ;
522+
523+ assert . isTrue ( statements . find ( ( s ) => s . Action [ 0 ] === 'xray:PutTelemetryRecords' ) === undefined ,
524+ 'provider.iamRoleStatements values shouldn\'t exists' ) ;
525+ assert . isObject (
526+ statements . find ( ( s ) => s . Action [ 0 ] === 'dynamodb:GetItem' ) ,
527+ 'per function statements imported upon inherit' ,
528+ ) ;
529+ } ) ;
530+
531+ it ( 'empty statements' , ( ) => {
532+ _ . set ( serverless . service , 'provider.iam' , {
533+ role : {
534+ statements : [ ]
535+ }
536+ } ) ;
537+
538+ const statements = getLambdaTestStatements ( ) ;
539+
540+ assert . isTrue ( statements . find ( ( s ) => s . Action [ 0 ] === 'xray:PutTelemetryRecords' ) === undefined ,
541+ 'provider.iamRoleStatements values shouldn\'t exists' ) ;
542+ assert . isObject (
543+ statements . find ( ( s ) => s . Action [ 0 ] === 'dynamodb:GetItem' ) ,
544+ 'per function statements imported upon inherit' ,
545+ ) ;
546+ } ) ;
547+ } ) ;
548+
549+ it ( 'global iam role statements exists in lambda role statements' , ( ) => {
550+ _ . set ( serverless . service , 'provider.iam' , {
551+ role : {
552+ statements : [ {
553+ Effect : 'Allow' ,
554+ Action : [
555+ 'ec2:CreateNetworkInterface'
556+ ] ,
557+ Resource : '*'
558+ } ]
559+ }
560+ } ) ;
561+
562+ const statements = getLambdaTestStatements ( ) ;
563+
564+ assert . isObject (
565+ statements . find ( ( s ) => s . Action [ 0 ] === 'ec2:CreateNetworkInterface' ) ,
566+ 'global iam role statements exists' ,
567+ ) ;
568+ assert . isTrue ( statements . find ( ( s ) => s . Action [ 0 ] === 'xray:PutTelemetryRecords' ) === undefined ,
569+ 'old provider.iamRoleStatements shouldn\'t exists' ) ;
570+ assert . isObject (
571+ statements . find ( ( s ) => s . Action [ 0 ] === 'dynamodb:GetItem' ) ,
572+ 'per function statements imported upon inherit' ,
573+ ) ;
574+ } ) ;
575+ } ) ;
444576} ) ;
0 commit comments