forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.controller.spec.ts
More file actions
74 lines (63 loc) · 2.21 KB
/
Copy pathusers.controller.spec.ts
File metadata and controls
74 lines (63 loc) · 2.21 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
import { ForbiddenException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AuthenticatedRequest, UsersController } from './users.controller';
import { UsersService } from './users.service';
import { User } from '../common/entities';
describe('UsersController', () => {
let controller: UsersController;
const mockUsersService = {
list: jest.fn(),
findById: jest.fn(),
setStellarAddress: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers: [
{
provide: UsersService,
useValue: mockUsersService,
},
],
}).compile();
controller = module.get<UsersController>(UsersController);
jest.clearAllMocks();
});
describe('setStellarAddress', () => {
it('allows a user to update their own stellar address', async () => {
const userId = 'user-123';
const dto = {
stellarAddress:
'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5',
};
const req = {
user: { userId: 'user-123', username: 'alice' },
} as unknown as AuthenticatedRequest;
const updatedUser = {
id: userId,
stellarAddress: dto.stellarAddress,
} as User;
mockUsersService.setStellarAddress.mockResolvedValue(updatedUser);
const result = await controller.setStellarAddress(userId, dto, req);
expect(mockUsersService.setStellarAddress).toHaveBeenCalledWith(
userId,
dto.stellarAddress,
);
expect(result).toEqual(updatedUser);
});
it('rejects update when authenticated user does not match the target id (403 Forbidden)', () => {
const targetUserId = 'user-target-456';
const dto = {
stellarAddress:
'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5',
};
const req = {
user: { userId: 'user-attacker-123', username: 'eve' },
} as unknown as AuthenticatedRequest;
expect(() =>
controller.setStellarAddress(targetUserId, dto, req),
).toThrow(ForbiddenException);
expect(mockUsersService.setStellarAddress).not.toHaveBeenCalled();
});
});
});