forked from MyFanss/MyFans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.controller.spec.ts
More file actions
220 lines (182 loc) · 7.16 KB
/
Copy pathauth.controller.spec.ts
File metadata and controls
220 lines (182 loc) · 7.16 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { Test, TestingModule } from '@nestjs/testing';
import { BadRequestException, HttpException, HttpStatus } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { WalletAuthService } from './wallet-auth.service';
describe('AuthController', () => {
let controller: AuthController;
let authService: jest.Mocked<Pick<AuthService, 'validateStellarAddress' | 'createSession'>>;
let walletAuthService: jest.Mocked<Pick<WalletAuthService, 'createChallenge' | 'verifyAndIssueToken'>>;
const validAddress = `G${'A'.repeat(55)}`;
beforeEach(async () => {
authService = {
validateStellarAddress: jest.fn().mockReturnValue(true),
createSession: jest.fn().mockResolvedValue({
userId: validAddress,
token: Buffer.from(validAddress).toString('base64'),
}),
};
walletAuthService = {
createChallenge: jest.fn().mockResolvedValue({
nonce: 'abc123',
expiresAt: new Date('2026-12-31'),
}),
verifyAndIssueToken: jest.fn().mockResolvedValue({
access_token: 'jwt-token',
token_type: 'Bearer',
}),
};
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [
{ provide: AuthService, useValue: authService },
{ provide: WalletAuthService, useValue: walletAuthService },
],
}).compile();
controller = module.get(AuthController);
});
describe('login', () => {
it('creates a session for a valid address', async () => {
const result = await controller.login({ address: validAddress });
expect(authService.validateStellarAddress).toHaveBeenCalledWith(validAddress);
expect(authService.createSession).toHaveBeenCalledWith(validAddress);
expect(result).toEqual({
userId: validAddress,
token: expect.any(String),
});
});
it('throws BadRequestException for invalid address', async () => {
authService.validateStellarAddress.mockReturnValue(false);
await expect(controller.login({ address: 'invalid' })).rejects.toThrow(
BadRequestException,
);
expect(authService.createSession).not.toHaveBeenCalled();
});
it('throws BadRequestException when address is omitted', async () => {
authService.validateStellarAddress.mockReturnValue(false);
await expect(controller.login({})).rejects.toThrow(BadRequestException);
});
it('throws on network mismatch', async () => {
await expect(
controller.login({ address: validAddress }, 'mainnet'),
).rejects.toThrow(HttpException);
});
it('passes when x-network matches server network', async () => {
const result = await controller.login({ address: validAddress }, 'testnet');
expect(result).toEqual({
userId: validAddress,
token: expect.any(String),
});
});
it('ignores network header when not provided', async () => {
const result = await controller.login({ address: validAddress }, undefined);
expect(result).toEqual({
userId: validAddress,
token: expect.any(String),
});
});
});
describe('register (deprecated)', () => {
it('creates a session for a valid address', async () => {
const result = await controller.register({ address: validAddress });
expect(authService.createSession).toHaveBeenCalledWith(validAddress);
expect(result).toEqual({
userId: validAddress,
token: expect.any(String),
});
});
it('throws BadRequestException for invalid address', async () => {
authService.validateStellarAddress.mockReturnValue(false);
await expect(controller.register({ address: 'bad' })).rejects.toThrow(
BadRequestException,
);
});
it('throws on network mismatch', async () => {
await expect(
controller.register({ address: validAddress }, 'mainnet'),
).rejects.toThrow(HttpException);
});
});
describe('requestChallenge', () => {
it('returns nonce and expiry for a valid address', async () => {
const result = await controller.requestChallenge({ address: validAddress });
expect(authService.validateStellarAddress).toHaveBeenCalledWith(validAddress);
expect(walletAuthService.createChallenge).toHaveBeenCalledWith(validAddress);
expect(result).toEqual({
nonce: 'abc123',
expiresAt: expect.any(Date),
});
});
it('throws BadRequestException for invalid address', async () => {
authService.validateStellarAddress.mockReturnValue(false);
await expect(
controller.requestChallenge({ address: 'invalid' }),
).rejects.toThrow(BadRequestException);
expect(walletAuthService.createChallenge).not.toHaveBeenCalled();
});
it('throws on network mismatch', async () => {
await expect(
controller.requestChallenge({ address: validAddress }, 'mainnet'),
).rejects.toThrow(HttpException);
});
});
describe('verifyChallenge', () => {
const dto = { address: validAddress, nonce: 'abc123', signature: 'deadbeef' };
it('returns JWT for valid verification', async () => {
const result = await controller.verifyChallenge(dto);
expect(walletAuthService.verifyAndIssueToken).toHaveBeenCalledWith(
validAddress,
'abc123',
'deadbeef',
);
expect(result).toEqual({
access_token: 'jwt-token',
token_type: 'Bearer',
});
});
it('throws BadRequestException for invalid address', async () => {
authService.validateStellarAddress.mockReturnValue(false);
await expect(controller.verifyChallenge(dto)).rejects.toThrow(
BadRequestException,
);
expect(walletAuthService.verifyAndIssueToken).not.toHaveBeenCalled();
});
it('throws on network mismatch', async () => {
await expect(
controller.verifyChallenge(dto, 'mainnet'),
).rejects.toThrow(HttpException);
});
it('propagates service errors', async () => {
walletAuthService.verifyAndIssueToken.mockRejectedValue(
new BadRequestException('Invalid signature'),
);
await expect(controller.verifyChallenge(dto)).rejects.toThrow(
BadRequestException,
);
});
});
describe('assertNetworkMatch (via endpoints)', () => {
it('accepts case-insensitive network match', async () => {
const result = await controller.login({ address: validAddress }, 'Testnet');
expect(result).toBeDefined();
});
it('trims whitespace from network header', async () => {
const result = await controller.login({ address: validAddress }, ' testnet ');
expect(result).toBeDefined();
});
it('includes expected and current network in mismatch error', async () => {
try {
await controller.login({ address: validAddress }, 'mainnet');
fail('should have thrown');
} catch (e) {
expect(e).toBeInstanceOf(HttpException);
const response = (e as HttpException).getResponse();
expect(response).toMatchObject({
error: 'NETWORK_MISMATCH',
expectedNetwork: 'testnet',
currentNetwork: 'mainnet',
});
}
});
});
});