2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2022-04-22 09:46:02 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const VersionNotificationsDataService = require('..');
|
|
|
|
|
|
|
|
describe('Version Notification Data Service', function () {
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchNotification', function () {
|
|
|
|
it('parses and filters out version notifications', async function () {
|
|
|
|
const settingsService = {
|
|
|
|
read: sinon.stub().resolves({
|
|
|
|
version_notifications: {
|
|
|
|
value: JSON.stringify([
|
|
|
|
'v3.4',
|
|
|
|
'v4.1',
|
|
|
|
'v5.0',
|
|
|
|
'v0.99'
|
|
|
|
])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const versionNotificationsDataService = new VersionNotificationsDataService({
|
|
|
|
UserModel: {},
|
2022-05-10 11:26:52 +03:00
|
|
|
ApiKeyModel: {},
|
2022-04-22 09:46:02 +03:00
|
|
|
settingsService
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(await versionNotificationsDataService.fetchNotification('v4.1'), 'v4.1');
|
|
|
|
assert.equal(await versionNotificationsDataService.fetchNotification('v9999.1'), undefined);
|
|
|
|
assert.equal(await versionNotificationsDataService.fetchNotification('v5'), undefined);
|
|
|
|
assert.equal(await versionNotificationsDataService.fetchNotification(), undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('saveNotification', function () {
|
|
|
|
it('parses and filters out version notifications', async function () {
|
|
|
|
const settingsService = {
|
|
|
|
read: sinon.stub().resolves({
|
|
|
|
version_notifications: {
|
|
|
|
value: JSON.stringify([
|
|
|
|
'v3.4',
|
|
|
|
'v4.1',
|
|
|
|
'v5.0',
|
|
|
|
'v0.99'
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
edit: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
|
|
|
|
const versionNotificationsDataService = new VersionNotificationsDataService({
|
|
|
|
UserModel: {},
|
2022-05-10 11:26:52 +03:00
|
|
|
ApiKeyModel: {},
|
2022-04-22 09:46:02 +03:00
|
|
|
settingsService
|
|
|
|
});
|
|
|
|
|
|
|
|
await versionNotificationsDataService.saveNotification('v5.0');
|
|
|
|
assert.equal(settingsService.edit.called, false);
|
|
|
|
|
|
|
|
await versionNotificationsDataService.saveNotification('v4.0');
|
|
|
|
assert.equal(settingsService.edit.called, true);
|
|
|
|
assert.deepEqual(settingsService.edit.firstCall.args, [[{
|
|
|
|
key: 'version_notifications',
|
|
|
|
value: JSON.stringify([
|
|
|
|
'v3.4',
|
|
|
|
'v4.1',
|
|
|
|
'v5.0',
|
|
|
|
'v0.99',
|
|
|
|
'v4.0'
|
|
|
|
])
|
|
|
|
}], {
|
|
|
|
context: {
|
|
|
|
internal: true
|
|
|
|
}
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getNotificationEmails', function () {
|
|
|
|
it('parses and filters out version notifications', async function () {
|
|
|
|
const UserModel = {
|
|
|
|
findAll: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs({
|
|
|
|
withRelated: ['roles'],
|
|
|
|
filter: 'status:active'
|
|
|
|
}, {
|
|
|
|
internal: true
|
|
|
|
})
|
|
|
|
.resolves({
|
|
|
|
toJSON: () => [{
|
|
|
|
email: 'simon@example.com',
|
|
|
|
roles: [{
|
|
|
|
name: 'Administrator'
|
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
email: 'bob@example.com',
|
|
|
|
roles: [{
|
|
|
|
name: 'Owner'
|
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
email: 'joe@example.com',
|
|
|
|
roles: [{
|
|
|
|
name: 'Publisher'
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const versionNotificationsDataService = new VersionNotificationsDataService({
|
|
|
|
UserModel,
|
2022-05-10 11:26:52 +03:00
|
|
|
ApiKeyModel: {},
|
2022-04-22 09:46:02 +03:00
|
|
|
settingsService: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
const emails = await versionNotificationsDataService.getNotificationEmails();
|
|
|
|
|
|
|
|
assert.equal(UserModel.findAll.called, true);
|
|
|
|
assert.deepEqual(emails, ['simon@example.com', 'bob@example.com']);
|
|
|
|
});
|
|
|
|
});
|
2022-05-10 11:26:52 +03:00
|
|
|
|
2023-01-30 12:57:02 +03:00
|
|
|
describe('getIntegration', function () {
|
2022-05-10 11:26:52 +03:00
|
|
|
it('Queries for Content API key when such is provided', async function () {
|
|
|
|
const ApiKeyModel = {
|
|
|
|
findOne: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs({
|
|
|
|
secret: 'super_secret'
|
|
|
|
}, {
|
|
|
|
withRelated: ['integration']
|
|
|
|
})
|
|
|
|
.resolves({
|
|
|
|
relations: {
|
|
|
|
integration: {
|
2023-01-30 12:57:02 +03:00
|
|
|
toJSON: () => ({
|
|
|
|
name: 'live fast die young',
|
|
|
|
type: 'custom'
|
|
|
|
})
|
2022-05-10 11:26:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const versionNotificationsDataService = new VersionNotificationsDataService({
|
|
|
|
UserModel: {},
|
|
|
|
ApiKeyModel,
|
|
|
|
settingsService: {}
|
|
|
|
});
|
|
|
|
|
2023-01-30 12:57:02 +03:00
|
|
|
const {name: integrationName, type: integrationType} = await versionNotificationsDataService.getIntegration('super_secret', 'content');
|
2022-05-10 11:26:52 +03:00
|
|
|
|
|
|
|
assert.equal(integrationName, 'live fast die young');
|
2023-01-30 12:57:02 +03:00
|
|
|
assert.equal(integrationType, 'custom');
|
2022-05-10 11:26:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Queries for Admin API key when such is provided', async function () {
|
|
|
|
const ApiKeyModel = {
|
|
|
|
findOne: sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs({
|
|
|
|
id: 'key_id'
|
|
|
|
}, {
|
|
|
|
withRelated: ['integration']
|
|
|
|
})
|
|
|
|
.resolves({
|
|
|
|
relations: {
|
2023-01-30 12:57:02 +03:00
|
|
|
|
2022-05-10 11:26:52 +03:00
|
|
|
integration: {
|
2023-01-30 12:57:02 +03:00
|
|
|
toJSON: () => ({
|
|
|
|
name: 'Tri Hita Karana',
|
|
|
|
type: 'core'
|
|
|
|
})
|
2022-05-10 11:26:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const versionNotificationsDataService = new VersionNotificationsDataService({
|
|
|
|
UserModel: {},
|
|
|
|
ApiKeyModel,
|
|
|
|
settingsService: {}
|
|
|
|
});
|
|
|
|
|
2023-01-30 12:57:02 +03:00
|
|
|
const {name: integrationName, type: integrationType} = await versionNotificationsDataService.getIntegration('key_id', 'admin');
|
2022-05-10 11:26:52 +03:00
|
|
|
|
|
|
|
assert.equal(integrationName, 'Tri Hita Karana');
|
2023-01-30 12:57:02 +03:00
|
|
|
assert.equal(integrationType, 'core');
|
2022-05-10 11:26:52 +03:00
|
|
|
});
|
|
|
|
});
|
2022-04-22 09:46:02 +03:00
|
|
|
});
|