diff --git a/requests/Switcher API.postman_collection.json b/requests/Switcher API.postman_collection.json index 5fea841..e2fa1aa 100644 --- a/requests/Switcher API.postman_collection.json +++ b/requests/Switcher API.postman_collection.json @@ -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" diff --git a/src/api-docs/paths/path-slack.js b/src/api-docs/paths/path-slack.js index 5177737..8e8a794 100644 --- a/src/api-docs/paths/path-slack.js +++ b/src/api-docs/paths/path-slack.js @@ -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' } } } diff --git a/src/routers/slack.js b/src/routers/slack.js index a4d2b3d..5f5488f 100644 --- a/src/routers/slack.js +++ b/src/routers/slack.js @@ -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); @@ -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); diff --git a/src/services/slack.js b/src/services/slack.js index acdd0d2..5975fae 100644 --- a/src/services/slack.js +++ b/src/services/slack.js @@ -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; @@ -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); @@ -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)) { diff --git a/tests/slack.test.js b/tests/slack.test.js index 8459740..8e8a79a 100644 --- a/tests/slack.test.js +++ b/tests/slack.test.js @@ -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'); @@ -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) @@ -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 () => { @@ -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); }); @@ -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 () => { @@ -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(); @@ -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); + }); + }); \ No newline at end of file