zhou zhou
3 天以前 3fdcf1d5e6468c735532e67bde5ff1cdf85bb0c6
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
import assert from 'node:assert/strict'
import { register } from 'node:module'
import test from 'node:test'
 
register(
  'data:text/javascript,export function resolve(specifier, context, nextResolve){ if(specifier===\'@/utils/http\'){ return { shortCircuit:true, url:\'data:text/javascript,export default { post(options){ globalThis.__authHttpCalls.push({ method:"post", options }); return Promise.resolve({ accessToken:"abc", refreshToken:"ref", user:{ id:1 } }) }, get(options){ globalThis.__authHttpCalls.push({ method:"get", options }); return Promise.resolve({ userId:1, roles:[{ code:"R_ADMIN", name:"管理员" }], authorities:[{ authority:"system:menu:save" },{ authority:"system:menu:update" }] }) } }\' } } return nextResolve(specifier, context) }',
  import.meta.url
)
 
globalThis.__authHttpCalls = []
 
const {
  buildLoginPayload,
  fetchGetUserInfo,
  fetchLogin,
  normalizeLoginResponse,
  normalizeUserInfo
} = await import('../src/api/auth.js')
 
test('buildLoginPayload matches the rsf-server login contract', () => {
  assert.deepEqual(buildLoginPayload({ username: 'demo', password: '123456' }), {
    username: 'demo',
    password: '123456'
  })
})
 
test('normalizeLoginResponse extracts the real token fields', () => {
  assert.deepEqual(
    normalizeLoginResponse({
      code: 200,
      data: { accessToken: 'abc', user: { id: 1 } }
    }),
    { accessToken: 'abc', refreshToken: '', user: { id: 1 } }
  )
})
 
test('normalizeLoginResponse also handles an inner data object', () => {
  assert.deepEqual(
    normalizeLoginResponse({
      accessToken: 'abc',
      refreshToken: 'ref',
      user: { id: 1 }
    }),
    { accessToken: 'abc', refreshToken: 'ref', user: { id: 1 } }
  )
})
 
test('normalizeLoginResponse accepts the old backend accessToken shape', () => {
  const result = normalizeLoginResponse({
    code: 200,
    data: { accessToken: 'token-1', refreshToken: '', user: { username: 'admin' } }
  })
 
  assert.equal(result.accessToken, 'token-1')
})
 
test('normalizeLoginResponse keeps missing accessToken empty', () => {
  const result = normalizeLoginResponse({
    code: 200,
    data: { refreshToken: 'ref', user: { username: 'admin' } }
  })
 
  assert.equal(result.accessToken, '')
})
 
test('normalizeUserInfo returns roles and buttons arrays safely', () => {
  assert.deepEqual(normalizeUserInfo({ userId: 1, roles: ['R_ADMIN'], buttons: ['add'] }), {
    userId: 1,
    roles: ['R_ADMIN'],
    buttons: ['add']
  })
})
 
test('normalizeUserInfo derives role codes and button aliases from rsf-server auth payloads', () => {
  assert.deepEqual(
    normalizeUserInfo({
      userId: 1,
      roles: [{ code: 'R_SUPER', name: '超级管理员' }],
      authorities: [{ authority: 'system:menu:save' }, { authority: 'system:menu:update' }]
    }),
    {
      userId: 1,
      roles: ['R_SUPER'],
      authorities: [{ authority: 'system:menu:save' }, { authority: 'system:menu:update' }],
      buttons: ['system:menu:save', 'system:menu:update']
    }
  )
})
 
test('fetchLogin keeps supporting legacy userName input at the boundary', async () => {
  const result = await fetchLogin({ userName: 'demo', password: '123456' })
 
  assert.deepEqual(globalThis.__authHttpCalls.at(-1), {
    method: 'post',
    options: {
      url: '/login',
      params: { username: 'demo', password: '123456' }
    }
  })
  assert.deepEqual(result, {
    token: 'abc',
    accessToken: 'abc',
    refreshToken: 'ref',
    user: { id: 1 }
  })
})
 
test('fetchGetUserInfo normalizes the returned user payload', async () => {
  const result = await fetchGetUserInfo()
 
  assert.deepEqual(globalThis.__authHttpCalls.at(-1), {
    method: 'get',
    options: {
      url: '/auth/user'
    }
  })
  assert.deepEqual(result, {
    userId: 1,
    roles: ['R_ADMIN'],
    authorities: [{ authority: 'system:menu:save' }, { authority: 'system:menu:update' }],
    buttons: ['system:menu:save', 'system:menu:update']
  })
})