|
| 1 | +import * as lambda from "lib/lambda"; |
| 2 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 3 | +import handler from "pages/api/log-uploader/backfill"; |
| 4 | + |
| 5 | +function mockRes() { |
| 6 | + const res: any = {}; |
| 7 | + res.status = jest.fn().mockReturnValue(res); |
| 8 | + res.json = jest.fn().mockReturnValue(res); |
| 9 | + return res as NextApiResponse & { |
| 10 | + status: jest.Mock; |
| 11 | + json: jest.Mock; |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +function mockReq(overrides: Partial<NextApiRequest> = {}): NextApiRequest { |
| 16 | + return { |
| 17 | + method: "POST", |
| 18 | + headers: { authorization: "secret" }, |
| 19 | + body: { repo: "pytorch/executorch", job_id: 999, conclusion: "failure" }, |
| 20 | + ...overrides, |
| 21 | + } as NextApiRequest; |
| 22 | +} |
| 23 | + |
| 24 | +describe("/api/log-uploader/backfill", () => { |
| 25 | + let invoke: jest.SpyInstance; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + invoke = jest.spyOn(lambda, "invokeLogUploader").mockResolvedValue(); |
| 29 | + process.env.LOG_UPLOADER_BOT_KEY = "secret"; |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + jest.restoreAllMocks(); |
| 34 | + delete process.env.LOG_UPLOADER_BOT_KEY; |
| 35 | + }); |
| 36 | + |
| 37 | + test("queues an upload for an authorized request", async () => { |
| 38 | + const res = mockRes(); |
| 39 | + await handler(mockReq(), res); |
| 40 | + |
| 41 | + expect(invoke).toHaveBeenCalledWith({ |
| 42 | + repo: "pytorch/executorch", |
| 43 | + job_id: 999, |
| 44 | + conclusion: "failure", |
| 45 | + }); |
| 46 | + expect(res.status).toHaveBeenCalledWith(200); |
| 47 | + }); |
| 48 | + |
| 49 | + test("accepts a job_id that arrived as a string", async () => { |
| 50 | + const res = mockRes(); |
| 51 | + await handler( |
| 52 | + mockReq({ body: { repo: "pytorch/pytorch", job_id: "42" } }), |
| 53 | + res |
| 54 | + ); |
| 55 | + |
| 56 | + expect(invoke).toHaveBeenCalledWith( |
| 57 | + expect.objectContaining({ job_id: 42 }) |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + test("rejects a request with no credentials", async () => { |
| 62 | + const res = mockRes(); |
| 63 | + await handler(mockReq({ headers: {} }), res); |
| 64 | + |
| 65 | + expect(invoke).not.toHaveBeenCalled(); |
| 66 | + expect(res.status).toHaveBeenCalledWith(403); |
| 67 | + }); |
| 68 | + |
| 69 | + test("rejects a request with the wrong credentials", async () => { |
| 70 | + const res = mockRes(); |
| 71 | + await handler(mockReq({ headers: { authorization: "guess" } }), res); |
| 72 | + |
| 73 | + expect(invoke).not.toHaveBeenCalled(); |
| 74 | + expect(res.status).toHaveBeenCalledWith(403); |
| 75 | + }); |
| 76 | + |
| 77 | + test("an unset key does not become an open endpoint", async () => { |
| 78 | + // Otherwise a missing env var would silently reproduce the unauthenticated |
| 79 | + // API Gateway endpoint this route exists to replace. |
| 80 | + delete process.env.LOG_UPLOADER_BOT_KEY; |
| 81 | + const res = mockRes(); |
| 82 | + await handler(mockReq({ headers: {} }), res); |
| 83 | + |
| 84 | + expect(invoke).not.toHaveBeenCalled(); |
| 85 | + expect(res.status).toHaveBeenCalledWith(403); |
| 86 | + }); |
| 87 | + |
| 88 | + test("rejects anything but POST", async () => { |
| 89 | + const res = mockRes(); |
| 90 | + await handler(mockReq({ method: "GET" }), res); |
| 91 | + |
| 92 | + expect(invoke).not.toHaveBeenCalled(); |
| 93 | + expect(res.status).toHaveBeenCalledWith(405); |
| 94 | + }); |
| 95 | + |
| 96 | + test.each([ |
| 97 | + ["a missing repo", { job_id: 1 }], |
| 98 | + ["a repo with no owner", { repo: "pytorch", job_id: 1 }], |
| 99 | + ["a missing job_id", { repo: "pytorch/pytorch" }], |
| 100 | + ["a non-numeric job_id", { repo: "pytorch/pytorch", job_id: "abc" }], |
| 101 | + ["a negative job_id", { repo: "pytorch/pytorch", job_id: -1 }], |
| 102 | + ])("rejects %s", async (_label, body) => { |
| 103 | + const res = mockRes(); |
| 104 | + await handler(mockReq({ body }), res); |
| 105 | + |
| 106 | + expect(invoke).not.toHaveBeenCalled(); |
| 107 | + expect(res.status).toHaveBeenCalledWith(400); |
| 108 | + }); |
| 109 | + |
| 110 | + test("an empty body does not throw", async () => { |
| 111 | + const res = mockRes(); |
| 112 | + await handler(mockReq({ body: undefined }), res); |
| 113 | + |
| 114 | + expect(res.status).toHaveBeenCalledWith(400); |
| 115 | + }); |
| 116 | + |
| 117 | + test("reports a failure to reach the uploader", async () => { |
| 118 | + invoke.mockRejectedValue(new Error("AccessDeniedException")); |
| 119 | + const res = mockRes(); |
| 120 | + await handler(mockReq(), res); |
| 121 | + |
| 122 | + expect(res.status).toHaveBeenCalledWith(502); |
| 123 | + }); |
| 124 | +}); |
0 commit comments