|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 3 | +import axios from "axios"; |
| 4 | +import fs from "fs"; |
| 5 | +import path from "path"; |
| 6 | +import handler from "pages/api/user/avatar"; // Update with actual path |
| 7 | + |
| 8 | +// Mock dependencies |
| 9 | +vi.mock("axios"); |
| 10 | +vi.mock("fs"); |
| 11 | +vi.mock("path"); |
| 12 | + |
| 13 | +describe("Avatar API Handler", () => { |
| 14 | + let req: Partial<NextApiRequest>; |
| 15 | + let res: Partial<NextApiResponse>; |
| 16 | + let statusMock: ReturnType<typeof vi.fn>; |
| 17 | + let jsonMock: ReturnType<typeof vi.fn>; |
| 18 | + let setHeaderMock: ReturnType<typeof vi.fn>; |
| 19 | + let endMock: ReturnType<typeof vi.fn>; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + // Reset mocks before each test |
| 23 | + vi.clearAllMocks(); |
| 24 | + |
| 25 | + // Setup response mocks |
| 26 | + statusMock = vi.fn().mockReturnThis(); |
| 27 | + jsonMock = vi.fn().mockReturnThis(); |
| 28 | + setHeaderMock = vi.fn(); |
| 29 | + endMock = vi.fn(); |
| 30 | + |
| 31 | + req = { |
| 32 | + query: {}, |
| 33 | + }; |
| 34 | + |
| 35 | + res = { |
| 36 | + status: statusMock, |
| 37 | + json: jsonMock, |
| 38 | + setHeader: setHeaderMock, |
| 39 | + end: endMock, |
| 40 | + }; |
| 41 | + |
| 42 | + // Setup environment variable |
| 43 | + process.env.NEXT_PUBLIC_SERVER_URL = "https://api.example.com"; |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + vi.restoreAllMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return 400 error when userId is missing", async () => { |
| 51 | + req.query = {}; |
| 52 | + |
| 53 | + await handler(req as NextApiRequest, res as NextApiResponse); |
| 54 | + |
| 55 | + expect(statusMock).toHaveBeenCalledWith(400); |
| 56 | + expect(jsonMock).toHaveBeenCalledWith({ error: "Missing userId" }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should fetch and return profile picture successfully", async () => { |
| 60 | + const mockImageData = Buffer.from("fake-image-data"); |
| 61 | + const mockAxiosResponse = { |
| 62 | + data: mockImageData, |
| 63 | + headers: { |
| 64 | + "content-type": "image/jpeg", |
| 65 | + }, |
| 66 | + }; |
| 67 | + |
| 68 | + req.query = { userId: "123" }; |
| 69 | + vi.mocked(axios.get).mockResolvedValue(mockAxiosResponse); |
| 70 | + |
| 71 | + await handler(req as NextApiRequest, res as NextApiResponse); |
| 72 | + |
| 73 | + expect(axios.get).toHaveBeenCalledWith( |
| 74 | + "https://api.example.com/user/profile-picture?userId=123", |
| 75 | + { responseType: "arraybuffer" }, |
| 76 | + ); |
| 77 | + expect(setHeaderMock).toHaveBeenCalledWith("Content-Type", "image/jpeg"); |
| 78 | + expect(setHeaderMock).toHaveBeenCalledWith( |
| 79 | + "Cache-Control", |
| 80 | + "public, max-age=3600", |
| 81 | + ); |
| 82 | + expect(endMock).toHaveBeenCalledWith(expect.any(Buffer), "binary"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should use default content-type when not provided in response", async () => { |
| 86 | + const mockImageData = Buffer.from("fake-image-data"); |
| 87 | + const mockAxiosResponse = { |
| 88 | + data: mockImageData, |
| 89 | + headers: {}, |
| 90 | + }; |
| 91 | + |
| 92 | + req.query = { userId: "123" }; |
| 93 | + vi.mocked(axios.get).mockResolvedValue(mockAxiosResponse); |
| 94 | + |
| 95 | + await handler(req as NextApiRequest, res as NextApiResponse); |
| 96 | + |
| 97 | + expect(setHeaderMock).toHaveBeenCalledWith("Content-Type", "image/png"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should return fallback avatar when backend returns 404", async () => { |
| 101 | + const mockFallbackImage = Buffer.from("fallback-image-data"); |
| 102 | + const mockError = { |
| 103 | + response: { |
| 104 | + status: 404, |
| 105 | + }, |
| 106 | + }; |
| 107 | + |
| 108 | + req.query = { userId: "123" }; |
| 109 | + vi.mocked(axios.get).mockRejectedValue(mockError); |
| 110 | + vi.mocked(path.join).mockReturnValue("/mock/path/to/img_avatar.png"); |
| 111 | + vi.mocked(fs.readFileSync).mockReturnValue(mockFallbackImage); |
| 112 | + |
| 113 | + await handler(req as NextApiRequest, res as NextApiResponse); |
| 114 | + |
| 115 | + expect(path.join).toHaveBeenCalledWith( |
| 116 | + process.cwd(), |
| 117 | + "public", |
| 118 | + "img_avatar.png", |
| 119 | + ); |
| 120 | + expect(fs.readFileSync).toHaveBeenCalledWith( |
| 121 | + "/mock/path/to/img_avatar.png", |
| 122 | + ); |
| 123 | + expect(setHeaderMock).toHaveBeenCalledWith("Content-Type", "image/png"); |
| 124 | + expect(setHeaderMock).toHaveBeenCalledWith( |
| 125 | + "Cache-Control", |
| 126 | + "public, max-age=3600", |
| 127 | + ); |
| 128 | + expect(endMock).toHaveBeenCalledWith(mockFallbackImage); |
| 129 | + }); |
| 130 | + |
| 131 | + it("should return 500 error when backend fails with non-404 error", async () => { |
| 132 | + const mockError = { |
| 133 | + response: { |
| 134 | + status: 500, |
| 135 | + }, |
| 136 | + }; |
| 137 | + |
| 138 | + req.query = { userId: "123" }; |
| 139 | + vi.mocked(axios.get).mockRejectedValue(mockError); |
| 140 | + |
| 141 | + await handler(req as NextApiRequest, res as NextApiResponse); |
| 142 | + |
| 143 | + expect(statusMock).toHaveBeenCalledWith(500); |
| 144 | + expect(jsonMock).toHaveBeenCalledWith({ error: "Unable to fetch avatar" }); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should return 500 error when no response status is available", async () => { |
| 148 | + const mockError = new Error("Network error"); |
| 149 | + |
| 150 | + req.query = { userId: "123" }; |
| 151 | + vi.mocked(axios.get).mockRejectedValue(mockError); |
| 152 | + |
| 153 | + await handler(req as NextApiRequest, res as NextApiResponse); |
| 154 | + |
| 155 | + expect(statusMock).toHaveBeenCalledWith(500); |
| 156 | + expect(jsonMock).toHaveBeenCalledWith({ error: "Unable to fetch avatar" }); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should handle userId as array and use first value", async () => { |
| 160 | + const mockImageData = Buffer.from("fake-image-data"); |
| 161 | + const mockAxiosResponse = { |
| 162 | + data: mockImageData, |
| 163 | + headers: { |
| 164 | + "content-type": "image/png", |
| 165 | + }, |
| 166 | + }; |
| 167 | + |
| 168 | + req.query = { userId: ["123", "456"] }; |
| 169 | + vi.mocked(axios.get).mockResolvedValue(mockAxiosResponse); |
| 170 | + |
| 171 | + await handler(req as NextApiRequest, res as NextApiResponse); |
| 172 | + |
| 173 | + expect(axios.get).toHaveBeenCalledWith( |
| 174 | + "https://api.example.com/user/profile-picture?userId=123,456", |
| 175 | + { responseType: "arraybuffer" }, |
| 176 | + ); |
| 177 | + }); |
| 178 | +}); |
0 commit comments