Skip to content

Commit

Permalink
feat(3266): make changes to models banner to include scope and scopeId (
Browse files Browse the repository at this point in the history
#639)

* add logic on banner to include scope

* add tests for banner factory

* removed invalid scope test

* cleanup commented lines

* Update lib/bannerFactory.js

Co-authored-by: Dayanand Sagar <sagar1312@gmail.com>

* Update lib/bannerFactory.js

Co-authored-by: Dayanand Sagar <sagar1312@gmail.com>

* Update lib/bannerFactory.js

Co-authored-by: Dayanand Sagar <sagar1312@gmail.com>

* fixed duplicate codes in banner model and test

* Update lib/bannerFactory.js

Co-authored-by: Dayanand Sagar <sagar1312@gmail.com>

* fixed tests

---------

Co-authored-by: Dayanand Sagar <sagar1312@gmail.com>
  • Loading branch information
VonnyJap and sagar1312 authored Jan 23, 2025
1 parent ea29c89 commit 376280e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
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 (config.scope !== 'GLOBAL') {
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
87 changes: 80 additions & 7 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 @@ -14,20 +15,29 @@ describe('Banner Factory', () => {
id: bannerId,
message,
type,
isActive
isActive,
scope: 'GLOBAL'
};

let BannerFactory;
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 +46,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 +59,15 @@ describe('Banner Factory', () => {
});

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

return factory
.create({
message,
type,
isActive
isActive,
scope: 'GLOBAL'
})
.then(model => {
assert.isTrue(datastore.save.calledOnce);
Expand All @@ -73,7 +88,8 @@ describe('Banner Factory', () => {
return factory
.create({
message,
isActive
isActive,
scope: 'GLOBAL'
})
.then(model => {
assert.isTrue(datastore.save.calledOnce);
Expand All @@ -94,7 +110,8 @@ describe('Banner Factory', () => {
return factory
.create({
message,
type
type,
scope: 'GLOBAL'
})
.then(model => {
assert.isTrue(datastore.save.calledOnce);
Expand All @@ -115,15 +132,71 @@ describe('Banner Factory', () => {

return factory
.create({
message
message,
scope: 'GLOBAL'
})
.then(model => {
assert.isTrue(datastore.save.calledOnce);
assert.instanceOf(model, Banner);

Object.keys(bannerData).forEach(key => {
assert.strictEqual(model[key], dataWithDefaults[key]);
});
});
});

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');
});
});
});
Expand Down

0 comments on commit 376280e

Please sign in to comment.