@@ -37,6 +37,14 @@ describe('Demo URL Processor', () => {
3737 // Mock context
3838 context = new MockContextBuilder ( )
3939 . withSandbox ( sandbox )
40+ . withDataAccess ( {
41+ Organization : {
42+ findById : sandbox . stub ( ) . resolves ( {
43+ name : 'Adobe Sites Engineering' ,
44+ imsOrgId : '8C6043F15F43B6390A49401A@AdobeOrg' ,
45+ } ) ,
46+ } ,
47+ } )
4048 . build ( ) ;
4149
4250 // Mock message
@@ -56,22 +64,92 @@ describe('Demo URL Processor', () => {
5664
5765 describe ( 'runDemoUrlProcessor' , ( ) => {
5866 it ( 'should process demo URL successfully' , async ( ) => {
67+ // Set up the IMSORG_TO_TENANT secret in context
68+ context . env . IMSORG_TO_TENANT = JSON . stringify ( {
69+ '8C6043F15F43B6390A49401A@AdobeOrg' : 'aem-sites-engineering' ,
70+ } ) ;
71+
5972 await runDemoUrlProcessor ( message , context ) ;
6073 expect ( context . log . info . calledWith ( 'Processing demo url for site:' , {
6174 taskType : 'demo-url-processor' ,
6275 siteId : 'test-site-id' ,
6376 siteUrl : 'https://example.com' ,
6477 organizationId : 'test-org-id' ,
6578 } ) ) . to . be . true ;
66- const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aemrefdemoshared /sites-optimizer/sites/test-site-id/home' ;
79+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aem-sites-engineering /sites-optimizer/sites/test-site-id/home' ;
6780 expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
6881 } ) ;
6982
7083 it ( 'should handle missing slackContext in taskContext' , async ( ) => {
84+ // Set up the IMSORG_TO_TENANT secret in context
85+ context . env . IMSORG_TO_TENANT = JSON . stringify ( {
86+ '8C6043F15F43B6390A49401A@AdobeOrg' : 'aem-sites-engineering' ,
87+ } ) ;
88+
7189 delete message . taskContext . slackContext ;
7290 await runDemoUrlProcessor ( message , context ) ;
73- const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aemrefdemoshared/sites-optimizer/sites/test-site-id/home' ;
91+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aem-sites-engineering/sites-optimizer/sites/test-site-id/home' ;
92+ expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
93+ } ) ;
94+
95+ it ( 'should use IMSORG_TO_TENANT mapping when available' , async ( ) => {
96+ // Set up the IMSORG_TO_TENANT secret in context
97+ context . env . IMSORG_TO_TENANT = JSON . stringify ( {
98+ '8C6043F15F43B6390A49401A@AdobeOrg' : 'aem-sites-engineering' ,
99+ } ) ;
100+
101+ await runDemoUrlProcessor ( message , context ) ;
102+
103+ // Should use the mapped tenant name instead of the fallback
104+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@aem-sites-engineering/sites-optimizer/sites/test-site-id/home' ;
105+ expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
106+ } ) ;
107+
108+ it ( 'should fallback to name-based tenant when IMSORG_TO_TENANT mapping is not available' , async ( ) => {
109+ // Don't set IMSORG_TO_TENANT secret
110+ delete context . env . IMSORG_TO_TENANT ;
111+
112+ await runDemoUrlProcessor ( message , context ) ;
113+
114+ // Should use the fallback name-based tenant (lowercase, no spaces)
115+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home' ;
74116 expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
75117 } ) ;
118+
119+ it ( 'should fallback to name-based tenant when IMSORG_TO_TENANT mapping is invalid JSON' , async ( ) => {
120+ // Set invalid JSON in IMSORG_TO_TENANT secret
121+ context . env . IMSORG_TO_TENANT = 'invalid-json' ;
122+
123+ await runDemoUrlProcessor ( message , context ) ;
124+
125+ // Should use the fallback name-based tenant
126+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home' ;
127+ expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
128+ } ) ;
129+
130+ it ( 'should fallback to name-based tenant when IMSORG_TO_TENANT mapping does not contain the imsOrgId' , async ( ) => {
131+ // Set IMSORG_TO_TENANT secret with different mapping
132+ context . env . IMSORG_TO_TENANT = JSON . stringify ( {
133+ 'DIFFERENT_ORG_ID@AdobeOrg' : 'different-team' ,
134+ } ) ;
135+
136+ await runDemoUrlProcessor ( message , context ) ;
137+
138+ // Should use the fallback name-based tenant since the imsOrgId is not in the mapping
139+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home' ;
140+ expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
141+ } ) ;
142+
143+ it ( 'should handle organization not found error' , async ( ) => {
144+ // Mock Organization.findById to return null
145+ context . dataAccess . Organization . findById . resolves ( null ) ;
146+
147+ await runDemoUrlProcessor ( message , context ) ;
148+
149+ // Should log error and return early
150+ expect ( context . log . error . calledWith ( 'Organization not found for organizationId: test-org-id' ) ) . to . be . true ;
151+ // Should not log the success message
152+ expect ( context . log . info . calledWithMatch ( sinon . match ( 'Setup complete!' ) ) ) . to . be . false ;
153+ } ) ;
76154 } ) ;
77155} ) ;
0 commit comments