forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.test.ts
More file actions
149 lines (121 loc) · 4.54 KB
/
Copy pathState.test.ts
File metadata and controls
149 lines (121 loc) · 4.54 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
// 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 { State } from "./State";
import { InMemoryWebStorage } from "./InMemoryWebStorage";
import { WebStorageStateStore } from "./WebStorageStateStore";
describe("State", () => {
describe("constructor", () => {
it("should generate id", () => {
// act
const subject = new State({
request_type: "type",
});
// assert
expect(subject.id).toBeDefined();
});
it("should accept id", () => {
// act
const subject = new State({
request_type: "type",
id: "5",
});
// assert
expect(subject.id).toEqual("5");
});
it("should accept data", () => {
// act
const subject = new State({
request_type: "type",
data: "test",
});
// assert
expect(subject.data).toEqual("test");
});
it("should accept data as objects", () => {
// act
const subject = new State({
request_type: "type",
data: { foo: "test" },
});
// assert
expect(subject.data).toEqual({ foo: "test" });
});
it("should accept created", () => {
// act
const subject = new State({
request_type: "type",
created: 1000,
});
// assert
expect(subject.created).toEqual(1000);
});
it("should use date.now for created", () => {
// arrange
const oldNow = Date.now;
Date.now = () => {
return 123 * 1000; // ms
};
// act
const subject = new State({
request_type: "type",
});
// assert
expect(subject.created).toEqual(123);
Date.now = oldNow;
});
it("should accept request_type", () => {
// act
const subject = new State({
request_type: "xoxo",
});
// assert
expect(subject.request_type).toEqual("xoxo");
});
});
it("can serialize and then deserialize", () => {
// arrange
const subject1 = new State({
data: { foo: "test" }, created: 1000, request_type:"type",
});
// act
const storage = subject1.toStorageString();
const subject2 = State.fromStorageString(storage);
// assert
expect(subject2).toEqual(subject1);
});
describe("clearStaleState", () => {
it("should remove old state entries", async () => {
// arrange
const oldNow = Date.now;
Date.now = () => {
return 200 * 1000; // ms
};
const prefix = "prefix.";
const inMemStore = new InMemoryWebStorage();
const store = new WebStorageStateStore({ prefix: prefix, store: inMemStore });
const s1 = new State({ id: "s1", created: 5, request_type:"type" });
const s2 = new State({ id: "s2", created: 99, request_type:"type" });
const s3 = new State({ id: "s3", created: 100, request_type:"type" });
const s4 = new State({ id: "s4", created: 101, request_type:"type" });
const s5 = new State({ id: "s5", created: 150, request_type:"type" });
inMemStore.setItem("junk0", "junk");
inMemStore.setItem(prefix + s1.id, s1.toStorageString());
inMemStore.setItem("junk1", "junk");
inMemStore.setItem(prefix + s2.id, s2.toStorageString());
inMemStore.setItem("junk2", "junk");
inMemStore.setItem(prefix + s3.id, s3.toStorageString());
inMemStore.setItem("junk3", "junk");
inMemStore.setItem(prefix + s4.id, s4.toStorageString());
inMemStore.setItem("junk4", "junk");
inMemStore.setItem(prefix + s5.id, s5.toStorageString());
inMemStore.setItem("junk5", "junk");
// act
await State.clearStaleState(store, 100);
// assert
expect(inMemStore.length).toEqual(8);
expect(inMemStore.getItem(prefix + "s4")).toBeDefined();
expect(inMemStore.getItem(prefix + "s5")).toBeDefined();
Date.now = oldNow;
});
});
});