sdadfadas

Форк
0
/
guestTokenRefresh.test.ts 
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

20
import {
21
  REFRESH_TIMING_BUFFER_MS,
22
  getGuestTokenRefreshTiming,
23
  MIN_REFRESH_WAIT_MS,
24
  DEFAULT_TOKEN_EXP_MS,
25
} from "./guestTokenRefresh";
26

27
describe("guest token refresh", () => {
28
  beforeAll(() => {
29
    jest.useFakeTimers();
30
    jest.setSystemTime(new Date("2022-03-03 01:00"));
31
    jest.spyOn(global, "setTimeout");
32
  });
33

34
  afterAll(() => {
35
    jest.useRealTimers();
36
  });
37

38
  function makeFakeJWT(claims: any) {
39
    // not a valid jwt, but close enough for this code
40
    const tokenifiedClaims = Buffer.from(JSON.stringify(claims)).toString(
41
      "base64"
42
    );
43
    return `abc.${tokenifiedClaims}.xyz`;
44
  }
45

46
  it("schedules refresh with an epoch exp", () => {
47
    // exp is in seconds
48
    const ttl = 1300;
49
    const exp = Date.now() / 1000 + ttl;
50
    const fakeToken = makeFakeJWT({ exp });
51

52
    const timing = getGuestTokenRefreshTiming(fakeToken);
53

54
    expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
55
    expect(timing).toBe(ttl * 1000 - REFRESH_TIMING_BUFFER_MS);
56
  });
57

58
  it("schedules refresh with an epoch exp containing a decimal", () => {
59
    const ttl = 1300.123;
60
    const exp = Date.now() / 1000 + ttl;
61
    const fakeToken = makeFakeJWT({ exp });
62

63
    const timing = getGuestTokenRefreshTiming(fakeToken);
64

65
    expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
66
    expect(timing).toBe(ttl * 1000 - REFRESH_TIMING_BUFFER_MS);
67
  });
68

69
  it("schedules refresh with iso exp", () => {
70
    const exp = new Date("2022-03-03 01:09").toISOString();
71
    const fakeToken = makeFakeJWT({ exp });
72

73
    const timing = getGuestTokenRefreshTiming(fakeToken);
74
    const expectedTiming = 1000 * 60 * 9 - REFRESH_TIMING_BUFFER_MS;
75

76
    expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
77
    expect(timing).toBe(expectedTiming);
78
  });
79

80
  it("avoids refresh spam", () => {
81
    const fakeToken = makeFakeJWT({ exp: Date.now() / 1000 });
82

83
    const timing = getGuestTokenRefreshTiming(fakeToken);
84

85
    expect(timing).toBe(MIN_REFRESH_WAIT_MS - REFRESH_TIMING_BUFFER_MS);
86
  });
87

88
  it("uses a default when it cannot parse the date", () => {
89
    const fakeToken = makeFakeJWT({ exp: "invalid date" });
90

91
    const timing = getGuestTokenRefreshTiming(fakeToken);
92

93
    expect(timing).toBeGreaterThan(MIN_REFRESH_WAIT_MS);
94
    expect(timing).toBe(DEFAULT_TOKEN_EXP_MS - REFRESH_TIMING_BUFFER_MS);
95
  });
96
});
97

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.