Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add a unit test for imports.write_all_domains scope #384

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/spacecat-shared-data-access/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [@adobe/spacecat-shared-data-access-v1.45.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.45.0...@adobe/spacecat-shared-data-access-v1.45.1) (2024-09-24)


### Bug Fixes

* SITES-25634 [Importer] Expose hasCustomImportJs and hasCustomHeaders as two new fields on a import job ([#381](https://github.com/adobe/spacecat-shared/issues/381)) ([b90a7c5](https://github.com/adobe/spacecat-shared/commit/b90a7c5623af90924a2122dbc10077f989a8550b))

# [@adobe/spacecat-shared-data-access-v1.45.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.44.5...@adobe/spacecat-shared-data-access-v1.45.0) (2024-09-24)


Expand Down
8 changes: 8 additions & 0 deletions packages/spacecat-shared-data-access/docs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,14 @@
{
"AttributeName": "initiatedBy",
"AttributeType": "M"
},
{
"AttributeName": "hasCustomHeaders",
"AttributeType": "B"
},
{
"AttributeName": "hasCustomImportJs",
"AttributeType": "B"
}
],
"GlobalSecondaryIndexes": [
Expand Down
2 changes: 1 addition & 1 deletion packages/spacecat-shared-data-access/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adobe/spacecat-shared-data-access",
"version": "1.45.0",
"version": "1.45.1",
"description": "Shared modules of the Spacecat Services - Data Access",
"type": "module",
"engines": {
Expand Down
4 changes: 4 additions & 0 deletions packages/spacecat-shared-data-access/src/dto/import-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const ImportJobDto = {
redirectCount: importJob.getRedirectCount(),
importQueueId: importJob.getImportQueueId(),
initiatedBy: importJob.getInitiatedBy(),
hasCustomHeaders: importJob.hasCustomHeaders(),
hasCustomImportJs: importJob.hasCustomImportJs(),
GSI1PK: 'ALL_IMPORT_JOBS',
}),

Expand All @@ -64,6 +66,8 @@ export const ImportJobDto = {
redirectCount: dynamoItem.redirectCount,
importQueueId: dynamoItem.importQueueId,
initiatedBy: dynamoItem.initiatedBy,
hasCustomHeaders: dynamoItem.hasCustomHeaders,
hasCustomImportJs: dynamoItem.hasCustomImportJs,
};

return createImportJob(importJobData);
Expand Down
10 changes: 10 additions & 0 deletions packages/spacecat-shared-data-access/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@ export interface ImportJob {
* Retrieves the initiatedBy metadata (name, imsOrgId, imsUserId, userAgent) of the import job.
*/
getInitiatedBy: () => object;

/**
* Indicates if the import job has custom headers.
*/
hasCustomHeaders: () => boolean;

/**
* Indicates if the import job has custom import js.
*/
hasCustomImportJs: () => boolean;
}

export interface ImportUrl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import {
hasText, isIsoDate, isValidUrl, isObject, isString, isNumber, isInteger,
hasText, isIsoDate, isValidUrl, isObject, isString, isNumber, isInteger, isBoolean,
} from '@adobe/spacecat-shared-utils';
import { Base } from '../base.js';
import { ImportJobStatus, ImportOptions } from './import-constants.js';
Expand Down Expand Up @@ -39,6 +39,8 @@ const ImportJob = (data) => {
self.getRedirectCount = () => self.state.redirectCount;
self.getImportQueueId = () => self.state.importQueueId;
self.getInitiatedBy = () => self.state.initiatedBy;
self.hasCustomHeaders = () => self.state.hasCustomHeaders || false;
self.hasCustomImportJs = () => self.state.hasCustomImportJs || false;

/**
* Updates the state of the ImportJob.
Expand Down Expand Up @@ -147,6 +149,29 @@ const ImportJob = (data) => {
},
);

/**
* Update the hasCustomHeaders value to true if the ImportJob has custom headers, false otherwise.
* @param {boolean} hasCustomHeaders - The new value for hasCustomHeaders.
* @return {ImportJob} The updated ImportJob object.
*/
self.updateHasCustomHeaders = (hasCustomHeaders) => updateState('hasCustomHeaders', hasCustomHeaders, (value) => {
if (!isBoolean(value)) {
throw new Error(`Invalid hasCustomHeaders value: ${value}`);
}
});

/**
* Update the hasCustomImportJs value to true if the ImportJob has custom import js, false
* otherwise.
* @param {boolean} hasCustomImportJs - The new value for hasCustomImportJs.
* @return {ImportJob} The updated ImportJob object.
*/
self.updateHasCustomImportJs = (hasCustomImportJs) => updateState('hasCustomImportJs', hasCustomImportJs, (value) => {
if (!isBoolean(value)) {
throw new Error(`Invalid hasCustomImportJs value: ${value}`);
}
});

return Object.freeze(self);
};

Expand Down Expand Up @@ -215,5 +240,13 @@ export const createImportJob = (data) => {
});
}

if (newState.hasCustomImportJs && !isBoolean(newState.hasCustomImportJs)) {
throw new Error(`Invalid hasCustomImportJs value: ${newState.hasCustomImportJs}`);
}

if (newState.hasCustomHeaders && !isBoolean(newState.hasCustomHeaders)) {
throw new Error(`Invalid hasCustomHeaders value: ${newState.hasCustomHeaders}`);
}

