Skip to content

Commit

Permalink
remove from registration method names and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sangeetha5491 committed Sep 26, 2022
1 parent 8847137 commit c57dbdc
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 55 deletions.
6 changes: 3 additions & 3 deletions e2e/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('test create event metadata', async () => {

test('test register journalling endpoint', async () => {
// create journal registration
journalReg = await sdkClient.createWebhookRegistration(consumerOrgId, projectId, workspaceId, {
journalReg = await sdkClient.createRegistration(consumerOrgId, projectId, workspaceId, {
name: 'Test Events SDK ' + randomNumber,
description: 'Test Events SDK ' + randomNumber,
client_id: apiKey,
Expand Down Expand Up @@ -139,7 +139,7 @@ test('test event received in journalling endpoint', async () => {
})

test('delete webhook registration', async () => {
await sdkClient.deleteWebhookRegistration(consumerOrgId, projectId, workspaceId, journalReg.registration_id)
await sdkClient.deleteRegistration(consumerOrgId, projectId, workspaceId, journalReg.registration_id)
journalReg = undefined
})

Expand All @@ -158,7 +158,7 @@ test('delete provider', async () => {
afterAll(async () => {
// delete webhook registration
if (journalReg) {
await sdkClient.deleteWebhookRegistration(consumerOrgId, projectId, workspaceId,
await sdkClient.deleteRegistration(consumerOrgId, projectId, workspaceId,
journalReg.registration_id)
}

Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class EventsCoreAPI {
* @param {object} body Json data contains details of the registration
* @returns {Promise<object>} Details of the webhook/journal registration created
*/
createWebhookRegistration (consumerOrgId, projectId, workspaceId, body) {
createRegistration (consumerOrgId, projectId, workspaceId, body) {
const headers = {}
const requestOptions = this.__createRequest('POST', headers, JSON.stringify(body))
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations`)
Expand All @@ -334,7 +334,7 @@ class EventsCoreAPI {
* @param {object} body Json data contains details of the registration
* @returns {Promise<object>} Details of the webhook/journal registration to be updated
*/
updateWebhookRegistration (consumerOrgId, projectId, workspaceId, registrationId, body) {
updateRegistration (consumerOrgId, projectId, workspaceId, registrationId, body) {
const headers = {}
const requestOptions = this.__createRequest('POST', headers, JSON.stringify(body))
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations/${registrationId}`)
Expand All @@ -351,7 +351,7 @@ class EventsCoreAPI {
* @param {string} registrationId Registration id whose details are to be fetched
* @returns {Promise<object>} Details of the webhook/journal registration
*/
getWebhookRegistration (consumerOrgId, projectId, workspaceId, registrationId) {
getRegistration (consumerOrgId, projectId, workspaceId, registrationId) {
const headers = {}
const requestOptions = this.__createRequest('GET', headers)
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations/${registrationId}`)
Expand All @@ -367,7 +367,7 @@ class EventsCoreAPI {
* @param {string} workspaceId Workspace Id from the console
* @returns {Promise<object>} List of all webhook/journal registrations
*/
getAllWebhookRegistrationsForWorkspace (consumerOrgId, projectId, workspaceId) {
getAllRegistrationsForWorkspace (consumerOrgId, projectId, workspaceId) {
const headers = {}
const requestOptions = this.__createRequest('GET', headers)
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations`)
Expand All @@ -387,7 +387,7 @@ class EventsCoreAPI {
* @param {Page} [page] page size and page number
* @returns {Promise<object>} Paginated response of all webhook/journal registrations for an org
*/
getAllWebhookRegistrationsForOrg (consumerOrgId, page) {
getAllRegistrationsForOrg (consumerOrgId, page) {
const headers = {}
const requestOptions = this.__createRequest('GET', headers)
const url = this.__getUrl(`/events/${consumerOrgId}/registrations`)
Expand All @@ -405,7 +405,7 @@ class EventsCoreAPI {
* @param {string} registrationId Id of the registration to be deleted
* @returns {Promise<object>} Empty object if deletion was successful
*/
deleteWebhookRegistration (consumerOrgId, projectId, workspaceId, registrationId) {
deleteRegistration (consumerOrgId, projectId, workspaceId, registrationId) {
const headers = {}
const requestOptions = this.__createRequest('DELETE', headers)
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations/${registrationId}`)
Expand Down
84 changes: 52 additions & 32 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,81 +334,101 @@ describe('Delete all eventmetadata', () => {
})
})

describe('Create webhook registration', () => {
it('Success on create webhook registration', async () => {
describe('Create registration', () => {
it('Success on create registration', async () => {
const sdkClient = await createSdkClient()
exponentialBackoffMockReturnValue(mock.data.createWebhookRegistrationResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.createWebhookRegistration('consumerId', 'projectId', 'workspaceId', mock.data.createWebhookRegistration)
exponentialBackoffMockReturnValue(mock.data.createRegistrationResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.createRegistration('consumerId', 'projectId', 'workspaceId', mock.data.createRegistration)
expect(res.id).toBe(248723)
expect(res.webhook_status).toBe('verified')
expect(res.enabled).toBe(true)
})
it('Bad request error on create webhook registration', async () => {
const api = 'createWebhookRegistration'
it('Bad request error on create registration', async () => {
const api = 'createRegistration'
exponentialBackoffMockReturnValue({}, { status: 400, statusText: 'Bad Request' })
await checkErrorResponse(api, new errorSDK.codes.ERROR_CREATE_REGISTRATION(), ['consumerId', 'projectId', 'workspaceId', mock.data.createWebhookRegistrationBadRequest])
await checkErrorResponse(api, new errorSDK.codes.ERROR_CREATE_REGISTRATION(), ['consumerId', 'projectId', 'workspaceId', mock.data.createRegistrationBadRequest])
})
})

describe('Update webhook registration', () => {
it('Success on update webhook registration', async () => {
describe('Update registration', () => {
it('Success on update registration', async () => {
const sdkClient = await createSdkClient()
exponentialBackoffMockReturnValue(mock.data.updateWebhookRegistrationResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.updateWebhookRegistration('consumerId', 'projectId', 'workspaceId', 'registrationId', mock.data.updateWebhookRegistration)
exponentialBackoffMockReturnValue(mock.data.updateRegistrationResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.updateRegistration('consumerId', 'projectId', 'workspaceId', 'registrationId', mock.data.updateRegistration)
expect(res.id).toBe(248723)
expect(res.webhook_status).toBe('verified')
expect(res.delivery_type).toBe('webhook_batch')
expect(res.enabled).toBe(true)
})
it('Bad request error on update webhook registration', async () => {
const api = 'updateWebhookRegistration'
it('Bad request error on update registration', async () => {
const api = 'updateRegistration'
exponentialBackoffMockReturnValue({}, { status: 400, statusText: 'Bad Request' })
await checkErrorResponse(api, new errorSDK.codes.ERROR_UPDATE_REGISTRATION(), ['consumerId', 'projectId', 'workspaceId', 'registrationId', mock.data.createWebhookRegistrationBadRequest])
await checkErrorResponse(api, new errorSDK.codes.ERROR_UPDATE_REGISTRATION(), ['consumerId', 'projectId', 'workspaceId', 'registrationId', mock.data.createRegistrationBadRequest])
})
})

describe('Get all webhook registration', () => {
it('Success on get all webhook registration', async () => {
describe('Get all registration', () => {
it('Success on get all registration', async () => {
const sdkClient = await createSdkClient()
exponentialBackoffMockReturnValue(mock.data.getAllWebhookRegistrationsResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.getAllWebhookRegistrationsForWorkspace('consumerId', 'projectId', 'workspaceId')
exponentialBackoffMockReturnValue(mock.data.getAllRegistrationsResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.getAllRegistrationsForWorkspace('consumerId', 'projectId', 'workspaceId')
expect(res._embedded.registrations.length).toBe(3)
const regs = res._embedded.registrations
expect(res._links.self.href).toBe('https://api.adobe.io/consumerId/projectId/workspaceId/registrations')
expect(res._links.self.href).toBe('https://api.adobe.io/events/consumerId/projectId/workspaceId/registrations')
expect(regs[0].id).toBe(30000)
expect(regs[1].webhook_status).toBe('hook_unreachable')
expect(regs[2].delivery_type).toBe('journal')
})
it('Not found error on get all webhook registration', async () => {
const api = 'getAllWebhookRegistrationsForWorkspace'
it('Not found error on get all registration', async () => {
const api = 'getAllRegistrationsForWorkspace'
exponentialBackoffMockReturnValue({}, { status: 404, statusText: 'Not Found' })
await checkErrorResponse(api, new errorSDK.codes.ERROR_GET_ALL_REGISTRATION(), ['consumerId', 'project-1', 'workspace-1'])
})
})

describe('Get a webhook registration', () => {
it('Success on get a webhook registration', async () => {
describe('Get a registration', () => {
it('Success on get a registration', async () => {
const sdkClient = await createSdkClient()
exponentialBackoffMockReturnValue(mock.data.createWebhookRegistrationResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.getWebhookRegistration('consumerId', 'projectId', 'workspaceId', 'registrationId')
exponentialBackoffMockReturnValue(mock.data.createRegistrationResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.getRegistration('consumerId', 'projectId', 'workspaceId', 'registrationId')
expect(res._links.self.href).toBe('https://api.adobe.io/events/consumerId/projectId/workspaceId/registrations/registrationId')
expect(res.id).toBe(248723)
expect(res.webhook_status).toBe('verified')
expect(res.enabled).toBe(true)
})
it('Not found error on get a webhook registration', async () => {
const api = 'getWebhookRegistration'
it('Not found error on get a registration', async () => {
const api = 'getRegistration'
exponentialBackoffMockReturnValue({}, { status: 404, statusText: 'Not Found' })
await checkErrorResponse(api, new errorSDK.codes.ERROR_GET_REGISTRATION(), ['consumerId', 'projectId', 'workspaceId', 'registrationId-1'])
})
})

describe('Get webhook registration with retries', () => {
describe('Get all registrations for org', () => {
it('Success on get all registrations for org', async () => {
const sdkClient = await createSdkClient()
exponentialBackoffMockReturnValue(mock.data.getAllRegistrationsForOrgResponse, { status: 200, statusText: 'OK' })
const res = await sdkClient.getAllRegistrationsForOrg('consumerId', { page: 1, size: 2 })
expect(res._links.self.href).toBe('https://api.adobe.io/events/consumerId/registrations?page=1&size=2')
expect(res._links.first.href).toBe('https://api.adobe.io/events/consumerId/registrations?page=0&size=2')
expect(res._links.last.href).toBe('https://api.adobe.io/events/consumerId/registrations?page=19&size=2')
expect(res._links.prev.href).toBe('https://api.adobe.io/events/consumerId/registrations?page=0&size=2')
expect(res._embedded.registrations.length).toBe(2)
expect(res.page.numberOfElements).toBe(2)
expect(res.page.totalElements).toBe(19)
})
it('Not found error on get a registration', async () => {
const api = 'getAllRegistrationsForOrg'
exponentialBackoffMockReturnValue({}, { status: 404, statusText: 'Not Found' })
await checkErrorResponse(api, new errorSDK.codes.ERROR_GET_ALL_REGISTRATION_FOR_ORG(), ['consumerId-2'])
})
})

describe('Get registration with retries', () => {
it('Test for retries on 5xx response', async () => {
const sdkClient = await sdk.init(gOrganizationId, gApiKey, gAccessToken, { retries: 3 })
const error = new errorSDK.codes.ERROR_GET_REGISTRATION()
exponentialBackoffMockReturnValue({}, { status: 500, statusText: 'Internal Server Error', url: journalUrl })
await sdkClient.getWebhookRegistration('consumerId', 'projectId', 'workspaceId', 'registrationId')
await sdkClient.getRegistration('consumerId', 'projectId', 'workspaceId', 'registrationId')
.then(res => {
throw new Error(' No error response')
})
Expand All @@ -432,16 +452,16 @@ describe('Get webhook registration with retries', () => {
})
})

describe('test delete webhook registration', () => {
describe('test delete registration', () => {
it('Success on delete registration', async () => {
const sdkClient = await createSdkClient()
exponentialBackoffMockReturnValue(undefined, { status: 204, statusText: 'No Content' })
const res = await sdkClient.deleteWebhookRegistration('consumerId', 'projectId', 'workspaceId',
const res = await sdkClient.deleteRegistration('consumerId', 'projectId', 'workspaceId',
'registrationId')
expect(res).toBe(undefined)
})
it('Not found error on delete registration', () => {
const api = 'deleteWebhookRegistration'
const api = 'deleteRegistration'
exponentialBackoffMockReturnValue({}, { status: 404, statusText: 'Not Found' })
checkErrorResponse(api, new errorSDK.codes.ERROR_DELETE_REGISTRATION(),
['consumerId', 'integrationId', 'registrationId1'])
Expand Down
Loading

0 comments on commit c57dbdc

Please sign in to comment.