1+ import * as iam from '@aws-cdk/aws-iam' ;
12import '@aws-cdk/assert/jest' ;
23import { App , SecretValue , Stack } from '@aws-cdk/core' ;
34import * as amplify from '../lib' ;
@@ -120,3 +121,113 @@ test('throws at synthesis without subdomains', () => {
120121 // THEN
121122 expect ( ( ) => app . synth ( ) ) . toThrow ( / T h e d o m a i n d o e s n ' t c o n t a i n a n y s u b d o m a i n s / ) ;
122123} ) ;
124+
125+ test ( 'auto subdomain all branches' , ( ) => {
126+ // GIVEN
127+ const stack = new Stack ( ) ;
128+ const app = new amplify . App ( stack , 'App' , {
129+ sourceCodeProvider : new amplify . GitHubSourceCodeProvider ( {
130+ owner : 'aws' ,
131+ repository : 'aws-cdk' ,
132+ oauthToken : SecretValue . plainText ( 'secret' ) ,
133+ } ) ,
134+ } ) ;
135+ const prodBranch = app . addBranch ( 'master' ) ;
136+
137+ // WHEN
138+ const domain = app . addDomain ( 'amazon.com' , {
139+ enableAutoSubdomain : true ,
140+ } ) ;
141+ domain . mapRoot ( prodBranch ) ;
142+
143+ // THEN
144+ expect ( stack ) . toHaveResource ( 'AWS::Amplify::Domain' , {
145+ EnableAutoSubDomain : true ,
146+ AutoSubDomainCreationPatterns : [
147+ '*' ,
148+ 'pr*' ,
149+ ] ,
150+ AutoSubDomainIAMRole : {
151+ 'Fn::GetAtt' : [
152+ 'AppRole1AF9B530' ,
153+ 'Arn' ,
154+ ] ,
155+ } ,
156+ } ) ;
157+ } ) ;
158+
159+ test ( 'auto subdomain some branches' , ( ) => {
160+ // GIVEN
161+ const stack = new Stack ( ) ;
162+ const app = new amplify . App ( stack , 'App' , {
163+ sourceCodeProvider : new amplify . GitHubSourceCodeProvider ( {
164+ owner : 'aws' ,
165+ repository : 'aws-cdk' ,
166+ oauthToken : SecretValue . plainText ( 'secret' ) ,
167+ } ) ,
168+ } ) ;
169+ const prodBranch = app . addBranch ( 'master' ) ;
170+
171+ // WHEN
172+ const domain = app . addDomain ( 'amazon.com' , {
173+ enableAutoSubdomain : true ,
174+ autoSubdomainCreationPatterns : [ 'features/**' ] ,
175+ } ) ;
176+ domain . mapRoot ( prodBranch ) ;
177+
178+ // THEN
179+ expect ( stack ) . toHaveResource ( 'AWS::Amplify::Domain' , {
180+ EnableAutoSubDomain : true ,
181+ AutoSubDomainCreationPatterns : [ 'features/**' ] ,
182+ AutoSubDomainIAMRole : {
183+ 'Fn::GetAtt' : [
184+ 'AppRole1AF9B530' ,
185+ 'Arn' ,
186+ ] ,
187+ } ,
188+ } ) ;
189+ } ) ;
190+
191+ test ( 'auto subdomain with IAM role' , ( ) => {
192+ // GIVEN
193+ const stack = new Stack ( ) ;
194+ const app = new amplify . App ( stack , 'App' , {
195+ sourceCodeProvider : new amplify . GitHubSourceCodeProvider ( {
196+ owner : 'aws' ,
197+ repository : 'aws-cdk' ,
198+ oauthToken : SecretValue . plainText ( 'secret' ) ,
199+ } ) ,
200+ role : iam . Role . fromRoleArn (
201+ stack ,
202+ 'AmplifyRole' ,
203+ `arn:aws:iam::${ Stack . of ( stack ) . account } :role/AmplifyRole` ,
204+ { mutable : false } ,
205+ ) ,
206+ } ) ;
207+ const prodBranch = app . addBranch ( 'master' ) ;
208+
209+ // WHEN
210+ const domain = app . addDomain ( 'amazon.com' , {
211+ enableAutoSubdomain : true ,
212+ autoSubdomainCreationPatterns : [ 'features/**' ] ,
213+ } ) ;
214+ domain . mapRoot ( prodBranch ) ;
215+
216+ // THEN
217+ expect ( stack ) . toHaveResource ( 'AWS::Amplify::Domain' , {
218+ EnableAutoSubDomain : true ,
219+ AutoSubDomainCreationPatterns : [ 'features/**' ] ,
220+ AutoSubDomainIAMRole : {
221+ 'Fn::Join' : [
222+ '' ,
223+ [
224+ 'arn:aws:iam::' ,
225+ {
226+ Ref : 'AWS::AccountId' ,
227+ } ,
228+ ':role/AmplifyRole' ,
229+ ] ,
230+ ] ,
231+ } ,
232+ } ) ;
233+ } ) ;
0 commit comments