-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated error handling * Error handling * Lint * Test
- Loading branch information
1 parent
bcfada0
commit 07e208a
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [], | ||
}) | ||
}) | ||
}) |