1
+ import * as AWS from 'aws-sdk' ;
2
+ import * as AWSMock from 'aws-sdk-mock' ;
3
+ import { ISDK } from '../../../lib/api/aws-auth' ;
4
+ import { determineAllowCrossAccountAssetPublishing , getBootstrapStackInfo } from '../../../lib/api/util/checks' ;
5
+
6
+ describe ( 'determineAllowCrossAccountAssetPublishing' , ( ) => {
7
+ let mockSDK : ISDK ;
8
+
9
+ beforeEach ( ( ) => {
10
+ mockSDK = {
11
+ cloudFormation : ( ) => new AWS . CloudFormation ( ) ,
12
+ } as ISDK ;
13
+ } ) ;
14
+
15
+ afterEach ( ( ) => {
16
+ AWSMock . restore ( ) ;
17
+ } ) ;
18
+
19
+ it ( 'should return true when hasStagingBucket is false' , async ( ) => {
20
+ AWSMock . mock ( 'CloudFormation' , 'describeStacks' , ( _params : any , callback : Function ) => {
21
+ callback ( null , {
22
+ Stacks : [ {
23
+ Outputs : [ { OutputKey : 'BootstrapVersion' , OutputValue : '1' } ] ,
24
+ } ] ,
25
+ } ) ;
26
+ } ) ;
27
+
28
+ AWSMock . mock ( 'CloudFormation' , 'describeStackResources' , ( _params : any , callback : Function ) => {
29
+ callback ( null , { StackResources : [ ] } ) ;
30
+ } ) ;
31
+
32
+ const result = await determineAllowCrossAccountAssetPublishing ( mockSDK ) ;
33
+ expect ( result ) . toBe ( true ) ;
34
+ } ) ;
35
+
36
+ it ( 'should return true when bootstrap version is >= 21' , async ( ) => {
37
+ AWSMock . mock ( 'CloudFormation' , 'describeStacks' , ( _params : any , callback : Function ) => {
38
+ callback ( null , {
39
+ Stacks : [ {
40
+ Outputs : [ { OutputKey : 'BootstrapVersion' , OutputValue : '21' } ] ,
41
+ } ] ,
42
+ } ) ;
43
+ } ) ;
44
+
45
+ AWSMock . mock ( 'CloudFormation' , 'describeStackResources' , ( _params : any , callback : Function ) => {
46
+ callback ( null , { StackResources : [ { ResourceType : 'AWS::S3::Bucket' , PhysicalResourceId : 'some-bucket' } ] } ) ;
47
+ } ) ;
48
+
49
+ const result = await determineAllowCrossAccountAssetPublishing ( mockSDK ) ;
50
+ expect ( result ) . toBe ( true ) ;
51
+ } ) ;
52
+
53
+ it ( 'should return false for other scenarios' , async ( ) => {
54
+ AWSMock . mock ( 'CloudFormation' , 'describeStacks' , ( _params : any , callback : Function ) => {
55
+ callback ( null , {
56
+ Stacks : [ {
57
+ Outputs : [ { OutputKey : 'BootstrapVersion' , OutputValue : '20' } ] ,
58
+ } ] ,
59
+ } ) ;
60
+ } ) ;
61
+
62
+ AWSMock . mock ( 'CloudFormation' , 'describeStackResources' , ( _params : any , callback : Function ) => {
63
+ callback ( null , { StackResources : [ { ResourceType : 'AWS::S3::Bucket' , PhysicalResourceId : 'some-bucket' } ] } ) ;
64
+ } ) ;
65
+
66
+ const result = await determineAllowCrossAccountAssetPublishing ( mockSDK ) ;
67
+ expect ( result ) . toBe ( false ) ;
68
+ } ) ;
69
+ } ) ;
70
+
71
+ describe ( 'getBootstrapStackInfo' , ( ) => {
72
+ let mockSDK : ISDK ;
73
+
74
+ beforeEach ( ( ) => {
75
+ mockSDK = {
76
+ cloudFormation : ( ) => new AWS . CloudFormation ( ) ,
77
+ } as ISDK ;
78
+ } ) ;
79
+
80
+ afterEach ( ( ) => {
81
+ AWSMock . restore ( ) ;
82
+ } ) ;
83
+
84
+ it ( 'should return correct BootstrapStackInfo' , async ( ) => {
85
+ AWSMock . mock ( 'CloudFormation' , 'describeStacks' , ( _params : any , callback : Function ) => {
86
+ callback ( null , {
87
+ Stacks : [ {
88
+ Outputs : [ { OutputKey : 'BootstrapVersion' , OutputValue : '21' } ] ,
89
+ } ] ,
90
+ } ) ;
91
+ } ) ;
92
+
93
+ AWSMock . mock ( 'CloudFormation' , 'describeStackResources' , ( _params : any , callback : Function ) => {
94
+ callback ( null , { StackResources : [ { ResourceType : 'AWS::S3::Bucket' , PhysicalResourceId : 'some-bucket' } ] } ) ;
95
+ } ) ;
96
+
97
+ const result = await getBootstrapStackInfo ( mockSDK , 'CDKToolkit' ) ;
98
+ expect ( result ) . toEqual ( {
99
+ hasStagingBucket : true ,
100
+ bootstrapVersion : 21 ,
101
+ } ) ;
102
+ } ) ;
103
+
104
+ it ( 'should throw error when stack is not found' , async ( ) => {
105
+ AWSMock . mock ( 'CloudFormation' , 'describeStacks' , ( _params : any , callback : Function ) => {
106
+ callback ( null , { Stacks : [ ] } ) ;
107
+ } ) ;
108
+
109
+ await expect ( getBootstrapStackInfo ( mockSDK , 'CDKToolkit' ) ) . rejects . toThrow ( 'Toolkit stack CDKToolkit not found' ) ;
110
+ } ) ;
111
+
112
+ it ( 'should throw error when BootstrapVersion output is missing' , async ( ) => {
113
+ AWSMock . mock ( 'CloudFormation' , 'describeStacks' , ( _params : any , callback : Function ) => {
114
+ callback ( null , {
115
+ Stacks : [ {
116
+ Outputs : [ ] ,
117
+ } ] ,
118
+ } ) ;
119
+ } ) ;
120
+
121
+ await expect ( getBootstrapStackInfo ( mockSDK , 'CDKToolkit' ) ) . rejects . toThrow ( 'Unable to find BootstrapVersion output in the toolkit stack CDKToolkit' ) ;
122
+ } ) ;
123
+ } ) ;
0 commit comments