Skip to content

Commit 9309df6

Browse files
authored
feat: introduce AMS support for preflight (#1459)
1 parent 6ca2e06 commit 9309df6

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/controllers/preflight.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ function PreflightController(ctx, log, env) {
9090
* @param {Object} context.data - The request data
9191
* @param {string[]} context.data.urls - Array of URLs to process
9292
* @param {string} context.data.step - The audit step
93+
* @param {string} context.data.siteId - The siteId, if it's an AMS site
9394
* @returns {Promise<Object>} The HTTP response object
9495
*/
9596
const createPreflightJob = async (context) => {
@@ -105,10 +106,20 @@ function PreflightController(ctx, log, env) {
105106
try {
106107
const isDev = env.AWS_ENV === 'dev';
107108
const step = data.step.toLowerCase();
108-
109+
let site;
109110
const url = new URL(data.urls[0]);
110111
const previewBaseURL = `${url.protocol}//${url.hostname}`;
111-
const site = await dataAccess.Site.findByPreviewURL(previewBaseURL);
112+
if (url.hostname.endsWith('adobecqms.net')) {
113+
// AMS
114+
site = await dataAccess.Site.findById(data.siteId);
115+
} else {
116+
// EDS and CS
117+
site = await dataAccess.Site.findByPreviewURL(previewBaseURL);
118+
}
119+
120+
if (!site) {
121+
throw new Error(`No site found for preview URL: ${previewBaseURL}`);
122+
}
112123
let enableAuthentication = false;
113124
// check head request for preview url
114125
const headResponse = await fetch(`${previewBaseURL}`, {
@@ -121,10 +132,6 @@ function PreflightController(ctx, log, env) {
121132
enableAuthentication = true;
122133
}
123134

124-
if (!site) {
125-
throw new Error(`No site found for preview URL: ${previewBaseURL}`);
126-
}
127-
128135
let promiseTokenResponse;
129136
if (CS_TYPES.includes(site.getAuthoringType())) {
130137
try {

test/controllers/preflight.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,51 @@ describe('Preflight Controller', () => {
186186
);
187187
});
188188

189+
it('creates a preflight job successfully for AMS', async () => {
190+
const context = {
191+
data: {
192+
urls: ['http://author.adobecqms.net/path'],
193+
step: 'identify',
194+
siteId: 'test-site-123',
195+
},
196+
};
197+
mockDataAccess.Site.findById = sandbox.stub().resolves(mockSite);
198+
199+
const response = await preflightController.createPreflightJob(context);
200+
expect(response.status).to.equal(202);
201+
202+
const result = await response.json();
203+
expect(result).to.deep.equal({
204+
jobId,
205+
status: 'IN_PROGRESS',
206+
createdAt: '2024-03-20T10:00:00Z',
207+
pollUrl: `https://spacecat.experiencecloud.live/api/v1/preflight/jobs/${jobId}`,
208+
});
209+
210+
expect(mockDataAccess.AsyncJob.create).to.have.been.calledWith({
211+
status: 'IN_PROGRESS',
212+
metadata: {
213+
payload: {
214+
siteId: 'test-site-123',
215+
urls: ['http://author.adobecqms.net/path'],
216+
step: 'identify',
217+
enableAuthentication: true,
218+
},
219+
jobType: 'preflight',
220+
tags: ['preflight'],
221+
},
222+
});
223+
224+
expect(mockSqs.sendMessage).to.have.been.calledWith(
225+
'https://sqs.test.amazonaws.com/audit-queue',
226+
{
227+
jobId,
228+
type: 'preflight',
229+
siteId: 'test-site-123',
230+
},
231+
);
232+
});
233+
189234
it('creates a preflight job successfully in production environment with authentication enabled', async () => {
190235
if (fetchStub && fetchStub.restore) {
191236
fetchStub.restore();

0 commit comments

Comments
 (0)