@55387.ai/uniauth-server 1.1.2 → 1.2.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.
@@ -1,231 +0,0 @@
1
- /**
2
- * UniAuth Server SDK Tests
3
- * UniAuth 服务端 SDK 测试
4
- */
5
-
6
- import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
7
- import { UniAuthServer } from './index.js';
8
-
9
- describe('UniAuthServer', () => {
10
- beforeEach(() => {
11
- vi.stubGlobal('fetch', vi.fn());
12
- });
13
-
14
- afterEach(() => {
15
- vi.unstubAllGlobals();
16
- vi.resetAllMocks();
17
- });
18
-
19
- describe('Configuration', () => {
20
- it('should create server with required config', () => {
21
- const server = new UniAuthServer({
22
- baseUrl: 'https://auth.example.com',
23
- appKey: 'test-app-key',
24
- appSecret: 'test-app-secret',
25
- });
26
-
27
- expect(server).toBeDefined();
28
- });
29
- });
30
-
31
- describe('verifyToken', () => {
32
- it('should verify token successfully', async () => {
33
- const mockPayload = {
34
- sub: 'user-123',
35
- phone: '+8613800138000',
36
- iat: Math.floor(Date.now() / 1000),
37
- exp: Math.floor(Date.now() / 1000) + 3600,
38
- };
39
-
40
- vi.mocked(fetch).mockResolvedValueOnce({
41
- ok: true,
42
- json: async () => ({ success: true, data: mockPayload }),
43
- } as Response);
44
-
45
- const server = new UniAuthServer({
46
- baseUrl: 'https://auth.example.com',
47
- appKey: 'test-key',
48
- appSecret: 'test-secret',
49
- });
50
-
51
- const result = await server.verifyToken('test-token');
52
-
53
- expect(result.sub).toBe('user-123');
54
- expect(result.phone).toBe('+8613800138000');
55
- });
56
-
57
- it('should throw error for invalid token', async () => {
58
- vi.mocked(fetch).mockResolvedValueOnce({
59
- ok: false,
60
- json: async () => ({
61
- success: false,
62
- error: { code: 'INVALID_TOKEN', message: 'Token is invalid' },
63
- }),
64
- } as Response);
65
-
66
- const server = new UniAuthServer({
67
- baseUrl: 'https://auth.example.com',
68
- appKey: 'test-key',
69
- appSecret: 'test-secret',
70
- });
71
-
72
- await expect(server.verifyToken('invalid-token')).rejects.toThrow('Token is invalid');
73
- });
74
-
75
- it('should cache verified tokens', async () => {
76
- const mockPayload = {
77
- sub: 'user-123',
78
- phone: '+8613800138000',
79
- iat: Math.floor(Date.now() / 1000),
80
- exp: Math.floor(Date.now() / 1000) + 3600,
81
- };
82
-
83
- vi.mocked(fetch).mockResolvedValueOnce({
84
- ok: true,
85
- json: async () => ({ success: true, data: mockPayload }),
86
- } as Response);
87
-
88
- const server = new UniAuthServer({
89
- baseUrl: 'https://auth.example.com',
90
- appKey: 'test-key',
91
- appSecret: 'test-secret',
92
- });
93
-
94
- // First call - should make HTTP request
95
- await server.verifyToken('cached-token');
96
-
97
- // Second call - should use cache
98
- await server.verifyToken('cached-token');
99
-
100
- // fetch should only be called once due to caching
101
- expect(fetch).toHaveBeenCalledTimes(1);
102
- });
103
- });
104
-
105
- describe('getUser', () => {
106
- it('should get user info successfully', async () => {
107
- const mockUser = {
108
- id: 'user-123',
109
- phone: '+8613800138000',
110
- nickname: 'Test User',
111
- avatar_url: null,
112
- phone_verified: true,
113
- };
114
-
115
- vi.mocked(fetch).mockResolvedValueOnce({
116
- ok: true,
117
- json: async () => ({ success: true, data: mockUser }),
118
- } as Response);
119
-
120
- const server = new UniAuthServer({
121
- baseUrl: 'https://auth.example.com',
122
- appKey: 'test-key',
123
- appSecret: 'test-secret',
124
- });
125
-
126
- const result = await server.getUser('user-123');
127
-
128
- expect(result.id).toBe('user-123');
129
- expect(result.nickname).toBe('Test User');
130
- });
131
-
132
- it('should throw error when user not found', async () => {
133
- vi.mocked(fetch).mockResolvedValueOnce({
134
- ok: false,
135
- status: 404,
136
- json: async () => ({
137
- success: false,
138
- error: { code: 'USER_NOT_FOUND', message: 'User not found' },
139
- }),
140
- } as Response);
141
-
142
- const server = new UniAuthServer({
143
- baseUrl: 'https://auth.example.com',
144
- appKey: 'test-key',
145
- appSecret: 'test-secret',
146
- });
147
-
148
- await expect(server.getUser('unknown-user')).rejects.toThrow('User not found');
149
- });
150
- });
151
-
152
- describe('clearCache', () => {
153
- it('should clear the token cache', async () => {
154
- const mockPayload = {
155
- sub: 'user-123',
156
- phone: '+8613800138000',
157
- iat: Math.floor(Date.now() / 1000),
158
- exp: Math.floor(Date.now() / 1000) + 3600,
159
- };
160
-
161
- vi.mocked(fetch).mockResolvedValue({
162
- ok: true,
163
- json: async () => ({ success: true, data: mockPayload }),
164
- } as Response);
165
-
166
- const server = new UniAuthServer({
167
- baseUrl: 'https://auth.example.com',
168
- appKey: 'test-key',
169
- appSecret: 'test-secret',
170
- });
171
-
172
- // First call
173
- await server.verifyToken('test-token');
174
- expect(fetch).toHaveBeenCalledTimes(1);
175
-
176
- // Clear cache
177
- server.clearCache();
178
-
179
- // Second call - should make new HTTP request
180
- await server.verifyToken('test-token');
181
- expect(fetch).toHaveBeenCalledTimes(2);
182
- });
183
- });
184
-
185
- describe('middleware', () => {
186
- it('should create middleware function', () => {
187
- const server = new UniAuthServer({
188
- baseUrl: 'https://auth.example.com',
189
- appKey: 'test-key',
190
- appSecret: 'test-secret',
191
- });
192
-
193
- const middleware = server.middleware();
194
-
195
- expect(typeof middleware).toBe('function');
196
- });
197
-
198
- it('should reject requests without authorization header', async () => {
199
- const server = new UniAuthServer({
200
- baseUrl: 'https://auth.example.com',
201
- appKey: 'test-key',
202
- appSecret: 'test-secret',
203
- });
204
-
205
- const middleware = server.middleware();
206
-
207
- const mockReq = {
208
- headers: {},
209
- };
210
-
211
- const mockRes = {
212
- status: vi.fn().mockReturnThis(),
213
- json: vi.fn(),
214
- };
215
-
216
- const mockNext = vi.fn();
217
-
218
- await middleware(mockReq as any, mockRes as any, mockNext);
219
-
220
- expect(mockRes.status).toHaveBeenCalledWith(401);
221
- expect(mockRes.json).toHaveBeenCalledWith({
222
- success: false,
223
- error: {
224
- code: 'UNAUTHORIZED',
225
- message: expect.stringContaining('Authorization header'),
226
- },
227
- });
228
- expect(mockNext).not.toHaveBeenCalled();
229
- });
230
- });
231
- });
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src"
6
- },
7
- "include": [
8
- "src/**/*"
9
- ],
10
- "exclude": [
11
- "node_modules",
12
- "dist",
13
- "tests"
14
- ]
15
- }