-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathauth.token.controller.unit.tests.js
More file actions
173 lines (147 loc) · 5.93 KB
/
auth.token.controller.unit.tests.js
File metadata and controls
173 lines (147 loc) · 5.93 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
/**
* Module dependencies.
*
* Unit tests for auth.controller `token` handler.
* Regression guard: /api/auth/token must include `complementary` in the user
* projection so per-user UI prefs / extras rehydrate correctly across refresh.
*/
import { jest, describe, test, expect, beforeEach } from '@jest/globals';
describe('auth.controller.token — user projection:', () => {
beforeEach(() => {
jest.resetModules();
jest.unstable_mockModule('../../../lib/services/logger.js', () => ({
default: { warn: jest.fn(), error: jest.fn(), info: jest.fn() },
}));
jest.unstable_mockModule('../../../modules/users/services/users.service.js', () => ({
default: { getBrut: jest.fn(), update: jest.fn(), create: jest.fn(), remove: jest.fn() },
}));
jest.unstable_mockModule('../../../modules/organizations/services/organizations.service.js', () => ({
default: { handleSignupOrganization: jest.fn() },
}));
jest.unstable_mockModule('../../../modules/organizations/services/organizations.crud.service.js', () => ({
default: { autoSetCurrentOrganization: jest.fn().mockResolvedValue(undefined) },
}));
jest.unstable_mockModule('../../../modules/organizations/services/organizations.membership.service.js', () => ({
default: {
findByUserAndOrganization: jest.fn().mockResolvedValue(null),
listPendingByUser: jest.fn().mockResolvedValue([]),
},
}));
jest.unstable_mockModule('../../../config/index.js', () => ({
default: {
sign: { up: true, in: true },
jwt: { secret: 'test-secret', expiresIn: 3600 },
cookie: { secure: false, sameSite: 'lax' },
organizations: { enabled: false },
app: { title: 'Test', contact: 'test@test.com' },
},
}));
jest.unstable_mockModule('../../../lib/middlewares/model.js', () => ({
default: { getResultFromZod: jest.fn(), checkError: jest.fn() },
}));
jest.unstable_mockModule('../../../lib/helpers/mailer/index.js', () => ({
default: { isConfigured: jest.fn().mockReturnValue(false), sendMail: jest.fn() },
}));
jest.unstable_mockModule('../../../lib/helpers/responses.js', () => ({
default: {
success: jest.fn().mockReturnValue(jest.fn()),
error: jest.fn().mockReturnValue(jest.fn()),
},
}));
jest.unstable_mockModule('../../../lib/helpers/errors.js', () => ({
default: { getMessage: jest.fn().mockReturnValue('error') },
}));
jest.unstable_mockModule('../../../lib/helpers/AppError.js', () => ({
default: class AppError extends Error {
constructor(msg, opts) {
super(msg);
this.code = opts?.code;
this.details = opts?.details;
}
},
}));
jest.unstable_mockModule('../../../modules/users/models/users.schema.js', () => ({
default: { User: {} },
}));
jest.unstable_mockModule('../../../lib/middlewares/policy.js', () => ({
default: { defineAbilityFor: jest.fn().mockResolvedValue({}) },
}));
jest.unstable_mockModule('../../../lib/helpers/abilities.js', () => ({
default: jest.fn().mockReturnValue([]),
}));
jest.unstable_mockModule('../../../lib/helpers/getBaseUrl.js', () => ({
default: jest.fn().mockReturnValue('http://localhost:3000'),
}));
jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({
default: { identify: jest.fn(), groupIdentify: jest.fn() },
}));
// Mock invite-only gate (auth.controller imports InvitationService)
jest.unstable_mockModule('../../../modules/auth/services/auth.invitation.service.js', () => ({
default: {
validateInviteToken: jest.fn().mockResolvedValue(null),
consumeInviteToken: jest.fn().mockResolvedValue(undefined),
},
}));
});
test('should include `complementary` in the returned user object (regression guard: per-user UI prefs / extras must rehydrate across refresh)', async () => {
const { default: AuthController } = await import('../../../modules/auth/controllers/auth.controller.js');
const req = {
user: {
id: 'u1',
provider: 'local',
roles: ['user'],
avatar: '',
email: 'a@b.com',
lastName: 'Doe',
firstName: 'Jane',
additionalProvidersData: {},
emailVerified: true,
currentOrganization: null,
lastLoginAt: new Date(0),
complementary: { ui: { layout: 'grid' } },
},
};
const res = {
status: jest.fn().mockReturnThis(),
cookie: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
await AuthController.token(req, res);
expect(res.json).toHaveBeenCalledTimes(1);
const payload = res.json.mock.calls[0][0];
expect(payload.user).toBeDefined();
expect(payload.user.complementary).toEqual({ ui: { layout: 'grid' } });
// Sanity — existing projection keys still present
expect(payload.user.id).toBe('u1');
expect(payload.user.email).toBe('a@b.com');
});
test('should preserve a missing/undefined `complementary` as undefined (no crash)', async () => {
const { default: AuthController } = await import('../../../modules/auth/controllers/auth.controller.js');
const req = {
user: {
id: 'u2',
provider: 'local',
roles: ['user'],
avatar: '',
email: 'c@d.com',
lastName: 'Smith',
firstName: 'John',
additionalProvidersData: {},
emailVerified: true,
currentOrganization: null,
lastLoginAt: new Date(0),
// complementary intentionally omitted
},
};
const res = {
status: jest.fn().mockReturnThis(),
cookie: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
await AuthController.token(req, res);
const payload = res.json.mock.calls[0][0];
expect(payload.user).toBeDefined();
expect('complementary' in payload.user).toBe(true);
expect(payload.user.complementary).toBeUndefined();
});
});