sdadfadas
96 строк · 3.0 Кб
1/**
2* Licensed to the Apache Software Foundation (ASF) under one
3* or more contributor license agreements. See the NOTICE file
4* distributed with this work for additional information
5* regarding copyright ownership. The ASF licenses this file
6* to you under the Apache License, Version 2.0 (the
7* "License"); you may not use this file except in compliance
8* with the License. You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing,
13* software distributed under the License is distributed on an
14* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15* KIND, either express or implied. See the License for the
16* specific language governing permissions and limitations
17* under the License.
18*/
19
20import {
21REFRESH_TIMING_BUFFER_MS,
22getGuestTokenRefreshTiming,
23MIN_REFRESH_WAIT_MS,
24DEFAULT_TOKEN_EXP_MS,
25} from "./guestTokenRefresh";
26
27describe("guest token refresh", () => {
28beforeAll(() => {
29jest.useFakeTimers();
30jest.setSystemTime(new Date("2022-03-03 01:00"));
31jest.spyOn(global, "setTimeout");
32});
33
34afterAll(() => {
35jest.useRealTimers();
36});
37
38function makeFakeJWT(claims: any) {
39// not a valid jwt, but close enough for this code
40const tokenifiedClaims = Buffer.from(JSON.stringify(claims)).toString(
41"base64"
42);
43return `abc.${tokenifiedClaims}.xyz`;
44}
45
46it("schedules refresh with an epoch exp", () => {
47// exp is in seconds
48const ttl = 1300;
49const exp = Date.now() / 1000 + ttl;
50const fakeToken = makeFakeJWT({ exp });
51
52const timing = getGuestTokenRefreshTiming(fakeToken);
53
54expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
55expect(timing).toBe(ttl * 1000 - REFRESH_TIMING_BUFFER_MS);
56});
57
58it("schedules refresh with an epoch exp containing a decimal", () => {
59const ttl = 1300.123;
60const exp = Date.now() / 1000 + ttl;
61const fakeToken = makeFakeJWT({ exp });
62
63const timing = getGuestTokenRefreshTiming(fakeToken);
64
65expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
66expect(timing).toBe(ttl * 1000 - REFRESH_TIMING_BUFFER_MS);
67});
68
69it("schedules refresh with iso exp", () => {
70const exp = new Date("2022-03-03 01:09").toISOString();
71const fakeToken = makeFakeJWT({ exp });
72
73const timing = getGuestTokenRefreshTiming(fakeToken);
74const expectedTiming = 1000 * 60 * 9 - REFRESH_TIMING_BUFFER_MS;
75
76expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
77expect(timing).toBe(expectedTiming);
78});
79
80it("avoids refresh spam", () => {
81const fakeToken = makeFakeJWT({ exp: Date.now() / 1000 });
82
83const timing = getGuestTokenRefreshTiming(fakeToken);
84
85expect(timing).toBe(MIN_REFRESH_WAIT_MS - REFRESH_TIMING_BUFFER_MS);
86});
87
88it("uses a default when it cannot parse the date", () => {
89const fakeToken = makeFakeJWT({ exp: "invalid date" });
90
91const timing = getGuestTokenRefreshTiming(fakeToken);
92
93expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
94expect(timing).toBe(DEFAULT_TOKEN_EXP_MS - REFRESH_TIMING_BUFFER_MS);
95});
96});
97