Add get/list/delete webhook resolver tests

This commit is contained in:
Hongbo Wu
2022-05-26 14:45:21 +08:00
parent a75ce3c44d
commit 26681e5ecd
2 changed files with 142 additions and 11 deletions

View File

@ -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)

View File

@ -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
})
})
})
})