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(sites-28856): update shared-data schema to include type in options #577

Merged
merged 6 commits into from
Feb 6, 2025
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,6 @@ junit

# Mac OS
.DS_Store

# NVM (Node Version Manager)
.nvmrc
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ class ImportJob extends BaseModel {
static ImportOptions = {
ENABLE_JAVASCRIPT: 'enableJavascript',
PAGE_LOAD_TIMEOUT: 'pageLoadTimeout',
TYPE: 'type',
DATA: 'data',
};

static ImportOptionTypes = {
DOC: 'doc',
XWALK: 'xwalk',
};

// add your custom methods or overrides here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import {
isInteger,
isIsoDate,
isNonEmptyObject,
isNumber,
isObject,
isValidUrl,
Expand All @@ -35,6 +36,16 @@ const ImportOptionTypeValidator = {
throw new Error(`Invalid value for ${ImportJob.ImportOptions.PAGE_LOAD_TIMEOUT}: ${value}`);
}
},
[ImportJob.ImportOptions.TYPE]: (value) => {
if (!Object.values(ImportJob.ImportOptionTypes).includes(value)) {
throw new Error(`Invalid value for ${ImportJob.ImportOptions.TYPE}: ${value}`);
}
},
[ImportJob.ImportOptions.DATA]: (value) => {
if (value && !isNonEmptyObject(value)) {
throw new Error(`Invalid value for ${ImportJob.ImportOptions.DATA}: ${value}`);
}
},
};

const validateOptions = (options) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';

import { ElectroValidationError } from 'electrodb';
import ImportJobModel from '../../../src/models/import-job/import-job.model.js';
import { getDataAccess } from '../util/db.js';
import { seedDatabase } from '../util/seed.js';
import { DataAccessError } from '../../../src/index.js';

use(chaiAsPromised);

Expand All @@ -41,16 +43,15 @@ function checkImportJob(importJob) {
describe('ImportJob IT', async () => {
let sampleData;
let ImportJob;
let newJobData;

before(async () => {
sampleData = await seedDatabase();

const dataAccess = getDataAccess();
ImportJob = dataAccess.ImportJob;
});

it('adds a new import job', async () => {
const data = {
newJobData = {
importQueueId: 'some-queue-id',
hashedApiKey: 'some-hashed-api-key',
baseURL: 'https://example-some.com/cars',
Expand All @@ -62,18 +63,69 @@ describe('ImportJob IT', async () => {
hasCustomImportJs: false,
hasCustomHeaders: true,
};
const importJob = await ImportJob.create(data);
});

it('adds a new import job', async () => {
const importJob = await ImportJob.create(newJobData);

checkImportJob(importJob);

expect(importJob.getImportQueueId()).to.equal(newJobData.importQueueId);
expect(importJob.getHashedApiKey()).to.equal(newJobData.hashedApiKey);
expect(importJob.getBaseURL()).to.equal(newJobData.baseURL);
expect(importJob.getStartedAt()).to.equal(newJobData.startedAt);
expect(importJob.getStatus()).to.equal(newJobData.status);
expect(importJob.getInitiatedBy()).to.eql(newJobData.initiatedBy);
expect(importJob.getHasCustomImportJs()).to.equal(newJobData.hasCustomImportJs);
expect(importJob.getHasCustomHeaders()).to.equal(newJobData.hasCustomHeaders);
});

it('adds a new import job with valid options', async () => {
const options = {
type: 'xwalk',
data: {
siteName: 'xwalk',
assetFolder: 'xwalk',
},
};

let data = { ...newJobData, options };
let importJob = await ImportJob.create(data);

checkImportJob(importJob);
expect(importJob.getOptions()).to.equal(data.options);

data = { ...newJobData, options: { type: 'doc' } };
importJob = await ImportJob.create(data);

checkImportJob(importJob);
expect(importJob.getOptions()).to.eql({ type: 'doc' });

// test to make sure data error is thrown if data is not an object
data = { ...newJobData, options: { data: 'not-an-object' } };
await ImportJob.create(data).catch((err) => {
expect(err).to.be.instanceOf(DataAccessError);
expect(err.cause).to.be.instanceOf(ElectroValidationError);
expect(err.cause.message).to.contain('Invalid value for data: not-an-object');
});

expect(importJob.getImportQueueId()).to.equal(data.importQueueId);
expect(importJob.getHashedApiKey()).to.equal(data.hashedApiKey);
expect(importJob.getBaseURL()).to.equal(data.baseURL);
expect(importJob.getStartedAt()).to.equal(data.startedAt);
expect(importJob.getStatus()).to.equal(data.status);
expect(importJob.getInitiatedBy()).to.eql(data.initiatedBy);
expect(importJob.getHasCustomImportJs()).to.equal(data.hasCustomImportJs);
expect(importJob.getHasCustomHeaders()).to.equal(data.hasCustomHeaders);
// test to make sure data is not an empty object
data = { ...newJobData, options: { data: { } } };
await ImportJob.create(data).catch((err) => {
expect(err).to.be.instanceOf(DataAccessError);
expect(err.cause).to.be.instanceOf(ElectroValidationError);
expect(err.cause.message).to.contain('Invalid value for data');
});
});

it('throws an error when adding a new import job with invalid options', async () => {
const data = { ...newJobData, options: { type: 'invalid' } };

await ImportJob.create(data).catch((err) => {
expect(err).to.be.instanceOf(DataAccessError);
expect(err.cause).to.be.instanceOf(ElectroValidationError);
expect(err.cause.message).to.contain('Invalid value for type: invalid');
});
});

it('updates an existing import job', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ describe('ImportJobModel', () => {
userAgent: 'someUserAgent',
},
options: {
someOption: 'someValue',
type: 'xwalk',
},
redirectCount: 0,
status: 'RUNNING',
startedAt: '2022-01-01T00:00:00.000Z',
successCount: 0,
urlCount: 0,
data: {
siteName: 'xwalk',
assetFolder: 'xwalk',
},
};

({
Expand Down Expand Up @@ -189,15 +193,26 @@ describe('ImportJobModel', () => {
});

describe('options', () => {
it('no options', () => {
instance.setOptions(undefined);
expect(instance.getOptions()).to.be.undefined;
});

it('gets options', () => {
expect(instance.getOptions()).to.deep.equal({ someOption: 'someValue' });
expect(instance.getOptions()).to.deep.equal({ type: 'xwalk' });
});

it('sets options', () => {
const newOptions = { newOption: 'newValue' };
instance.setOptions(newOptions);
expect(instance.getOptions()).to.deep.equal(newOptions);
});

it('sets options with data attribute', () => {
const newOptions = { data: { siteFolder: 'xwalk', assetFolder: 'xwalk' } };
instance.setOptions(newOptions);
expect(instance.getOptions()).to.deep.equal(newOptions);
});
});

describe('redirectCount', () => {
Expand Down