Skip to content

Commit

Permalink
test: add it for admin role - OKTA-288641 (#158)
Browse files Browse the repository at this point in the history
* test: add it for admin role - OKTA-288641

* test: update test with openapi updates
  • Loading branch information
shuowu committed Jun 19, 2020
1 parent 9fa10f4 commit a65f024
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/factories/UserFactorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class UserFactorFactory extends ModelResolutionFactory {
'token:software:totp': models.TotpUserFactor,
'u2f': models.U2fUserFactor,
'web': models.WebUserFactor,
'webauthn': models.WebAuthnUserFactor,
};
}

Expand Down
34 changes: 34 additions & 0 deletions src/models/WebAuthnUserFactor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
* Copyright (c) 2017-2020, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/


/* THIS FILE IS AUTO-GENERATED - SEE CONTRIBUTOR DOCUMENTATION */

var UserFactor = require('./UserFactor');
const WebAuthnUserFactorProfile = require('./WebAuthnUserFactorProfile');

/**
* @class WebAuthnUserFactor
* @extends UserFactor
* @property { WebAuthnUserFactorProfile } profile
*/
class WebAuthnUserFactor extends UserFactor {
constructor(resourceJson, client) {
super(resourceJson, client);
if (resourceJson && resourceJson.profile) {
this.profile = new WebAuthnUserFactorProfile(resourceJson.profile);
}
}

}

module.exports = WebAuthnUserFactor;
33 changes: 33 additions & 0 deletions src/models/WebAuthnUserFactorProfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
* Copyright (c) 2017-2020, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/


/* THIS FILE IS AUTO-GENERATED - SEE CONTRIBUTOR DOCUMENTATION */

var Resource = require('../resource');


/**
* @class WebAuthnUserFactorProfile
* @extends Resource
* @property { string } authenticatorName
* @property { string } credentialId
*/
class WebAuthnUserFactorProfile extends Resource {
constructor(resourceJson, client) {
super(resourceJson, client);

}

}

module.exports = WebAuthnUserFactorProfile;
2 changes: 2 additions & 0 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ exports.UserStatusPolicyRuleCondition = require('./UserStatusPolicyRuleCondition
exports.UserType = require('./UserType');
exports.VerifyFactorRequest = require('./VerifyFactorRequest');
exports.VerifyUserFactorResponse = require('./VerifyUserFactorResponse');
exports.WebAuthnUserFactor = require('./WebAuthnUserFactor');
exports.WebAuthnUserFactorProfile = require('./WebAuthnUserFactorProfile');
exports.WebUserFactor = require('./WebUserFactor');
exports.WebUserFactorProfile = require('./WebUserFactorProfile');
exports.WsFederationApplication = require('./WsFederationApplication');
Expand Down
35 changes: 35 additions & 0 deletions test/it/group-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const expect = require('chai').expect;
const okta = require('../../src');
const models = require('../../src/models');
const mockGroup = require('./mocks/group.json');
let orgUrl = process.env.OKTA_CLIENT_ORGURL;

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

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

describe('Group role API', () => {
describe('Role assignment', () => {
let group;
beforeEach(async () => {
group = await client.createGroup(mockGroup);
});
afterEach(async () => {
await group.delete();
});

it('should assign and unassign role to/from group', async () => {
const role = await group.assignRole({ type: 'APP_ADMIN' });
expect(role).to.be.instanceOf(models.Role);

const res = await client.removeRoleFromGroup(group.id, role.id);
expect(res.status).to.equal(204);
});
});
});
2 changes: 1 addition & 1 deletion test/it/session-end-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Sessions API', () => {
});

// 3 - end all user sessions
await createdUser.endAllSessions();
await createdUser.clearSessions();

