authelia/server/test/routes/errors/401/get.test.ts
Clement Michaud 6b78240d39 Fix endpoints redirection on errors
From this commit on, api endpoints reply with a 401 error code and non api
endpoints redirect to /error/40X.

This commit also fixes missing restrictions on /loggedin (the "already logged
in page). This was not a security issue, though.

The change also makes error pages automatically redirect the user after few
seconds based on the referrer or the default_redirection_url if provided in the
configuration.

Warning: The old /verify endpoint of the REST API has moved to /api/verify.
You will need to update your nginx configuration to take this change into
account.
2017-11-01 14:46:23 +01:00

61 lines
1.8 KiB
TypeScript

import Sinon = require("sinon");
import Express = require("express");
import Assert = require("assert");
import Get401 from "../../../../src/lib/routes/error/401/get";
import { ServerVariables } from "../../../../src/lib/ServerVariables";
import { ServerVariablesMockBuilder, ServerVariablesMock }
from "../../../mocks/ServerVariablesMockBuilder";
describe("Server error 401", function () {
let vars: ServerVariables;
let mocks: ServerVariablesMock;
let req: any;
let res: any;
let renderSpy: Sinon.SinonSpy;
beforeEach(function () {
const s = ServerVariablesMockBuilder.build();
vars = s.variables;
mocks = s.mocks;
renderSpy = Sinon.spy();
req = {
headers: {}
};
res = {
render: renderSpy
};
});
it("should set redirection url to the default redirection url", function () {
vars.config.default_redirection_url = "http://default-redirection";
return Get401(vars)(req, res as any)
.then(function () {
Assert(renderSpy.calledOnce);
Assert(renderSpy.calledWithExactly("errors/401", {
redirection_url: "http://default-redirection"
}));
});
});
it("should set redirection url to the referer", function () {
req.headers["referer"] = "http://redirection";
return Get401(vars)(req, res as any)
.then(function () {
Assert(renderSpy.calledOnce);
Assert(renderSpy.calledWithExactly("errors/401", {
redirection_url: "http://redirection"
}));
});
});
it("should render without redirecting the user", function () {
return Get401(vars)(req, res as any)
.then(function () {
Assert(renderSpy.calledOnce);
Assert(renderSpy.calledWithExactly("errors/401", {
redirection_url: undefined
}));
});
});
});