-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganization.e2e-spec.ts
More file actions
353 lines (311 loc) · 11.2 KB
/
Copy pathorganization.e2e-spec.ts
File metadata and controls
353 lines (311 loc) · 11.2 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import { INestApplication } from '@nestjs/common';
import { getRepositoryToken } from '@nestjs/typeorm';
import request from 'supertest';
import { App } from 'supertest/types';
import { Repository } from 'typeorm';
import { AuthHelper } from './auth/AuthHelper';
import { initNestTestApp } from './utils/initNestTestApp';
import { Account } from '../src/models/entity/Account.entity';
import { Member } from '../src/models/entity/Member.entity';
import { Organization } from '../src/models/entity/Organization.entity';
import { PushNotificationToken } from '../src/models/entity/PushNotificationToken.entity';
import { Session } from '../src/models/entity/Session.entity';
import { User } from '../src/models/entity/User.entity';
describe('OrganizationController (e2e)', () => {
let app: INestApplication<App>;
let authHelper: AuthHelper;
let userRepository: Repository<User>;
let organizationRepository: Repository<Organization>;
let memberRepository: Repository<Member>;
let sessionRepository: Repository<Session>;
let accountRepository: Repository<Account>;
let pushNotificationTokenRepository: Repository<PushNotificationToken>;
let testUser: User;
let testUser2: User;
let testOrganization: Organization;
let testOrganization2: Organization;
let user1Cookies: string[];
let user2Cookies: string[];
beforeAll(async () => {
const { app: testApp, moduleFixture } = await initNestTestApp();
app = testApp;
authHelper = new AuthHelper(app);
userRepository = moduleFixture.get<Repository<User>>(
getRepositoryToken(User),
);
organizationRepository = moduleFixture.get<Repository<Organization>>(
getRepositoryToken(Organization),
);
memberRepository = moduleFixture.get<Repository<Member>>(
getRepositoryToken(Member),
);
sessionRepository = moduleFixture.get<Repository<Session>>(
getRepositoryToken(Session),
);
accountRepository = moduleFixture.get<Repository<Account>>(
getRepositoryToken(Account),
);
pushNotificationTokenRepository = moduleFixture.get<
Repository<PushNotificationToken>
>(getRepositoryToken(PushNotificationToken));
});
beforeEach(async () => {
// Clear all data in correct order to respect foreign key constraints
try {
await memberRepository.delete({});
await pushNotificationTokenRepository.delete({});
await sessionRepository.delete({});
await accountRepository.delete({});
await userRepository.delete({});
await organizationRepository.delete({});
} catch (error) {
console.log('Delete failed:', error.message);
}
// Create and authenticate users through Better Auth
user1Cookies = await authHelper.createAuthenticatedUser({
name: 'Test User',
email: '[email protected]',
password: 'TestPassword123!',
});
user2Cookies = await authHelper.createAuthenticatedUser({
name: 'Test User 2',
email: '[email protected]',
password: 'TestPassword123!',
});
// Get the created users from database
const foundUser = await userRepository.findOne({
where: { email: '[email protected]' },
});
const foundUser2 = await userRepository.findOne({
where: { email: '[email protected]' },
});
if (!foundUser || !foundUser2) {
throw new Error('Failed to create test users');
}
testUser = foundUser;
testUser2 = foundUser2;
// Create test organizations
const org1 = organizationRepository.create({
name: 'Test Organization',
slug: 'test-org',
logo: '',
metadata: '{"key": "value"}',
});
testOrganization = await organizationRepository.save(org1);
const org2 = organizationRepository.create({
name: 'Test Organization 2',
slug: 'test-org-2',
logo: '',
metadata: '',
});
testOrganization2 = await organizationRepository.save(org2);
// Make testUser a member of testOrganization
await memberRepository.save({
user: testUser,
organization: testOrganization,
role: 'member',
});
// Make testUser2 a member of testOrganization2
await memberRepository.save({
user: testUser2,
organization: testOrganization2,
role: 'member',
});
});
afterAll(async () => {
await memberRepository.delete({});
await pushNotificationTokenRepository.delete({});
await sessionRepository.delete({});
await accountRepository.delete({});
await userRepository.delete({});
await organizationRepository.delete({});
await app.close();
});
describe('Authentication and Authorization', () => {
it('should return 403 when no authentication token is provided for GET /v1/organizations', () => {
return request(app.getHttpServer())
.get('/api/v1/organizations')
.expect(403);
});
it('should return 403 when no authentication token is provided for GET /v1/organizations/:id', () => {
return request(app.getHttpServer())
.get(`/api/v1/organizations/${testOrganization.id}`)
.expect(403);
});
it('should return 403 when invalid session token is provided for GET /v1/organizations', () => {
return request(app.getHttpServer())
.get('/api/v1/organizations')
.set('Cookie', 'better-auth.session_token=invalid-token')
.expect(403);
});
it('should return 403 when invalid session token is provided for GET /v1/organizations/:id', () => {
return request(app.getHttpServer())
.get(`/api/v1/organizations/${testOrganization.id}`)
.set('Cookie', 'better-auth.session_token=invalid-token')
.expect(403);
});
it('should return 403 when expired session token is provided', async () => {
// Create an expired session manually for this specific test
const expiredSession = await sessionRepository.save({
user: testUser,
expiresAt: new Date(Date.now() - 24 * 60 * 60 * 1000), // 24 hours ago
ipAddress: '127.0.0.1',
userAgent: 'test-agent',
token: 'expired-session-token',
});
return request(app.getHttpServer())
.get('/api/v1/organizations')
.set('Cookie', `better-auth.session_token=${expiredSession.token}`)
.expect(403);
});
});
describe('Organization Access Control', () => {
it('should return 403 when user tries to access organization they are not a member of', async () => {
// Use testUser2 who is not a member of testOrganization
return authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${testOrganization.id}`,
user2Cookies,
)
.expect(403)
.expect((res) => {
expect(res.body.error.message).toBe(
"You don't have an access to this organization or this organization doesn't exist",
);
});
});
it('should return 403 when organization does not exist', async () => {
const nonExistentOrgId = '604c0032-9556-4cf0-8fd2-b7c43cfedf04';
return authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${nonExistentOrgId}`,
user1Cookies,
)
.expect(403)
.expect((res) => {
expect(res.body.error.message).toBe(
"You don't have an access to this organization or this organization doesn't exist",
);
});
});
it('should return 400 when user tries to access organization with malformed ID', async () => {
return authHelper
.makeAuthenticatedRequest(
'get',
'/api/v1/organizations/malformed-id',
user1Cookies,
)
.expect(400);
});
it('should return 400 when user tries to access organization with very long ID', async () => {
const longId = 'a'.repeat(1000);
return authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${longId}`,
user1Cookies,
)
.expect(400);
});
it('should return 400 when user tries to access organization with special characters in ID', async () => {
return authHelper
.makeAuthenticatedRequest(
'get',
'/api/v1/organizations/org-id-with-special-chars!@#$%^&*()',
user1Cookies,
)
.expect(400);
});
});
describe('Valid Organization Access', () => {
it('should allow user to access their organization when they are a member', async () => {
return authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${testOrganization.id}`,
user1Cookies,
)
.expect(200)
.expect((res) => {
expect(res.body).toHaveProperty('id', testOrganization.id);
expect(res.body).toHaveProperty('name', 'Test Organization');
expect(res.body).toHaveProperty('slug', 'test-org');
expect(res.body).toHaveProperty('logo', '');
expect(res.body).toHaveProperty('metadata', '{"key": "value"}');
});
});
it('should allow user to access organization with empty fields', async () => {
return authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${testOrganization2.id}`,
user2Cookies,
)
.expect(200)
.expect((res) => {
expect(res.body).toHaveProperty('id', testOrganization2.id);
expect(res.body).toHaveProperty('name', 'Test Organization 2');
expect(res.body).toHaveProperty('slug', 'test-org-2');
expect(res.body).toHaveProperty('logo', '');
expect(res.body).toHaveProperty('metadata', '');
});
});
});
describe('Multiple Users and Organizations', () => {
it('should prevent cross-access between users', async () => {
// User 1 should not be able to access User 2's organization
await authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${testOrganization2.id}`,
user1Cookies,
)
.expect(403);
// User 2 should not be able to access User 1's organization
await authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${testOrganization.id}`,
user2Cookies,
)
.expect(403);
});
it('should handle user with multiple organization memberships', async () => {
// Add testUser to testOrganization2 as well
await memberRepository.save({
user: testUser,
organization: testOrganization2,
role: 'member',
});
// User should now be able to access both organizations
await authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${testOrganization.id}`,
user1Cookies,
)
.expect(200);
await authHelper
.makeAuthenticatedRequest(
'get',
`/api/v1/organizations/${testOrganization2.id}`,
user1Cookies,
)
.expect(200);
});
});
describe('Edge Cases', () => {
it('should handle empty organization ID', () => {
return request(app.getHttpServer())
.get('/api/v1/organizations/')
.expect(403);
});
it('should handle non-existent routes', () => {
return request(app.getHttpServer())
.get('/api/v1/organizations/nonexistent/route')
.expect(404);
});
});
});