// 4 - attempt to retrieve session1
let sess1;
Expand Down
6 changes: 3 additions & 3 deletions test/it/user-group-target-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('User Role API Tests', () => {
const role = await createdUser.assignRole(roleType);

// 3. Add Group Target to User Admin Role
await createdUser.addGroupTargetToRole(role.id, createdGroup.id);
await createdUser.addGroupTarget(role.id, createdGroup.id);

// 4. List Group Targets for Role
let groupTargetPresent = await utils.isGroupTargetPresent(createdUser, createdGroup, role);
Expand All @@ -59,9 +59,9 @@ describe('User Role API Tests', () => {
await utils.cleanup(client, null, group);

const adminGroup = await client.createGroup(group);
await createdUser.addGroupTargetToRole(role.id, adminGroup.id);
await createdUser.addGroupTarget(role.id, adminGroup.id);

await createdUser.removeGroupTargetFromRole(role.id, createdGroup.id);
await createdUser.removeGroupTarget(role.id, createdGroup.id);
groupTargetPresent = await utils.isGroupTargetPresent(createdUser, createdGroup, role);
expect(groupTargetPresent).to.equal(false);

Expand Down
2 changes: 1 addition & 1 deletion test/it/user-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('User lifecycle API', () => {
});

it('should get response with status 204', async () => {
const response = await createdUser.endAllSessions();
const response = await createdUser.clearSessions();
expect(response.status).to.be.equal(204);
});
});
Expand Down
145 changes: 145 additions & 0 deletions test/it/user-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
const expect = require('chai').expect;
const okta = require('../../src');
const models = require('../../src/models');
const Collection = require('../../src/collection');
const mockGroup = require('./mocks/group.json');
const mockUser = require('./mocks/user-without-credentials.json');
const utils = require('../utils');
let orgUrl = process.env.OKTA_CLIENT_ORGURL;

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

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

describe('User role API', () => {
let user;
beforeEach(async () => {
user = await client.createUser(mockUser, { activate: false });
});
afterEach(async () => {
await utils.cleanupUser(client, user);
});

describe('Role assignment', () => {
let role;
afterEach(async () => {
user.removeRole(role.id);
});

it('should assign role to user', async () => {
role = await user.assignRole({ type: 'APP_ADMIN' });
expect(role).to.be.instanceOf(models.Role);
expect(role.id).to.be.exist;
expect(role.type).to.equal('APP_ADMIN');
});
});

describe('Role unassignment', () => {
let role;
beforeEach(async () => {
role = await user.assignRole({ type: 'APP_ADMIN' });
});

it('should unassign role from user', async () => {
const res = await user.removeRole(role.id);
expect(res.status).to.equal(204);
});
});

describe('List user assigned roles', () => {
let role;
beforeEach(async () => {
role = await user.assignRole({ type: 'APP_ADMIN' });
});
afterEach(async () => {
user.removeRole(role.id);
});

it('should return a Collection of roles', async () => {
const roles = await user.listAssignedRoles();
expect(roles).to.be.instanceOf(Collection);
await roles.each(roleFromCollection => {
expect(roleFromCollection).to.be.instanceOf(models.Role);
expect(roleFromCollection.id).to.be.equal(role.id);
});
});
});

describe('App targets for admin role', () => {
let role;
let application;
beforeEach(async () => {
role = await user.assignRole({ type: 'APP_ADMIN' });
const mockApplication = utils.getBookmarkApplication();
application = await client.createApplication(mockApplication);
});
afterEach(async () => {
await application.deactivate();
await application.delete();
await user.removeRole(role.id);
});

describe('Add app target', () => {
it('should add app target to admin user', async () => {
const res = await role.addAppTargetToAdminRoleForUser(user.id, application.name);
expect(res.status).to.equal(204);
});
});

describe('List app targets', () => {
beforeEach(async () => {
await role.addAppTargetToAdminRoleForUser(user.id, application.name);
});

it('should return a Collection of CatalogApplications', async () => {
const apps = await client.listApplicationTargetsForApplicationAdministratorRoleForUser(user.id, role.id);
expect(apps).to.be.instanceOf(Collection);
await apps.each(app => {
expect(app).to.be.instanceOf(models.CatalogApplication);
expect(app.name).to.be.equal(application.name);
});
});
});
});

describe('Group targets for admin role', () => {
let role;
let group;
beforeEach(async () => {
role = await user.assignRole({ type: 'USER_ADMIN' });
group = await client.createGroup(mockGroup);
});
afterEach(async () => {
await user.removeRole(role.id);
await group.delete();
});

describe('Add group target', () => {
it('should add group target to admin user', async () => {
const res = await user.addGroupTarget(role.id, group.id);
expect(res.status).to.equal(204);
});
});

describe('List group targets', () => {
beforeEach(async () => {
await user.addGroupTarget(role.id, group.id);
});

it('should return a Collection of Groups', async () => {
const groups = await client.listApplicationTargetsForApplicationAdministratorRoleForUser(user.id, role.id);
expect(groups).to.be.instanceOf(Collection);
await groups.each(groupFromCollection => {
expect(groupFromCollection).to.be.instanceOf(models.Group);
expect(groupFromCollection.id).to.be.equal(group.id);
});
});
});
});
});
4 changes: 2 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function isGroupPresent(client, expectedGroup, queryParameters) {

async function doesUserHaveRole(user, roleType) {
let hasRole = false;
await user.listRoles().each(role => {
await user.listAssignedRoles().each(role => {
expect(role).to.be.an.instanceof(models.Role);
if (role.type === roleType) {
hasRole = true;
Expand All @@ -107,7 +107,7 @@ async function doesUserHaveRole(user, roleType) {

async function isGroupTargetPresent(user, userGroup, role) {
let groupTargetPresent = false;
const groupTargets = user.listGroupTargetsForRole(role.id);
const groupTargets = user.listGroupTargets(role.id);
await groupTargets.each(group => {
if (group.profile.name === userGroup.profile.name) {
groupTargetPresent = true;
Expand Down

0 comments on commit a65f024

Please sign in to comment.