From ecf00f01bf720ed9ddb7fbb907c3ca50e52fb2ef Mon Sep 17 00:00:00 2001 From: Patrick Ammann Date: Tue, 13 Jul 2021 09:25:20 +0200 Subject: [PATCH] feat: #1 adapt unit-tests --- test/unit/AccessTokenEvents.spec.js | 127 ---- test/unit/AccessTokenEvents.test.ts | 142 ++++ test/unit/ErrorResponse.spec.js | 77 -- test/unit/ErrorResponse.test.ts | 96 +++ test/unit/Event.spec.js | 113 --- test/unit/Event.test.ts | 122 ++++ test/unit/Log.spec.js | 140 ---- test/unit/Log.test.ts | 157 ++++ test/unit/OidcClientSettings.spec.js | 509 ------------- test/unit/OidcClientSettings.test.ts | 676 ++++++++++++++++++ test/unit/UrlUtility.spec.js | 79 -- test/unit/UrlUtility.test.ts | 118 +++ test/unit/UserManager.spec.js | 158 ---- test/unit/UserManager.test.ts | 176 +++++ test/unit/UserManagerEvents.spec.js | 56 -- test/unit/UserManagerEvents.test.ts | 59 ++ ...gs.spec.js => UserManagerSettings.test.ts} | 221 ++++-- test/unit/WebStorageStateStore.spec.js | 175 ----- test/unit/WebStorageStateStore.test.ts | 195 +++++ test/unit/{random.spec.js => random.test.ts} | 15 +- 20 files changed, 1915 insertions(+), 1496 deletions(-) delete mode 100644 test/unit/AccessTokenEvents.spec.js create mode 100644 test/unit/AccessTokenEvents.test.ts delete mode 100644 test/unit/ErrorResponse.spec.js create mode 100644 test/unit/ErrorResponse.test.ts delete mode 100644 test/unit/Event.spec.js create mode 100644 test/unit/Event.test.ts delete mode 100644 test/unit/Log.spec.js create mode 100644 test/unit/Log.test.ts delete mode 100644 test/unit/OidcClientSettings.spec.js create mode 100644 test/unit/OidcClientSettings.test.ts delete mode 100644 test/unit/UrlUtility.spec.js create mode 100644 test/unit/UrlUtility.test.ts delete mode 100644 test/unit/UserManager.spec.js create mode 100644 test/unit/UserManager.test.ts delete mode 100644 test/unit/UserManagerEvents.spec.js create mode 100644 test/unit/UserManagerEvents.test.ts rename test/unit/{UserManagerSettings.spec.js => UserManagerSettings.test.ts} (50%) delete mode 100644 test/unit/WebStorageStateStore.spec.js create mode 100644 test/unit/WebStorageStateStore.test.ts rename test/unit/{random.spec.js => random.test.ts} (57%) diff --git a/test/unit/AccessTokenEvents.spec.js b/test/unit/AccessTokenEvents.spec.js deleted file mode 100644 index 43005a720..000000000 --- a/test/unit/AccessTokenEvents.spec.js +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { AccessTokenEvents } from '../../src/AccessTokenEvents'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -class StubTimer { - - constructor() { - this.cancelWasCalled = false; - } - - init(duration) { - this.duration = duration; - } - - cancel() { - this.cancelWasCalled = true; - } - - addHandler(){} - removeHandler(){} -} - -describe("AccessTokenEvents", function () { - - let subject; - let accessTokenExpiringTimer; - let accessTokenExpiredTimer; - - beforeEach(function () { - accessTokenExpiringTimer = new StubTimer(); - accessTokenExpiredTimer = new StubTimer(); - subject = new AccessTokenEvents({ - accessTokenExpiringTimer, accessTokenExpiredTimer - }); - }); - - describe("constructor", function () { - - it("should use default expiringNotificationTime", function () { - subject._accessTokenExpiringNotificationTime.should.equal(60); - }); - - }); - - describe("load", function () { - - it("should cancel existing timers", function () { - subject.load({}); - - accessTokenExpiringTimer.cancelWasCalled.should.be.true; - accessTokenExpiredTimer.cancelWasCalled.should.be.true; - }); - - it("should initialize timers", function () { - subject.load({ - access_token:"token", - expires_in : 70 - }); - - accessTokenExpiringTimer.duration.should.equal(10); - accessTokenExpiredTimer.duration.should.equal(71); - }); - - it("should immediately schedule expiring timer if expiration is soon", function () { - subject.load({ - access_token:"token", - expires_in : 10 - }); - - accessTokenExpiringTimer.duration.should.equal(1); - }); - - it("should not initialize expiring timer if already expired", function () { - subject.load({ - access_token:"token", - expires_in : 0 - }); - - assert.isUndefined(accessTokenExpiringTimer.duration); - }); - - it("should initialize expired timer if already expired", function () { - subject.load({ - access_token:"token", - expires_in : 0 - }); - - accessTokenExpiredTimer.duration.should.equal(1); - }); - - it("should not initialize timers if no access token", function () { - subject.load({ - expires_in : 70 - }); - - assert.isUndefined(accessTokenExpiringTimer.duration); - assert.isUndefined(accessTokenExpiredTimer.duration); - }); - - it("should not initialize timers if no expiration on access token", function () { - subject.load({ - access_token:"token" - }); - - assert.isUndefined(accessTokenExpiringTimer.duration); - assert.isUndefined(accessTokenExpiredTimer.duration); - }); - }); - - describe("unload", function () { - - it("should cancel timers", function () { - - subject.unload(); - - accessTokenExpiringTimer.cancelWasCalled.should.be.true; - accessTokenExpiredTimer.cancelWasCalled.should.be.true; - }); - - }); - -}); diff --git a/test/unit/AccessTokenEvents.test.ts b/test/unit/AccessTokenEvents.test.ts new file mode 100644 index 000000000..dd6470cf0 --- /dev/null +++ b/test/unit/AccessTokenEvents.test.ts @@ -0,0 +1,142 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { AccessTokenEvents } from '../../src/AccessTokenEvents'; +import { Timer } from '../../src/Timer'; +import { User } from '../../src/User'; + +describe("AccessTokenEvents", () => { + + let subject: AccessTokenEvents; + let accessTokenExpiringTimer: StubTimer; + let accessTokenExpiredTimer: StubTimer; + + beforeEach(() => { + accessTokenExpiringTimer = new StubTimer("stub expiring timer"); + accessTokenExpiredTimer = new StubTimer("stub expired timer"); + subject = new AccessTokenEvents({ + accessTokenExpiringTimer, accessTokenExpiredTimer + }); + }); + + /* TODO: port-ts + describe("constructor", () => { + + it("should use default expiringNotificationTime", () => { + subject._accessTokenExpiringNotificationTime.should.equal(60); + }); + + });*/ + + describe("load", () => { + + it("should cancel existing timers", () => { + // act + subject.load({} as User); + + // assert + expect(accessTokenExpiringTimer.cancelWasCalled).toEqual(true); + expect(accessTokenExpiredTimer.cancelWasCalled).toEqual(true); + }); + + it("should initialize timers", () => { + // act + subject.load({ + access_token:"token", + expires_in : 70 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toEqual(10); + expect(accessTokenExpiredTimer.duration).toEqual(71); + }); + + it("should immediately schedule expiring timer if expiration is soon", () => { + // act + subject.load({ + access_token:"token", + expires_in : 10 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toEqual(1); + }); + + it("should not initialize expiring timer if already expired", () => { + // act + subject.load({ + access_token:"token", + expires_in : 0 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toEqual(undefined); + }); + + it("should initialize expired timer if already expired", () => { + // act + subject.load({ + access_token:"token", + expires_in : 0 + } as User); + + // assert + expect(accessTokenExpiredTimer.duration).toEqual(1); + }); + + it("should not initialize timers if no access token", () => { + // act + subject.load({ + expires_in : 70 + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toEqual(undefined); + expect(accessTokenExpiredTimer.duration).toEqual(undefined); + }); + + it("should not initialize timers if no expiration on access token", () => { + // act + subject.load({ + access_token:"token" + } as User); + + // assert + expect(accessTokenExpiringTimer.duration).toEqual(undefined); + expect(accessTokenExpiredTimer.duration).toEqual(undefined); + }); + }); + + describe("unload", () => { + + it("should cancel timers", () => { + // act + subject.unload(); + + // assert + expect(accessTokenExpiringTimer.cancelWasCalled).toEqual(true); + expect(accessTokenExpiredTimer.cancelWasCalled).toEqual(true); + }); + }); +}); + +class StubTimer extends Timer { + cancelWasCalled: boolean; + duration: any; + + constructor(name: string) { + super(name) + this.cancelWasCalled = false; + } + + init(duration: number) { + this.duration = duration; + } + + cancel() { + this.cancelWasCalled = true; + } + + addHandler() {} + removeHandler() {} +} diff --git a/test/unit/ErrorResponse.spec.js b/test/unit/ErrorResponse.spec.js deleted file mode 100644 index 3ae611b8e..000000000 --- a/test/unit/ErrorResponse.spec.js +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Log } from '../../src/Log'; -import { ErrorResponse } from '../../src/ErrorResponse'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; -let expect = chai.expect; - -describe("ErrorResponse", function() { - - describe("constructor", function() { - - it("should require a error param", function() { - try { - new ErrorResponse({}); - } - catch (e) { - e.message.should.contain('error'); - return; - } - assert.fail(); - }); - - it("should read error", function() { - let subject = new ErrorResponse({error:"foo"}); - subject.error.should.equal("foo"); - }); - - it("should read error_description", function() { - let subject = new ErrorResponse({error:"error", error_description:"foo"}); - subject.error_description.should.equal("foo"); - }); - - it("should read error_uri", function() { - let subject = new ErrorResponse({error:"error", error_uri:"foo"}); - subject.error_uri.should.equal("foo"); - }); - - it("should read state", function() { - let subject = new ErrorResponse({error:"error", state:"foo"}); - subject.state.should.equal("foo"); - }); - }); - - describe("message", function() { - it("should be description if set", function() { - let subject = new ErrorResponse({error:"error", error_description:"foo"}); - subject.message.should.equal("foo"); - }); - - it("should be error if description not set", function() { - let subject = new ErrorResponse({error:"error"}); - subject.message.should.equal("error"); - }); - - }); - - describe("name", function() { - it("should be class name", function() { - let subject = new ErrorResponse({error:"error"}); - subject.name.should.equal("ErrorResponse"); - }); - - }); - - describe("stack", function() { - - it("should be set", function() { - let subject = new ErrorResponse({error:"error"}); - subject.stack.should.be.ok; - }); - - }); -}); diff --git a/test/unit/ErrorResponse.test.ts b/test/unit/ErrorResponse.test.ts new file mode 100644 index 000000000..a615733dc --- /dev/null +++ b/test/unit/ErrorResponse.test.ts @@ -0,0 +1,96 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { ErrorResponse } from '../../src/ErrorResponse'; + +describe("ErrorResponse", () => { + + describe("constructor", () => { + + it("should require a error param", () => { + // act + try { + new ErrorResponse({}); + } + catch (e) { + expect(e.message).toContain("error"); + return; + } + + fail("should not come here"); + }); + + it("should read error", () => { + // act + let subject = new ErrorResponse({error:"foo"}); + + // assert + expect(subject.error).toEqual("foo"); + }); + + it("should read error_description", () => { + // act + let subject = new ErrorResponse({error:"error", error_description:"foo"}); + + // assert + expect(subject.error_description).toEqual("foo"); + }); + + it("should read error_uri", () => { + // act + let subject = new ErrorResponse({error:"error", error_uri:"foo"}); + + // assert + expect(subject.error_uri).toEqual("foo"); + }); + + it("should read state", () => { + // act + let subject = new ErrorResponse({error:"error", state:"foo"}); + + // assert + expect(subject.state).toEqual("foo"); + }); + + }); + + describe("message", () => { + it("should be description if set", () => { + // act + let subject = new ErrorResponse({error:"error", error_description:"foo"}); + + // assert + expect(subject.message).toEqual("foo"); + }); + + it("should be error if description not set", () => { + // act + let subject = new ErrorResponse({error:"error"}); + + // assert + expect(subject.message).toEqual("error"); + }); + }); + + describe("name", () => { + it("should be class name", () => { + // act + let subject = new ErrorResponse({error:"error"}); + + // assert + expect(subject.name).toEqual("ErrorResponse"); + }); + + }); + + describe("stack", () => { + it("should be set", () => { + // act + let subject = new ErrorResponse({error:"error"}); + + // assert + expect(subject.stack).not.toBeNull(); + }); + + }); +}); diff --git a/test/unit/Event.spec.js b/test/unit/Event.spec.js deleted file mode 100644 index 20bfa19a0..000000000 --- a/test/unit/Event.spec.js +++ /dev/null @@ -1,113 +0,0 @@ - -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Event } from '../../src/Event'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("Event", function () { - - let subject; - - beforeEach(function () { - subject = new Event("test name"); - }); - - describe("addHandler", function () { - - it("should allow callback to be invoked", function () { - var cb = function () { - cb.wasCalled = true; - }; - subject.addHandler(cb); - - subject.raise(); - - cb.wasCalled.should.be.true; - }); - - it("should allow multiple callbacks", function () { - var count = 0; - var cb = function () { - count++; - }; - subject.addHandler(cb); - subject.addHandler(cb); - subject.addHandler(cb); - subject.addHandler(cb); - - subject.raise(); - - count.should.equal(4); - }); - - }); - - describe("removeHandler", function () { - - it("should remove callback from being invoked", function () { - var cb = function () { - cb.wasCalled = true; - }; - cb.wasCalled = false; - - subject.addHandler(cb); - subject.removeHandler(cb); - subject.raise(); - - cb.wasCalled.should.be.false; - }); - - it("should remove individual callback", function () { - var count = 0; - var cb1 = function () { - count++; - }; - var cb2 = function () { - cb2.wasCalled = true; - }; - - subject.addHandler(cb1); - subject.addHandler(cb2); - subject.addHandler(cb1); - subject.removeHandler(cb1); - subject.removeHandler(cb1); - - subject.raise(); - - count.should.equal(0); - cb2.wasCalled.should.be.true; - }); - - }); - - describe("raise", function () { - - it("should pass params", function () { - var cb = function (a,b,c) { - a.should.equal(1); - b.should.equal(2); - c.should.equal(3); - }; - subject.addHandler(cb); - - subject.raise(1,2,3); - }); - - it("should allow passing no params", function () { - var cb = function (a,b,c) { - assert.isUndefined(a); - assert.isUndefined(b); - assert.isUndefined(c); - }; - subject.addHandler(cb); - - subject.raise(); - }); - - }); - -}); diff --git a/test/unit/Event.test.ts b/test/unit/Event.test.ts new file mode 100644 index 000000000..290b5f88c --- /dev/null +++ b/test/unit/Event.test.ts @@ -0,0 +1,122 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { Event } from '../../src/Event'; + +describe("Event", () => { + + let subject: Event; + + beforeEach(() => { + subject = new Event("test name"); + }); + + describe("addHandler", () => { + + it("should allow callback to be invoked", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addHandler(cb); + subject.raise(); + + // assert + expect(cb).toBeCalled(); + }); + + it("should allow multiple callbacks", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addHandler(cb); + subject.addHandler(cb); + subject.addHandler(cb); + subject.addHandler(cb); + subject.raise(); + + // assert + expect(cb).toBeCalledTimes(4); + }); + }); + + describe("removeHandler", () => { + + it("should remove callback from being invoked", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addHandler(cb); + subject.removeHandler(cb); + subject.raise(); + + // assert + expect(cb).toBeCalledTimes(0); + }); + + it("should remove individual callback", () => { + // arrange + var cb1 = jest.fn(); + var cb2 = jest.fn(); + + // act + subject.addHandler(cb1); + subject.addHandler(cb2); + subject.addHandler(cb1); + subject.removeHandler(cb1); + subject.removeHandler(cb1); + + subject.raise(); + + // assert + expect(cb1).toBeCalledTimes(0); + expect(cb2).toBeCalledTimes(1); + }); + }); + + describe("raise", () => { + + it("should pass params", () => { + // arrange + let a: any = 10; + let b: any = 11; + let c: any = 12; + var cb = function (arg_a: any, arg_b: any, arg_c: any) { + a = arg_a; + b = arg_b; + c = arg_c; + }; + subject.addHandler(cb); + + // act + subject.raise(1, 2, 3); + + // assert + expect(a).toEqual(1); + expect(b).toEqual(2); + expect(c).toEqual(3); + }); + + it("should allow passing no params", () => { + // arrange + let a: any = 10; + let b: any = 11; + let c: any = 12; + var cb = function (arg_a: any, arg_b: any, arg_c: any) { + a = arg_a; + b = arg_b; + c = arg_c; + }; + subject.addHandler(cb); + + subject.raise(); + + // assert + expect(a).toEqual(undefined); + expect(b).toEqual(undefined); + expect(c).toEqual(undefined); + }); + }); +}); diff --git a/test/unit/Log.spec.js b/test/unit/Log.spec.js deleted file mode 100644 index d6c560a7f..000000000 --- a/test/unit/Log.spec.js +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Log } from '../../src/Log'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("Log", function() { - - beforeEach(function() { - Log.reset(); - Log.level = Log.INFO; - }); - - describe("level", function() { - - it("should not log when set to NONE", function() { - let stub = new StubLog(); - Log.logger = stub; - Log.level = Log.NONE; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.false; - stub.warnWasCalled.should.be.false; - stub.errorWasCalled.should.be.false; - }); - - it("should not log info or warn for ERROR level", function() { - let stub = new StubLog(); - - Log.logger = stub; - Log.level = Log.ERROR; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.false; - stub.warnWasCalled.should.be.false; - stub.errorWasCalled.should.be.true; - }); - - it("should not log info for WARN level", function() { - let stub = new StubLog(); - - Log.logger =stub; - Log.level = Log.WARN; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.false; - stub.warnWasCalled.should.be.true; - stub.errorWasCalled.should.be.true; - }); - - it("should log to all for INFO level", function() { - let stub = new StubLog(); - - Log.logger = stub; - Log.level = Log.INFO; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoWasCalled.should.be.true; - stub.warnWasCalled.should.be.true; - stub.errorWasCalled.should.be.true; - }); - - }); - - describe("logger", function() { - - it("should use the logger specified", function() { - let stub = new StubLog(); - Log.logger = stub; - - Log.info("test info"); - Log.warn("test warn"); - Log.error("test error"); - - stub.infoParam.should.equal("test info"); - stub.warnParam.should.equal("test warn"); - stub.errorParam.should.equal("test error"); - }); - - }); - - describe("info", function() { - - it("should work with no config", function() { - Log.info("test"); - }); - - }); - - describe("warn", function() { - - it("should work with no config", function() { - Log.warn("test"); - }); - - }); - - describe("error", function() { - - it("should work with no config", function() { - Log.error("test"); - }); - - }); -}); - -class StubLog { - constructor(){ - this.infoWasCalled = false; - this.warnWasCalled = false; - this.errorWasCalled = false; - } - info(arg) { - this.infoParam = arg; - this.infoWasCalled = true; - } - warn(arg) { - this.warnParam = arg; - this.warnWasCalled = true; - } - error(arg) { - this.errorParam = arg; - this.errorWasCalled = true; - } -} diff --git a/test/unit/Log.test.ts b/test/unit/Log.test.ts new file mode 100644 index 000000000..656fa51b9 --- /dev/null +++ b/test/unit/Log.test.ts @@ -0,0 +1,157 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { Log, Logger } from '../../src/Log'; + +describe("Log", () => { + beforeEach(() => { + Log.reset(); + Log.level = Log.INFO; + }); + + describe("level", () => { + + it("should not log when set to NONE", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.NONE; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toEqual(false); + expect(stub.warnWasCalled).toEqual(false); + expect(stub.errorWasCalled).toEqual(false); + }); + + it("should not log info or warn for ERROR level", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.ERROR; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toEqual(false); + expect(stub.warnWasCalled).toEqual(false); + expect(stub.errorWasCalled).toEqual(true); + }); + + it("should not log info for WARN level", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.WARN; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toEqual(false); + expect(stub.warnWasCalled).toEqual(true); + expect(stub.errorWasCalled).toEqual(true); + }); + + it("should log to all for INFO level", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + Log.level = Log.INFO; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoWasCalled).toEqual(true); + expect(stub.warnWasCalled).toEqual(true); + expect(stub.errorWasCalled).toEqual(true); + }); + }); + + describe("logger", () => { + + it("should use the logger specified", () => { + // arrange + let stub = new StubLog(); + Log.logger = stub; + + // act + Log.info("test info"); + Log.warn("test warn"); + Log.error("test error"); + + // assert + expect(stub.infoParam).toEqual("test info"); + expect(stub.warnParam).toEqual("test warn"); + expect(stub.errorParam).toEqual("test error"); + }); + }); + + describe("info", () => { + + it("should work with no config", () => { + Log.info("test"); + }); + }); + + describe("warn", () => { + + it("should work with no config", () => { + Log.warn("test"); + }); + + }); + + describe("error", () => { + + it("should work with no config", () => { + Log.error("test"); + }); + }); +}); + +class StubLog implements Logger { + debugWasCalled: boolean; + infoWasCalled: boolean; + warnWasCalled: boolean; + errorWasCalled: boolean; + debugParam: any; + infoParam: any; + warnParam: any; + errorParam: any; + + constructor() { + this.debugWasCalled = false; + this.infoWasCalled = false; + this.warnWasCalled = false; + this.errorWasCalled = false; + } + debug(arg: any) { + this.debugParam = arg; + this.debugWasCalled = true; + } + info(arg: any) { + this.infoParam = arg; + this.infoWasCalled = true; + } + warn(arg: any) { + this.warnParam = arg; + this.warnWasCalled = true; + } + error(arg: any) { + this.errorParam = arg; + this.errorWasCalled = true; + } +} diff --git a/test/unit/OidcClientSettings.spec.js b/test/unit/OidcClientSettings.spec.js deleted file mode 100644 index 1d9cd6d25..000000000 --- a/test/unit/OidcClientSettings.spec.js +++ /dev/null @@ -1,509 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Log } from '../../src/Log'; -import { OidcClientSettings } from '../../src/OidcClientSettings'; -import { Global } from '../../src/Global'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("OidcClientSettings", function () { - - beforeEach(function () { - Global._testing(); - Log.logger = console; - Log.level = Log.NONE; - }); - - describe("client_id", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client' - }); - subject.client_id.should.equal("client"); - }); - - it("should not allow setting if previously set", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - authority: "http://sts" - }); - try { - subject.client_id = "diff"; - assert.fail(); - } - catch (e) { - e.message.should.contain("client_id"); - } - }); - - it("should allow setting if not previously set", function () { - let subject = new OidcClientSettings({ - }); - subject.client_id = "test"; - subject.client_id.should.equal("test"); - }); - }); - - describe("client_secret", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_secret: 'secret' - }); - subject.client_secret.should.equal("secret"); - }); - }); - - describe("response_type", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - response_type: "foo" - }); - subject.response_type.should.equal("foo"); - }); - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client' - }); - subject.response_type.should.equal("id_token"); - }); - - }); - - describe("scope", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - scope: "foo" - }); - subject.scope.should.equal("foo"); - }); - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client' - }); - subject.scope.should.equal("openid"); - }); - - }); - - describe("redirect_uri", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - redirect_uri: "foo" - }); - subject.redirect_uri.should.equal("foo"); - }); - - }); - - describe("post_logout_redirect_uri", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - post_logout_redirect_uri: "http://app/loggedout" - }); - subject.post_logout_redirect_uri.should.equal("http://app/loggedout"); - }); - }); - - describe("prompt", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - prompt: "foo" - }); - subject.prompt.should.equal("foo"); - }); - }); - - describe("display", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - display: "foo" - }); - subject.display.should.equal("foo"); - }); - }); - - describe("max_age", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - max_age: "foo" - }); - subject.max_age.should.equal("foo"); - }); - }); - - describe("ui_locales", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - ui_locales: "foo" - }); - subject.ui_locales.should.equal("foo"); - }); - }); - - describe("acr_values", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - acr_values: "foo" - }); - subject.acr_values.should.equal("foo"); - }); - }); - - describe("resource", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - resource: "foo" - }); - subject.resource.should.equal("foo"); - }); - }); - - describe("response_mode", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - response_mode: "foo" - }); - subject.response_mode.should.equal("foo"); - }); - }); - - describe("authority", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - authority: "http://sts" - }); - subject.authority.should.equal("http://sts"); - }); - - it("should not allow setting if previously set", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - authority: "http://sts" - }); - try { - subject.authority = "http://sts2"; - assert.fail(); - } - catch (e) { - e.message.should.contain("authority"); - } - }); - - it("should allow setting if not previously set", function () { - let subject = new OidcClientSettings({ - client_id: 'client' - }); - subject.authority = "http://sts"; - subject.authority.should.equal("http://sts"); - }); - }); - - describe("metadataUrl", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - metadataUrl: "http://sts/metadata" - }); - subject.metadataUrl.should.equal("http://sts/metadata"); - }); - - it("should infer value from authority", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - authority: "http://sts" - }); - subject.metadataUrl.should.equal("http://sts/.well-known/openid-configuration"); - - subject = new OidcClientSettings({ - client_id: 'client', - authority: "http://sts/" - }); - subject.metadataUrl.should.equal("http://sts/.well-known/openid-configuration"); - }); - - }); - - describe("metadata", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - metadata: "test" - }); - subject.metadata.should.equal("test"); - }); - - it("should store value", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - }); - subject.metadata = "test"; - subject.metadata.should.equal("test"); - }); - - }); - - describe("signingKeys", function () { - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - signingKeys: "test" - }); - subject.signingKeys.should.equal("test"); - }); - - it("should store value", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - }); - subject.signingKeys = "test"; - subject.signingKeys.should.equal("test"); - }); - - }); - - describe("filterProtocolClaims", function () { - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - }); - subject.filterProtocolClaims.should.equal(true); - }); - - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - filterProtocolClaims: true - }); - subject.filterProtocolClaims.should.equal(true); - - subject = new OidcClientSettings({ - client_id: 'client', - filterProtocolClaims: false - }); - subject.filterProtocolClaims.should.equal(false); - }); - }); - - describe("loadUserInfo", function () { - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - }); - subject.loadUserInfo.should.equal(true); - }); - - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - loadUserInfo: true - }); - subject.loadUserInfo.should.equal(true); - - subject = new OidcClientSettings({ - client_id: 'client', - loadUserInfo: false - }); - subject.loadUserInfo.should.equal(false); - }); - }); - - describe("staleStateAge", function () { - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - }); - subject.staleStateAge.should.equal(900); - }); - - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - staleStateAge: 100 - }); - subject.staleStateAge.should.equal(100); - }); - }); - - describe("clockSkew", function () { - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client' - }); - subject.clockSkew.should.equal(5 * 60); // 5 mins - }); - - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - clockSkew: 10 - }); - subject.clockSkew.should.equal(10); - }); - }); - - describe("stateStore", function () { - - it("should return value from initial settings", function () { - let temp = {}; - let subject = new OidcClientSettings({ - client_id: 'client', - stateStore: temp - }); - subject.stateStore.should.equal(temp); - }); - }); - - describe("stateStore", function () { - - it("should return value from initial settings", function () { - let temp = {}; - let subject = new OidcClientSettings({ - client_id: 'client', - stateStore: temp - }); - subject.stateStore.should.equal(temp); - }); - }); - - describe("validator", function () { - - it("should return value from initial settings", function () { - - let temp = {}; - let subject = new OidcClientSettings({ - client_id: 'client', - ResponseValidatorCtor: function () { return temp } - }); - subject.validator.should.equal(temp); - }); - }); - - describe("metadataServiceCtor", function () { - - it("should return value from initial settings", function () { - - let temp = {}; - let subject = new OidcClientSettings({ - client_id: 'client', - MetadataServiceCtor: function () { return temp } - }); - subject.metadataService.should.equal(temp); - }); - }); - - describe("extraQueryParams", function() { - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client' - }); - subject.extraQueryParams.should.deep.equal({}); - }); - - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - extraQueryParams: { - 'hd': 'domain.com' - } - }); - subject.extraQueryParams.should.deep.equal({ 'hd': 'domain.com' }); - }); - - it("should not set value from initial settings if not object, but set default value ({})", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - extraQueryParams: 123456 - }); - subject.extraQueryParams.should.deep.equal({}); - }); - - it("should set it if object", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - }); - subject.extraQueryParams = { 'hd': 'domain.com' }; - subject.extraQueryParams.should.deep.equal({ 'hd': 'domain.com' }); - }); - - it("should clear it if not object", function() { - let subject = new OidcClientSettings({ - client_id: 'client', - extraQueryParams: { - 'hd': 'domain.com', - } - }); - subject.extraQueryParams = undefined; - subject.extraQueryParams.should.deep.equal({}); - }); - }) - - describe("extraTokenParams", function() { - - it("should use default value", function () { - let subject = new OidcClientSettings({ - client_id: 'client' - }); - subject.extraTokenParams.should.deep.equal({}); - }); - - it("should return value from initial settings", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - extraTokenParams: { - 'resourceServer': 'abc' - } - }); - subject.extraTokenParams.should.deep.equal({ 'resourceServer': 'abc' }); - }); - - it("should not set value from initial settings if not object, but set default value ({})", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - extraTokenParams: 123456 - }); - subject.extraTokenParams.should.deep.equal({}); - }); - - it("should set it if object", function () { - let subject = new OidcClientSettings({ - client_id: 'client', - }); - subject.extraTokenParams = { 'resourceServer': 'abc' }; - subject.extraTokenParams.should.deep.equal({ 'resourceServer': 'abc' }); - }); - - it("should clear it if not object", function() { - let subject = new OidcClientSettings({ - client_id: 'client', - extraTokenParams: { - 'resourceServer': 'abc', - } - }); - subject.extraTokenParams = undefined; - subject.extraTokenParams.should.deep.equal({}); - }); - }) - -}); diff --git a/test/unit/OidcClientSettings.test.ts b/test/unit/OidcClientSettings.test.ts new file mode 100644 index 000000000..9f598b4c2 --- /dev/null +++ b/test/unit/OidcClientSettings.test.ts @@ -0,0 +1,676 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { Log } from '../../src/Log'; +import { MetadataService } from '../../src/MetadataService'; +import { OidcClientSettings } from '../../src/OidcClientSettings'; +import { ResponseValidator } from '../../src/ResponseValidator'; +import { StateStore } from '../../src/StateStore'; + +// workaround jest parse error +jest.mock('../../jsrsasign/dist/jsrsasign.js', () => { + return { + jws: jest.fn(), + KEYUTIL: jest.fn(), + X509: jest.fn(), + crypto: jest.fn(), + hextob64u: jest.fn(), + b64tohex: jest.fn() + }; +}); + +describe("OidcClientSettings", () => { + + beforeEach(() => { + Log.logger = console; + Log.level = Log.NONE; + }); + + describe("client_id", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client' + }); + + // assert + expect(subject.client_id).toEqual("client"); + }); + + it("should not allow setting if previously set", () => { + // arrange + let subject = new OidcClientSettings({ + client_id: 'client', + authority: "http://sts" + }); + + // act + try { + subject.client_id = "diff"; + fail("should not come here"); + } + catch (e) { + expect(e.message).toContain("client_id"); + } + }); + + it("should allow setting if not previously set", () => { + // arrange + let subject = new OidcClientSettings({ + }); + + // act + subject.client_id = "test"; + + // assert + expect(subject.client_id).toEqual("test"); + }); + }); + + describe("client_secret", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_secret: 'secret' + }); + + // assert + expect(subject.client_secret).toEqual("secret"); + }); + }); + + describe("response_type", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + response_type: "foo" + }); + + // assert + expect(subject.response_type).toEqual("foo"); + }); + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client' + }); + + // assert + expect(subject.response_type).toEqual("id_token"); + }); + + }); + + describe("scope", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + scope: "foo" + }); + + // assert + expect(subject.scope).toEqual("foo"); + }); + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client' + }); + + // assert + expect(subject.scope).toEqual("openid"); + }); + + }); + + describe("redirect_uri", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + redirect_uri: "foo" + }); + + // assert + expect(subject.redirect_uri).toEqual("foo"); + }); + + }); + + describe("post_logout_redirect_uri", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + post_logout_redirect_uri: "http://app/loggedout" + }); + + // assert + expect(subject.post_logout_redirect_uri).toEqual("http://app/loggedout"); + }); + }); + + describe("prompt", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + prompt: "foo" + }); + + // assert + expect(subject.prompt).toEqual("foo"); + }); + }); + + describe("display", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + display: "foo" + }); + + // assert + expect(subject.display).toEqual("foo"); + }); + }); + + describe("max_age", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + max_age: 22 + }); + + // assert + expect(subject.max_age).toEqual(22); + }); + }); + + describe("ui_locales", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + ui_locales: "foo" + }); + + // assert + expect(subject.ui_locales).toEqual("foo"); + }); + }); + + describe("acr_values", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + acr_values: "foo" + }); + + // assert + expect(subject.acr_values).toEqual("foo"); + }); + }); + + describe("resource", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + resource: "foo" + }); + + // assert + expect(subject.resource).toEqual("foo"); + }); + }); + + describe("response_mode", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + response_mode: "foo" + }); + + // assert + expect(subject.response_mode).toEqual("foo"); + }); + }); + + describe("authority", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + authority: "http://sts" + }); + + // assert + expect(subject.authority).toEqual("http://sts"); + }); + + it("should not allow setting if previously set", () => { + // arrange + let subject = new OidcClientSettings({ + client_id: 'client', + authority: "http://sts" + }); + + // act + try { + subject.authority = "http://sts2"; + fail("should not come here"); + } + catch (e) { + expect(e.message).toContain("authority"); + } + }); + + it("should allow setting if not previously set", () => { + // arrange + let subject = new OidcClientSettings({ + client_id: 'client' + }); + + // act + subject.authority = "http://sts"; + + // assert + expect(subject.authority).toEqual("http://sts"); + }); + }); + + describe("metadataUrl", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + metadataUrl: "http://sts/metadata" + }); + + // assert + expect(subject.metadataUrl).toEqual("http://sts/metadata"); + }); + + it("should infer value from authority", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + authority: "http://sts" + }); + + // assert + expect(subject.metadataUrl).toEqual("http://sts/.well-known/openid-configuration"); + }); + }); + + describe("metadata", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + metadata: { issuer: "test" } + }); + + // assert + expect(subject.metadata).toEqual({ issuer: "test" }); + }); + + it("should store value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + }); + subject.metadata = { issuer: "test" }; + + // assert + expect(subject.metadata).toEqual({ issuer: "test" }); + }); + + }); + + describe("signingKeys", () => { + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + signingKeys: ["test"] + }); + + // assert + expect(subject.signingKeys).toEqual(["test"]); + }); + + it("should store value", () => { + // arrange + let subject = new OidcClientSettings({ + client_id: 'client', + }); + + // act + subject.signingKeys = ["test"]; + + // assert + expect(subject.signingKeys).toEqual(["test"]); + }); + + }); + + describe("filterProtocolClaims", () => { + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + }); + + // assert + expect(subject.filterProtocolClaims).toEqual(true); + }); + + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + filterProtocolClaims: true + }); + + // assert + expect(subject.filterProtocolClaims).toEqual(true); + + // act + subject = new OidcClientSettings({ + client_id: 'client', + filterProtocolClaims: false + }); + + // assert + expect(subject.filterProtocolClaims).toEqual(false); + }); + }); + + describe("loadUserInfo", () => { + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + }); + + // assert + expect(subject.loadUserInfo).toEqual(true); + }); + + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + loadUserInfo: true + }); + + // assert + expect(subject.loadUserInfo).toEqual(true); + + // act + subject = new OidcClientSettings({ + client_id: 'client', + loadUserInfo: false + }); + + // assert + expect(subject.loadUserInfo).toEqual(false); + }); + }); + + describe("staleStateAge", () => { + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + }); + + // assert + expect(subject.staleStateAge).toEqual(900); + }); + + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + staleStateAge: 100 + }); + + // assert + expect(subject.staleStateAge).toEqual(100); + }); + }); + + describe("clockSkew", () => { + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client' + }); + + // assert + expect(subject.clockSkew).toEqual(5 * 60); // 5 mins + }); + + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + clockSkew: 10 + }); + + // assert + expect(subject.clockSkew).toEqual(10); + }); + }); + + describe("stateStore", () => { + + it("should return value from initial settings", () => { + // arrange + let temp = {} as StateStore; + + // act + let subject = new OidcClientSettings({ + client_id: 'client', + stateStore: temp + }); + + // assert + expect(subject.stateStore).toEqual(temp); + }); + }); + + describe("stateStore", () => { + + it("should return value from initial settings", () => { + // arrange + let temp = {} as StateStore; + + // act + let subject = new OidcClientSettings({ + client_id: 'client', + stateStore: temp + }); + + // assert + expect(subject.stateStore).toEqual(temp); + }); + }); + + describe("validator", () => { + + it("should return value from initial settings", () => { + // arrange + let temp = class { name123 = "123"; } as unknown as typeof ResponseValidator; + + // act + let subject = new OidcClientSettings({ + client_id: 'client', + ResponseValidatorCtor: temp + }); + + // assert + expect(subject.validator).toHaveProperty("name123"); + }); + }); + + describe("metadataServiceCtor", () => { + + it("should return value from initial settings", () => { + // arrange + let temp = class { name123 = "123"; } as unknown as typeof MetadataService; + + // act + let subject = new OidcClientSettings({ + client_id: 'client', + MetadataServiceCtor: temp + }); + + // assert + expect(subject.metadataService).toHaveProperty("name123"); + }); + }); + + describe("extraQueryParams", () => { + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client' + }); + + // assert + expect(subject.extraQueryParams).toEqual({}); + }); + + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + extraQueryParams: { + 'hd': 'domain.com' + } + }); + + // assert + expect(subject.extraQueryParams).toEqual({ 'hd': 'domain.com' }); + }); + + it("should not set value from initial settings if not object, but set default value ({})", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + extraQueryParams: 123456 as unknown as Record + }); + + // assert + expect(subject.extraQueryParams).toEqual({}); + }); + + it("should set it if object", () => { + // arrange + let subject = new OidcClientSettings({ + client_id: 'client', + }); + + // act + subject.extraQueryParams = { 'hd': 'domain.com' }; + + // assert + expect(subject.extraQueryParams).toEqual({ 'hd': 'domain.com' }); + }); + + it("should clear it if not object", () => { + // arrange + let subject = new OidcClientSettings({ + client_id: 'client', + extraQueryParams: { + 'hd': 'domain.com', + } + }); + + // act + subject.extraQueryParams = undefined; + + // assert + expect(subject.extraQueryParams).toEqual({}); + }); + }) + + describe("extraTokenParams", () => { + + it("should use default value", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client' + }); + + // assert + expect(subject.extraTokenParams).toEqual({}); + }); + + it("should return value from initial settings", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + extraTokenParams: { + 'resourceServer': 'abc' + } + }); + + // assert + expect(subject.extraTokenParams).toEqual({ 'resourceServer': 'abc' }); + }); + + it("should not set value from initial settings if not object, but set default value ({})", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + extraTokenParams: 123456 as unknown as Record + }); + + // assert + expect(subject.extraTokenParams).toEqual({}); + }); + + it("should set it if object", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + }); + subject.extraTokenParams = { 'resourceServer': 'abc' }; + + // assert + expect(subject.extraTokenParams).toEqual({ 'resourceServer': 'abc' }); + }); + + it("should clear it if not object", () => { + // act + let subject = new OidcClientSettings({ + client_id: 'client', + extraTokenParams: { + 'resourceServer': 'abc', + } + }); + subject.extraTokenParams = undefined; + + // assert + expect(subject.extraTokenParams).toEqual({}); + }); + }); +}); diff --git a/test/unit/UrlUtility.spec.js b/test/unit/UrlUtility.spec.js deleted file mode 100644 index bc9437672..000000000 --- a/test/unit/UrlUtility.spec.js +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { UrlUtility } from '../../src/UrlUtility'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("UrlUtility", function() { - - describe("addQueryParam", function() { - - it("should add ? if not present", function() { - UrlUtility.addQueryParam("url", "foo", "test").should.equal("url?foo=test"); - }); - - it("should not add ? if already present", function() { - UrlUtility.addQueryParam("url?", "foo", "test").should.equal("url?foo=test"); - }); - - it("should add & if needed", function() { - UrlUtility.addQueryParam("url?x=1", "foo", "test").should.equal("url?x=1&foo=test"); - }); - - it("should urlencode key and value", function() { - UrlUtility.addQueryParam("url", "#", "#").should.equal("url?%23=%23"); - }); - }); - - describe("parseUrlFragment", function() { - - it("should parse key/value pairs", function() { - let result = UrlUtility.parseUrlFragment("a=apple&b=banana&c=carrot"); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should parse any order", function() { - let result = UrlUtility.parseUrlFragment("b=banana&c=carrot&a=apple"); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should parse past host name and hash fragment", function() { - let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=xoxo/#a=apple&b=banana&c=carrot"); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should parse query string", function() { - let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo", "?"); - result.should.deep.equal({ test1: "xoxo", test2: "yoyo" }); - }); - - it("should parse query string up to hash", function() { - let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo#a=apple&b=banana&c=carrot", "?"); - result.should.deep.equal({ test1: "xoxo", test2: "yoyo" }); - }); - - it("should return error for long values", function() { - let result = UrlUtility.parseUrlFragment("a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple"); - result.should.have.property('error'); - }); - - it("should use Global.location when no value is passed", function() { - let w = { - location: { - href : "a=apple&b=banana&c=carrot" - } - }; - let result = UrlUtility.parseUrlFragment(null, "#", w); - result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" }); - }); - - it("should return empty object for empty string", function() { - let result = UrlUtility.parseUrlFragment(""); - result.should.deep.equal({}); - }); - }); - -}); diff --git a/test/unit/UrlUtility.test.ts b/test/unit/UrlUtility.test.ts new file mode 100644 index 000000000..d61d3ff39 --- /dev/null +++ b/test/unit/UrlUtility.test.ts @@ -0,0 +1,118 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { UrlUtility } from '../../src/UrlUtility'; + +describe("UrlUtility", () => { + + describe("addQueryParam", () => { + + it("should add ? if not present", () => { + // act + let result = UrlUtility.addQueryParam("url", "foo", "test"); + + // assert + expect(result).toEqual("url?foo=test"); + }); + + it("should not add ? if already present", () => { + // act + let result = UrlUtility.addQueryParam("url?", "foo", "test"); + + // assert + expect(result).toEqual("url?foo=test"); + }); + + it("should add & if needed", () => { + // act + let result = UrlUtility.addQueryParam("url?x=1", "foo", "test"); + + // assert + expect(result).toEqual("url?x=1&foo=test"); + }); + + it("should urlencode key and value", () => { + // act + let result = UrlUtility.addQueryParam("url", "#", "#"); + + // assert + expect(result).toEqual("url?%23=%23"); + }); + }); + + describe("parseUrlFragment", () => { + + it("should parse key/value pairs", () => { + // act + let result = UrlUtility.parseUrlFragment("a=apple&b=banana&c=carrot"); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + + it("should parse any order", () => { + // act + let result = UrlUtility.parseUrlFragment("b=banana&c=carrot&a=apple"); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + + it("should parse past host name and hash fragment", () => { + // act + let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=xoxo/#a=apple&b=banana&c=carrot"); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + + it("should parse query string", () => { + // act + let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo", "?"); + + // assert + expect(result).toEqual({ test1: "xoxo", test2: "yoyo" }); + }); + + it("should parse query string up to hash", () => { + // act + let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=yoyo#a=apple&b=banana&c=carrot", "?"); + + // assert + expect(result).toEqual({ test1: "xoxo", test2: "yoyo" }); + }); + + it("should return error for long values", () => { + // act + let result = UrlUtility.parseUrlFragment("a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple"); + + // assert + expect(result).toHaveProperty("error"); + }); + + /* TODO: port-ts + it("should use Global.location when no value is passed", () => { + // arrange + let w = { + location: { + href : "a=apple&b=banana&c=carrot" + } + }; + + // act + let result = UrlUtility.parseUrlFragment(null, "#", w); + + // assert + expect(result).toEqual({ a: "apple", b: "banana", c: "carrot" }); + }); + */ + + it("should return empty object for empty string", () => { + // act + let result = UrlUtility.parseUrlFragment(""); + + // assert + expect(result).toEqual({}); + }); + }); +}); diff --git a/test/unit/UserManager.spec.js b/test/unit/UserManager.spec.js deleted file mode 100644 index dba949f21..000000000 --- a/test/unit/UserManager.spec.js +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { UserManager } from '../../src/UserManager'; -import { Log } from '../../src/Log'; -import { Global } from '../../src/Global'; -import { UserManagerSettings } from '../../src/UserManagerSettings'; -import { User } from '../../src/User'; - -import { StubMetadataService } from './StubMetadataService'; -import { StubSilentRenewService } from './StubSilentRenewService'; -import { StubStateStore } from './StubStateStore'; -import { StubResponseValidator } from './StubResponseValidator'; -import { StubTokenRevocationClient } from './StubTokenRevocationClient'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("UserManager", function () { - let settings; - let subject; - let stubMetadataService; - let stubStateStore; - let stubValidator; - let stubSilentRenewService; - let stubNavigator; - let stubUserStore; - let stubTokenRevocationClient; - - beforeEach(function () { - - Global._testing(); - - Log.logger = console; - Log.level = Log.NONE; - - stubNavigator = {}; - stubUserStore = new StubStateStore(); - stubStateStore = new StubStateStore(); - stubValidator = new StubResponseValidator(); - stubSilentRenewService = new StubSilentRenewService(); - stubMetadataService = new StubMetadataService(); - stubTokenRevocationClient = new StubTokenRevocationClient(); - - settings = { - authority: 'http://sts/oidc', - client_id: 'client', - monitorSession : false, - navigator: stubNavigator, - userStore: stubUserStore, - stateStore: stubStateStore, - ResponseValidatorCtor: () => stubValidator, - MetadataServiceCtor: () => stubMetadataService - }; - - subject = new UserManager(settings, - () => stubSilentRenewService, - null, - () => stubTokenRevocationClient); - }); - - describe("constructor", function () { - - it("should accept settings", function () { - subject.settings.client_id.should.equal('client'); - }); - - }); - - describe("settings", function () { - - it("should be UserManagerSettings", function () { - subject.settings.should.be.instanceof(UserManagerSettings); - }); - - }); - - describe("userLoaded", function () { - - it("should be able to call getUser without recursion", function (done) { - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - subject.events.addUserLoaded(user => { - subject.getUser().then(user => { - done(); - }); - }); - - subject.events.load({}); - }); - - }); - - describe("signinSilent", function(){ - - it("should pass silentRequestTimeout from settings", function(done) { - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - settings.silentRequestTimeout = 123; - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(args, nav, navArgs){ - Log.debug("_signin", args, nav, navArgs); - - navArgs.silentRequestTimeout.should.equal(123); - done(); - return Promise.resolve() - } - subject.signinSilent(); - }); - - it("should pass silentRequestTimeout from params", function(done){ - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(args, nav, navArgs){ - navArgs.silentRequestTimeout.should.equal(234); - done(); - return Promise.resolve() - } - subject.signinSilent({silentRequestTimeout:234}); - }); - - it("should pass prompt from params", function(done){ - - stubUserStore.item = new User({id_token:"id_token"}).toStorageString(); - - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(args, nav, navArgs){ - args.prompt.should.equal("foo"); - done(); - return Promise.resolve() - } - subject.signinSilent({prompt:"foo"}); - }); - - it("should work when having no User present", function(done) { - settings.silent_redirect_uri = "http://client/silent_callback"; - subject = new UserManager(settings); - - subject._signin = function(){ - done(); - return Promise.resolve() - } - subject.signinSilent({prompt:"foo"}); - }) - }); - -}); diff --git a/test/unit/UserManager.test.ts b/test/unit/UserManager.test.ts new file mode 100644 index 000000000..295405c3b --- /dev/null +++ b/test/unit/UserManager.test.ts @@ -0,0 +1,176 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { Log } from '../../src/Log'; +import { UserManager } from '../../src/UserManager'; +import { UserManagerSettings } from '../../src/UserManagerSettings'; +import { User } from '../../src/User'; +import { WebStorageStateStore } from '../../src/WebStorageStateStore'; +import { mocked } from 'ts-jest/utils'; + +// workaround jest parse error +jest.mock('../../jsrsasign/dist/jsrsasign.js', () => { + return { + jws: jest.fn(), + KEYUTIL: jest.fn(), + X509: jest.fn(), + crypto: jest.fn(), + hextob64u: jest.fn(), + b64tohex: jest.fn() + }; +}); + +describe("UserManager", () => { + let settings: UserManagerSettings; + let userStoreMock: any; + let subject: UserManager; + + beforeEach(() => { + Log.logger = console; + Log.level = Log.NONE; + + userStoreMock = mocked(new WebStorageStateStore()) + + settings = { + authority: "http://sts/oidc", + client_id: "client", + monitorSession : false, + userStore: userStoreMock, + } as unknown as UserManagerSettings; + + subject = new UserManager(settings); + }); + + describe("constructor", () => { + + it("should accept settings", () => { + // act + expect(subject.settings.client_id).toEqual("client"); + }); + }); + + describe("settings", () => { + + it("should be UserManagerSettings", () => { + // act + expect(subject.settings).toBeInstanceOf(UserManagerSettings); + }); + }); + + describe("userLoaded", () => { + + it("should be able to call getUser without recursion", async () => { + // arrange + const user = new User({ id_token: "id_token" }); + userStoreMock.item = user.toStorageString(); + + subject.events.addUserLoaded(async (_user) => { + await subject.getUser(); + }); + + // act + subject.events.load({} as User); + }); + }); + + describe("signinSilent", function(){ + + it("should pass silentRequestTimeout from settings", async () => { + // arrange + const user = new User({id_token:"id_token"}); + userStoreMock.item = user.toStorageString(); + + settings = { + ...settings, + silentRequestTimeout: 123, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + let navArgs: any = null; + + // @ts-ignore + subject._signin = function(args: any, nav: any, arg_navArgs: any) { + Log.debug("_signin", args, nav, navArgs); + + navArgs = arg_navArgs; + return Promise.resolve() + } + + // act + await subject.signinSilent(); + + // assert + expect(navArgs.silentRequestTimeout).toEqual(123); + }); + + it("should pass silentRequestTimeout from params", async () =>{ + // arrange + const user = new User({id_token:"id_token"}); + userStoreMock.item = user.toStorageString(); + + settings = { + ...settings, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + let navArgs: any = null; + + // @ts-ignore + subject._signin = function(args: any, nav: any, arg_navArgs: any) { + navArgs = arg_navArgs; + return Promise.resolve() + } + + // act + await subject.signinSilent({ silentRequestTimeout: 234 }); + + // assert + expect(navArgs.silentRequestTimeout).toEqual(234); + }); + + it("should pass prompt from params", async () =>{ + // arrange + const user = new User({id_token:"id_token"}) + userStoreMock.item = user.toStorageString(); + + settings = { + ...settings, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + let args: any = null; + + // @ts-ignore + subject._signin = function(arg_args: any, nav: any, navArgs: any) { + args = arg_args; + return Promise.resolve() + } + + // act + await subject.signinSilent({ prompt:"foo" }); + + // assert + expect(args.prompt).toEqual("foo"); + }); + + it("should work when having no User present", async () => { + // arrange + settings = { + ...settings, + silent_redirect_uri: "http://client/silent_callback" + } as unknown as UserManagerSettings; + subject = new UserManager(settings); + + // @ts-ignore + subject._signin = function(args: any, nav: any, arg_navArgs: any) { + return Promise.resolve() + } + + // act + await subject.signinSilent({ prompt:"foo" }); + }); + }); +}); diff --git a/test/unit/UserManagerEvents.spec.js b/test/unit/UserManagerEvents.spec.js deleted file mode 100644 index 72f0e2e0a..000000000 --- a/test/unit/UserManagerEvents.spec.js +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { UserManagerEvents } from '../../src/UserManagerEvents'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; - -describe("UserManagerEvents", function () { - - let subject; - - beforeEach(function () { - subject = new UserManagerEvents(); - }); - - describe("silent renew error", function () { - - it("should allow callback", function () { - var cb = function () { - cb.wasCalled = true; - }; - subject.addSilentRenewError(cb); - - subject._raiseSilentRenewError(new Error("boom")); - - cb.wasCalled.should.be.true; - }); - - it("should allow unregistering callback", function () { - var cb = function () { - cb.wasCalled = true; - }; - cb.wasCalled = false; - - subject.addSilentRenewError(cb); - subject.removeSilentRenewError(cb); - - subject._raiseSilentRenewError(new Error("boom")); - - cb.wasCalled.should.be.false; - }); - - it("should pass error to callback", function () { - var cb = function (e) { - e.message.should.equal("boom"); - }; - subject.addSilentRenewError(cb); - - subject._raiseSilentRenewError(new Error("boom")); - }); - - }); - -}); diff --git a/test/unit/UserManagerEvents.test.ts b/test/unit/UserManagerEvents.test.ts new file mode 100644 index 000000000..9ca0a58a0 --- /dev/null +++ b/test/unit/UserManagerEvents.test.ts @@ -0,0 +1,59 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { UserManagerEvents } from '../../src/UserManagerEvents'; +import { UserManagerSettings } from '../../src/UserManagerSettings'; + +describe("UserManagerEvents", () => { + + let subject: UserManagerEvents; + + beforeEach(() => { + subject = new UserManagerEvents({} as UserManagerSettings); + }); + + describe("silent renew error", () => { + + it("should allow callback", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addSilentRenewError(cb); + subject._raiseSilentRenewError(new Error("boom")); + + // assert + expect(cb).toBeCalled(); + }); + + it("should allow unregistering callback", () => { + // arrange + var cb = jest.fn(); + + // act + subject.addSilentRenewError(cb); + subject.removeSilentRenewError(cb); + subject._raiseSilentRenewError(new Error("boom")); + + // assert + expect(cb).toBeCalledTimes(0); + }); + + it("should pass error to callback", () => { + // arrange + let e: Error | null = null; + var cb = function (arg_e: Error) { + console.log(arg_e); + e = arg_e; + }; + const expected = new Error("boom"); + + // act + subject.addSilentRenewError(cb); + subject._raiseSilentRenewError(expected); + + // assert + expect(e).toEqual(expected); + }); + }); +}); diff --git a/test/unit/UserManagerSettings.spec.js b/test/unit/UserManagerSettings.test.ts similarity index 50% rename from test/unit/UserManagerSettings.spec.js rename to test/unit/UserManagerSettings.test.ts index 83941a418..a9065565d 100644 --- a/test/unit/UserManagerSettings.spec.js +++ b/test/unit/UserManagerSettings.test.ts @@ -3,10 +3,19 @@ import { Log } from '../../src/Log'; import { UserManagerSettings } from '../../src/UserManagerSettings'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; +import { WebStorageStateStore } from '../../src/WebStorageStateStore'; + +// workaround jest parse error +jest.mock('../../jsrsasign/dist/jsrsasign.js', () => { + return { + jws: jest.fn(), + KEYUTIL: jest.fn(), + X509: jest.fn(), + crypto: jest.fn(), + hextob64u: jest.fn(), + b64tohex: jest.fn() + }; +}); describe("UserManagerSettings", function () { @@ -18,12 +27,21 @@ describe("UserManagerSettings", function () { describe("constructor", function () { it("should allow no settings", function () { + // act let subject = new UserManagerSettings(); + + // assert + expect(subject).not.toBeNull(); }); it("should pass settings to base class", function () { - let subject = new UserManagerSettings({ client_id: 'client' }); - subject.client_id.should.equal('client'); + // act + let subject = new UserManagerSettings({ + client_id: 'client' + }); + + // assert + expect(subject.client_id).toEqual('client'); }); }); @@ -31,8 +49,13 @@ describe("UserManagerSettings", function () { describe("popup_redirect_uri", function () { it("should return value from initial settings", function () { - let subject = new UserManagerSettings({ popup_redirect_uri: 'test' }); - subject.popup_redirect_uri.should.equal('test'); + // act + let subject = new UserManagerSettings({ + popup_redirect_uri: 'test' + }); + + // assert + expect(subject.popup_redirect_uri).toEqual('test'); }); }); @@ -40,8 +63,13 @@ describe("UserManagerSettings", function () { describe("popupWindowFeatures", function () { it("should return value from initial settings", function () { - let subject = new UserManagerSettings({ popupWindowFeatures: 'foo' }); - subject.popupWindowFeatures.should.equal('foo'); + // act + let subject = new UserManagerSettings({ + popupWindowFeatures: 'foo' + }); + + // assert + expect(subject.popupWindowFeatures).toEqual('foo'); }); }); @@ -49,8 +77,13 @@ describe("UserManagerSettings", function () { describe("popupWindowTarget", function () { it("should return value from initial settings", function () { - let subject = new UserManagerSettings({ popupWindowTarget: 'foo' }); - subject.popupWindowTarget.should.equal('foo'); + // act + let subject = new UserManagerSettings({ + popupWindowTarget: 'foo' + }); + + // assert + expect(subject.popupWindowTarget).toEqual('foo'); }); }); @@ -58,8 +91,13 @@ describe("UserManagerSettings", function () { describe("silent_redirect_uri", function () { it("should return value from initial settings", function () { - let subject = new UserManagerSettings({ silent_redirect_uri: 'test' }); - subject.silent_redirect_uri.should.equal('test'); + // act + let subject = new UserManagerSettings({ + silent_redirect_uri: 'test' + }); + + // assert + expect(subject.silent_redirect_uri).toEqual('test'); }); }); @@ -67,8 +105,13 @@ describe("UserManagerSettings", function () { describe("silentRequestTimeout", function () { it("should return value from initial settings", function () { - let subject = new UserManagerSettings({ silentRequestTimeout: 123 }); - subject.silentRequestTimeout.should.equal(123); + // act + let subject = new UserManagerSettings({ + silentRequestTimeout: 123 + }); + + // assert + expect(subject.silentRequestTimeout).toEqual(123); }); }); @@ -76,16 +119,22 @@ describe("UserManagerSettings", function () { describe("automaticSilentRenew", function () { it("should return value from initial settings", function () { + // act let subject = new UserManagerSettings({ automaticSilentRenew: true }); - subject.automaticSilentRenew.should.be.true; + + // assert + expect(subject.automaticSilentRenew).toEqual(true); }); it("should use default value", function () { + // act let subject = new UserManagerSettings({ }); - subject.automaticSilentRenew.should.be.false; + + // assert + expect(subject.automaticSilentRenew).toEqual(false); }); }); @@ -93,157 +142,219 @@ describe("UserManagerSettings", function () { describe("validateSubOnSilentRenew", function () { it("should return value from initial settings", function () { + // act let subject = new UserManagerSettings({ validateSubOnSilentRenew: true }); - subject.validateSubOnSilentRenew.should.be.true; + + // assert + expect(subject.validateSubOnSilentRenew).toEqual(true); }); it("should use default value", function () { + // act let subject = new UserManagerSettings({ }); - subject.validateSubOnSilentRenew.should.be.false; + + // assert + expect(subject.validateSubOnSilentRenew).toEqual(false); }); }); describe("includeIdTokenInSilentRenew", function () { it("should return true value from initial settings", function () { + // act let subject = new UserManagerSettings({ includeIdTokenInSilentRenew: true, }); - subject.includeIdTokenInSilentRenew.should.be.true; + + // assert + expect(subject.includeIdTokenInSilentRenew).toEqual(true); }); it("should return false value from initial settings", function () { + // act let subject = new UserManagerSettings({ includeIdTokenInSilentRenew: false, }); - subject.includeIdTokenInSilentRenew.should.be.false; + + // assert + expect(subject.includeIdTokenInSilentRenew).toEqual(false); }); it("should use default value", function () { + // act let subject = new UserManagerSettings({ }); - subject.includeIdTokenInSilentRenew.should.be.true; + + // assert + expect(subject.includeIdTokenInSilentRenew).toEqual(true); }); }); describe("accessTokenExpiringNotificationTime", function () { it("should return value from initial settings", function () { + // act let subject = new UserManagerSettings({ accessTokenExpiringNotificationTime: 10 }); - subject.accessTokenExpiringNotificationTime.should.equal(10); + + // assert + expect(subject.accessTokenExpiringNotificationTime).toEqual(10); }); it("should use default value", function () { + // act let subject = new UserManagerSettings({ }); - subject.accessTokenExpiringNotificationTime.should.equal(60); + + // assert + expect(subject.accessTokenExpiringNotificationTime).toEqual(60); }); }); - describe("redirectNavigator", function() { - it("should return value from initial settings", function() { + describe("redirectNavigator", () => { + it("should return value from initial settings", () => { let temp = {}; + + // act let subject = new UserManagerSettings({ redirectNavigator : temp }); - subject.redirectNavigator.should.equal(temp); + + // assert + expect(subject.redirectNavigator).toEqual(temp); }); }); - describe("popupNavigator", function() { - it("should return value from initial settings", function() { + describe("popupNavigator", () => { + it("should return value from initial settings", () => { let temp = {}; + + // act let subject = new UserManagerSettings({ popupNavigator : temp }); - subject.popupNavigator.should.equal(temp); + + // assert + expect(subject.popupNavigator).toEqual(temp); }); }); - describe("iframeNavigator", function() { - it("should return value from initial settings", function() { + describe("iframeNavigator", () => { + it("should return value from initial settings", () => { let temp = {}; + + // act let subject = new UserManagerSettings({ iframeNavigator : temp }); - subject.iframeNavigator.should.equal(temp); + + // assert + expect(subject.iframeNavigator).toEqual(temp); }); }); - describe("redirectNavigator", function() { - it("should return value from initial settings", function() { - let temp = {}; + describe("redirectNavigator", () => { + it("should return value from initial settings", () => { + let temp = {} as WebStorageStateStore; + + // act let subject = new UserManagerSettings({ userStore : temp }); - subject.userStore.should.equal(temp); + + // assert + expect(subject.userStore).toEqual(temp); }); }); - describe("revokeAccessTokenOnSignout", function() { - it("should return value from initial settings", function() { + describe("revokeAccessTokenOnSignout", () => { + it("should return value from initial settings", () => { + // act let subject = new UserManagerSettings({ revokeAccessTokenOnSignout : true }); - subject.revokeAccessTokenOnSignout.should.equal(true); + + // assert + expect(subject.revokeAccessTokenOnSignout).toEqual(true); }); }); - describe("checkSessionInterval", function() { - it("should return value from initial settings", function() { + describe("checkSessionInterval", () => { + it("should return value from initial settings", () => { + // act let subject = new UserManagerSettings({ checkSessionInterval : 6000 }); - subject.checkSessionInterval.should.equal(6000); + + // assert + expect(subject.checkSessionInterval).toEqual(6000); }); it("should use default value", function () { + // act let subject = new UserManagerSettings({ }); - subject.checkSessionInterval.should.equal(2000); + + // assert + expect(subject.checkSessionInterval).toEqual(2000); }); }); - describe("query_status_response_type", function() { - it("should return value from initial settings", function() { + describe("query_status_response_type", () => { + it("should return value from initial settings", () => { let temp = 'type'; + + // act let subject = new UserManagerSettings({ query_status_response_type : temp }); - subject.query_status_response_type.should.equal(temp); + + // assert + expect(subject.query_status_response_type).toEqual(temp); }); it("should infer default value", function () { { + // act let subject = new UserManagerSettings({ response_type: "id_token token" }); - subject.query_status_response_type.should.equal("id_token"); + + // assert + expect(subject.query_status_response_type).toEqual("id_token"); } - { + { + // act let subject = new UserManagerSettings({ response_type: "code" }); - subject.query_status_response_type.should.equal("code"); + + // assert + expect(subject.query_status_response_type).toEqual("code"); } }); }); - describe("stopCheckSessionOnError", function() { - it("should return value from initial settings", function() { + describe("stopCheckSessionOnError", () => { + it("should return value from initial settings", () => { + // act let subject = new UserManagerSettings({ stopCheckSessionOnError : false }); - subject.stopCheckSessionOnError.should.be.false; + + // assert + expect(subject.stopCheckSessionOnError).toEqual(false); }); it("should use default value", function () { + // act let subject = new UserManagerSettings({ }); - subject.stopCheckSessionOnError.should.be.true; + + // assert + expect(subject.stopCheckSessionOnError).toEqual(true); }); }); }); diff --git a/test/unit/WebStorageStateStore.spec.js b/test/unit/WebStorageStateStore.spec.js deleted file mode 100644 index b5b06bd4d..000000000 --- a/test/unit/WebStorageStateStore.spec.js +++ /dev/null @@ -1,175 +0,0 @@ -// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. -// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. - -import { Log } from '../../src/Log'; -import { WebStorageStateStore } from '../../src/WebStorageStateStore'; -import { InMemoryWebStorage } from '../../src/InMemoryWebStorage'; - -import chai from 'chai'; -chai.should(); -let assert = chai.assert; -let expect = chai.expect; - -describe("WebStorageStateStore", function() { - let prefix; - let subject; - let store; - - beforeEach(function() { - prefix = ""; - store = new InMemoryWebStorage(); - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - }); - - describe("set", function() { - - it("should return a promise", function() { - var p = subject.set("key", "value"); - p.should.be.instanceof(Promise); - p.catch(e=>{}); - }); - - it("should store item", function(done) { - subject.set("key", "value").then(() => { - store.getItem("key").should.equal("value"); - done(); - }) - }); - - it("should use prefix if specified", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - subject.set("key", "value").then(() => { - store.getItem(prefix + "key").should.equal("value"); - done(); - }) - }); - - }); - - describe("remove", function() { - - it("should return a promise", function() { - var p = subject.remove("key"); - p.should.be.instanceof(Promise); - p.catch(e=>{}); - }); - - it("should remove item", function(done) { - store.setItem("key", "value"); - - subject.remove("key").then(item => { - expect(store.getItem("key")).to.be.undefined; - done(); - }); - }); - - it("should return value if exists", function(done) { - store.setItem("key", "test"); - - subject.remove("key").then(value => { - value.should.equal('test'); - done(); - }); - }); - - it("should return undefined if doesn't exist", function(done) { - subject.remove("key").then(value => { - expect(value).to.be.undefined; - done(); - }); - }); - - it("should use prefix if specified", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - store.setItem("foo.key", "value"); - - subject.remove("key").then(item => { - expect(store.getItem("foo.key")).to.be.undefined; - done(); - }); - }); - - }); - - describe("get", function() { - - it("should return a promise", function() { - var p = subject.get("key"); - p.should.be.instanceof(Promise); - }); - - it("should return value if exists", function(done) { - store.setItem("key", "test"); - - subject.get("key").then(value => { - value.should.equal('test'); - done(); - }); - }); - - it("should return undefined if doesn't exist", function(done) { - subject.get("key").then(value => { - expect(value).to.be.undefined; - done(); - }); - }); - - it("should use prefix if specified", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - store.setItem("foo.key", "value"); - - subject.get("key").then(item => { - item.should.equal("value"); - done(); - }); - }); - - }); - - describe("getAllKeys", function() { - - it("should return a promise", function() { - var p = subject.getAllKeys(); - p.should.be.instanceof(Promise); - p.catch(e=>{}); - }); - - it("should return keys", function(done) { - store.setItem("key1", "test"); - store.setItem("key2", "test"); - - subject.getAllKeys().then(keys => { - keys.should.deep.equal(["key1", "key2"]); - done(); - }); - }); - - it("should return keys without prefix", function(done) { - prefix = "foo."; - subject = new WebStorageStateStore({ prefix: prefix, store: store }); - - store.setItem("foo.key1", "test"); - store.setItem("foo.key2", "test"); - - subject.getAllKeys().then(keys => { - keys.should.deep.equal(["key1", "key2"]); - done(); - }); - }); - - it("should return empty keys when empty", function(done) { - subject.getAllKeys().then(keys => { - keys.should.deep.equal([]); - done(); - }); - }); - - }); - -}); diff --git a/test/unit/WebStorageStateStore.test.ts b/test/unit/WebStorageStateStore.test.ts new file mode 100644 index 000000000..e3ef238f6 --- /dev/null +++ b/test/unit/WebStorageStateStore.test.ts @@ -0,0 +1,195 @@ +// Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. + +import { WebStorageStateStore } from '../../src/WebStorageStateStore'; +import { InMemoryWebStorage } from '../../src/InMemoryWebStorage'; + +describe("WebStorageStateStore", () => { + let prefix: string; + let store: Storage; + let subject: WebStorageStateStore; + + beforeEach(() => { + prefix = ""; + store = new InMemoryWebStorage(); + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + }); + + describe("set", () => { + + it("should return a promise", () => { + // act + let p = subject.set("key", "value"); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should store item", async () => { + // act + await subject.set("key", "value"); + let result = await store.getItem("key"); + + // assert + expect(result).toEqual("value"); + }); + + it("should use prefix if specified", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + + // act + await subject.set("key", "value"); + + // assert + let result = await store.getItem(prefix + "key"); + expect(result).toEqual("value"); + }); + }); + + describe("remove", () => { + + it("should return a promise", () => { + // act + let p = subject.remove("key"); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should remove item", async () => { + // arrange + await store.setItem("key", "value"); + + // act + await subject.remove("key"); + let result = await store.getItem("key"); + + // assert + expect(result).toBeUndefined(); + }); + + it("should return value if exists", async () => { + // arrange + await store.setItem("key", "test"); + + // act + let result = await subject.remove("key"); + + // assert + expect(result).toEqual("test"); + }); + + it("should return undefined if doesn't exist", async () => { + // act + let result = await subject.remove("key"); + + // assert + expect(result).toBeUndefined(); + }); + + it("should use prefix if specified", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + await subject.set("key", "value"); + + // act + let result = await subject.remove("key"); + + // assert + expect(result).toEqual("value"); + }); + + }); + + describe("get", () => { + + it("should return a promise", () => { + // act + var p = subject.get("key"); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should return value if exists", async () => { + // arrange + await store.setItem("key", "test"); + + // act + let result = await subject.get("key"); + + // assert + expect(result).toEqual("test"); + }); + + it("should return undefined if doesn't exist", async () => { + // act + let result = await subject.get("key"); + + // assert + expect(result).toBeUndefined(); + }); + + it("should use prefix if specified", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + store.setItem("foo.key", "value"); + + // act + let result = await subject.get("key"); + + // assert + expect(result).toEqual("value"); + }); + + }); + + describe("getAllKeys", () => { + + it("should return a promise", () => { + // act + var p = subject.getAllKeys(); + + // assert + expect(p).toBeInstanceOf(Promise); + }); + + it("should return keys", async () => { + // arrange + await store.setItem("key1", "test"); + await store.setItem("key2", "test"); + + // act + let result = await subject.getAllKeys(); + + // assert + expect(result).toStrictEqual(["key1", "key2"]); + }); + + it("should return keys without prefix", async () => { + // arrange + prefix = "foo."; + subject = new WebStorageStateStore({ prefix: prefix, store: store }); + await store.setItem("foo.key1", "test"); + await store.setItem("foo.key2", "test"); + + // act + let result = await subject.getAllKeys(); + + // assert + expect(result).toStrictEqual(["key1", "key2"]); + }); + + it("should return empty keys when empty", async () => { + // act + let result = await subject.getAllKeys(); + + // assert + expect(result).toStrictEqual([]); + }); + }); +}); diff --git a/test/unit/random.spec.js b/test/unit/random.test.ts similarity index 57% rename from test/unit/random.spec.js rename to test/unit/random.test.ts index 050cb3010..27e168619 100644 --- a/test/unit/random.spec.js +++ b/test/unit/random.test.ts @@ -5,10 +5,11 @@ const pattern = /^[0-9a-f]{8}[0-9a-f]{4}4[0-9a-f]{3}[89ab][0-9a-f]{3}[0-9a-f]{12 describe('random', function() { - it('should return a valid RFC4122 v4 guid (sans dashes)', function(){ - const rnd = random() - console.log(rnd); - rnd.should.match(pattern) - }) -}) - + it('should return a valid RFC4122 v4 guid (sans dashes)', () => { + // act + const rnd = random() + + // assert + expect(rnd).toMatch(pattern); + }); +});