return ImportJob(newState);
};
6 changes: 6 additions & 0 deletions packages/spacecat-shared-data-access/test/it/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,8 @@ describe('DynamoDB Integration Test', async () => {
options: {
[ImportOptions.ENABLE_JAVASCRIPT]: true,
},
hasCustomImportJs: true,
hasCustomHeaders: false,
});

// helper
Expand All @@ -1036,6 +1038,8 @@ describe('DynamoDB Integration Test', async () => {
const job = await createNewImportJob();
expect(job.getId()).to.be.a('string');
expect(job.getCreatedAt()).to.be.a('string');
expect(job.hasCustomHeaders()).to.be.false;
expect(job.hasCustomImportJs()).to.be.true;
});

it('Verify updateImportJob', async () => {
Expand All @@ -1047,6 +1051,7 @@ describe('DynamoDB Integration Test', async () => {
newJob.updateDuration(1234);
newJob.updateUrlCount(100);
newJob.updateImportQueueId('Q-456');
newJob.updateHasCustomHeaders(true);

const updatedJob = await dataAccess.updateImportJob(newJob);

Expand All @@ -1058,6 +1063,7 @@ describe('DynamoDB Integration Test', async () => {
expect(updatedJob.getOptions()).to.deep.equal({
[ImportOptions.ENABLE_JAVASCRIPT]: true,
});
expect(updatedJob.hasCustomHeaders()).to.be.true;
});

it('Verify getImportJobsByStatus', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ describe('ApiKey Model tests', () => {
expect(apiKey.getCreatedAt()).is.not.empty;
});

it('creates an ApiKey object for a user with scope - imports.write_all_domains', () => {
const apiKey = createApiKey({ ...validApiKey, scopes: [{ name: 'imports.write_all_domains', domains: [] }] });
expect(apiKey.getScopes()).to.deep.equal([{ name: 'imports.write_all_domains', domains: [] }]);
bhellema marked this conversation as resolved.
Show resolved Hide resolved
});

it('creates an ApiKey object for a user with scope - imports.write', () => {
const apiKey = createApiKey({ ...validApiKey, scopes: [{ name: 'imports.write', domains: ['https://adobe.com', 'https://test.com'] }] });
expect(apiKey.getScopes()).to.deep.equal([{ name: 'imports.write', domains: ['https://adobe.com', 'https://test.com'] }]);
});

it('throws an error if revokedAt is not a valid date', () => {
expect(() => createApiKey({ ...validApiKey, revokedAt: 'invalid-date' })).to.throw('revokedAt should be a valid ISO 8601 string: invalid-date');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ describe('ImportJob Model tests', () => {
const importJob = createImportJob({ ...validImportJob, startTime: '' });
expect(importJob.getStartTime()).to.match(/^20/);
});

it('create an import job with custom fields', () => {
let headersJob = createImportJob({ ...validImportJob, hasCustomHeaders: true });
expect(headersJob.hasCustomHeaders()).to.be.true;
headersJob = createImportJob({ ...validImportJob, hasCustomHeaders: false });
expect(headersJob.hasCustomHeaders()).to.be.false;
headersJob = createImportJob({ ...validImportJob });
expect(headersJob.hasCustomHeaders()).to.be.false;
expect(() => createImportJob({ ...validImportJob, hasCustomHeaders: 'x' })).to.throw('Invalid hasCustomHeaders value: x');

let importJsJob = createImportJob({ ...validImportJob, hasCustomImportJs: true });
expect(importJsJob.hasCustomImportJs()).to.be.true;
importJsJob = createImportJob({ ...validImportJob, hasCustomImportJs: false });
expect(importJsJob.hasCustomImportJs()).to.be.false;
importJsJob = createImportJob({ ...validImportJob });
expect(importJsJob.hasCustomImportJs()).to.be.false;
expect(() => createImportJob({ ...validImportJob, hasCustomImportJs: 'x' })).to.throw('Invalid hasCustomImportJs value: x');
});
});

describe('Import Job Functionality Tests', () => {
Expand Down Expand Up @@ -171,6 +189,26 @@ describe('ImportJob Model tests', () => {
expect(() => importJob.updateRedirectCount(-1)).to.throw('Invalid redirect count during update: -1');
});

it('update hasCustomHeaders of import job', () => {
importJob.updateHasCustomHeaders(true);
expect(importJob.hasCustomHeaders()).to.equal(true);

importJob.updateHasCustomHeaders(false);
expect(importJob.hasCustomHeaders()).to.equal(false);

expect(() => importJob.updateHasCustomHeaders(undefined)).to.throw('Invalid hasCustomHeaders value: undefined');
});

it('update hasCustomImportJs of import job', () => {
importJob.updateHasCustomImportJs(true);
expect(importJob.hasCustomImportJs()).to.equal(true);

importJob.updateHasCustomImportJs(false);
expect(importJob.hasCustomImportJs()).to.equal(false);

expect(() => importJob.updateHasCustomImportJs(undefined)).to.throw('Invalid hasCustomImportJs value: undefined');
});

it('updates import queue id of import job', () => {
importJob.updateImportQueueId('123');
expect(importJob.getImportQueueId()).to.equal('123');
Expand Down
Loading
Loading