|
| 1 | +import { Types } from 'mongoose'; |
| 2 | + |
| 3 | +import { getLoggedInAgent, getServer } from '@/fixtures'; |
| 4 | +import Webhook, { WebhookService } from '@/models/webhook'; |
| 5 | + |
| 6 | +const MOCK_WEBHOOK = { |
| 7 | + name: 'Test Webhook', |
| 8 | + service: WebhookService.Slack, |
| 9 | + url: 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX', |
| 10 | + description: 'Test webhook for Slack', |
| 11 | + queryParams: { param1: 'value1' }, |
| 12 | + headers: { 'X-Custom-Header': 'Header Value' }, |
| 13 | + body: '{"text": "Test message"}', |
| 14 | +}; |
| 15 | + |
| 16 | +describe('webhooks router', () => { |
| 17 | + const server = getServer(); |
| 18 | + |
| 19 | + beforeAll(async () => { |
| 20 | + await server.start(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(async () => { |
| 24 | + await server.clearDBs(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterAll(async () => { |
| 28 | + await server.stop(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('GET / - returns webhooks filtered by service', async () => { |
| 32 | + const { agent, team } = await getLoggedInAgent(server); |
| 33 | + |
| 34 | + // Create test webhook |
| 35 | + await Webhook.create({ |
| 36 | + ...MOCK_WEBHOOK, |
| 37 | + team: team._id, |
| 38 | + }); |
| 39 | + |
| 40 | + // Create a webhook for a different service |
| 41 | + await Webhook.create({ |
| 42 | + ...MOCK_WEBHOOK, |
| 43 | + service: WebhookService.Generic, |
| 44 | + url: 'https://example.com/webhook/generic', |
| 45 | + team: team._id, |
| 46 | + }); |
| 47 | + |
| 48 | + // Get webhooks for Slack |
| 49 | + const response = await agent |
| 50 | + .get('/webhooks') |
| 51 | + .query({ service: WebhookService.Slack }) |
| 52 | + .expect(200); |
| 53 | + |
| 54 | + expect(response.body.data).toHaveLength(1); |
| 55 | + expect(response.body.data[0]).toMatchObject({ |
| 56 | + name: MOCK_WEBHOOK.name, |
| 57 | + service: MOCK_WEBHOOK.service, |
| 58 | + url: MOCK_WEBHOOK.url, |
| 59 | + }); |
| 60 | + |
| 61 | + // Get webhooks for multiple services |
| 62 | + const multiResponse = await agent |
| 63 | + .get('/webhooks') |
| 64 | + .query({ service: [WebhookService.Slack, WebhookService.Generic] }) |
| 65 | + .expect(200); |
| 66 | + |
| 67 | + expect(multiResponse.body.data).toHaveLength(2); |
| 68 | + }); |
| 69 | + |
| 70 | + it('GET / - returns empty array when no webhooks exist', async () => { |
| 71 | + const { agent } = await getLoggedInAgent(server); |
| 72 | + |
| 73 | + const response = await agent |
| 74 | + .get('/webhooks') |
| 75 | + .query({ service: WebhookService.Slack }) |
| 76 | + .expect(200); |
| 77 | + |
| 78 | + expect(response.body.data).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('POST / - creates a new webhook', async () => { |
| 82 | + const { agent } = await getLoggedInAgent(server); |
| 83 | + |
| 84 | + const response = await agent |
| 85 | + .post('/webhooks') |
| 86 | + .send(MOCK_WEBHOOK) |
| 87 | + .expect(200); |
| 88 | + |
| 89 | + expect(response.body.data).toMatchObject({ |
| 90 | + name: MOCK_WEBHOOK.name, |
| 91 | + service: MOCK_WEBHOOK.service, |
| 92 | + url: MOCK_WEBHOOK.url, |
| 93 | + description: MOCK_WEBHOOK.description, |
| 94 | + }); |
| 95 | + |
| 96 | + // Verify webhook was created in database |
| 97 | + const webhooks = await Webhook.find({}); |
| 98 | + expect(webhooks).toHaveLength(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it('POST / - returns 400 when webhook with same URL already exists', async () => { |
| 102 | + const { agent, team } = await getLoggedInAgent(server); |
| 103 | + |
| 104 | + // Create webhook first |
| 105 | + await Webhook.create({ |
| 106 | + ...MOCK_WEBHOOK, |
| 107 | + team: team._id, |
| 108 | + }); |
| 109 | + |
| 110 | + // Try to create the same webhook again |
| 111 | + const response = await agent |
| 112 | + .post('/webhooks') |
| 113 | + .send(MOCK_WEBHOOK) |
| 114 | + .expect(400); |
| 115 | + |
| 116 | + expect(response.body.message).toBe('Webhook already exists'); |
| 117 | + |
| 118 | + // Verify only one webhook exists |
| 119 | + const webhooks = await Webhook.find({}); |
| 120 | + expect(webhooks).toHaveLength(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it('POST / - returns 400 when request body is invalid', async () => { |
| 124 | + const { agent } = await getLoggedInAgent(server); |
| 125 | + |
| 126 | + // Missing required fields |
| 127 | + await agent |
| 128 | + .post('/webhooks') |
| 129 | + .send({ |
| 130 | + name: 'Invalid Webhook', |
| 131 | + }) |
| 132 | + .expect(400); |
| 133 | + |
| 134 | + // Invalid URL |
| 135 | + await agent |
| 136 | + .post('/webhooks') |
| 137 | + .send({ |
| 138 | + ...MOCK_WEBHOOK, |
| 139 | + url: 'not-a-url', |
| 140 | + }) |
| 141 | + .expect(400); |
| 142 | + |
| 143 | + // Invalid service |
| 144 | + await agent |
| 145 | + .post('/webhooks') |
| 146 | + .send({ |
| 147 | + ...MOCK_WEBHOOK, |
| 148 | + service: 'INVALID_SERVICE', |
| 149 | + }) |
| 150 | + .expect(400); |
| 151 | + }); |
| 152 | + |
| 153 | + it('DELETE /:id - deletes a webhook', async () => { |
| 154 | + const { agent, team } = await getLoggedInAgent(server); |
| 155 | + |
| 156 | + // Create test webhook |
| 157 | + const webhook = await Webhook.create({ |
| 158 | + ...MOCK_WEBHOOK, |
| 159 | + team: team._id, |
| 160 | + }); |
| 161 | + |
| 162 | + await agent.delete(`/webhooks/${webhook._id}`).expect(200); |
| 163 | + |
| 164 | + // Verify webhook was deleted |
| 165 | + const deletedWebhook = await Webhook.findById(webhook._id); |
| 166 | + expect(deletedWebhook).toBeNull(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('DELETE /:id - returns 200 when webhook does not exist', async () => { |
| 170 | + const { agent } = await getLoggedInAgent(server); |
| 171 | + |
| 172 | + const nonExistentId = new Types.ObjectId().toString(); |
| 173 | + |
| 174 | + // This will succeed even if the ID doesn't exist, consistent with the implementation |
| 175 | + await agent.delete(`/webhooks/${nonExistentId}`).expect(200); |
| 176 | + }); |
| 177 | + |
| 178 | + it('DELETE /:id - returns 400 when ID is invalid', async () => { |
| 179 | + const { agent } = await getLoggedInAgent(server); |
| 180 | + |
| 181 | + await agent.delete('/webhooks/invalid-id').expect(400); |
| 182 | + }); |
| 183 | +}); |
0 commit comments