forked from node-saml/node-saml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.spec.ts
More file actions
163 lines (140 loc) · 6.37 KB
/
Copy pathcache.spec.ts
File metadata and controls
163 lines (140 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { expect } from "chai";
import * as sinon from "sinon";
import { SAML } from "../src/saml";
import { SamlConfig, ValidateInResponseTo } from "../src/types";
import { FAKE_CERT } from "./types";
describe("Cache tests /", () => {
let fakeClock: sinon.SinonFakeTimers;
beforeEach(function () {
fakeClock = sinon.useFakeTimers();
});
afterEach(function () {
fakeClock.restore();
});
it("should expire a cached request id after the time", async () => {
const requestId = "_dfab47d5d46374cd4b71";
const requestIdExpirationPeriodMs = 100;
const samlConfig: SamlConfig = {
callbackUrl: "http://localhost/saml/consume",
validateInResponseTo: ValidateInResponseTo.always,
requestIdExpirationPeriodMs,
idpCert: FAKE_CERT,
issuer: "onesaml_login",
};
const samlObj = new SAML(samlConfig);
await samlObj.cacheProvider.saveAsync(requestId, new Date().toISOString());
await fakeClock.tickAsync(300);
const value = await samlObj.cacheProvider.getAsync(requestId);
expect(value).to.not.exist;
});
it("should not return an expired item", async () => {
const requestId1 = "_dfab47d5d46374cd4b71";
const requestId2 = "_dfab47d5d46374cd4b72";
const requestId3 = "_dfab47d5d46374cd4b73";
const requestIdExpirationPeriodMs = 100;
const samlConfig: SamlConfig = {
callbackUrl: "http://localhost/saml/consume",
validateInResponseTo: ValidateInResponseTo.always,
requestIdExpirationPeriodMs,
idpCert: FAKE_CERT,
issuer: "onesaml_login",
};
const samlObj = new SAML(samlConfig);
await samlObj.cacheProvider.saveAsync(requestId1, new Date().toISOString());
await fakeClock.tickAsync(requestIdExpirationPeriodMs / 2 + 1);
await samlObj.cacheProvider.saveAsync(requestId2, new Date().toISOString());
await fakeClock.tickAsync(requestIdExpirationPeriodMs / 2 + 1);
await samlObj.cacheProvider.saveAsync(requestId3, new Date().toISOString());
await fakeClock.tickAsync(requestIdExpirationPeriodMs / 2 + 1);
const value1 = await samlObj.cacheProvider.getAsync(requestId1);
expect(value1).to.not.exist;
const value2 = await samlObj.cacheProvider.getAsync(requestId2);
expect(value2).to.not.exist;
const value3 = await samlObj.cacheProvider.getAsync(requestId3);
expect(value3).to.exist;
});
it("should expire many cached request ids after the time", async () => {
const expiredRequestId1 = "_dfab47d5d46374cd4b71";
const expiredRequestId2 = "_dfab47d5d46374cd4b72";
const requestId1 = "_dfab47d5d46374cd4b73";
const requestIdExpirationPeriodMs = 100;
const samlConfig: SamlConfig = {
callbackUrl: "http://localhost/saml/consume",
validateInResponseTo: ValidateInResponseTo.always,
requestIdExpirationPeriodMs,
idpCert: FAKE_CERT,
issuer: "onesaml_login",
};
const samlObj = new SAML(samlConfig);
await samlObj.cacheProvider.saveAsync(expiredRequestId1, new Date().toISOString());
await samlObj.cacheProvider.saveAsync(expiredRequestId2, new Date().toISOString());
await fakeClock.tickAsync(requestIdExpirationPeriodMs * 3);
await samlObj.cacheProvider.saveAsync(requestId1, new Date().toISOString());
const value1 = await samlObj.cacheProvider.getAsync(expiredRequestId1);
expect(value1).to.not.exist;
const value2 = await samlObj.cacheProvider.getAsync(expiredRequestId2);
expect(value2).to.not.exist;
const value3 = await samlObj.cacheProvider.getAsync(requestId1);
expect(value3).to.exist;
await fakeClock.tickAsync(requestIdExpirationPeriodMs * 3);
const value4 = await samlObj.cacheProvider.getAsync(requestId1);
expect(value4).to.not.exist;
});
it("should expire a key if it is old when we add it again", async () => {
const requestId1 = "_dfab47d5d46374cd4b74";
const requestIdExpirationPeriodMs = 100;
const samlConfig: SamlConfig = {
callbackUrl: "http://localhost/saml/consume",
validateInResponseTo: ValidateInResponseTo.always,
requestIdExpirationPeriodMs,
idpCert: FAKE_CERT,
issuer: "onesaml_login",
};
const samlObj = new SAML(samlConfig);
await samlObj.cacheProvider.saveAsync(requestId1, new Date().toISOString());
// Check to make sure that we will remove an expired key exactly when we should if we try to save again
await fakeClock.tickAsync(requestIdExpirationPeriodMs);
const removed = await samlObj.cacheProvider.saveAsync(requestId1, new Date().toISOString());
expect(removed?.createdAt).to.equal(100);
});
it("should expire a key if it is old when we add a different one", async () => {
const requestId1 = "_dfab47d5d46374cd4b74";
const requestId2 = "_dfab47d5d46374cd4b75";
const requestId3 = "_dfab47d5d46374cd4b76";
const samlConfig: SamlConfig = {
callbackUrl: "http://localhost/saml/consume",
validateInResponseTo: ValidateInResponseTo.always,
idpCert: FAKE_CERT,
issuer: "onesaml_login",
};
const samlObj = new SAML(samlConfig);
const cacheRemoveSpy = sinon.spy(samlObj.cacheProvider, "removeAsync");
await samlObj.cacheProvider.saveAsync(requestId1, new Date().toISOString());
await fakeClock.tickAsync("05:00:00");
await samlObj.cacheProvider.saveAsync(requestId2, new Date().toISOString());
await fakeClock.tickAsync("05:00:00");
await samlObj.cacheProvider.saveAsync(requestId3, new Date().toISOString());
await fakeClock.tickAsync("05:00:00");
expect(cacheRemoveSpy.called).to.be.true;
expect(cacheRemoveSpy.calledWith(requestId1)).to.be.true;
const removed = await samlObj.cacheProvider.getAsync(requestId1);
expect(removed).to.not.exist;
sinon.restore();
});
it("should not update the expire time of duplicate entries", async () => {
const requestId = "_dfab47d5d46374cd4b74";
const requestIdExpirationPeriodMs = 100;
const samlConfig: SamlConfig = {
callbackUrl: "http://localhost/saml/consume",
validateInResponseTo: ValidateInResponseTo.always,
requestIdExpirationPeriodMs,
idpCert: FAKE_CERT,
issuer: "onesaml_login",
};
const samlObj = new SAML(samlConfig);
await samlObj.cacheProvider.saveAsync(requestId, new Date().toISOString());
// Check to make sure that we can't add the same data twice
const duplicate = await samlObj.cacheProvider.saveAsync(requestId, new Date().toISOString());
expect(duplicate).to.not.exist;
});
});