Skip to content

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
gugu committed Feb 10, 2021
1 parent ea1af27 commit fe9e5f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/passport-saml/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface SAMLOptions {
// Additional SAML behaviors
additionalParams: Record<string, string>;
additionalAuthorizeParams: Record<string, string>;
identifierFormat: string;
identifierFormat: string | null;
acceptedClockSkewMs: number;
attributeConsumingServiceIndex: string | null;
disableRequestedAuthnContext: boolean;
Expand Down Expand Up @@ -73,7 +73,7 @@ interface StrategyOptions {
export interface SamlScopingConfig {
idpList?: SamlIDPListConfig[];
proxyCount?: number;
requesterId?: string[];
requesterId?: string[] | string;
}

export type XMLValue = string | number | boolean | null | XMLObject | XMLValue[];
Expand Down
2 changes: 1 addition & 1 deletion test/multiSamlStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe("strategy#generateServiceProviderMetadata", function () {
const strategy = new MultiSamlStrategy(passportOptions, verify);
strategy.generateServiceProviderMetadata("foo" as any, "bar", "baz", function (error, result) {
try {
should(error).equal("My error");
should(error?.message).equal("My error");
done();
} catch (err2) {
done(err2);
Expand Down
87 changes: 47 additions & 40 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import * as express from "express";
import * as bodyParser from "body-parser";
import * as passport from "passport";
import { Strategy as SamlStrategy, SAML } from "../src/passport-saml/index.js";
import { Strategy as SamlStrategy, SAML } from "../src/passport-saml";
// @ts-ignore no typings for request, TODO: we need to remove this dependency instead of adding typings
import request from "request";
import request = require("request");
import "should";
import * as zlib from "zlib";
import * as querystring from "querystring";
Expand All @@ -17,6 +17,8 @@ import {
SamlConfig,
SAMLOptions,
VerifiedCallback,
VerifyWithoutRequest,
VerifyWithRequest,
} from "../src/passport-saml/types.js";
import * as should from "should";
import { Server } from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vbS5hdYIJAKZgJdKdCdL6MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAMi5HyvXgRa4+kKz3dk4SwAEXzeZRcsbeDJWVUxdb6a+JQxIoG7L9rSbd6yZvP/Xel5TrcwpCpl5eikzXB02/C0wZKWicNmDEBlOfw0Pc5ngdoh6ntxHIWm5QMlAfjR0dgTlojN4Msw2qk7cP1QEkV96e2BJUaqaNnM3zMvd7cfRjPNfbsbwl6hCCCAdwrALKYtBnjKVrCGPwO+xiw5mUJhZ1n6ZivTOdQEWbl26UO60J9ItiWP8VK0d0aChn326Ovt7qC4S3AgDlaJwcKe5Ifxl/UOWePGRwXj2UUuDWFhjtVmRntMmNZbe5yE8MkEvU+4/c6LqGwTCgDenRbK53Dg=";

interface CapturedCheck {
samlRequest?: any;
result?: any;
name: string;
expectedStatusCode: number;
samlResponse: any;
samlResponse?: any;
config: Partial<SamlConfig>;
expectedNameIDStartsWith?: string;
mockDate: string;
}

interface LogoutCheck {
interface SAMLCheck {
samlRequest?: any;
result?: any;
name: string;
result: any;
samlRequest: any;
config: any;
samlResponse?: any;
config: Partial<SamlConfig>;
expectedNameIDStartsWith?: string;
}

describe("passport-saml /", function () {
Expand Down Expand Up @@ -139,12 +145,12 @@ describe("passport-saml /", function () {
app.use(pp.initialize());
const config = check.config;
config.callbackUrl = "http://localhost:3033/login";
let profile: Profile = null;
let profile: Profile | null = null;
pp.use(
new SamlStrategy(config, function (_profile: Profile, done: VerifiedCallback) {
profile = _profile;
done(null, { id: profile.nameID });
})
} as VerifyWithoutRequest)
);

let userSerialized = false;
Expand Down Expand Up @@ -203,15 +209,16 @@ describe("passport-saml /", function () {
const config = check.config;
config.callbackUrl = "http://localhost:3033/login";
config.passReqToCallback = true;
let passedRequest: express.Request = null;
let passedRequest: express.Request | null = null;
pp.use(
new SamlStrategy(
config,
function (req: express.Request, _profile: Profile, done: VerifiedCallback) {
passedRequest = req;
done(null, { id: _profile!.nameID });
}
)
new SamlStrategy(config, function (
req: express.Request,
_profile: Profile,
done: VerifiedCallback
) {
passedRequest = req;
done(null, { id: _profile!.nameID });
} as VerifyWithRequest)
);
pp.serializeUser(function (user, done) {
done(null, user);
Expand Down Expand Up @@ -242,9 +249,9 @@ describe("passport-saml /", function () {
response.statusCode.should.equal(check.expectedStatusCode);
if (response.statusCode == 200) {
should.exist(passedRequest);
passedRequest.url.should.eql("/login");
passedRequest.method.should.eql("POST");
should(passedRequest.body).match(check.samlResponse);
passedRequest!.url!.should.eql("/login");
passedRequest!.method!.should.eql("POST");
should(passedRequest!.body).match(check.samlResponse);
} else {
should.not.exist(passedRequest);
}
Expand All @@ -270,7 +277,7 @@ describe("passport-saml /", function () {
});

describe("captured SAML requests /", function () {
const logoutChecks: LogoutCheck[] = [
const logoutChecks: CapturedCheck[] = [
{
name: "Logout",
config: {
Expand Down Expand Up @@ -313,7 +320,7 @@ describe("passport-saml /", function () {
},
];

const capturedChecks = [
const capturedChecks: SAMLCheck[] = [
{
name: "Empty Config",
config: {},
Expand Down Expand Up @@ -396,7 +403,7 @@ describe("passport-saml /", function () {
issuer: "http://exampleSp.com/saml",
identifierFormat: "alternateIdentifier",
passive: true,
attributeConsumingServiceIndex: 123,
attributeConsumingServiceIndex: "123",
forceAuthn: false,
},
result: {
Expand Down Expand Up @@ -445,7 +452,7 @@ describe("passport-saml /", function () {
issuer: "http://exampleSp.com/saml",
identifierFormat: "alternateIdentifier",
passive: true,
attributeConsumingServiceIndex: 123,
attributeConsumingServiceIndex: "123",
skipRequestCompression: true,
},
result: {
Expand Down Expand Up @@ -494,7 +501,7 @@ describe("passport-saml /", function () {
issuer: "http://exampleSp.com/saml",
identifierFormat: "alternateIdentifier",
passive: true,
attributeConsumingServiceIndex: 123,
attributeConsumingServiceIndex: "123",
skipRequestCompression: true,
disableRequestedAuthnContext: true,
forceAuthn: true,
Expand Down Expand Up @@ -535,7 +542,7 @@ describe("passport-saml /", function () {
issuer: "http://exampleSp.com/saml",
identifierFormat: "alternateIdentifier",
passive: true,
attributeConsumingServiceIndex: 123,
attributeConsumingServiceIndex: "123",
authnContext: "myAuthnContext",
},
result: {
Expand Down Expand Up @@ -584,7 +591,7 @@ describe("passport-saml /", function () {
issuer: "http://exampleSp.com/saml",
identifierFormat: "alternateIdentifier",
passive: true,
attributeConsumingServiceIndex: 123,
attributeConsumingServiceIndex: "123",
authnContext: ["myAuthnContext", "myAuthnContext2"],
},
result: {
Expand Down Expand Up @@ -1152,8 +1159,8 @@ describe("passport-saml /", function () {

let server: Server;

function testForCheck(check) {
return function (done) {
function testForCheck(check: SAMLCheck) {
return function (done: Mocha.Done) {
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(passport.initialize());
Expand All @@ -1162,10 +1169,10 @@ describe("passport-saml /", function () {
config.entryPoint = "https://wwwexampleIdp.com/saml";
let profile = null;
passport.use(
new SamlStrategy(config, function (_profile, done: VerifiedCallback) {
new SamlStrategy(config, function (_profile: Profile, done: VerifiedCallback) {
profile = _profile;
done(null, profile);
})
} as VerifyWithoutRequest)
);

let userSerialized = false;
Expand Down Expand Up @@ -1245,7 +1252,7 @@ describe("passport-saml /", function () {
};
}

function testForCheckLogout(check: LogoutCheck) {
function testForCheckLogout(check: CapturedCheck) {
return function (done: Mocha.Done) {
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
Expand All @@ -1255,10 +1262,10 @@ describe("passport-saml /", function () {
config.entryPoint = "https://wwwexampleIdp.com/saml";
let profile = null;
passport.use(
new SamlStrategy(config, function (_profile, done: VerifiedCallback) {
new SamlStrategy(config, function (_profile: Profile, done: VerifiedCallback) {
profile = _profile;
done(null, profile);
})
} as VerifyWithoutRequest)
);

let userSerialized = false;
Expand Down Expand Up @@ -2165,7 +2172,7 @@ describe("passport-saml /", function () {
privateCert: fs.readFileSync(__dirname + "/static/acme_tools_com.key", "utf-8"),
authnContext:
"http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/password",
identifierFormat: undefined,
identifierFormat: null,
signatureAlgorithm: "sha256",
additionalParams: {
customQueryStringParam: "CustomQueryStringParamValue",
Expand Down Expand Up @@ -2197,7 +2204,7 @@ describe("passport-saml /", function () {
privateKey: fs.readFileSync(__dirname + "/static/acme_tools_com.key", "utf-8"),
authnContext:
"http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/password",
identifierFormat: undefined,
identifierFormat: null,
signatureAlgorithm: "sha256" as const,
additionalParams: {
customQueryStringParam: "CustomQueryStringParamValue",
Expand Down Expand Up @@ -2293,7 +2300,7 @@ describe("passport-saml /", function () {
privateCert: fs.readFileSync(__dirname + "/static/acme_tools_com.key", "utf-8"),
authnContext:
"http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/password",
identifierFormat: undefined,
identifierFormat: null,
signatureAlgorithm: "sha1" as const,
additionalParams: {
customQueryStringParam: "CustomQueryStringParamValue",
Expand Down Expand Up @@ -2325,7 +2332,7 @@ describe("passport-saml /", function () {
privateKey: fs.readFileSync(__dirname + "/static/acme_tools_com.key", "utf-8"),
authnContext:
"http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/password",
identifierFormat: undefined,
identifierFormat: null,
signatureAlgorithm: "sha1",
additionalParams: {
customQueryStringParam: "CustomQueryStringParamValue",
Expand Down Expand Up @@ -2810,7 +2817,7 @@ describe("passport-saml /", function () {
await samlObj.cacheProvider.saveAsync(requestId, new Date().toISOString());

await new Promise((resolve) => setTimeout(resolve, 300));
const value = samlObj.cacheProvider.getAsync(requestId);
const value = await samlObj.cacheProvider.getAsync(requestId);
should.not.exist(value);
});

Expand Down Expand Up @@ -3479,7 +3486,7 @@ describe("passport-saml /", function () {
}
});
});
it("errors if bad signature", async function (done) {
it("errors if bad signature", async function () {
await samlObj.cacheProvider.saveAsync("_79db1e7ad12ca1d63e5b", new Date().toISOString());
this.request.Signature = "foo";
try {
Expand Down

0 comments on commit fe9e5f5

Please sign in to comment.