From 412d5572a88bdfdd14d1b3df9cb9eb519c15e7dc Mon Sep 17 00:00:00 2001 From: Patrick Kubiak Date: Tue, 23 Jul 2024 16:05:21 -0400 Subject: [PATCH] Unit tests for RuleToAction --- package.json | 2 +- test/RuleToAction.test.js | 76 +++++++++++++++++++++++++++++++++++++++ test/convert.test.js | 41 --------------------- 3 files changed, 77 insertions(+), 42 deletions(-) create mode 100644 test/RuleToAction.test.js diff --git a/package.json b/package.json index d9ddccc..3e99f8d 100644 --- a/package.json +++ b/package.json @@ -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 ", "main": "dist/RuleToAction.js", diff --git a/test/RuleToAction.test.js b/test/RuleToAction.test.js new file mode 100644 index 0000000..d3b3337 --- /dev/null +++ b/test/RuleToAction.test.js @@ -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") + ); + } + 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"); + }); + }) +}); \ No newline at end of file diff --git a/test/convert.test.js b/test/convert.test.js index fd2ad56..edb8d1b 100644 --- a/test/convert.test.js +++ b/test/convert.test.js @@ -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"); - }); }); \ No newline at end of file