-
Notifications
You must be signed in to change notification settings - Fork 111
Add helper method for creating and uploading a Webflow Asset #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a60f8c
[draft] add asset create and upload utility helper
zplata 4f77dfb
fix asset upload to s3
zplata 86cfe2a
Add tests for asset upload utility method
zplata 9cb4037
fix testing command
zplata 5fcaa3c
review feedback cleanup
zplata 0a283f9
fix tests
zplata 37d33fb
bump version to 3.1.2
zplata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add tests for asset upload utility method
- Loading branch information
commit 86cfe2aa5b9b3c89debfecf471a484ca5a9ff98f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
require('jest-fetch-mock').enableMocks(); | ||
import { Client as AssetsUtilitiesClient } from "../../src/wrapper/AssetsUtilitiesClient"; | ||
import * as Webflow from "../../src/api"; | ||
import fetchMock from "jest-fetch-mock"; | ||
import crypto from "crypto"; | ||
import FormDataConstructor from 'form-data'; | ||
|
||
fetchMock.enableMocks(); | ||
|
||
describe("AssetsUtilitiesClient", () => { | ||
const mockOptions = { | ||
environment: () => "test-environment", | ||
accessToken: () => "test-access-token", | ||
}; | ||
|
||
const siteId = "test-site-id"; | ||
const mockUploadUrl = "https://mock-s3-upload-url.com"; | ||
const mockFileName = "test-file.txt"; | ||
const mockFileContent = "Hello, world!"; | ||
const mockFileBuffer = Buffer.from(mockFileContent); | ||
const mockFileHash = crypto.createHash("md5").update(mockFileBuffer).digest("hex"); | ||
|
||
let client: AssetsUtilitiesClient; | ||
|
||
beforeEach(() => { | ||
fetchMock.resetMocks(); | ||
client = new AssetsUtilitiesClient(mockOptions); | ||
}); | ||
|
||
it("should throw an error if it cannot fetch the asset successfully", async () => { | ||
const invalidUrl = "https://invalid-url.com"; | ||
|
||
// Mock the fetch response to simulate a failure | ||
fetchMock.mockResponseOnce("", { status: 404, statusText: "Not Found" }); | ||
|
||
await expect(client["_getBufferFromUrl"](invalidUrl)).rejects.toThrow( | ||
"Failed to fetch asset from URL: https://invalid-url.com. Status: 404 Not Found" | ||
); | ||
|
||
// Ensure fetch was called with the correct URL | ||
expect(fetchMock).toHaveBeenCalledWith(invalidUrl); | ||
}); | ||
|
||
it("should throw an error for invalid file input", async () => { | ||
await expect(client.createAndUpload(siteId, { | ||
fileName: mockFileName, | ||
file: null as unknown as ArrayBuffer, // Invalid file | ||
})).rejects.toThrow("Invalid file"); | ||
}); | ||
|
||
it("should throw an error if it fails to create Webflow Asset metadata", async () => { | ||
// Mock the Webflow API to throw an error | ||
jest.spyOn(client, "create").mockRejectedValue(new Error("Webflow API error")); | ||
|
||
await expect(client.createAndUpload(siteId, { | ||
fileName: mockFileName, | ||
file: mockFileBuffer.buffer, // Pass ArrayBuffer | ||
})).rejects.toThrow("Failed to create Asset metadata in Webflow: Webflow API error"); | ||
|
||
// Ensure the create method was called | ||
expect(client.create).toHaveBeenCalledWith(siteId, expect.objectContaining({ | ||
fileName: mockFileName, | ||
fileHash: expect.any(String), | ||
}), undefined); | ||
}); | ||
|
||
it("should throw an error if it fails to upload to S3", async () => { | ||
// Mock the Webflow API response for creating asset metadata | ||
const mockCreateResponse = { | ||
uploadUrl: mockUploadUrl, | ||
uploadDetails: { | ||
"xAmzAlgorithm": "AWS4-HMAC-SHA256", | ||
"xAmzDate": "20231010T000000Z", | ||
"xAmzCredential": "mock-credential", | ||
"xAmzSignature": "mock-signature", | ||
"successActionStatus": "201", | ||
"contentType": "text/plain", | ||
}, | ||
}; | ||
jest.spyOn(client, "create").mockResolvedValue(mockCreateResponse as Webflow.AssetUpload); | ||
|
||
// Mock the S3 upload response to fail | ||
fetchMock.mockResponseOnce("S3 upload error", { status: 500 }); | ||
|
||
await expect(client.createAndUpload(siteId, { | ||
fileName: mockFileName, | ||
file: mockFileBuffer.buffer, // Pass ArrayBuffer | ||
})).rejects.toThrow("Failed to upload to S3. Status: 500, Response: S3 upload error"); | ||
|
||
// Ensure the S3 upload was attempted | ||
expect(fetchMock).toHaveBeenCalledWith(mockUploadUrl, expect.objectContaining({ | ||
method: "POST", | ||
body: expect.any(FormDataConstructor), | ||
})); | ||
}); | ||
|
||
it("should create and upload a file from an ArrayBuffer", async () => { | ||
// Mock the Webflow API response for creating asset metadata | ||
const mockCreateResponse = { | ||
uploadUrl: mockUploadUrl, | ||
uploadDetails: { | ||
"xAmzAlgorithm": "AWS4-HMAC-SHA256", | ||
"xAmzDate": "20231010T000000Z", | ||
"xAmzCredential": "mock-credential", | ||
"xAmzSignature": "mock-signature", | ||
"successActionStatus": "201", | ||
"contentType": "text/plain", | ||
}, | ||
}; | ||
jest.spyOn(client, "create").mockResolvedValue(mockCreateResponse as Webflow.AssetUpload); | ||
|
||
// Mock the S3 upload response | ||
fetchMock.mockResponseOnce(JSON.stringify({ success: true }), { status: 201 }); | ||
|
||
const result = await client.createAndUpload(siteId, { | ||
fileName: mockFileName, | ||
file: mockFileBuffer.buffer, // Pass ArrayBuffer | ||
}); | ||
|
||
// Assertions | ||
expect(client.create).toHaveBeenCalledWith(siteId, expect.objectContaining({ | ||
fileName: mockFileName, | ||
fileHash: expect.any(String), | ||
}), undefined); | ||
|
||
expect(fetchMock).toHaveBeenCalledWith(mockUploadUrl, expect.objectContaining({ | ||
method: "POST", | ||
body: expect.any(FormDataConstructor), | ||
})); | ||
|
||
expect(result).toEqual(mockCreateResponse); | ||
}); | ||
|
||
it("should create and upload a file from a URL", async () => { | ||
// Mock the file fetch response (first fetch call) | ||
fetchMock.mockResponseOnce(mockFileContent); | ||
|
||
// Mock the Webflow API response for creating asset metadata | ||
const mockCreateResponse = { | ||
uploadUrl: mockUploadUrl, | ||
uploadDetails: { | ||
"xAmzAlgorithm": "AWS4-HMAC-SHA256", | ||
"xAmzDate": "20231010T000000Z", | ||
"xAmzCredential": "mock-credential", | ||
"xAmzSignature": "mock-signature", | ||
"successActionStatus": "201", | ||
"contentType": "text/plain", | ||
}, | ||
}; | ||
jest.spyOn(client, "create").mockResolvedValue(mockCreateResponse as Webflow.AssetUpload); | ||
|
||
// Mock the S3 upload response (second fetch call) | ||
fetchMock.mockResponseOnce(JSON.stringify({ success: true }), { status: 201 }); | ||
|
||
const result = await client.createAndUpload(siteId, { | ||
fileName: mockFileName, | ||
file: "https://mock-file-url.com", // Pass asset URL | ||
}); | ||
|
||
// Assertions for the file fetch | ||
expect(fetchMock).toHaveBeenNthCalledWith(1, "https://mock-file-url.com"); | ||
|
||
// Assertions for the Webflow API call | ||
expect(client.create).toHaveBeenCalledWith(siteId, { | ||
fileName: mockFileName, | ||
fileHash: mockFileHash, | ||
}, undefined); | ||
|
||
// Assertions for the S3 upload | ||
expect(fetchMock).toHaveBeenNthCalledWith(2, mockUploadUrl, expect.objectContaining({ | ||
method: "POST", | ||
body: expect.any(FormDataConstructor), | ||
})); | ||
|
||
expect(result).toEqual(mockCreateResponse); | ||
}); | ||
}); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.