Added Integration name extraction based on API Key data
refs https://github.com/TryGhost/Toolbox/issues/292 - This information is needed for the emails used to notify the instance admins about version mismatch. - The identification information is different for Content and Admin keys because of their nature. Content API keys send the unique "secret" value in plaintext, Admin API keys send key id (kid) as a part of token payload and that's the easiest accessible information available without a need to query the db in another parts of the system.
This commit is contained in:
parent
fbd7e206dc
commit
baef69f968
@ -6,10 +6,12 @@ class VersionNotificationsDataService {
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @param {Object} options.UserModel - ghost user model
|
||||
* @param {Object} options.ApiKeyModel - ghost api key model
|
||||
* @param {Object} options.settingsService - ghost settings service
|
||||
*/
|
||||
constructor({UserModel, settingsService}) {
|
||||
constructor({UserModel, ApiKeyModel, settingsService}) {
|
||||
this.UserModel = UserModel;
|
||||
this.ApiKeyModel = ApiKeyModel;
|
||||
this.settingsService = settingsService;
|
||||
}
|
||||
|
||||
@ -49,6 +51,26 @@ class VersionNotificationsDataService {
|
||||
|
||||
return adminEmails;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} key - api key identification value, it's "secret" in case of Content API key and "id" for Admin API
|
||||
* @param {String} type - one of "content" or "admin" values
|
||||
* @returns
|
||||
*/
|
||||
async getIntegrationName(key, type) {
|
||||
let queryOptions = null;
|
||||
|
||||
if (type === 'content') {
|
||||
queryOptions = {secret: key};
|
||||
} else if (type === 'admin') {
|
||||
queryOptions = {id: key};
|
||||
}
|
||||
|
||||
const apiKey = await this.ApiKeyModel.findOne(queryOptions, {withRelated: ['integration']});
|
||||
|
||||
return apiKey.relations.integration.get('name');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = VersionNotificationsDataService;
|
||||
|
@ -24,6 +24,7 @@ describe('Version Notification Data Service', function () {
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel: {},
|
||||
ApiKeyModel: {},
|
||||
settingsService
|
||||
});
|
||||
|
||||
@ -52,6 +53,7 @@ describe('Version Notification Data Service', function () {
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel: {},
|
||||
ApiKeyModel: {},
|
||||
settingsService
|
||||
});
|
||||
|
||||
@ -110,6 +112,7 @@ describe('Version Notification Data Service', function () {
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel,
|
||||
ApiKeyModel: {},
|
||||
settingsService: {}
|
||||
});
|
||||
|
||||
@ -119,4 +122,64 @@ describe('Version Notification Data Service', function () {
|
||||
assert.deepEqual(emails, ['simon@example.com', 'bob@example.com']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getIntegrationName', function () {
|
||||
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: {
|
||||
get: () => 'live fast die young'
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel: {},
|
||||
ApiKeyModel,
|
||||
settingsService: {}
|
||||
});
|
||||
|
||||
const integrationName = await versionNotificationsDataService.getIntegrationName('super_secret', 'content');
|
||||
|
||||
assert.equal(integrationName, 'live fast die young');
|
||||
});
|
||||
|
||||
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: {
|
||||
integration: {
|
||||
get: () => 'Tri Hita Karana'
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
const versionNotificationsDataService = new VersionNotificationsDataService({
|
||||
UserModel: {},
|
||||
ApiKeyModel,
|
||||
settingsService: {}
|
||||
});
|
||||
|
||||
const integrationName = await versionNotificationsDataService.getIntegrationName('key_id', 'admin');
|
||||
|
||||
assert.equal(integrationName, 'Tri Hita Karana');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user