login-authorization-v2 1.1.4 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +54 -54
  2. package/babel.config.js +6 -0
  3. package/coverage/clover.xml +140 -0
  4. package/coverage/coverage-final.json +8 -0
  5. package/coverage/lcov-report/base.css +224 -0
  6. package/coverage/lcov-report/block-navigation.js +87 -0
  7. package/coverage/lcov-report/common-authorization-v2/index.html +116 -0
  8. package/coverage/lcov-report/common-authorization-v2/index.ts.html +547 -0
  9. package/coverage/lcov-report/common-authorization-v2/src/compatible.ts.html +223 -0
  10. package/coverage/lcov-report/common-authorization-v2/src/constance.ts.html +106 -0
  11. package/coverage/lcov-report/common-authorization-v2/src/cookie.ts.html +346 -0
  12. package/coverage/lcov-report/common-authorization-v2/src/dom.ts.html +262 -0
  13. package/coverage/lcov-report/common-authorization-v2/src/index.html +206 -0
  14. package/coverage/lcov-report/common-authorization-v2/src/request.ts.html +361 -0
  15. package/coverage/lcov-report/common-authorization-v2/src/shares.ts.html +163 -0
  16. package/coverage/lcov-report/common-authorization-v2/src/types.ts.html +412 -0
  17. package/coverage/lcov-report/common-authorization-v2/src/utils.ts.html +145 -0
  18. package/coverage/lcov-report/compatible.ts.html +223 -0
  19. package/coverage/lcov-report/constance.ts.html +106 -0
  20. package/coverage/lcov-report/cookie.ts.html +310 -0
  21. package/coverage/lcov-report/dom.ts.html +262 -0
  22. package/coverage/lcov-report/favicon.png +0 -0
  23. package/coverage/lcov-report/index.html +206 -0
  24. package/coverage/lcov-report/prettify.css +1 -0
  25. package/coverage/lcov-report/prettify.js +2 -0
  26. package/coverage/lcov-report/shares.ts.html +163 -0
  27. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  28. package/coverage/lcov-report/sorter.js +210 -0
  29. package/coverage/lcov-report/types.ts.html +412 -0
  30. package/coverage/lcov-report/utils.ts.html +145 -0
  31. package/coverage/lcov.info +268 -0
  32. package/dist/compatible.d.ts +10 -0
  33. package/dist/compatible.d.ts.map +1 -0
  34. package/dist/constance.d.ts +6 -0
  35. package/dist/constance.d.ts.map +1 -0
  36. package/dist/cookie.d.ts +8 -0
  37. package/dist/cookie.d.ts.map +1 -0
  38. package/dist/dom.d.ts +2 -0
  39. package/dist/dom.d.ts.map +1 -0
  40. package/dist/index.d.ts +15 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.esm.js +498 -0
  43. package/dist/index.js.LICENSE.txt +1 -0
  44. package/dist/{index.js → index.umd.js} +125 -14
  45. package/dist/request.d.ts +36 -0
  46. package/dist/request.d.ts.map +1 -0
  47. package/dist/shares.d.ts +10 -0
  48. package/dist/shares.d.ts.map +1 -0
  49. package/dist/src/compatible.d.ts +10 -0
  50. package/dist/src/compatible.d.ts.map +1 -0
  51. package/dist/src/constance.d.ts +8 -0
  52. package/dist/src/constance.d.ts.map +1 -0
  53. package/dist/src/cookie.d.ts +8 -0
  54. package/dist/src/cookie.d.ts.map +1 -0
  55. package/dist/src/dom.d.ts +3 -0
  56. package/dist/src/dom.d.ts.map +1 -0
  57. package/dist/src/request.d.ts +36 -0
  58. package/dist/src/request.d.ts.map +1 -0
  59. package/dist/src/shares.d.ts +10 -0
  60. package/dist/src/shares.d.ts.map +1 -0
  61. package/dist/src/types.d.ts +89 -0
  62. package/dist/src/types.d.ts.map +1 -0
  63. package/dist/src/utils.d.ts +4 -0
  64. package/dist/src/utils.d.ts.map +1 -0
  65. package/dist/test/cookie.test.d.ts +2 -0
  66. package/dist/test/cookie.test.d.ts.map +1 -0
  67. package/dist/test/shares.test.d.ts +2 -0
  68. package/dist/test/shares.test.d.ts.map +1 -0
  69. package/dist/types.d.ts +89 -0
  70. package/dist/types.d.ts.map +1 -0
  71. package/dist/utils.d.ts +2 -0
  72. package/dist/utils.d.ts.map +1 -0
  73. package/index.html +28 -0
  74. package/index.ts +154 -0
  75. package/jest.config.js +6 -0
  76. package/package.json +55 -28
  77. package/src/compatible.ts +46 -0
  78. package/src/constance.ts +7 -0
  79. package/src/cookie.ts +75 -0
  80. package/src/dom.ts +59 -0
  81. package/src/request.ts +99 -0
  82. package/src/shares.ts +26 -0
  83. package/src/types.ts +109 -0
  84. package/src/utils.ts +20 -0
  85. package/tests/compatible.test.ts +420 -0
  86. package/tests/cookie.test.ts +78 -0
  87. package/tests/dom.test.ts +95 -0
  88. package/tests/index.test.ts +126 -0
  89. package/tests/shares.test.ts +52 -0
  90. package/tests/utils.test.ts +79 -0
  91. package/tsconfig.json +49 -0
  92. package/webpack.config.mjs +47 -0
  93. package/index.js +0 -304
  94. package/webpack.config.js +0 -9
