Files
eSIM-Tools/tests/setup.js
Abner f97d8339e4 feat(auth): 增加 OAuth 2.0 PKCE 令牌交换功能
- 新增 giffgaff-token-exchange.js 函数,用于交换 authorization code 和 access token
- 更新 verify-cookie.js 函数,增加对 cookie 中 access token 的提取和验证
- 修改 giffgaff-graphql.js、giffgaff-mfa-challenge.js 和 giffgaff-mfa-validation.js 函数,支持使用 access token 或 cookie 进行身份验证
- 更新 README.md 和 COOKIE_LOGIN_SETUP.md 文档,说明新的身份验证流程和安全注意事项
2025-08-09 14:39:29 +08:00

95 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Jest 测试设置文件
*/
// 导入 Jest DOM 扩展
import '@testing-library/jest-dom';
// 设置全局测试超时
jest.setTimeout(10000);
// 模拟 localStorage
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
global.localStorage = localStorageMock;
// 模拟 sessionStorage
const sessionStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
global.sessionStorage = sessionStorageMock;
// 模拟 fetch API
global.fetch = jest.fn();
// 模拟 crypto API提供 webcrypto.subtle 接口)
const webcryptoMock = {
getRandomValues: (arr) => {
for (let i = 0; i < arr.length; i++) {
arr[i] = Math.floor(Math.random() * 256);
}
return arr;
},
subtle: {
digest: jest.fn(() => Promise.resolve(new ArrayBuffer(32)))
}
};
global.crypto = webcryptoMock;
globalThis.crypto = webcryptoMock;
if (typeof window !== 'undefined') {
window.crypto = webcryptoMock;
}
// 模拟 navigator.clipboard
global.navigator.clipboard = {
writeText: jest.fn(() => Promise.resolve()),
readText: jest.fn(() => Promise.resolve(''))
};
// 模拟 IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {}
observe() {}
unobserve() {}
takeRecords() {
return [];
}
};
// 模拟 Service Worker
global.navigator.serviceWorker = {
register: jest.fn(() => Promise.resolve({
installing: null,
waiting: null,
active: null,
addEventListener: jest.fn()
}))
};
// Polyfill: TextEncoderNode 环境下用于 PKCE 生成)
if (typeof global.TextEncoder === 'undefined') {
const { TextEncoder } = require('util');
global.TextEncoder = TextEncoder;
}
// Polyfill: document.execCommand降级复制方案
if (typeof document.execCommand === 'undefined') {
document.execCommand = jest.fn(() => true);
}
// 清理每个测试后的模拟
afterEach(() => {
jest.clearAllMocks();
localStorageMock.getItem.mockClear();
localStorageMock.setItem.mockClear();
localStorageMock.removeItem.mockClear();
localStorageMock.clear.mockClear();
});