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
2 changes: 1 addition & 1 deletion requests/Switcher API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -4803,7 +4803,7 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"team_id\": \"TEAM_ID\"\r\n}",
"raw": "{\r\n \"team_id\": \"TEAM_ID\",\r\n \"domain_id\": \"DOMAIN_ID\"\r\n}",
"options": {
"raw": {
"language": "json"
Expand Down
5 changes: 5 additions & 0 deletions src/api-docs/paths/path-slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export default {
team_id: {
type: 'string',
description: 'The Slack team ID',
},
domain_id: {
type: 'string',
description: 'The domain ID',
format: 'uuid'
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/routers/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ router.post('/slack/v1/authorize', auth, [
});

router.post('/slack/v1/ticket/clear', auth, [
check('team_id').exists()
check('team_id').exists(),
check('domain_id').isMongoId()
], validate, async (req, res) => {
try {
await Services.resetTicketHistory(req.body, req.admin, req.body.enterprise_id);
Expand Down Expand Up @@ -213,7 +214,7 @@ router.delete('/slack/v1/installation/decline', auth, [
});

router.delete('/slack/v1/installation/unlink', auth, [
query('domain').exists()
query('domain').isMongoId()
], validate, async (req, res) => {
try {
await Services.unlinkSlack(req.query.domain, req.admin);
Expand Down
16 changes: 12 additions & 4 deletions src/services/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ async function hasSlackTicket(slack, ticket_content) {
const tickets = await SlackTicket.find({
slack: slack._id,
domain: slack.domain,
...ticket_content
environment: ticket_content.environment,
group: ticket_content.group,
switcher: ticket_content.switcher,
status: ticket_content.status,
});

return tickets;
Expand Down Expand Up @@ -141,16 +144,21 @@ export async function authorizeSlackInstallation(args, admin) {
}

/**
* Delete non-installed Slack integration
*
* @param {*} enterprise_id (optional)
*/
export async function deleteSlack(args, enterprise_id) {
const { team_id } = args;
const slack = await getSlack({ team_id, enterprise_id });
const slack = await getSlack({ team_id, enterprise_id }, true);
if (slack) {
return deleteSlackInstallation(slack);
}
}

/**
* Delete installed Slack integration
*/
export async function unlinkSlack(domainid, admin) {
const domain = await getDomainById(domainid);

Expand Down Expand Up @@ -181,8 +189,8 @@ export async function updateSettings(domainId, param, request) {
* @param {*} enterprise_id (optional)
*/
export async function resetTicketHistory(args, admin, enterprise_id) {
const { team_id } = args;
const slack = await getSlackOrError({ team_id, enterprise_id });
const { team_id, domain_id } = args;
const slack = await getSlackOrError({ domain: domain_id, team_id, enterprise_id });
const domain = await getDomainById(slack.domain);

if (String(domain.owner) != String(admin._id)) {
Expand Down
65 changes: 38 additions & 27 deletions tests/slack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ describe('Slack Installation', () => {

test('SLACK_SUITE - Should generate token', async () => {
const token = generateToken('30m');
console.log(token);
const decoded = jwt.verify(token, process.env.SWITCHER_SLACK_JWT_SECRET);
expect(decoded.iss).toBe('Switcher Slack App');
expect(decoded.sub).toBe('/resource');
Expand Down Expand Up @@ -375,19 +374,9 @@ describe('Slack Installation', () => {
expect(slackDb).toBe(null);
});

test('SLACK_SUITE - Should delete authorized installation', async () => {
test('SLACK_SUITE - Should delete NOT authorized installation', async () => {
//given
const installation = await buildInstallation('SHOULD_DELETE_AUTHORIZE_INSTALLATION', null);
await authorizeInstallation(installation, domainId, adminMasterAccountToken);

const { ticket } = await createTicket(installation.team_id);

//verify that
let domain = await getDomainById(domainId);
expect(domain.integrations.slack).not.toBe(null);

let slackTickets = await SlackTicket.find({ slack: ticket.slack }).exec();
expect(slackTickets.length).toBe(1);

//test
await request(app)
Expand All @@ -396,16 +385,8 @@ describe('Slack Installation', () => {
.send().expect(200);

//check DB
domain = await getDomainById(domainId);
expect(domain.integrations.slack).toBe(null);

const slackDb = await Services.getSlack({
team_id: installation.team_id
});
const slackDb = await Services.getSlack({ team_id: installation.team_id });
expect(slackDb).toBe(null);

slackTickets = await SlackTicket.find({ slack: ticket.slack }).exec();
expect(slackTickets.length).toBe(0);
});

test('SLACK_SUITE - Should NOT delete installation - Not found', async () => {
Expand Down Expand Up @@ -441,9 +422,7 @@ describe('Slack Installation', () => {
domain = await getDomainById(domainId);
expect(domain.integrations.slack).toBe(null);

const slackDb = await Services.getSlack({
team_id: 'SHOULD_UNLINK_INTEGRATION'
});
const slackDb = await Services.getSlack({ team_id: installation.team_id });
expect(slackDb).toBe(null);
});

Expand Down Expand Up @@ -771,11 +750,13 @@ describe('Slack Route - Create Ticket', () => {
environment: ticket.environment,
group: ticket.group,
switcher: ticket.switcher,
status: ticket.status
status: ticket.status,
observations: 'Should return existing ticket'
}
}).expect(201);

expect(String(response.body.ticket._id)).toBe(String(ticket._id));
expect(response.body.ticket.observations).not.toBe('Should return existing ticket');
});

test('SLACK_SUITE - Should NOT create a ticket - Group not found', async () => {
Expand Down Expand Up @@ -1084,7 +1065,8 @@ describe('Slack Route - Process Ticket', () => {
.post('/slack/v1/ticket/clear')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send({
team_id: slack.team_id
team_id: slack.team_id,
domain_id: domainId
}).expect(200);

slackTickets = await SlackTicket.find({ slack: slack._id }).exec();
Expand All @@ -1096,8 +1078,37 @@ describe('Slack Route - Process Ticket', () => {
.post('/slack/v1/ticket/clear')
.set('Authorization', `Bearer ${adminAccountToken}`)
.send({
team_id: slack.team_id
team_id: slack.team_id,
domain_id: domainId
}).expect(403);
});

test('SLACK_SUITE - Should NOT reset installation tickets - Invalid Domain Id', async () => {
//test - invalid domain id
await request(app)
.post('/slack/v1/ticket/clear')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send({
team_id: slack.team_id,
domain_id: 'INVALID'
}).expect(422);

//test - domain not provided
await request(app)
.post('/slack/v1/ticket/clear')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send({
team_id: slack.team_id
}).expect(422);

//test - domain not found
await request(app)
.post('/slack/v1/ticket/clear')
.set('Authorization', `Bearer ${adminMasterAccountToken}`)
.send({
team_id: slack.team_id,
domain_id: new mongoose.Types.ObjectId()
}).expect(404);
});

});