Skip to content

Commit

Permalink
test: add integration tests for event hook apis - OKTA-288645 (#164)
Browse files Browse the repository at this point in the history
* test: add integration tests for event hook apis - OKTA-288645

* test: add doc link in comment
  • Loading branch information
shuowu authored Jun 11, 2020
1 parent 07a598d commit d9691a3
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
107 changes: 107 additions & 0 deletions test/it/eventhook-crud.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
});
47 changes: 47 additions & 0 deletions test/it/eventhook-lifecycle.js
Original file line number Diff line number Diff line change
@@ -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');
}
});
});
28 changes: 28 additions & 0 deletions test/it/mocks/eventhook.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}

0 comments on commit d9691a3

Please sign in to comment.