-
My project implemented the Authorization as a fixture so that all test modules can work as a logged-in user.
-
There is a function to upload a file to s3, which actually consists of a few steps:
- initiate upload. It will return the s3 upload_url and something else.
- upload to s3. It will use the upload_url and disallow Authorization.
Error message:
InvalidArgument
Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified
I tried to set the headers to empty in my function, but it doesn’t work. Can anyone please help me? Do I must add the browserContext and use the route() function?
export const demandTags = async (
apiContext,
campaign,
campaignLinesDetails: CampaignLineResponse[],
demandTagsDetails: DemandTagRequest[],
use: (r: DemandTagResponse[]) => Promise<void>,
) => {
const results: DemandTagResponse[] = [];
// Get uploadCreative.mp4 from the files
const filePromise = new Promise((resolve, reject) => {
readFile('./uploadItems/uploadCreative.mp4', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const file: any = await filePromise;
// Initiate upload
const initiateUpload = await apiContext.post('/api/demandtag/initiate_upload/', {
data: {
file_name: 'uploadCreative.mp4',
file_type: 'video/mp4',
file_size: file.length,
} as apiTypes.UploadRequest,
});
await expect(initiateUpload).toBeOK();
const responseJSONinitiateUpload = await initiateUpload.json();
expect(responseJSONinitiateUpload.parts[0].offset).toBe(0);
expect(responseJSONinitiateUpload.parts[0].limit).toBe(file.length);
expect(responseJSONinitiateUpload.parts).toHaveLength(1);
// Upload the part
const partResponse = await apiContext.put(responseJSONinitiateUpload.parts[0].url, {
headers: {
// 'Content-Type': '',
},
data: file,
});
await expect(partResponse).toBeOK();
const etag = await partResponse.headers()['etag'];
const completeUpload = await apiContext.post('/api/demandtag/complete_upload/', {
data: {
upload_token: responseJSONinitiateUpload.upload_token,
etags: [etag],
} as apiTypes.CompleteUploadRequest,
});
await expect(completeUpload).toBeOK();
const responseJSONcompleteUpload = await completeUpload.json();
// Create demand tag
for (const demandTagDetails of demandTagsDetails) {
const createDemandTag = await apiContext.post('/api/demandtag/', {
data: {
name: demandTagDetails.name,
budget_value: demandTagDetails.budgetValue,
campaign_line: campaignLinesDetails[0].id,
pixel_url: demandTagDetails.pixelUrl,
creative_landing_page_url: demandTagDetails.creativeLandingPageUrl,
creative_s3: responseJSONcompleteUpload.url,
creative_slug: demandTagDetails.creativeSlug,
start_date: demandTagDetails.startDate,
end_date: demandTagDetails.endDate,
} as apiTypes.DemandTagRequest,
});
await expect(createDemandTag).toBeOK();
results.push({ ...demandTagDetails, id: (await createDemandTag.json()).id });
}
await use(results);
// Delete created demand tags
for (const demandTag of results) {
const responseDelete = await apiContext.delete(`/api/demandtag/${demandTag.id}/`);
expect([204, 404]).toContain(responseDelete.status());
}
};
The fixture defined in another file:
export const test = base.extend<TestFixtures, WorkerFixtures>({
authToken: async ({ request }, use) => {
await authToken(request, use);
},
apiContext: async ({ authToken }, use) => {
await use(
await request.newContext({
baseURL: `${process.env.PROTOCOL!}://${process.env.HOST!}:${process.env.PORT!}`,
extraHTTPHeaders: {
authorization: `Token ${authToken}`,
},
}),
);
},
staticUrl: [
async ({}, use) => {
await staticUrlFixture(use);
},
{ scope: 'worker' },
],
});
3