Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/services/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
}
Expand All @@ -73,23 +73,21 @@ 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();
}

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)) {
Expand Down
54 changes: 53 additions & 1 deletion tests/slack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down Expand Up @@ -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)}`)
Expand Down