|
1 | | -import { YepCodeApi } from "../../src/api/yepcodeApi"; |
2 | | -import { StorageObject } from "../../src/api/types"; |
| 1 | +import { YepCodeApi, YepCodeApiError } from "../../src/api/yepcodeApi"; |
| 2 | +import { SignedUrl, StorageObject } from "../../src/api/types"; |
3 | 3 | import fs, { createWriteStream, readFileSync } from "fs"; |
4 | 4 | import path from "path"; |
5 | 5 | import { Readable } from "stream"; |
@@ -113,4 +113,68 @@ describe.skip("YepCodeApi", () => { |
113 | 113 | await verifyDownloadedFile(result, downloadedFile, testFilePath); |
114 | 114 | }); |
115 | 115 | }); |
| 116 | + |
| 117 | + describe("createSignedUrl", () => { |
| 118 | + it("should return a signed url with the default expiry", async () => { |
| 119 | + const file: File = new File([readFileSync(testFilePath)], testName); |
| 120 | + await api.createObject({ name: testName, file }); |
| 121 | + |
| 122 | + const result: SignedUrl = await api.createSignedUrl({ path: testName }); |
| 123 | + |
| 124 | + expect(typeof result.url).toBe("string"); |
| 125 | + expect(result.url.length).toBeGreaterThan(0); |
| 126 | + expect(result.path).toBe(testName); |
| 127 | + |
| 128 | + const expiresAt = new Date(result.expiresAt).getTime(); |
| 129 | + const expectedExpiry = Date.now() + 3600 * 1000; |
| 130 | + expect(Math.abs(expiresAt - expectedExpiry)).toBeLessThan(60 * 1000); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should return a signed url with a custom expiry", async () => { |
| 134 | + const file: File = new File([readFileSync(testFilePath)], testName); |
| 135 | + await api.createObject({ name: testName, file }); |
| 136 | + |
| 137 | + const result: SignedUrl = await api.createSignedUrl({ |
| 138 | + path: testName, |
| 139 | + expiresInSeconds: 60, |
| 140 | + }); |
| 141 | + |
| 142 | + const expiresAt = new Date(result.expiresAt).getTime(); |
| 143 | + const expectedExpiry = Date.now() + 60 * 1000; |
| 144 | + expect(Math.abs(expiresAt - expectedExpiry)).toBeLessThan(30 * 1000); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should return content matching the original file when fetched", async () => { |
| 148 | + const file: File = new File([readFileSync(testFilePath)], testName); |
| 149 | + await api.createObject({ name: testName, file }); |
| 150 | + |
| 151 | + const { url } = await api.createSignedUrl({ path: testName }); |
| 152 | + |
| 153 | + const response = await fetch(url); |
| 154 | + expect(response.ok).toBe(true); |
| 155 | + const body = await response.text(); |
| 156 | + expect(body).toBe(readFileSync(testFilePath, "utf8")); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should throw a 404 when the file does not exist", async () => { |
| 160 | + await expect( |
| 161 | + api.createSignedUrl({ path: "does-not-exist.txt" }) |
| 162 | + ).rejects.toMatchObject({ |
| 163 | + name: "YepCodeApiError", |
| 164 | + status: 404, |
| 165 | + } as Partial<YepCodeApiError>); |
| 166 | + }); |
| 167 | + |
| 168 | + it("should throw a 400 when expiresInSeconds is out of range", async () => { |
| 169 | + const file: File = new File([readFileSync(testFilePath)], testName); |
| 170 | + await api.createObject({ name: testName, file }); |
| 171 | + |
| 172 | + await expect( |
| 173 | + api.createSignedUrl({ path: testName, expiresInSeconds: 999999 }) |
| 174 | + ).rejects.toMatchObject({ |
| 175 | + name: "YepCodeApiError", |
| 176 | + status: 400, |
| 177 | + } as Partial<YepCodeApiError>); |
| 178 | + }); |
| 179 | + }); |
116 | 180 | }); |
0 commit comments