UNPKG

opds-web-client

Version:
273 lines (272 loc) 12.1 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var chai_1 = require("chai"); var sinon_1 = require("sinon"); var authMiddleware_1 = require("../authMiddleware"); var ActionCreator = require("../actions"); var DataFetcher_1 = require("../DataFetcher"); var hideAuthFormStub; var clearAuthCredentialsStub; var showAuthFormStub; var MockActionCreator = (function (_super) { __extends(MockActionCreator, _super); function MockActionCreator() { return _super.apply(this, arguments) || this; } MockActionCreator.prototype.hideAuthForm = function () { return hideAuthFormStub(); }; MockActionCreator.prototype.clearAuthCredentials = function () { return clearAuthCredentialsStub(); }; MockActionCreator.prototype.showAuthForm = function (callback, cancel, authProviders, title, error) { callback(); return showAuthFormStub(callback, cancel, authProviders, title, error); }; return MockActionCreator; }(ActionCreator.default)); describe("authMiddleware", function () { var actionCreatorStub; var next; var authMiddleware; var plugin; var pathFor; var store; var dataFetcher; beforeEach(function () { dataFetcher = new DataFetcher_1.default(); dataFetcher.clearAuthCredentials(); hideAuthFormStub = sinon_1.stub(); clearAuthCredentialsStub = sinon_1.stub(); showAuthFormStub = sinon_1.stub(); actionCreatorStub = sinon_1.stub(ActionCreator, "default", MockActionCreator); next = sinon_1.stub().returns(new Promise(function (resolve, reject) { resolve({}); })); store = { dispatch: sinon_1.stub().returns(new Promise(function (resolve, reject) { resolve({}); })), getState: sinon_1.stub() }; plugin = { type: "test", lookForCredentials: sinon_1.stub(), formComponent: null, buttonComponent: null }; pathFor = sinon_1.stub(); authMiddleware = authMiddleware_1.default([plugin], pathFor); }); afterEach(function () { actionCreatorStub.restore(); }); it("handles a plain action (not a thunk)", function () { var next = sinon_1.stub(); authMiddleware(store)(next)({}); chai_1.expect(hideAuthFormStub.callCount).to.equal(1); chai_1.expect(next.callCount).to.equal(2); }); it("hides the auth form, calls the action, and does nothing else if it succeeds", function (done) { authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(hideAuthFormStub.callCount).to.equal(1); chai_1.expect(next.callCount).to.equal(2); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("hides the auth form, calls the action, and hides the auth form again if the action returns a non-401 error", function (done) { next.onCall(1).returns(new Promise(function (resolve, reject) { reject({ status: 500 }); })); authMiddleware(store)(next)(function () { }).then(function (arg) { chai_1.expect(hideAuthFormStub.callCount).to.equal(2); chai_1.expect(next.callCount).to.equal(3); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("hides the auth form, calls the action, and does nothing else if the error response wasn't json", function (done) { var error = { status: 401, response: "not json" }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(hideAuthFormStub.callCount).to.equal(1); chai_1.expect(next.callCount).to.equal(2); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("hides the auth form, calls the action, and does nothing else if the browser's default basic auth form was shown", function (done) { var error = { status: 401, response: JSON.stringify({ title: "error" }), headers: { "www-authenticate": "basic library card" } }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(hideAuthFormStub.callCount).to.equal(1); chai_1.expect(next.callCount).to.equal(2); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("clears existing credentials", function (done) { dataFetcher.setAuthCredentials({ provider: "test", credentials: "credentials" }); store.getState.returns({ auth: {}, collection: {}, book: {} }); var error = { status: 401, response: JSON.stringify({ title: "error" }) }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(clearAuthCredentialsStub.callCount).to.equal(1); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("shows auth form with provider info from failed request if there were no existing credentials", function (done) { store.getState.returns({ auth: {}, collection: {}, book: {} }); var providers = { provider: { name: "a provider", methods: { test: "test method" } } }; var error = { status: 401, response: JSON.stringify({ name: "Library", providers: providers }) }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(showAuthFormStub.callCount).to.equal(1); chai_1.expect(showAuthFormStub.args[0][2]).to.deep.equal([{ name: "a provider", plugin: plugin, method: "test method" }]); chai_1.expect(showAuthFormStub.args[0][3]).to.equal("Library"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("shows auth form with provider info from store if existing credentials failed", function (done) { dataFetcher.setAuthCredentials({ provider: "test", credentials: "credentials" }); var providers = [{ name: "a provider", plugin: plugin, method: "test method" }]; store.getState.returns({ auth: { title: "Library", providers: providers }, collection: {}, book: {} }); var error = { status: 401, response: JSON.stringify({ title: "error" }) }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(showAuthFormStub.callCount).to.equal(1); chai_1.expect(showAuthFormStub.args[0][2]).to.deep.equal([{ name: "a provider", plugin: plugin, method: "test method" }]); chai_1.expect(showAuthFormStub.args[0][3]).to.equal("Library"); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("retries action without credentials if existing credentials failed and there aren't providers in the store", function (done) { dataFetcher.setAuthCredentials({ provider: "test", credentials: "credentials" }); store.getState.returns({ auth: { providers: null }, collection: {}, book: {} }); var error = { status: 401, response: JSON.stringify({ title: "error" }) }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); var action = sinon_1.stub(); authMiddleware(store)(next)(action).then(function () { chai_1.expect(showAuthFormStub.callCount).to.equal(0); chai_1.expect(clearAuthCredentialsStub.callCount).to.equal(1); chai_1.expect(store.dispatch.callCount).to.equal(2); chai_1.expect(store.dispatch.args[1][0]).to.equal(action); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("does not call showAuthForm if there's no supported auth method in the provider info", function (done) { store.getState.returns({ auth: {}, collection: {}, book: {} }); var providers = { provider: { name: "a provider", methods: { unknownMethod: "unknown method" } } }; var error = { status: 401, response: JSON.stringify({ name: "Library", providers: providers }) }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(showAuthFormStub.callCount).to.equal(0); chai_1.expect(hideAuthFormStub.callCount).to.equal(2); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("makes cancel go to previous page if url has changed", function (done) { store.getState.returns({ auth: {}, collection: { url: "old" }, book: {} }); pathFor.returns("new"); var providers = { provider: { name: "a provider", methods: { test: "test method" } } }; var error = { status: 401, response: JSON.stringify({ name: "Library", providers: providers }) }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(showAuthFormStub.callCount).to.equal(1); var cancel = showAuthFormStub.args[0][1]; var historySpy = sinon_1.spy(history, "back"); cancel(); historySpy.restore(); chai_1.expect(historySpy.callCount).to.equal(1); done(); }).catch(function (err) { console.log(err); throw (err); }); }); it("makes cancel hide the form if url hasn't changed", function (done) { store.getState.returns({ auth: {}, collection: {}, book: {} }); // blank is the value of window.location.pathname when running tests pathFor.returns("blank"); var providers = { provider: { name: "a provider", methods: { test: "test method" } } }; var error = { status: 401, response: JSON.stringify({ name: "Library", providers: providers }) }; next.onCall(1).returns(new Promise(function (resolve, reject) { reject(error); })); authMiddleware(store)(next)(function () { }).then(function () { chai_1.expect(showAuthFormStub.callCount).to.equal(1); chai_1.expect(hideAuthFormStub.callCount).to.equal(1); var cancel = showAuthFormStub.args[0][1]; cancel(); chai_1.expect(hideAuthFormStub.callCount).to.equal(2); done(); }).catch(function (err) { console.log(err); throw (err); }); }); });