-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-url-processor.test.js
More file actions
154 lines (126 loc) · 5.99 KB
/
demo-url-processor.test.js
File metadata and controls
154 lines (126 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright 2025 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env mocha */
import { expect } from 'chai';
import sinon from 'sinon';
import { MockContextBuilder } from '../../shared.js';
// Dynamic import for ES modules
let runDemoUrlProcessor;
describe('Demo URL Processor', () => {
let context;
let message;
beforeEach(async () => {
// Dynamic import
const handlerModule = await import('../../../src/tasks/demo-url-processor/handler.js');
runDemoUrlProcessor = handlerModule.runDemoUrlProcessor;
// Reset all stubs
sinon.restore();
// Create sandbox
const sandbox = sinon.createSandbox();
// Mock context
context = new MockContextBuilder()
.withSandbox(sandbox)
.withDataAccess({
Organization: {
findById: sandbox.stub().resolves({
name: 'Adobe Sites Engineering',
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
}),
},
})
.build();
// Mock message
message = {
siteId: 'test-site-id',
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
organizationId: 'test-org-id',
taskContext: {
experienceUrl: 'https://example.com',
slackContext: 'test-slack-context',
},
};
});
afterEach(() => {
sinon.restore();
});
describe('runDemoUrlProcessor', () => {
it('should handle organization not found error', async () => {
// Mock Organization.findById to return null
context.dataAccess.Organization.findById.resolves(null);
await runDemoUrlProcessor(message, context);
// Should log error and return early
expect(context.log.error.calledWith('Organization not found for organizationId: test-org-id')).to.be.true;
// Should not log the success message
expect(context.log.info.calledWithMatch(sinon.match('Setup complete!'))).to.be.false;
});
it('should handle organization with missing name property', async () => {
// Mock Organization.findById to return organization without name
context.dataAccess.Organization.findById.resolves({
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
// name property is missing
});
// Set default tenant ID
context.env.DEFAULT_TENANT_ID = 'default-tenant';
await runDemoUrlProcessor(message, context);
// Should log error about missing name and use fallback
expect(context.log.error.calledWith('Organization name is missing, using default tenant ID')).to.be.true;
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@default-tenant/sites-optimizer/sites/test-site-id/home';
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
});
it('should handle organization with missing tenantId property', async () => {
// Mock Organization.findById to return organization without tenantId
context.dataAccess.Organization.findById.resolves({
name: 'Adobe Sites Engineering',
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
// tenantId property is missing
});
// Set default tenant ID
context.env.DEFAULT_TENANT_ID = 'default-tenant';
await runDemoUrlProcessor(message, context);
// Should log error about missing name and use fallback
expect(context.log.error.calledWith('Organization name is missing, using default tenant ID')).to.be.true;
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@default-tenant/sites-optimizer/sites/test-site-id/home';
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
});
it('should use organization name when both name and tenantId are present', async () => {
// Mock Organization.findById to return organization with both name and tenantId
context.dataAccess.Organization.findById.resolves({
name: 'Adobe Sites Engineering',
tenantId: 'adobe-sites-engineering',
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
});
await runDemoUrlProcessor(message, context);
// Should use the name-based tenant (lowercase, no spaces)
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home';
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
});
it('should handle successful demo URL processing', async () => {
// Mock Organization.findById to return organization with both name and tenantId
context.dataAccess.Organization.findById.resolves({
name: 'Adobe Sites Engineering',
tenantId: 'adobe-sites-engineering',
imsOrgId: '8C6043F15F43B6390A49401A@AdobeOrg',
});
await runDemoUrlProcessor(message, context);
// Should log the processing message
expect(context.log.info.calledWith('Processing demo url for site:', {
taskType: 'demo-url-processor',
siteId: 'test-site-id',
experienceUrl: 'https://example.com',
organizationId: 'test-org-id',
})).to.be.true;
// Should log the completion message
const expectedDemoUrl = 'https://example.com?organizationId=test-org-id#/@adobesitesengineering/sites-optimizer/sites/test-site-id/home';
expect(context.log.info.calledWith(`Setup complete! Access your demo environment here: ${expectedDemoUrl}`)).to.be.true;
});
});
});