|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +import request from 'supertest'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +import { bootstrap } from '../../../lib/app.js'; |
| 8 | +import mongooseService from '../../../lib/services/mongoose.js'; |
| 9 | +import config from '../../../config/index.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Integration tests for organization routes. |
| 13 | + * Verifies that authenticated users can access organization endpoints |
| 14 | + * without receiving 403 errors. |
| 15 | + */ |
| 16 | +describe('Organizations integration tests:', () => { |
| 17 | + let UserService; |
| 18 | + let OrganizationsRepository; |
| 19 | + let MembershipRepository; |
| 20 | + let agent; |
| 21 | + |
| 22 | + // Store original config |
| 23 | + const originalOrganizations = { ...config.organizations }; |
| 24 | + |
| 25 | + /** |
| 26 | + * @description Clean up a user and their associated organizations/memberships. |
| 27 | + * @param {Object} user - The user object to clean up. |
| 28 | + */ |
| 29 | + const cleanupUser = async (user) => { |
| 30 | + if (!user) return; |
| 31 | + try { |
| 32 | + const memberships = await MembershipRepository.list({ userId: user.id || user._id }); |
| 33 | + for (const m of memberships) { |
| 34 | + const orgId = m.organizationId._id || m.organizationId; |
| 35 | + await MembershipRepository.deleteMany({ organizationId: orgId }); |
| 36 | + await OrganizationsRepository.deleteMany({ _id: orgId }); |
| 37 | + } |
| 38 | + await UserService.remove(user); |
| 39 | + } catch (_) { /* cleanup — ignore errors */ } |
| 40 | + }; |
| 41 | + |
| 42 | + // init |
| 43 | + beforeAll(async () => { |
| 44 | + try { |
| 45 | + const init = await bootstrap(); |
| 46 | + UserService = (await import(path.resolve('./modules/users/services/users.service.js'))).default; |
| 47 | + OrganizationsRepository = (await import(path.resolve('./modules/organizations/repositories/organizations.repository.js'))).default; |
| 48 | + MembershipRepository = (await import(path.resolve('./modules/organizations/repositories/organizations.membership.repository.js'))).default; |
| 49 | + agent = request.agent(init.app); |
| 50 | + } catch (err) { |
| 51 | + console.log(err); |
| 52 | + expect(err).toBeFalsy(); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + describe('GET /api/organizations', () => { |
| 57 | + let user; |
| 58 | + |
| 59 | + beforeAll(async () => { |
| 60 | + config.organizations = { enabled: true, autoCreate: true, domainMatching: false }; |
| 61 | + try { |
| 62 | + const result = await agent |
| 63 | + .post('/api/auth/signup') |
| 64 | + .send({ |
| 65 | + firstName: 'OrgList', |
| 66 | + lastName: 'User', |
| 67 | + email: 'orglist@test.com', |
| 68 | + password: 'W@os.jsI$Aw3$0m3', |
| 69 | + provider: 'local', |
| 70 | + }) |
| 71 | + .expect(200); |
| 72 | + user = result.body.user; |
| 73 | + } catch (err) { |
| 74 | + console.log(err); |
| 75 | + expect(err).toBeFalsy(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + test('should return 200 and an array for an authenticated user', async () => { |
| 80 | + try { |
| 81 | + const result = await agent.get('/api/organizations').expect(200); |
| 82 | + |
| 83 | + expect(result.body.type).toBe('success'); |
| 84 | + expect(result.body.message).toBe('organization list'); |
| 85 | + expect(result.body.data).toBeInstanceOf(Array); |
| 86 | + } catch (err) { |
| 87 | + console.log(err); |
| 88 | + expect(err).toBeFalsy(); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + afterAll(async () => { |
| 93 | + await cleanupUser(user); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + // Mongoose disconnect |
| 98 | + afterAll(async () => { |
| 99 | + config.organizations = { ...originalOrganizations }; |
| 100 | + try { |
| 101 | + await mongooseService.disconnect(); |
| 102 | + } catch (err) { |
| 103 | + console.log(err); |
| 104 | + expect(err).toBeFalsy(); |
| 105 | + } |
| 106 | + }); |
| 107 | +}); |
0 commit comments