-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webhook): make sure webhook urls are valid (#133)
* feat(webhook): make sure webhook urls are valid and reachable uri * added unit tests for sync schema validation * temporarily disable coverage threshold * fix references * removed webhook validation using option request * fix tests
- Loading branch information
Showing
7 changed files
with
186 additions
and
80 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 type { Config } from '@jest/types' | ||
import { defaults as tsjPreset } from 'ts-jest/presets' | ||
|
||
const config: Config.InitialOptions = { | ||
preset: 'ts-jest', | ||
// TODO: Re-enable coverage threshold once we have enough tests | ||
/*coverageThreshold: { | ||
global: { | ||
branches: 60, | ||
functions: 60, | ||
lines: 60, | ||
statements: 60 | ||
} | ||
}*/ | ||
|
||
projects: [ | ||
{ | ||
testMatch: ['<rootDir>/packages/server/test/**/(*.)test.ts'], | ||
displayName: { name: 'Server', color: 'blue' }, | ||
testEnvironment: 'node', | ||
transform: tsjPreset.transform, | ||
clearMocks: true | ||
} | ||
] | ||
} | ||
|
||
export default config |
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 was deleted.
Oops, something went wrong.
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,145 @@ | ||
import cloneDeep from 'lodash/cloneDeep' | ||
|
||
import { ChannelService } from '../../src/channels/service' | ||
import { ConfigService } from '../../src/config/service' | ||
import { DatabaseService } from '../../src/database/service' | ||
import { makeSyncRequestSchema } from '../../src/sync/schema' | ||
|
||
jest.mock('../../src/database/service') | ||
jest.mock('../../src/config/service') | ||
|
||
const channels = { | ||
messenger: { | ||
accessToken: 'accessToken', | ||
appSecret: 'appSecret', | ||
verifyToken: 'verifyToken' | ||
}, | ||
slack: { | ||
signingSecret: 'signingSecret', | ||
useRTM: false, | ||
botToken: 'botToken' | ||
}, | ||
smooch: { | ||
keyId: 'keyId', | ||
secret: 'secret', | ||
forwardRawPayloads: ['text'] | ||
}, | ||
teams: { | ||
appId: 'appId', | ||
appPassword: 'appPassword', | ||
proactiveMessages: { | ||
proactiveMessages: 'proactiveMessages' | ||
} | ||
}, | ||
telegram: { | ||
botToken: 'botToken' | ||
}, | ||
twilio: { | ||
accountSID: 'accountSID', | ||
authToken: 'authToken' | ||
}, | ||
vonage: { | ||
useTestingApi: true, | ||
apiKey: 'apiKey', | ||
apiSecret: 'apiSecret', | ||
signatureSecret: 'signatureSecret', | ||
applicationId: 'applicationId', | ||
privateKey: 'privateKey' | ||
} | ||
} | ||
const webhooks = [ | ||
{ | ||
url: 'https://url.com' | ||
}, | ||
{ | ||
url: 'http://localhost:3100' | ||
} | ||
] | ||
const invalidWebhook = [ | ||
{ | ||
url: '83.23.12.1:3100' | ||
} | ||
] | ||
|
||
describe('Sync', () => { | ||
let channelService: ChannelService | ||
|
||
const validObj = { | ||
channels, | ||
webhooks, | ||
name: 'a test config', | ||
id: 'a5869bcf-fee3-45f4-b083-1d177ea0d9cc', | ||
token: 'Eg4NWrUhW8TEMrZ39Gc4Y+vs2tl0VYt0/PgL4yhCZx1r/5QP0RQ2hbg/bWsKSfdn4R4/pQcK9S9tMk5XDdzkAbsb' | ||
} | ||
const validObjWithUnknownProperties = { | ||
...cloneDeep(validObj), | ||
unknownProperty: 'unknown' | ||
} | ||
const validObjWithInvalidWebhook = { | ||
...cloneDeep(validObj), | ||
webhooks: invalidWebhook | ||
} | ||
|
||
beforeEach(() => { | ||
const configService = new ConfigService() | ||
const databaseService = new DatabaseService(configService) | ||
channelService = new ChannelService(databaseService) | ||
}) | ||
|
||
describe('makeSyncRequestSchema', () => { | ||
test('Should not throw any error with all required fields', async () => { | ||
const schema = makeSyncRequestSchema(channelService.list()) | ||
|
||
const obj = {} | ||
const { error, value } = schema.validate(obj) | ||
if (error) { | ||
throw error | ||
} | ||
|
||
expect(value).toEqual(obj) | ||
}) | ||
|
||
test('Should not throw any error with an object that contains only valid properties', async () => { | ||
const schema = makeSyncRequestSchema(channelService.list()) | ||
|
||
const { error, value } = schema.validate(validObj) | ||
if (error) { | ||
throw error | ||
} | ||
|
||
expect(value).toEqual(validObj) | ||
}) | ||
|
||
test('Should strip unknown properties from object', async () => { | ||
const schema = makeSyncRequestSchema(channelService.list()) | ||
|
||
const { error, value } = schema.validate(validObjWithUnknownProperties) | ||
if (error) { | ||
throw error | ||
} | ||
|
||
expect(value).toEqual(validObj) | ||
}) | ||
|
||
test('Should only accept objects', async () => { | ||
const schema = makeSyncRequestSchema(channelService.list()) | ||
|
||
const val = '' | ||
const { error, value } = schema.validate(val) | ||
|
||
expect(error).not.toBeUndefined() | ||
expect(error!.message).toEqual('"value" must be of type object') | ||
expect(value).toEqual(val) | ||
}) | ||
|
||
test('Should only accept valid URI as webhook url', async () => { | ||
const schema = makeSyncRequestSchema(channelService.list()) | ||
|
||
const { error, value } = schema.validate(validObjWithInvalidWebhook) | ||
|
||
expect(error).not.toBeUndefined() | ||
expect(error!.message).toEqual('"webhooks[0].url" must be a valid uri') | ||
expect(value).toEqual(validObjWithInvalidWebhook) | ||
}) | ||
}) | ||
}) |
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