@@ -0,0 +1,95 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+ import { describe, expect, test, jest, beforeEach, afterEach } from '@jest/globals'
5
+ import { closeDialog, openDialog } from '../src/dom'
6
+
7
+ // 模拟 utils 模块中的 isMobile 函数
8
+ jest.mock('../src/utils', () => ({
9
+ isMobile: jest.fn()
10
+ }))
11
+
12
+ // 模拟 shares 模块中的 loginPageUrl
13
+ jest.mock('../src/shares', () => ({
14
+ loginPageUrl: 'https://example.com/login'
15
+ }))
16
+
17
+ // 导入被模拟后的模块
18
+ import { isMobile } from '../src/utils'
19
+
20
+ describe('dom.ts 方法单元测试', () => {
21
+ let originalLocation: Location
22
+
23
+ beforeEach(() => {
24
+ originalLocation = window.location
25
+
26
+ document.body.innerHTML = ''
27
+
28
+ // 重置所有模拟
29
+ jest.clearAllMocks()
30
+ })
31
+
32
+ describe('openDialog', () => {
33
+ test('应该在 PC 端正确创建对话框', () => {
34
+ // 模拟 PC 端
35
+ (isMobile as jest.Mock).mockReturnValue(false)
36
+
37
+ // 执行
38
+ openDialog()
39
+
40
+ // 验证对话框容器是否存在
41
+ const container = document.querySelector('div > div#confirm-container')
42
+ expect(container).not.toBeNull()
43
+
44
+ // 验证 PC 端特定样式
45
+ expect(container?.outerHTML).toContain('width:615px')
46
+ expect(container?.outerHTML).toContain('left: 35%')
47
+
48
+ // 验证按钮存在
49
+ const confirmButton = document.querySelector('#confirm') as HTMLButtonElement
50
+ const cancelButton = document.querySelector('#cancel') as HTMLButtonElement
51
+ expect(confirmButton).not.toBeNull()
52
+ expect(cancelButton).not.toBeNull()
53
+
54
+ // 验证文本内容
55
+ expect(container?.textContent).toContain('You do not have access to the system.')
56
+ expect(container?.textContent).toContain('Sign in to another ZERO account')
57
+ expect(container?.textContent).toContain('Cancel')
58
+ })
59
+
60
+ test('应该在移动端正确创建对话框', () => {
61
+ (isMobile as jest.Mock).mockReturnValue(true)
62
+
63
+ openDialog()
64
+
65
+ const container = document.querySelector('div > div#confirm-container')
66
+ expect(container).not.toBeNull()
67
+
68
+ expect(container?.outerHTML).toContain('width:97.6vw')
69
+ expect(container?.outerHTML).toContain('height:145px')
70
+
71
+ const confirmButton = document.querySelector('#confirm')
72
+ const cancelButton = document.querySelector('#cancel')
73
+
74
+ expect(confirmButton).not.toBeNull()
75
+ expect(cancelButton).not.toBeNull()
76
+
77
+ expect(container?.textContent).toContain('You do not have access to the system.')
78
+ expect(container?.textContent).toContain('Sign in to another ZERO account')
79
+ expect(container?.textContent).toContain('Cancel')
80
+ })
81
+
82
+ test('close dialog 应该正常关闭', () => {
83
+ (isMobile as jest.Mock).mockReturnValue(false)
84
+
85
+ openDialog()
86
+ let container = document.querySelector('div > div#confirm-container')
87
+
88
+ expect(container).not.toBeNull()
89
+
90
+ closeDialog()
91
+ container = document.querySelector('div > div#confirm-container')
92
+ expect(container).toBeNull()
93
+ })
94
+ })
95
+ })
@@ -0,0 +1,126 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+ import { describe, expect, test, jest, beforeEach, afterEach } from '@jest/globals'
5
+ import { getUserInfo, make } from '../src/utils'
6
+ import { Base64 } from 'js-base64'
7
+ import {
8
+ getAccessToken,
9
+ getRefreshToken,
10
+ setAccessToken,
11
+ setRefreshToken,
12
+ setSystemType
13
+ } from '../src/cookie'
14
+ import { openDialog } from '../src/dom'
15
+ import {
16
+ fetchServerListHTTP,
17
+ fetchRefreshTokenHTTP,
18
+ initInstance,
19
+ fetchLogoutHTTP
20
+ } from '../src/request'
21
+ import {
22
+ setTenantId,
23
+ setBrand,
24
+ setModuleBaseUrl,
25
+ setLoginPageUrl
26
+ } from '../src/shares'
27
+ import { NO_ACCESS_TOKEN, NO_MODULE_BASE_URL, NO_REFRESH_TOKEN } from '../src/constance'
28
+ import { SystemType } from '../src/types'
29
+
30
+ jest.mock('../src/cookie', () => ({
31
+ getAccessToken: jest.fn(),
32
+ getRefreshToken: jest.fn(),
33
+ setAccessToken: jest.fn(),
34
+ setRefreshToken: jest.fn(),
35
+ setSystemType: jest.fn()
36
+ }))
37
+
38
+ jest.mock('../src/dom', () => ({
39
+ openDialog: jest.fn()
40
+ }))
41
+
42
+ jest.mock('../src/request', () => ({
43
+ fetchServerListHTTP: jest.fn(),
44
+ fetchRefreshTokenHTTP: jest.fn(),
45
+ initInstance: jest.fn(),
46
+ fetchLogoutHTTP: jest.fn()
47
+ }))
48
+
49
+ jest.mock('../src/shares', () => ({
50
+ setTenantId: jest.fn(),
51
+ setBrand: jest.fn(),
52
+ setModuleBaseUrl: jest.fn(),
53
+ setLoginPageUrl: jest.fn()
54
+ }))
55
+
56
+ // 模拟 js-base64
57
+ jest.mock('js-base64', () => ({
58
+ Base64: {
59
+ decode: jest.fn()
60
+ }
61
+ }))
62
+
63
+ describe('index.ts 入口文件单元测试', () => {
64
+ // 模拟的定时器ID
65
+ let mockTimerId: number
66
+
67
+ beforeEach(() => {
68
+ // 重置所有模拟
69
+ jest.clearAllMocks()
70
+
71
+ // 设置假定时器
72
+ jest.useFakeTimers()
73
+
74
+ // 初始化 mock 定时器ID
75
+ mockTimerId = 123
76
+ jest.spyOn(global, 'setInterval').mockImplementation(() => mockTimerId)
77
+ jest.spyOn(global, 'clearInterval').mockImplementation(() => undefined)
78
+
79
+ // 设置默认的 mock 返回值
80
+ ;(getAccessToken as jest.Mock).mockReturnValue('mock.access.token')
81
+ ;(getRefreshToken as jest.Mock).mockReturnValue('mock.refresh.token')
82
+ ;(Base64.decode as jest.Mock).mockReturnValue(JSON.stringify({
83
+ 'cognito:groups': ['testModule', 'commonLogin']
84
+ }))
85
+ ;(fetchServerListHTTP as jest.Mock).mockResolvedValue({
86
+ data: {
87
+ content: [
88
+ {
89
+ groupName: 'AdminPortal',
90
+ staffEndpoint: true
91
+ },
92
+ {
93
+ groupName: 'App',
94
+ staffEndpoint: false
95
+ }
96
+ ]
97
+ }
98
+ })
99
+ })
100
+
101
+ afterEach(() => {
102
+ // 恢复真定时器
103
+ jest.useRealTimers()
104
+ // 清理所有模拟
105
+ jest.clearAllMocks()
106
+ })
107
+
108
+ describe('getUserInfo 函数', () => {
109
+ test('应该正确解析 token 并返回用户信息', async () => {
110
+ const mockUserInfo = {
111
+ sub: '123456',
112
+ email: 'test@example.com',
113
+ 'cognito:groups': ['App']
114
+ }
115
+
116
+ ;(Base64.decode as jest.Mock).mockReturnValue(JSON.stringify(mockUserInfo))
117
+
118
+ const result = await getUserInfo()
119
+ console.log(result)
120
+
121
+ expect(result).toEqual(mockUserInfo)
122
+ expect(getAccessToken).toHaveBeenCalled()
123
+ expect(Base64.decode).toHaveBeenCalledWith('mock.access.token'.split('.')[1])
124
+ })
125
+ })
126
+ })
@@ -0,0 +1,52 @@
1
+ import { describe, expect, test } from '@jest/globals'
2
+ import {
3
+ setBrand,
4
+ setTenantId,
5
+ setModuleBaseUrl,
6
+ setLoginPageUrl,
7
+ moduleBaseUrl,
8
+ tenantId,
9
+ brand,
10
+ loginPageUrl
11
+ } from '../src/shares'
12
+ import { Brand, Tenant } from '../src/types'
13
+
14
+ describe('shares.ts set tenant id 方法单元测试', () => {
15
+ test('测试设置 tenant id 为 ZERO', () => {
16
+ setTenantId(Tenant.ZERO_BR)
17
+ expect(tenantId).toEqual(Tenant.ZERO_BR)
18
+ })
19
+
20
+ test('测试设置 tenant id 边界值情况', () => {
21
+ expect(() => setTenantId('asd')).toThrow()
22
+ })
23
+ })
24
+
25
+ describe('shares.ts set brand id 方法单元测试', () => {
26
+ test('测试设置 brand id 为 ZERO', () => {
27
+ setBrand(Brand.ZERO)
28
+ expect(brand).toEqual(Brand.ZERO)
29
+ })
30
+
31
+ test('测试设置 brand id 边界值情况', () => {
32
+ expect(() => setBrand('asd')).toThrow()
33
+ })
34
+ })
35
+
36
+ describe('shares.ts set moduleBaseUrl 方法单元测试', () => {
37
+ test('测试设置 moduleBaseUrl', () => {
38
+ const testModuleBaseUrl = 'http://localhost:8080'
39
+
40
+ setModuleBaseUrl(testModuleBaseUrl)
41
+ expect(moduleBaseUrl).toEqual(testModuleBaseUrl)
42
+ })
43
+ })
44
+
45
+ describe('shares.ts set loginPageUrl 方法单元测试', () => {
46
+ test('测试设置 loginPageUrl', () => {
47
+ const testLoginPageUrl = 'http://localhost:8080'
48
+
49
+ setLoginPageUrl(testLoginPageUrl)
50
+ expect(loginPageUrl).toEqual(testLoginPageUrl)
51
+ })
52
+ })
@@ -0,0 +1,79 @@
1
+ import { describe, expect, test, jest, beforeEach } from '@jest/globals'
2
+ import { isMobile } from '../src/utils'
3
+
4
+ describe('isMobile 函数单元测试', () => {
5
+ let originalNavigator: Navigator
6
+
7
+ beforeEach(() => {
8
+ originalNavigator = global.navigator
9
+
10
+ // 重置所有 mock
11
+ jest.clearAllMocks()
12
+ })
13
+
14
+ afterEach(() => {
15
+ // 恢复原始 navigator
16
+ if (originalNavigator) {
17
+ Object.defineProperty(global, 'navigator', {
18
+ value: originalNavigator,
19
+ writable: true,
20
+ configurable: true
21
+ })
22
+ }
23
+ })
24
+
25
+ describe('移动设备检测', () => {
26
+ test('应该检测 Android 设备', () => {
27
+ Object.defineProperty(global, 'navigator', {
28
+ value: {
29
+ userAgent: 'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36'
30
+ },
31
+ writable: true,
32
+ configurable: true
33
+ })
34
+
35
+ expect(isMobile()).toBe(true)
36
+ })
37
+
38
+ test('应该检测 iPhone 设备', () => {
39
+ // 模拟 iPhone 设备的 userAgent
40
+ Object.defineProperty(global, 'navigator', {
41
+ value: {
42
+ userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1'
43
+ },
44
+ writable: true,
45
+ configurable: true
46
+ })
47
+
48
+ expect(isMobile()).toBe(true)
49
+ })
50
+
51
+ describe('桌面设备检测', () => {
52
+ test('应该检测 Windows 桌面浏览器', () => {
53
+ // 模拟 Windows 桌面浏览器的 userAgent
54
+ Object.defineProperty(global, 'navigator', {
55
+ value: {
56
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
57
+ },
58
+ writable: true,
59
+ configurable: true
60
+ })
61
+
62
+ expect(isMobile()).toBe(false)
63
+ })
64
+ })
65
+
66
+ test('应该检测 macOS 桌面浏览器', () => {
67
+ // 模拟 macOS 桌面浏览器的 userAgent
68
+ Object.defineProperty(global, 'navigator', {
69
+ value: {
70
+ userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
71
+ },
72
+ writable: true,
73
+ configurable: true
74
+ })
75
+
76
+ expect(isMobile()).toBe(false)
77
+ })
78
+ })
79
+ })
package/tsconfig.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ // "rootDir": "./src",
6
+ "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "esnext",
11
+ "target": "esnext",
12
+ "types": [],
13
+ // For nodejs:
14
+ "lib": ["esnext", "dom"],
15
+ "moduleResolution": "bundler",
16
+ // "types": ["node"],
17
+ // and npm install -D @types/node
18
+
19
+ // Other Outputs
20
+ "sourceMap": false,
21
+ "declaration": true,
22
+ "declarationMap": true,
23
+
24
+ // Stricter Typechecking Options
25
+ "noUncheckedIndexedAccess": true,
26
+ "exactOptionalPropertyTypes": true,
27
+
28
+ // Style Options
29
+ // "noImplicitReturns": true,
30
+ // "noImplicitOverride": true,
31
+ // "noUnusedLocals": true,
32
+ // "noUnusedParameters": true,
33
+ // "noFallthroughCasesInSwitch": true,
34
+ // "noPropertyAccessFromIndexSignature": true,
35
+
36
+ // Recommended Options
37
+ "strict": true,
38
+ "jsx": "react-jsx",
39
+ "verbatimModuleSyntax": true,
40
+ "isolatedModules": true,
41
+ "noUncheckedSideEffectImports": true,
42
+ "moduleDetection": "force",
43
+ "skipLibCheck": true,
44
+ "allowSyntheticDefaultImports": true
45
+ },
46
+ "exclude": [
47
+ "tests"
48
+ ]
49
+ }
@@ -0,0 +1,47 @@
1
+ // webpack.config.mjs
2
+ import path from 'path'
3
+
4
+ const baseConfig = {
5
+ entry: './index.ts',
6
+ mode: 'development',
7
+ module: {
8
+ rules: [
9
+ {
10
+ test: /\.ts$/,
11
+ loader: 'ts-loader',
12
+ }
13
+ ]
14
+ },
15
+ resolve: {
16
+ extensions: ['.ts', '.js'], // 将resolve配置移到baseConfig中
17
+ }
18
+ }
19
+
20
+ export default [
21
+ {
22
+ ...baseConfig,
23
+ output: {
24
+ path: path.resolve(process.cwd(), './dist'),
25
+ filename: 'index.umd.js',
26
+ library: {
27
+ type: 'umd',
28
+ },
29
+ globalObject: 'this',
30
+ }
31
+ },
32
+ {
33
+ ...baseConfig,
34
+ output: {
35
+ path: path.resolve(process.cwd(), './dist'),
36
+ filename: 'index.esm.js',
37
+ library: {
38
+ type: 'module'
39
+ },
40
+ module: true,
41
+ environment: { module: true }
42
+ },
43
+ experiments: {
44
+ outputModule: true
45
+ }
46
+ }
47
+ ]