Skip to content

Commit

Permalink
Discord webhook (#621)
Browse files Browse the repository at this point in the history
* Updated error handling

* Error handling

* Lint

* Test
  • Loading branch information
icemedia001 authored Jan 28, 2025
1 parent bcfada0 commit 07e208a
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/App/utils/discordWebhookHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Test, TestingModule } from '@nestjs/testing'
import { HttpService } from '@nestjs/axios'
import { ConfigService } from '@nestjs/config'
import { DataSource } from 'typeorm'
import WebhookHandler from './discordWebhookHandler'
import { ActionTypes } from './utilTypes'

describe('WebhookHandler', () => {
let webhookHandler: WebhookHandler
let httpService: HttpService
let configService: ConfigService
// let dataSource: DataSource

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
WebhookHandler,
{
provide: HttpService,
useValue: {
post: jest.fn(),
},
},
{
provide: ConfigService,
useValue: {
get: jest.fn(),
},
},
{
provide: DataSource,
useValue: {
getRepository: jest.fn().mockReturnValue({
findOneBy: jest.fn(),
find: jest.fn(),
}),
},
},
],
}).compile()

webhookHandler = module.get<WebhookHandler>(WebhookHandler)
httpService = module.get<HttpService>(HttpService)
configService = module.get<ConfigService>(ConfigService)
// dataSource = module.get<DataSource>(DataSource)
})

it('should be defined', () => {
expect(webhookHandler).toBeDefined()
})

// it('should handle ADMIN_ACTION correctly', async () => {
// const payload = {
// user: 'testUser',
// adminAction: 'HIDE_WIKI',
// urlId: '123',
// }
// jest.spyOn(configService, 'get').mockReturnValue('{}')
// jest.spyOn(httpService, 'post').mockReturnValue({
// subscribe: jest.fn(),
// } as any)

// await webhookHandler.postWebhook(ActionTypes.ADMIN_ACTION, payload)

// expect(httpService.post).toHaveBeenCalled()
// })

it('should handle FLAG_WIKI correctly', async () => {
const payload = {
user: 'testUser',
urlId: '123',
description: 'Test description',
}
jest.spyOn(configService, 'get').mockReturnValue('{}')
jest.spyOn(httpService, 'post').mockReturnValue({
subscribe: jest.fn(),
} as any)

await webhookHandler.postWebhook(ActionTypes.FLAG_WIKI, payload)

expect(httpService.post).toHaveBeenCalled()
})
})
10 changes: 10 additions & 0 deletions src/App/utils/discordWebhookHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ export default class WebhookHandler {
error: async (err) => {
await fss.unlink('./uploads/message.json')
console.log(err.response)
const errorMessage = `Request to API failed\nError code: ${
err.response?.status
}\nError message: ${err.response?.data?.message || err.message}`
await this.sendToChannel(
boundary,
JSON.stringify({
content: errorMessage,
}),
braindaoAlarms,
)
},
})
} catch (e) {
Expand Down
76 changes: 76 additions & 0 deletions src/App/utils/discordWebhookService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Test, TestingModule } from '@nestjs/testing'
import { CACHE_MANAGER } from '@nestjs/common'
import { Cache } from 'cache-manager'
import DiscordWebhookService from './discordWebhookService'
import WebhookHandler from './discordWebhookHandler'
import { ActionTypes } from './utilTypes'

describe('DiscordWebhookService', () => {
let discordWebhookService: DiscordWebhookService
let webhookHandler: WebhookHandler
let cacheManager: Cache

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
DiscordWebhookService,
{
provide: WebhookHandler,
useValue: {
postWebhook: jest.fn(),
},
},
{
provide: CACHE_MANAGER,
useValue: {
get: jest.fn(),
set: jest.fn(),
},
},
],
}).compile()

discordWebhookService = module.get<DiscordWebhookService>(
DiscordWebhookService,
)
webhookHandler = module.get<WebhookHandler>(WebhookHandler)
cacheManager = module.get<Cache>(CACHE_MANAGER)
})

it('should be defined', () => {
expect(discordWebhookService).toBeDefined()
})

it('should handle addressToWikiRequestLogs correctly', async () => {
const cachedData = {
knownAddresses: { test: 1 },
unknownAddresses: ['unknown1'],
}
jest.spyOn(cacheManager, 'get').mockResolvedValue(cachedData)
jest.spyOn(webhookHandler, 'postWebhook').mockResolvedValue(true)

await discordWebhookService.addressToWikiRequestLogs()

expect(webhookHandler.postWebhook).toHaveBeenCalledWith(
ActionTypes.WIKI_ETH_ADDRESS,
cachedData,
)
})

it('should update addressToWikiCache correctly', async () => {
const addressData = { token: { name: 'testToken' }, hash: 'testHash' }
const cachedData = {
knownAddresses: {},
unknownAddresses: [],
}
jest.spyOn(cacheManager, 'get').mockResolvedValue(cachedData)
jest.spyOn(cacheManager, 'set').mockResolvedValue(undefined)

await discordWebhookService.updateAddressToWikiCache(addressData)

expect(cacheManager.set).toHaveBeenCalledWith('address_to_wiki_cache', {
knownAddresses: { testToken: 1 },
unknownAddresses: [],
})
})
})

0 comments on commit 07e208a

Please sign in to comment.