Skip to content

Commit

Permalink
Unit tests for RuleToAction
Browse files Browse the repository at this point in the history
  • Loading branch information
patkub committed Jul 23, 2024
1 parent fd1f21b commit 412d557
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auth0-rule-as-action",
"version": "0.2.4",
"version": "0.2.5",
"description": "Run an Auth0 Rule as an Action",
"author": "Patrick Kubiak <[email protected]>",
"main": "dist/RuleToAction.js",
Expand Down
76 changes: 76 additions & 0 deletions test/RuleToAction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"use strict";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

import { beforeEach, afterEach, describe, it } from "mocha";
const chai = require('chai');
const spies = require('chai-spies');
chai.use(spies);

const sandbox = chai.spy.sandbox();

import { createEvent } from "./_mocks/event.js";
import { api } from "./_mocks/api.js";
import { setupApiSpy } from "./_helpers/setupApiSpy.js";
import { convert } from "../src/RuleToAction.mjs"

let event;

describe('RuleToAction', function () {

beforeEach(function () {
// reset Auth0 event
event = createEvent();
// spy on all Auth0 api methods
setupApiSpy(sandbox, api);
});

afterEach(function () {
sandbox.restore();
});

describe('using rules', function() {
it('denies access for rule that throws error', async function () {
// Prepare
let rule = function (user, context, callback) {
return callback(
new UnauthorizedError("This app is unavailable")

Check failure on line 38 in test/RuleToAction.test.js

View workflow job for this annotation

GitHub Actions / build

'UnauthorizedError' is not defined

Check failure on line 38 in test/RuleToAction.test.js

View workflow job for this annotation

GitHub Actions / build

'UnauthorizedError' is not defined

Check failure on line 38 in test/RuleToAction.test.js

View workflow job for this annotation

GitHub Actions / build

'UnauthorizedError' is not defined
);
}
let context = {};

// Act
await convert(event, api, rule, context);

// Assert
chai.expect(api.access.deny).to.have.been.called.with("This app is unavailable");
});

it('converts exampleRule rule', async function () {
// Prepare
let rule = function exampleRule(user, context, callback) {
// ID and Access token claims
context.idToken["https://example.com/testIDToken"] = "testIDTokenValue";
context.accessToken["https://example.com/testAccessToken"] = "testAccessTokenValue";
// SAML
context.samlConfiguration.mappings = {
'https://example.com/SAML/Attributes/Role': 'role',
'https://example.com/SAML/Attributes/RoleSessionName': 'session'
};

callback(null, user, context);
}
let context = {};

// Act
await convert(event, api, rule, context);

// Assert
chai.expect(api.idToken.setCustomClaim).to.have.been.called.with("https://example.com/testIDToken", "testIDTokenValue");
chai.expect(api.accessToken.setCustomClaim).to.have.been.called.with("https://example.com/testAccessToken", "testAccessTokenValue");
chai.expect(api.samlResponse.setAttribute).to.have.been.called.with("https://example.com/SAML/Attributes/Role", "role");
chai.expect(api.samlResponse.setAttribute).to.have.been.called.with("https://example.com/SAML/Attributes/RoleSessionName", "session");
});
})
});
41 changes: 0 additions & 41 deletions test/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,46 +133,5 @@ describe('convert', function () {
chai.expect(recievedConvertGlobals.oldContext).to.deep.equal(expectedContext);
});

it('denies access for rule that throws error', async function () {
// Prepare
let rule = function (user, context, callback) {
return callback(
new UnauthorizedError("This app is unavailable")
);
}
let context = {};

// Act
await convert(event, api, rule, context);

// Assert
chai.expect(api.access.deny).to.have.been.called.with("This app is unavailable");
});
})

it('convert exampleRule rule', async function () {
// Prepare
let rule = function exampleRule(user, context, callback) {
// ID and Access token claims
context.idToken["https://example.com/testIDToken"] = "testIDTokenValue";
context.accessToken["https://example.com/testAccessToken"] = "testAccessTokenValue";
// SAML
context.samlConfiguration.mappings = {
'https://example.com/SAML/Attributes/Role': 'role',
'https://example.com/SAML/Attributes/RoleSessionName': 'session'
};

callback(null, user, context);
}
let context = {};

// Act
await convert(event, api, rule, context);

// Assert
chai.expect(api.idToken.setCustomClaim).to.have.been.called.with("https://example.com/testIDToken", "testIDTokenValue");
chai.expect(api.accessToken.setCustomClaim).to.have.been.called.with("https://example.com/testAccessToken", "testAccessTokenValue");
chai.expect(api.samlResponse.setAttribute).to.have.been.called.with("https://example.com/SAML/Attributes/Role", "role");
chai.expect(api.samlResponse.setAttribute).to.have.been.called.with("https://example.com/SAML/Attributes/RoleSessionName", "session");
});
});

0 comments on commit 412d557

Please sign in to comment.