diff --git a/test/it/eventhook-crud.js b/test/it/eventhook-crud.js new file mode 100644 index 000000000..4e2ac539a --- /dev/null +++ b/test/it/eventhook-crud.js @@ -0,0 +1,107 @@ +const expect = require('chai').expect; +const okta = require('../../src'); +const models = require('../../src/models'); +const Collection = require('../../src/collection'); +const mockEventHook = require('./mocks/eventhook.json'); +let orgUrl = process.env.OKTA_CLIENT_ORGURL; + +if (process.env.OKTA_USE_MOCK) { + orgUrl = `${orgUrl}/eventhook-crud`; +} + +const client = new okta.Client({ + orgUrl: orgUrl, + token: process.env.OKTA_CLIENT_TOKEN, + requestExecutor: new okta.DefaultRequestExecutor() +}); + +describe('Event Hook Crud API', () => { + describe('Create Event hook', () => { + let eventHook; + afterEach(async () => { + await eventHook.deactivate(); + await eventHook.delete(); + }); + + it('should return correct model', async () => { + eventHook = await client.createEventHook(mockEventHook); + expect(eventHook).to.be.instanceOf(models.EventHook); + expect(eventHook.id).to.be.exist; + expect(eventHook.name).to.be.equal(mockEventHook.name); + }); + }); + + describe('List Event Hooks', () => { + let eventHook; + beforeEach(async () => { + eventHook = await client.createEventHook(mockEventHook); + }); + afterEach(async () => { + await eventHook.deactivate(); + await eventHook.delete(); + }); + + it('should return a collection of EventHooks', async () => { + const collection = await client.listEventHooks(); + expect(collection).to.be.instanceOf(Collection); + await collection.each(eh => { + expect(eh).to.be.instanceOf(models.EventHook); + expect(eh.name).to.equal(eventHook.name); + }); + }); + }); + + describe('Get EventHook', () => { + let eventHook; + beforeEach(async () => { + eventHook = await client.createEventHook(mockEventHook); + }); + afterEach(async () => { + await eventHook.deactivate(); + await eventHook.delete(); + }); + + it('should get EventHook by id', async () => { + const eventHookFromGet = await client.getEventHook(eventHook.id); + expect(eventHookFromGet).to.be.instanceOf(models.EventHook); + expect(eventHookFromGet.name).to.equal(mockEventHook.name); + }); + }); + + describe('Update EventHook', () => { + let eventHook; + beforeEach(async () => { + eventHook = await client.createEventHook(mockEventHook); + }); + afterEach(async () => { + await eventHook.deactivate(); + await eventHook.delete(); + }); + + it('should update name for created eventHook', async () => { + const mockName = 'Mock update event hook'; + eventHook.name = mockName; + const updatedEventHook = await eventHook.update(); + expect(updatedEventHook.id).to.equal(eventHook.id); + expect(updatedEventHook.name).to.equal(mockName); + }); + }); + + describe('Delete EventHook', () => { + let eventHook; + beforeEach(async () => { + eventHook = await client.createEventHook(mockEventHook); + }); + + it('should not get eventHook after deletion', async () => { + await eventHook.deactivate(); + const res = await eventHook.delete(); + expect(res.status).to.equal(204); + try { + await client.getEventHook(eventHook.id); + } catch (e) { + expect(e.status).to.equal(404); + } + }); + }); +}); diff --git a/test/it/eventhook-lifecycle.js b/test/it/eventhook-lifecycle.js new file mode 100644 index 000000000..95f227d4e --- /dev/null +++ b/test/it/eventhook-lifecycle.js @@ -0,0 +1,47 @@ +const expect = require('chai').expect; +const okta = require('../../src'); +const mockEventHook = require('./mocks/eventhook.json'); +let orgUrl = process.env.OKTA_CLIENT_ORGURL; + +if (process.env.OKTA_USE_MOCK) { + orgUrl = `${orgUrl}/eventhook-lifecycle`; +} + +const client = new okta.Client({ + orgUrl: orgUrl, + token: process.env.OKTA_CLIENT_TOKEN, + requestExecutor: new okta.DefaultRequestExecutor() +}); + +describe('Event Hook Lifecycle API', () => { + let eventHook; + beforeEach(async () => { + eventHook = await client.createEventHook(mockEventHook); + }); + afterEach(async () => { + await eventHook.deactivate(); + await eventHook.delete(); + }); + + it('should activate event hook', async () => { + const res = await eventHook.activate(); + expect(res.status).to.equal('ACTIVE'); + }); + + it('should deactive event hook', async () => { + const res = await eventHook.deactivate(); + expect(res.status).to.equal('INACTIVE'); + }); + + // Since event hook verification need real hook endpoint to be setup + // Here we only test on error response to make sure proper endpoint be hit + // https://developer.okta.com/docs/reference/api/event-hooks/#verify-event-hook + it('should get error response with status 400 and code E0000001', async () => { + try { + await eventHook.verify(); + } catch (err) { + expect(err.status).to.equal(400); + expect(err.errorCode).to.equal('E0000001'); + } + }); +}); diff --git a/test/it/mocks/eventhook.json b/test/it/mocks/eventhook.json new file mode 100644 index 000000000..cabffb15d --- /dev/null +++ b/test/it/mocks/eventhook.json @@ -0,0 +1,28 @@ +{ + "name": "Mock Login And Create User event hook", + "events": { + "type": "EVENT_TYPE", + "items": [ + "user.session.start", + "user.lifecycle.create" + ] + }, + "channel": { + "type": "HTTP", + "version": "1.0.0", + "config": { + "uri": "https://example.com/eventHooks", + "headers": [ + { + "key": "x-any-key", + "value": "my-header-value" + } + ], + "authScheme": { + "type": "HEADER", + "key": "Authorization", + "value": "my-shared-secret" + } + } + } +} \ No newline at end of file