From 26681e5ecdb33997cdca66f2c5a70daaaa264705 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 26 May 2022 14:45:21 +0800 Subject: [PATCH] Add get/list/delete webhook resolver tests --- packages/api/src/resolvers/webhooks/index.ts | 3 +- packages/api/test/resolvers/webhooks.test.ts | 150 +++++++++++++++++-- 2 files changed, 142 insertions(+), 11 deletions(-) diff --git a/packages/api/src/resolvers/webhooks/index.ts b/packages/api/src/resolvers/webhooks/index.ts index 91817c69c..718c7f6fc 100644 --- a/packages/api/src/resolvers/webhooks/index.ts +++ b/packages/api/src/resolvers/webhooks/index.ts @@ -130,6 +130,7 @@ export const deleteWebhookResolver = authorized< } const deletedWebhook = await getRepository(Webhook).remove(webhook) + deletedWebhook.id = id analytics.track({ userId: uid, @@ -141,7 +142,7 @@ export const deleteWebhookResolver = authorized< }) return { - webhook: webhookDataToResponse(webhook), + webhook: webhookDataToResponse(deletedWebhook), } } catch (error) { log.error(error) diff --git a/packages/api/test/resolvers/webhooks.test.ts b/packages/api/test/resolvers/webhooks.test.ts index 7bc48ef31..4e318276d 100644 --- a/packages/api/test/resolvers/webhooks.test.ts +++ b/packages/api/test/resolvers/webhooks.test.ts @@ -12,7 +12,6 @@ describe('Webhooks API', () => { let user: User let authToken: string - let webhook: Webhook before(async () => { // create test user and login @@ -24,11 +23,18 @@ describe('Webhooks API', () => { authToken = res.body.authToken // create test webhooks - webhook = await getRepository(Webhook).save({ - url: 'http://localhost:3000/webhooks/test', - user: { id: user.id }, - eventTypes: [WebhookEvent.PageCreated], - }) + await getRepository(Webhook).save([ + { + url: 'http://localhost:3000/webhooks/test', + user: { id: user.id }, + eventTypes: [WebhookEvent.PageCreated], + }, + { + url: 'http://localhost:3000/webhooks/test', + user: { id: user.id }, + eventTypes: [WebhookEvent.PageUpdated], + }, + ]) }) after(async () => { @@ -36,6 +42,78 @@ describe('Webhooks API', () => { await deleteTestUser(username) }) + describe('Get webhook', () => { + let webhook: Webhook + + before(async () => { + // create test webhooks + webhook = await getRepository(Webhook).save({ + url: 'http://localhost:3000/webhooks/test', + user: { id: user.id }, + eventTypes: [WebhookEvent.PageDeleted], + }) + }) + + it('should return a webhook', async () => { + const query = ` + query { + webhook(id: "${webhook.id}") { + ... on WebhookSuccess { + webhook { + id + url + eventTypes + enabled + } + } + } + } + ` + + const res = await graphqlRequest(query, authToken) + + expect(res.body.data.webhook.webhook.id).to.eql(webhook.id) + expect(res.body.data.webhook.webhook.url).to.eql(webhook.url) + expect(res.body.data.webhook.webhook.eventTypes).to.eql( + webhook.eventTypes + ) + expect(res.body.data.webhook.webhook.enabled).to.eql(webhook.enabled) + }) + }) + + describe('List webhooks', () => { + it('should return a list of webhooks', async () => { + const query = ` + query { + webhooks { + ... on WebhooksSuccess { + webhooks { + id + url + eventTypes + enabled + } + } + } + } + ` + + const res = await graphqlRequest(query, authToken) + const webhooks = await getRepository(Webhook).findBy({ + user: { id: user.id }, + }) + + expect(res.body.data.webhooks.webhooks).to.eql( + webhooks.map((w) => ({ + id: w.id, + url: w.url, + eventTypes: w.eventTypes, + enabled: w.enabled, + })) + ) + }) + }) + describe('Set webhook', () => { let eventTypes: WebhookEvent[] let query: string @@ -73,7 +151,7 @@ describe('Webhooks API', () => { context('when id is not set', () => { before(() => { webhookId = '' - webhookUrl = 'https://example.com/webhook' + webhookUrl = 'http://localhost:3000/webhooks/test' eventTypes = [WebhookEvent.HighlightCreated] enabled = true }) @@ -89,10 +167,18 @@ describe('Webhooks API', () => { }) context('when id is there', () => { - before(() => { + before(async () => { + const webhook = await getRepository(Webhook).save({ + url: 'http://localhost:3000/webhooks/test', + user: { id: user.id }, + eventTypes: [WebhookEvent.HighlightUpdated], + }) webhookId = webhook.id - webhookUrl = 'https://example.com/webhook_2' - eventTypes = [WebhookEvent.PageCreated] + webhookUrl = 'http://localhost:3000/webhooks/test_2' + eventTypes = [ + WebhookEvent.HighlightUpdated, + WebhookEvent.HighlightCreated, + ] enabled = false }) @@ -106,4 +192,48 @@ describe('Webhooks API', () => { }) }) }) + + describe('Delete webhook', () => { + let query: string + let webhookId: string + + beforeEach(async () => { + query = ` + mutation { + deleteWebhook(id: "${webhookId}") { + ... on DeleteWebhookSuccess { + webhook { + id + } + } + ... on DeleteWebhookError { + errorCodes + } + } + } + ` + }) + + context('when webhook exists', () => { + before(async () => { + const webhook = await getRepository(Webhook).save({ + url: 'http://localhost:3000/webhooks/test', + user: { id: user.id }, + eventTypes: [WebhookEvent.LabelCreated], + }) + webhookId = webhook.id + }) + + it('should delete a webhook', async () => { + const res = await graphqlRequest(query, authToken) + const webhook = await getRepository(Webhook).findOneBy({ + id: webhookId, + }) + + expect(res.body.data.deleteWebhook.webhook).to.be.an('object') + expect(res.body.data.deleteWebhook.webhook.id).to.eql(webhookId) + expect(webhook).to.be.undefined + }) + }) + }) })