Skip to content

Commit

Permalink
test: add it for application - OKTA-288642
Browse files Browse the repository at this point in the history
  • Loading branch information
shuowu committed Jun 9, 2020
1 parent 7981a73 commit 551b979
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
100 changes: 100 additions & 0 deletions test/it/application-grant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
const expect = require('chai').expect;
const okta = require('../../');
const models = require('../../src/models');
const Collection = require('../../src/collection');
const mockApplication = require('./mocks/application-oidc.json');

let orgUrl = process.env.OKTA_CLIENT_ORGURL;

if (process.env.OKTA_USE_MOCK) {
orgUrl = `${orgUrl}/application-grant`;
}

const client = new okta.Client({
orgUrl: orgUrl,
token: process.env.OKTA_CLIENT_TOKEN,
requestExecutor: new okta.DefaultRequestExecutor()
});

describe('Application OAuth2 grant API', () => {
let application;
let grant;
beforeEach(async () => {
application = await client.createApplication(mockApplication);
});
afterEach(async () => {
await application.deactivate();
await application.delete();
});

describe('Grant consent', () => {
it('should grant consent to scope', async () => {
grant = await application.grantConsentToScope({
issuer: client.baseUrl,
scopeId: 'okta.users.manage'
});
expect(grant).to.be.instanceOf(models.OAuth2ScopeConsentGrant);
expect(grant.issuer).to.equal(client.baseUrl);
});
});

describe('List scope consent grants', () => {
beforeEach(async () => {
grant = await application.grantConsentToScope({
issuer: client.baseUrl,
scopeId: 'okta.users.manage'
});
});
afterEach(async () => {
await application.revokeScopeConsentGrant(grant.id);
});

it('should return a collection of OAuth2ScopeConsentGrant', async () => {
const grants = await application.listScopeConsentGrants({
applicationId: application.id
});
expect(grants).to.be.instanceOf(Collection);
await grants.each(grantFromCollection => {
expect(grantFromCollection).to.be.instanceOf(models.OAuth2ScopeConsentGrant);
expect(grantFromCollection.id).to.equal(grant.id);
});
});
});

describe('Get scope consent grant', () => {
beforeEach(async () => {
grant = await application.grantConsentToScope({
issuer: client.baseUrl,
scopeId: 'okta.users.manage'
});
});
afterEach(async () => {
await application.revokeScopeConsentGrant(grant.id);
});

it('should get grant by id', async () => {
const grantFromGet = await application.getScopeConsentGrant(grant.id);
expect(grantFromGet).to.be.exist;
expect(grantFromGet).to.be.instanceOf(models.OAuth2ScopeConsentGrant);
});
});

describe('Revoke grant', () => {
beforeEach(async () => {
grant = await application.grantConsentToScope({
issuer: client.baseUrl,
scopeId: 'okta.users.manage'
});
});

it('should revoke grant', async () => {
const res = await application.revokeScopeConsentGrant(grant.id);
expect(res.status).to.equal(204);
try {
await application.getScopeConsentGrant(grant.id);
} catch (err) {
expect(err.status).to.equal(404);
}
});
});
});
41 changes: 41 additions & 0 deletions test/it/application-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const expect = require('chai').expect;
const okta = require('../../');
const Collection = require('../../src/collection');
const mockApplication = require('./mocks/application-oidc.json');

let orgUrl = process.env.OKTA_CLIENT_ORGURL;

if (process.env.OKTA_USE_MOCK) {
orgUrl = `${orgUrl}/application-token`;
}

const client = new okta.Client({
orgUrl: orgUrl,
token: process.env.OKTA_CLIENT_TOKEN,
requestExecutor: new okta.DefaultRequestExecutor()
});

// As there is no way to create oauth2 token in test env
// Only test if list and revoke tokens endpoints get triggered and proper response get returned
describe('Application OAuth2 token API', () => {
let application;
beforeEach(async () => {
application = await client.createApplication(mockApplication);
});
afterEach(async () => {
await application.deactivate();
await application.delete();
});

it('should list a collection of tokens', async () => {
const grants = await application.listOAuth2Tokens();
expect(grants).to.be.instanceOf(Collection);
const res = await grants.getNextPage();
expect(res).to.be.an('array').that.is.empty;
});

it('should return status 204 when revoke tokens for application', async () => {
const res = await application.revokeOAuth2Tokens();
expect(res.status).to.equal(204);
});
});
19 changes: 19 additions & 0 deletions test/it/mocks/application-oidc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "oidc_client",
"label": "MOCK_CLIENT_CRED",
"signOnMode": "OPENID_CONNECT",
"settings": {
"oauthClient": {
"redirect_uris": [
"https://example.com"
],
"response_types": [
"code"
],
"grant_types": [
"authorization_code"
],
"application_type": "native"
}
}
}

0 comments on commit 551b979

Please sign in to comment.