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

feat(3266): make changes to models banner to include scope and scopeId #639

Merged
merged 11 commits into from
Jan 23, 2025
21 changes: 20 additions & 1 deletion lib/bannerFactory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const BaseFactory = require('./baseFactory');
const PipelineFactory = require('./pipelineFactory');
const BuildFactory = require('./buildFactory');
const Banner = require('./banner');

let instance;
Expand Down Expand Up @@ -33,16 +35,33 @@ class BannerFactory extends BaseFactory {
* @param {String} config.createdBy The username of the associated user
* @param {Boolean} [config.isActive=false] Whether the banner is active
* @param {String} [config.type='info'] Type of banner (info|warn|etc)
* @param {String} [config.scope='GLOBAL'] Scope of the banner (GLOBAL|PIPELINE|BUILD)
* @memberof BannerFactory
*/
create(config) {
async create(config) {
if (!config.type) {
config.type = 'info';
}
if (!config.isActive) {
config.isActive = false;
}

const scopeFactories = {
PIPELINE: PipelineFactory,
BUILD: BuildFactory
};

if (scopeFactories[config.scope]) {
const factory = scopeFactories[config.scope];
const scopeInstance = await factory.getInstance().get(config.scopeId);

if (!scopeInstance) {
throw new Error(
`${config.scope.charAt(0) + config.scope.slice(1).toLowerCase()} ${config.scopeId} does not exist`
);
}
}

config.createTime = new Date().toISOString();

return super.create(config);
Expand Down
79 changes: 75 additions & 4 deletions test/lib/bannerFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { assert } = require('chai');
const sinon = require('sinon');
const rewiremock = require('rewiremock/node');

sinon.assert.expose(assert, { prefix: '' });

Expand All @@ -21,13 +22,21 @@ describe('Banner Factory', () => {
let datastore;
let factory;
let Banner;
let pipelineFactoryMock;

beforeEach(() => {
pipelineFactoryMock = {
get: sinon.stub()
};
rewiremock('../../lib/pipelineFactory').with({
getInstance: sinon.stub().returns(pipelineFactoryMock)
});
rewiremock.enable();

datastore = {
save: sinon.stub(),
get: sinon.stub()
};

/* eslint-disable global-require */
Banner = require('../../lib/banner');
BannerFactory = require('../../lib/bannerFactory');
Expand All @@ -36,6 +45,10 @@ describe('Banner Factory', () => {
factory = new BannerFactory({ datastore });
});

afterEach(() => {
rewiremock.disable();
});

describe('createClass', () => {
it('should return a Collection', () => {
const model = factory.createClass(bannerData);
Expand All @@ -45,14 +58,17 @@ describe('Banner Factory', () => {
});

describe('create', () => {
it('should create a Banner', () => {
datastore.save.resolves(bannerData);
it('should create a Banner with GLOBAL scope', () => {
const dataWithGlobalScope = { ...bannerData, scope: 'GLOBAL' };

datastore.save.resolves(dataWithGlobalScope);

return factory
.create({
message,
type,
isActive
isActive,
scope: 'GLOBAL'
})
.then(model => {
assert.isTrue(datastore.save.calledOnce);
Expand Down Expand Up @@ -126,6 +142,61 @@ describe('Banner Factory', () => {
});
});
});

it('should throw error when pipeline ID does not exist', () => {
const dataWithDefaults = { ...bannerData };

dataWithDefaults.scope = 'PIPELINE';
dataWithDefaults.scopeId = '1234';
datastore.save.resolves(dataWithDefaults);
pipelineFactoryMock.get.returns(null);

return factory
.create({
message,
type,
isActive,
scope: 'PIPELINE',
scopeId: '1234'
})
.then(() => {
assert.fail('nope');
})
.catch(err => {
assert.isTrue(pipelineFactoryMock.get.calledOnce);
assert.equal('Pipeline 1234 does not exist', err.message);
});
});

it('should create banner with scope: PIPELINE and scopeId: 1234', () => {
const dataWithDefaults = { ...bannerData };

dataWithDefaults.scope = 'PIPELINE';
dataWithDefaults.scopeId = '1234';
datastore.save.resolves(dataWithDefaults);
pipelineFactoryMock.get.returns({ id: '1234' });

return factory
.create({
message,
type,
isActive,
scope: 'PIPELINE',
scopeId: '1234'
})
.then(model => {
assert.isTrue(datastore.save.calledOnce);
assert.isTrue(pipelineFactoryMock.get.calledOnce);
assert.instanceOf(model, Banner);

Object.keys(bannerData).forEach(key => {
assert.strictEqual(model[key], dataWithDefaults[key]);
});
})
.catch(() => {
assert.fail('should not have failed');
});
});
});

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