diff --git a/src/services/slack.js b/src/services/slack.js index 83e4907..47cebfe 100644 --- a/src/services/slack.js +++ b/src/services/slack.js @@ -53,8 +53,8 @@ async function deleteSlackInstallation(slack) { return slack.deleteOne(); } -export async function getSlackOrError(where) { - const slack = await getSlack(where); +export async function getSlackOrError(where, newInstallation = false) { + const slack = await getSlack(where, newInstallation); if (!slack) { throw new NotFoundError('Slack installation not found'); @@ -63,7 +63,7 @@ export async function getSlackOrError(where) { return slack; } -export async function getSlack(where) { +export async function getSlack(where, newInstallation = false) { if (!isQueryValid(where)) { throw new NotFoundError('Slack installation not found - no valid query provided'); } @@ -73,6 +73,7 @@ export async function getSlack(where) { if (where.id) query.where('_id', where.id); if (where.team_id) query.where('team_id', where.team_id); if (where.enterprise_id) query.where('enterprise_id', where.enterprise_id); + if (newInstallation) query.where('domain', null); return query.exec(); } @@ -80,16 +81,13 @@ export async function getSlack(where) { export async function createSlackInstallation(args) { await checkSlackIntegration(args.team_id); - // Remove old installation - await deleteSlack(args.enterprise_id, args.team_id); - // Create new slack instance let slackInstallation = new Slack({...args}); return slackInstallation.save(); } export async function authorizeSlackInstallation(domain, team_id, admin) { - const slack = await getSlackOrError({ team_id }); + const slack = await getSlackOrError({ team_id }, true); const _domain = await getDomainById(domain); if (String(_domain.owner) != String(admin._id)) { diff --git a/tests/slack.test.js b/tests/slack.test.js index 068ef82..1c0aae5 100644 --- a/tests/slack.test.js +++ b/tests/slack.test.js @@ -17,8 +17,10 @@ import { domainId, config1Document, groupConfigDocument, - adminAccountToken + adminAccountToken, + adminMasterAccountId } from './fixtures/db_api'; +import Domain from '../src/models/domain'; afterAll(async () => { await Slack.deleteMany().exec(); @@ -44,6 +46,20 @@ const buildInstallation = async (team_id, domain) => { return installation; }; +const createDomain = async (name) => { + const _id = new mongoose.Types.ObjectId(); + const domainDocument = { + _id, + name, + description: 'Test Domain for Slack Integration', + activated: new Map().set(EnvType.DEFAULT, true), + owner: adminMasterAccountId + }; + + await new Domain(domainDocument).save(); + return _id; +}; + describe('Slack Installation', () => { beforeAll(setupDatabase); @@ -386,6 +402,42 @@ describe('Slack Installation', () => { expect(slackDb).toBe(null); }); + test('SLACK_SUITE - Should unlink single installation from Multi Slack Installation', async () => { + //given - installation/authorization for Domain 1 + const installation = await buildInstallation('MULTI_DOMAIN_TEAM_ID', null); + await request(app) + .post('/slack/v1/authorize') + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send({ + domain: domainId, + team_id: installation.team_id + }).expect(200); + + //given - installation/authorization for Domain 2 + const domainId2 = await createDomain('Domain 2'); + const installation2 = await buildInstallation('MULTI_DOMAIN_TEAM_ID', null); + await request(app) + .post('/slack/v1/authorize') + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send({ + domain: domainId2, + team_id: installation2.team_id + }).expect(200); + + //test - unlink installation for Domain 1 without affecting Domain 2 + await request(app) + .delete(`/slack/v1/installation/unlink?domain=${String(domainId)}`) + .set('Authorization', `Bearer ${adminMasterAccountToken}`) + .send().expect(200); + + //check DB + const domain = await getDomainById(domainId); + expect(domain.integrations.slack).toBe(null); + + const domain2 = await getDomainById(domainId2); + expect(domain2.integrations.slack).not.toBe(null); + }); + test('SLACK_SUITE - Should NOT unlink installation - Admin is not owner', async () => { const response = await request(app) .delete(`/slack/v1/installation/unlink?domain=${String(domainId)}`)