|
| 1 | +import { createMember, createOwner } from '../shared/db/users'; |
| 2 | +import type { SuperAgentTest } from '../shared/types'; |
| 3 | +import { setupTestServer } from '../shared/utils'; |
| 4 | + |
| 5 | +jest.mock('fs/promises', () => ({ |
| 6 | + readFile: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +import { readFile } from 'fs/promises'; |
| 10 | +const mockReadFile = readFile as jest.MockedFunction<typeof readFile>; |
| 11 | + |
| 12 | +describe('ThirdPartyLicensesController', () => { |
| 13 | + const testServer = setupTestServer({ endpointGroups: ['third-party-licenses'] }); |
| 14 | + let ownerAgent: SuperAgentTest; |
| 15 | + let memberAgent: SuperAgentTest; |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + const owner = await createOwner(); |
| 19 | + const member = await createMember(); |
| 20 | + ownerAgent = testServer.authAgentFor(owner); |
| 21 | + memberAgent = testServer.authAgentFor(member); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('GET /third-party-licenses', () => { |
| 25 | + beforeEach(() => { |
| 26 | + jest.resetAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should require authentication', async () => { |
| 30 | + await testServer.authlessAgent.get('/third-party-licenses').expect(401); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('when license file exists', () => { |
| 34 | + beforeEach(() => { |
| 35 | + mockReadFile.mockResolvedValue('# Third Party Licenses\n\nSome license content...'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should allow authenticated owner to get third-party licenses', async () => { |
| 39 | + const response = await ownerAgent.get('/third-party-licenses'); |
| 40 | + expect(response.status).toBe(200); |
| 41 | + expect(response.headers['content-type']).toMatch(/text\/markdown/); |
| 42 | + expect(response.text).toBe('# Third Party Licenses\n\nSome license content...'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should allow authenticated member to get third-party licenses', async () => { |
| 46 | + const response = await memberAgent.get('/third-party-licenses'); |
| 47 | + expect(response.status).toBe(200); |
| 48 | + expect(response.headers['content-type']).toMatch(/text\/markdown/); |
| 49 | + expect(response.text).toBe('# Third Party Licenses\n\nSome license content...'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('when license file does not exist', () => { |
| 54 | + beforeEach(() => { |
| 55 | + mockReadFile.mockRejectedValue(new Error('ENOENT: no such file or directory')); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return 404 for authenticated owner', async () => { |
| 59 | + const response = await ownerAgent.get('/third-party-licenses'); |
| 60 | + expect(response.status).toBe(404); |
| 61 | + expect(response.text).toBe('Third-party licenses file not found'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return 404 for authenticated member', async () => { |
| 65 | + const response = await memberAgent.get('/third-party-licenses'); |
| 66 | + expect(response.status).toBe(404); |
| 67 | + expect(response.text).toBe('Third-party licenses file not found'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('when file read fails with other errors', () => { |
| 72 | + beforeEach(() => { |
| 73 | + mockReadFile.mockRejectedValue(new Error('EACCES: permission denied')); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return 404 for permission errors', async () => { |
| 77 | + const response = await ownerAgent.get('/third-party-licenses'); |
| 78 | + expect(response.status).toBe(404); |
| 79 | + expect(response.text).toBe('Third-party licenses file not found'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('file path resolution', () => { |
| 84 | + it('should request the correct file path', async () => { |
| 85 | + mockReadFile.mockResolvedValue('test content'); |
| 86 | + |
| 87 | + await ownerAgent.get('/third-party-licenses'); |
| 88 | + |
| 89 | + expect(mockReadFile).toHaveBeenCalledWith( |
| 90 | + expect.stringMatching(/THIRD_PARTY_LICENSES\.md$/), |
| 91 | + 'utf-8', |
| 92 | + ); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments