generated from dvdksn/clockbox
-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
307 additions
and
39 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,27 @@ | ||
import request from 'supertest' | ||
import { mock } from 'jest-mock-extended' | ||
import { App } from '../../src/app' | ||
import { Incoming } from '../../src/services/incoming' | ||
import { Outgoing } from '../../src/services/outgoing' | ||
import { defaultConfig, getConfig } from '../../src/services/config' | ||
import { SessionStore } from '../../src/services/session_store' | ||
import { OnNewLogin } from '../../src/services/socket' | ||
|
||
const addToBlacklist = jest.fn().mockReturnValue(Promise.resolve(true)) | ||
|
||
const sessionStore = mock<SessionStore>() | ||
const getConfigTest: getConfig = async (_phone: string) => { | ||
return defaultConfig | ||
} | ||
|
||
describe('blacklist routes', () => { | ||
test('update', async () => { | ||
const incoming = mock<Incoming>() | ||
const outgoing = mock<Outgoing>() | ||
const onNewLogin = mock<OnNewLogin>() | ||
const app: App = new App(incoming, outgoing, '', getConfigTest, sessionStore, onNewLogin, addToBlacklist) | ||
const res = await request(app.server).post('/2/blacklist/1').send({ttl: 1, to: '3'}) | ||
expect(addToBlacklist).toHaveBeenCalledWith('2', '1', '3', 1); | ||
expect(res.status).toEqual(200) | ||
}) | ||
}) |
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
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
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,50 @@ | ||
|
||
jest.mock('../../src/services/redis') | ||
import { extractDestinyPhone, isInBlacklistInMemory, addToBlacklistInMemory, cleanBlackList, isInBlacklistInRedis } from '../../src/services/blacklist' | ||
import { redisGet, redisKeys, blacklist } from '../../src/services/redis' | ||
|
||
const redisGetMock = redisGet as jest.MockedFunction<typeof redisGet> | ||
const redisKeysMock = redisKeys as jest.MockedFunction<typeof redisKeys> | ||
const blacklistMock = blacklist as jest.MockedFunction<typeof blacklist> | ||
|
||
describe('service blacklist webhook', () => { | ||
test('return empty extractDestinyPhone from webhook payload', async () => { | ||
const payload = { | ||
entry: [ | ||
{ | ||
changes: [ | ||
{ | ||
value: { | ||
contacts: [{ wa_id: 'y' }], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
expect(extractDestinyPhone(payload)).toBe('y') | ||
}) | ||
|
||
test('return empty extractDestinyPhone from api payload', async () => { | ||
expect(extractDestinyPhone({ to: 'y'})).toBe('y') | ||
}) | ||
|
||
test('return false isInBlacklistInMemory', async () => { | ||
await cleanBlackList() | ||
expect(await isInBlacklistInMemory('x', 'y', { to: 'w' })).toBe('') | ||
}) | ||
|
||
test('return addToBlacklistInMemory', async () => { | ||
await cleanBlackList() | ||
expect(await addToBlacklistInMemory('x', 'y', 'w', 100000)).toBe(true) | ||
expect(await isInBlacklistInMemory('x', 'y', { to: 'w' })).toBe('w') | ||
}) | ||
|
||
test('return false isInBlacklistInRedis', async () => { | ||
await cleanBlackList() | ||
redisKeysMock.mockReturnValue(Promise.resolve(['unoapi-webhook-blacklist:x:y:w'])) | ||
redisGetMock.mockReturnValue(Promise.resolve('1')) | ||
blacklistMock.mockReturnValue('unoapi-webhook-blacklist:::') | ||
expect(await isInBlacklistInRedis('x', 'y', { to: 'w' })).toBe('w') | ||
}) | ||
}) |
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
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,22 @@ | ||
import { Request, Response } from 'express' | ||
import logger from '../services/logger' | ||
import { addToBlacklist } from '../services/blacklist' | ||
|
||
export class BlacklistController { | ||
private addToBlacklist: addToBlacklist | ||
|
||
constructor(addToBlacklist: addToBlacklist) { | ||
this.addToBlacklist = addToBlacklist | ||
} | ||
|
||
async update(req: Request, res: Response) { | ||
logger.debug('blacklist method %s', req.method) | ||
logger.debug('blacklist headers %s', JSON.stringify(req.headers)) | ||
logger.debug('blacklist params %s', JSON.stringify(req.params)) | ||
logger.debug('blacklist body %s', JSON.stringify(req.body)) | ||
const { to, ttl } = req.body | ||
const { webhook_id, phone } = req.params | ||
await this.addToBlacklist(phone, webhook_id, to, ttl) | ||
res.status(200).send(`{"success": true}`) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import { Request, Response } from 'express' | ||
import logger from '../services/logger' | ||
|
||
class WebwookController { | ||
export class WebhookController { | ||
public whatsapp(req: Request, res: Response) { | ||
logger.debug('webhook method %s', req.method) | ||
logger.debug('webhook headers %s', JSON.stringify(req.headers)) | ||
logger.debug('webhook params %s', JSON.stringify(req.params)) | ||
logger.debug('webhook body %s', JSON.stringify(req.body)) | ||
logger.debug('webhook whatsapp method %s', req.method) | ||
logger.debug('webhook whatsapp headers %s', JSON.stringify(req.headers)) | ||
logger.debug('webhook whatsapp params %s', JSON.stringify(req.params)) | ||
logger.debug('webhook whatsapp body %s', JSON.stringify(req.body)) | ||
res.status(200).send(`{"success": true}`) | ||
} | ||
} | ||
|
||
export const webhookController = new WebwookController() |
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
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,8 @@ | ||
import { addToBlacklistInMemory } from '../services/blacklist' | ||
import { setBlacklist } from '../services/redis' | ||
|
||
export const addToBlacklist = async (_phone: string, data: object) => { | ||
const { from, webhookId, to, ttl } = data as any | ||
await setBlacklist(from, webhookId, to, ttl) | ||
await addToBlacklistInMemory(from, webhookId, to, ttl) | ||
} |
Oops, something went wrong.