Skip to content

Commit

Permalink
feat: data model for organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Jan 17, 2024
1 parent 261a9d7 commit 609b454
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 21 deletions.
37 changes: 16 additions & 21 deletions packages/spacecat-shared-data-access/src/models/site/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,25 @@ export const defaultConfig = {
alerts: [],
};

// Function to validate incoming configuration
function validateConfiguration(config) {
const { error, value } = configSchema.validate(config);

if (error) {
throw new Error(`Configuration validation error: ${error.message}`);
}

return value; // Validated and sanitized configuration
}

export const Config = (data = {}) => {
const validConfig = validateConfiguration(data);
const state = {
slack: {
channel: data?.slack?.channel,
workspace: data?.slack?.workspace,
...(validConfig?.slack?.channel ? { channel: validConfig?.slack?.channel } : {}),
...(validConfig?.slack?.workspace ? { workspace: validConfig?.slack?.workspace } : {}),
},
alerts: data.alerts,
alerts: validConfig.alerts || [],
};

const self = {
Expand All @@ -47,23 +59,6 @@ export const Config = (data = {}) => {
return Object.freeze(self);
};

// Function to validate incoming configuration
function validateConfiguration(config) {
const { error, value } = configSchema.validate(config);

if (error) {
throw new Error(`Configuration validation error: ${error.message}`);
}

return value; // Validated and sanitized configuration
}

Config.fromDynamoItem = (dynamoItem) => Config(dynamoItem);

Config.toDynamoItem = (config) => {
try {
return validateConfiguration(config);
} catch (e) {
throw new Error(`Error validating config ${e.message}`);
}
};
Config.toDynamoItem = (config) => validateConfiguration(config);
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2023 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 { Config } from '../../../../src/models/site/config.js';

describe('Config Tests', () => {
describe('Config Creation', () => {
it('creates an Config with defaults when no data is provided', () => {
const config = Config();
expect(config.slack).to.be.empty;
expect(config.alerts).to.deep.equal([]);
});

it('creates an Config with provided data when data is valid', () => {
const data = {
slack: {
channel: 'channel1',
workspace: 'workspace1',
},
alerts: [{
type: '404',
mentions: [{ slack: ['id1'] }],
byOrg: true,
}],
};
const config = Config(data);
expect(config.slack.channel).to.equal('channel1');
expect(config.slack.workspace).to.equal('workspace1');
expect(config.alerts[0].mentions[0].slack[0]).to.equal('id1');
expect(config.alerts[0].byOrg).to.be.true;
});

it('throws an error when data is invalid', () => {
const data = {
slack: {
channel: 'channel1',
workspace: 'workspace1',
},
alerts: [{
type: 404,
mentions: [{ email: ['id1'] }],
byOrg: true,
}],
};
expect(() => Config(data)).to.throw('Configuration validation error: "alerts[0].type" must be a string');
});
});

describe('fromDynamoItem Static Method', () => {
it('correctly converts from DynamoDB item', () => {
const dynamoItem = {
slack: {
channel: 'channel1',
workspace: 'workspace1',
},
alerts: [{
type: '404',
mentions: [{ slack: ['id1'] }],
byOrg: true,
}],
};
const config = Config.fromDynamoItem(dynamoItem);
expect(config.slack.channel).to.equal('channel1');
expect(config.slack.workspace).to.equal('workspace1');
expect(config.alerts[0].mentions[0].slack[0]).to.equal('id1');
expect(config.alerts[0].byOrg).to.be.true;
});
});

describe('toDynamoItem Static Method', () => {
it('correctly converts to DynamoDB item format', () => {
const data = {
slack: {
channel: 'channel1',
workspace: 'workspace1',
},
alerts: [{
type: '404',
mentions: [{ slack: ['id1'] }],
byOrg: true,
}],
};
const dynamoItem = Config.toDynamoItem(data);
expect(dynamoItem.slack.channel).to.equal('channel1');
expect(dynamoItem.slack.workspace).to.equal('workspace1');
expect(dynamoItem.alerts[0].mentions[0].slack[0]).to.equal('id1');
expect(dynamoItem.alerts[0].byOrg).to.be.true;
});

it('thorws error when item is invalid', () => {
const data = {
slack: {
channel: 'channel1',
workspace: 'workspace1',
},
alerts: [{
type: 404,
mentions: [{ email: ['id1'] }],
byOrg: true,
}],
};
expect(() => Config.toDynamoItem(data)).to.throw('Configuration validation error: "alerts[0].type" must be a string');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ describe('Site Access Pattern Tests', () => {
expect(mockDynamoClient.query.called).to.be.true;
});

it('calls getSitesByOrganizationId and returns an array', async () => {
const result = await exportedFunctions.getSitesByOrganizationId('OrgId1');
expect(result).to.be.an('array');
expect(mockDynamoClient.query.called).to.be.true;
});

it('calls getSitesToAudit and returns an array', async () => {
const result = await exportedFunctions.getSitesToAudit();
expect(result).to.be.an('array');
Expand Down

0 comments on commit 609b454

Please sign in to comment.