diff --git a/src/external/switcher-api-facade.js b/src/external/switcher-api-facade.js index af6f48f..0db1572 100644 --- a/src/external/switcher-api-facade.js +++ b/src/external/switcher-api-facade.js @@ -190,6 +190,21 @@ export async function checkSlackIntegration(value) { ]), 'Slack Integration is not available.'); } +export async function checkSlackAvailability(admin, feature) { + if (process.env.SWITCHER_API_ENABLE != 'true') + return true; + + if (!process.env.SWITCHER_SLACK_JWT_SECRET) + return false; + + const result = await checkFeature(feature, [checkValue(admin._id)], [ + SwitcherKeys.SLACK_INTEGRATION, + SwitcherKeys.SLACK_UPDATE + ]); + + return result; +} + export function notifyAcCreation(adminid) { if (process.env.SWITCHER_API_ENABLE != 'true') return; diff --git a/src/services/slack.js b/src/services/slack.js index e441834..e5b5714 100644 --- a/src/services/slack.js +++ b/src/services/slack.js @@ -1,14 +1,12 @@ import Slack from '../models/slack'; -import { checkValue, Switcher } from 'switcher-client'; import { TicketStatusType, SLACK_SUB, TicketValidationType } from '../models/slack_ticket'; import { BadRequestError, NotFoundError, PermissionError } from '../exceptions'; -import { checkSlackIntegration, checkFeature, SwitcherKeys } from '../external/switcher-api-facade'; +import { checkSlackIntegration, checkSlackAvailability } from '../external/switcher-api-facade'; import { getConfig } from './config'; import { getDomainById } from './domain'; import { getEnvironment } from './environment'; import { getGroupConfig } from './group-config'; import { containsValue } from '../helpers'; -import Logger from '../helpers/logger'; /** * Validates if ticket already exists, if so, return it. @@ -71,16 +69,7 @@ export async function getSlack(where) { } export async function checkAvailability(admin, feature) { - if (!process.env.SWITCHER_SLACK_JWT_SECRET) - return false; - - const result = await checkFeature(feature, [checkValue(admin._id)], [ - SwitcherKeys.SLACK_INTEGRATION, - SwitcherKeys.SLACK_UPDATE - ]); - - Logger.info(`checkAvailability [${feature}]`, { log: Switcher.getLogger(feature) }); - return result; + return checkSlackAvailability(admin, feature); } export async function createSlackInstallation(args) { diff --git a/tests/slack.test.js b/tests/slack.test.js index cb71258..0ee6cf2 100644 --- a/tests/slack.test.js +++ b/tests/slack.test.js @@ -60,7 +60,7 @@ describe('Slack Feature Availability', () => { process.env.SWITCHER_SLACK_JWT_SECRET = 'SLACK_APP_SECRET'; }); - test('SLACK_SUITE - Should check feature - Available', async () => { + test('SLACK_SUITE - Should check feature - Available (API enabled)', async () => { Switcher.assume('SLACK_INTEGRATION').true(); const response = await request(app) .post('/slack/v1/availability') @@ -72,6 +72,19 @@ describe('Slack Feature Availability', () => { expect(response.body.result).toBe(true); }); + test('SLACK_SUITE - Should check feature - Available (API disabled)', async () => { + process.env.SWITCHER_API_ENABLE = false; + const response = await request(app) + .post('/slack/v1/availability') + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send({ + feature: 'SLACK_INTEGRATION' + }).expect(200); + + expect(response.body.result).toBe(true); + process.env.SWITCHER_API_ENABLE = true; + }); + test('SLACK_SUITE - Should check feature - Not Available', async () => { Switcher.assume('SLACK_INTEGRATION').false(); const response = await request(app)