@@ -122,8 +122,8 @@ describe('Demo URL Processor', () => {
122122
123123 await runDemoUrlProcessor ( message , context ) ;
124124
125- // Should log error about missing name and tenantId and use fallback
126- expect ( context . log . error . calledWith ( 'Organization name and tenantId are missing, using default tenant ID' ) ) . to . be . true ;
125+ // Should log error about using default tenant ID
126+ expect ( context . log . error . calledWith ( 'Using default tenant ID' ) ) . to . be . true ;
127127 const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@default-tenant/sites-optimizer/sites/test-site-id/home' ;
128128 expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
129129 } ) ;
@@ -194,12 +194,116 @@ describe('Demo URL Processor', () => {
194194 organizationId : 'test-org-id' ,
195195 } ) ) . to . be . true ;
196196
197- // Should log error about missing name and tenantId
198- expect ( context . log . error . calledWith ( 'Organization name and tenantId are missing, using default tenant ID' ) ) . to . be . true ;
197+ // Should log error about using default tenant ID
198+ expect ( context . log . error . calledWith ( 'Using default tenant ID' ) ) . to . be . true ;
199199
200200 // Should log the completion message with default tenant
201201 const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@default-tenant/sites-optimizer/sites/test-site-id/home' ;
202202 expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
203203 } ) ;
204+
205+ it ( 'should get tenantId from imsClient when not available in organization' , async ( ) => {
206+ // Mock Organization.findById to return organization without tenantId
207+ context . dataAccess . Organization . findById . resolves ( {
208+ name : 'Adobe Sites Engineering' ,
209+ imsOrgId : '8C6043F15F43B6390A49401A@AdobeOrg' ,
210+ // tenantId property is missing
211+ } ) ;
212+
213+ // Mock imsClient to return tenantId
214+ context . imsClient = {
215+ getImsOrganizationDetails : sinon . stub ( ) . resolves ( {
216+ tenantId : 'ims-tenant-id-from-client' ,
217+ } ) ,
218+ } ;
219+
220+ await runDemoUrlProcessor ( message , context ) ;
221+
222+ // Should call imsClient.getImsOrganizationDetails
223+ expect ( context . imsClient . getImsOrganizationDetails . calledWith ( '8C6043F15F43B6390A49401A@AdobeOrg' ) ) . to . be . true ;
224+
225+ // Should use the tenantId from imsClient
226+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@ims-tenant-id-from-client/sites-optimizer/sites/test-site-id/home' ;
227+ expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
228+ } ) ;
229+
230+ it ( 'should fallback to name when imsClient fails and tenantId is missing' , async ( ) => {
231+ // Mock Organization.findById to return organization without tenantId
232+ context . dataAccess . Organization . findById . resolves ( {
233+ name : 'Adobe Sites Engineering' ,
234+ imsOrgId : '8C6043F15F43B6390A49401A@AdobeOrg' ,
235+ // tenantId property is missing
236+ } ) ;
237+
238+ // Mock imsClient to throw an error
239+ context . imsClient = {
240+ getImsOrganizationDetails : sinon . stub ( ) . rejects ( new Error ( 'IMS API error' ) ) ,
241+ } ;
242+
243+ await runDemoUrlProcessor ( message , context ) ;
244+
245+ // Should call imsClient.getImsOrganizationDetails
246+ expect ( context . imsClient . getImsOrganizationDetails . calledWith ( '8C6043F15F43B6390A49401A@AdobeOrg' ) ) . to . be . true ;
247+
248+ // Should log error about IMS API failure
249+ expect ( context . log . error . calledWith ( 'Error retrieving IMS Org details: IMS API error' ) ) . to . be . true ;
250+
251+ // Should fallback to name-based tenant
252+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home' ;
253+ expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
254+ } ) ;
255+
256+ it ( 'should fallback to DEFAULT_TENANT_ID when imsClient fails and name is missing' , async ( ) => {
257+ // Mock Organization.findById to return organization without tenantId and name
258+ context . dataAccess . Organization . findById . resolves ( {
259+ imsOrgId : '8C6043F15F43B6390A49401A@AdobeOrg' ,
260+ // tenantId and name properties are missing
261+ } ) ;
262+
263+ // Mock imsClient to throw an error
264+ context . imsClient = {
265+ getImsOrganizationDetails : sinon . stub ( ) . rejects ( new Error ( 'IMS API error' ) ) ,
266+ } ;
267+
268+ // Set default tenant ID
269+ context . env . DEFAULT_TENANT_ID = 'default-tenant' ;
270+
271+ await runDemoUrlProcessor ( message , context ) ;
272+
273+ // Should call imsClient.getImsOrganizationDetails
274+ expect ( context . imsClient . getImsOrganizationDetails . calledWith ( '8C6043F15F43B6390A49401A@AdobeOrg' ) ) . to . be . true ;
275+
276+ // Should log error about IMS API failure
277+ expect ( context . log . error . calledWith ( 'Error retrieving IMS Org details: IMS API error' ) ) . to . be . true ;
278+
279+ // Should log error about using default tenant ID
280+ expect ( context . log . error . calledWith ( 'Using default tenant ID' ) ) . to . be . true ;
281+
282+ // Should use DEFAULT_TENANT_ID as final fallback
283+ const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@default-tenant/sites-optimizer/sites/test-site-id/home' ;
284+ expect ( context . log . info . calledWith ( `Setup complete! Access your demo environment here: ${ expectedDemoUrl } ` ) ) . to . be . true ;
285+ } ) ;
286+
287+ it ( 'should send Slack message when using default tenant ID' , async ( ) => {
288+ // Mock Organization.findById to return organization without tenantId and name
289+ context . dataAccess . Organization . findById . resolves ( {
290+ imsOrgId : '8C6043F15F43B6390A49401A@AdobeOrg' ,
291+ // tenantId and name properties are missing
292+ } ) ;
293+
294+ // Mock imsClient to throw an error
295+ context . imsClient = {
296+ getImsOrganizationDetails : sinon . stub ( ) . rejects ( new Error ( 'IMS API error' ) ) ,
297+ } ;
298+
299+ // Set default tenant ID
300+ context . env . DEFAULT_TENANT_ID = 'default-tenant' ;
301+
302+ await runDemoUrlProcessor ( message , context ) ;
303+
304+ // Should send Slack message about using default tenant ID
305+ // Note: We can't directly test the say function call, but we can verify the log message
306+ expect ( context . log . error . calledWith ( 'Using default tenant ID' ) ) . to . be . true ;
307+ } ) ;
204308 } ) ;
205309} ) ;
0 commit comments