|
| 1 | +import request from "supertest"; |
| 2 | + |
| 3 | +import app from "../../app"; |
| 4 | +import * as DocumentService from "../../services/Document"; |
| 5 | +import { |
| 6 | + createTestContainer, |
| 7 | + removeTestContainer, |
| 8 | +} from "../helper/testContainer"; |
| 9 | + |
| 10 | +const authUser = { id: 1, role: "admin" }; |
| 11 | + |
| 12 | +jest.mock("../../middlewares/bo-check-JWT", () => { |
| 13 | + return (req: any, _res: any, next: any) => { |
| 14 | + req.decoded = authUser; |
| 15 | + next(); |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +jest.mock("../../services/Document", () => ({ |
| 20 | + getFile: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +beforeAll(async () => { |
| 24 | + await createTestContainer(); |
| 25 | +}); |
| 26 | + |
| 27 | +afterAll(async () => { |
| 28 | + await removeTestContainer(); |
| 29 | +}); |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + jest.clearAllMocks(); |
| 33 | +}); |
| 34 | + |
| 35 | +describe("GET /documents/admin/:uuid", () => { |
| 36 | + it("devrait retourner 404 si le fichier n'existe pas", async () => { |
| 37 | + (DocumentService.getFile as jest.Mock).mockResolvedValue(null); |
| 38 | + |
| 39 | + const response = await request(app).get( |
| 40 | + "/documents/admin/550e8400-e29b-41d4-a716-446655440000", |
| 41 | + ); |
| 42 | + |
| 43 | + expect(response.status).toBe(404); |
| 44 | + expect(DocumentService.getFile).toHaveBeenCalledWith( |
| 45 | + "550e8400-e29b-41d4-a716-446655440000", |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it("devrait retourner le fichier avec les bons headers", async () => { |
| 50 | + const fileContent = Buffer.from("contenu admin du fichier"); |
| 51 | + (DocumentService.getFile as jest.Mock).mockResolvedValue({ |
| 52 | + category: "declaration", |
| 53 | + file: fileContent, |
| 54 | + filename: "document-admin.pdf", |
| 55 | + mimeType: "application/pdf", |
| 56 | + userId: String(authUser.id), |
| 57 | + uuid: "550e8400-e29b-41d4-a716-446655440000", |
| 58 | + }); |
| 59 | + |
| 60 | + const response = await request(app).get( |
| 61 | + "/documents/admin/550e8400-e29b-41d4-a716-446655440000", |
| 62 | + ); |
| 63 | + |
| 64 | + expect(response.status).toBe(200); |
| 65 | + expect(response.headers["content-disposition"]).toBe( |
| 66 | + "attachment; filename=document-admin.pdf", |
| 67 | + ); |
| 68 | + expect(response.headers["content-type"]).toContain("text/plain"); |
| 69 | + expect(response.text).toBe("contenu admin du fichier"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("devrait propager l'erreur si getFile échoue", async () => { |
| 73 | + const error = new Error("Erreur S3"); |
| 74 | + (DocumentService.getFile as jest.Mock).mockRejectedValue(error); |
| 75 | + |
| 76 | + const response = await request(app).get( |
| 77 | + "/documents/admin/550e8400-e29b-41d4-a716-446655440000", |
| 78 | + ); |
| 79 | + |
| 80 | + expect(response.status).toBe(500); |
| 81 | + }); |
| 82 | +}); |
0 commit comments