-
Notifications
You must be signed in to change notification settings - Fork 13.3k
test: add tests for image upload with rotation and EXIF #39252
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
Open
jessicaschelly
wants to merge
3
commits into
develop
Choose a base branch
from
test/image-rotation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−0
Open
Changes from all commits
Commits
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
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
apps/meteor/tests/end-to-end/api/file-upload-image-rotation.ts
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,112 @@ | ||
| import path from 'path'; | ||
|
|
||
| import type { Credentials } from '@rocket.chat/api-client'; | ||
| import type { ImageAttachmentProps, IMessage, IRoom, IUser, SettingValue } from '@rocket.chat/core-typings'; | ||
| import { expect } from 'chai'; | ||
| import { after, before, describe, it } from 'mocha'; | ||
| import sharp from 'sharp'; | ||
|
|
||
| import { getCredentials, request } from '../../data/api-data'; | ||
| import { uploadFileToRC } from '../../data/file.helper'; | ||
| import { getSettingValueById, updateSetting } from '../../data/permissions.helper'; | ||
| import { createRoom, deleteRoom } from '../../data/rooms.helper'; | ||
| import type { IRequestConfig, TestUser } from '../../data/users.helper'; | ||
| import { createUser, deleteUser, login } from '../../data/users.helper'; | ||
|
|
||
| const testImagePath = path.join(__dirname, '../../mocks/files/exif-orientation-6.jpg'); | ||
| const testImageName = 'exif-orientation-6.jpg'; | ||
|
|
||
| const downloadBuffer = async (url: string, auth: Credentials): Promise<Buffer> => { | ||
| const response = await request.get(url).set(auth).buffer(true).expect(200); | ||
| return response.body as Buffer; | ||
| }; | ||
|
|
||
| describe('[File Upload - Image Rotation]', () => { | ||
| before((done) => getCredentials(done)); | ||
|
|
||
| let user: TestUser<IUser>; | ||
| const userPassword = `pass${Date.now()}`; | ||
| let userCredentials: Credentials; | ||
| let testRoom: IRoom; | ||
| let rotateImagesSetting: SettingValue; | ||
| let stripExifSetting: SettingValue; | ||
| let thumbnailsEnabledSetting: SettingValue; | ||
|
|
||
| before(async () => { | ||
| user = await createUser({ joinDefaultChannels: false, password: userPassword }); | ||
| userCredentials = await login(user.username, userPassword); | ||
| testRoom = (await createRoom({ type: 'p', name: `rotate-upload-${Date.now()}`, members: [user.username] })).body.group; | ||
|
|
||
| rotateImagesSetting = await getSettingValueById('FileUpload_RotateImages'); | ||
| stripExifSetting = await getSettingValueById('Message_Attachments_Strip_Exif'); | ||
| thumbnailsEnabledSetting = await getSettingValueById('Message_Attachments_Thumbnails_Enabled'); | ||
|
|
||
| await updateSetting('FileUpload_RotateImages', true); | ||
| await updateSetting('Message_Attachments_Strip_Exif', true); | ||
| await updateSetting('Message_Attachments_Thumbnails_Enabled', true); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await Promise.all([ | ||
| updateSetting('FileUpload_RotateImages', rotateImagesSetting), | ||
| updateSetting('Message_Attachments_Strip_Exif', stripExifSetting), | ||
| updateSetting('Message_Attachments_Thumbnails_Enabled', thumbnailsEnabledSetting), | ||
| testRoom ? deleteRoom({ type: 'p', roomId: testRoom._id }) : Promise.resolve(), | ||
| user ? deleteUser(user) : Promise.resolve(), | ||
| ]); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it('should rotate pixels, strip EXIF orientation, and generate thumbnail from rotated image', async () => { | ||
| const fixtureMetadata = await sharp(testImagePath).metadata(); | ||
| expect(fixtureMetadata.width).to.equal(719); | ||
| expect(fixtureMetadata.height).to.equal(479); | ||
| expect(fixtureMetadata.orientation).to.equal(6); | ||
|
|
||
| const requestConfig: IRequestConfig = { request, credentials: userCredentials }; | ||
| const { message } = await uploadFileToRC(testRoom._id, testImagePath, 'rotation-exif-test', requestConfig); | ||
| const uploadMessage = message as IMessage; | ||
|
|
||
| expect(uploadMessage).to.have.property('attachments'); | ||
| const attachment = uploadMessage.attachments?.find((item) => item.title === testImageName); | ||
| expect(attachment).to.be.an('object'); | ||
|
|
||
| const fileUrl = (attachment as { title_link?: string }).title_link; | ||
| const thumbUrl = (attachment as ImageAttachmentProps).image_url; | ||
|
|
||
| expect(fileUrl).to.be.a('string'); | ||
| expect(thumbUrl).to.be.a('string'); | ||
|
|
||
| const originalBuffer = await downloadBuffer(fileUrl as string, userCredentials); | ||
| const originalMetadata = await sharp(originalBuffer).metadata(); | ||
|
|
||
| expect(originalMetadata.width).to.equal(479); | ||
| expect(originalMetadata.height).to.equal(719); | ||
| expect(originalMetadata.exif).to.be.undefined; | ||
|
|
||
| const thumbBuffer = await downloadBuffer(thumbUrl as string, userCredentials); | ||
| const thumbMetadata = await sharp(thumbBuffer).metadata(); | ||
|
|
||
| expect(thumbMetadata.width).to.be.lessThan(thumbMetadata.height as number); | ||
| }); | ||
|
|
||
| it('should NOT rotate pixels when FileUpload_RotateImages is disabled', async () => { | ||
| await updateSetting('FileUpload_RotateImages', false); | ||
|
|
||
| const requestConfig: IRequestConfig = { request, credentials: userCredentials }; | ||
| const { message } = await uploadFileToRC(testRoom._id, testImagePath, 'no-rotation-test', requestConfig); | ||
| const uploadMessage = message as IMessage; | ||
|
|
||
| expect(uploadMessage).to.have.property('attachments'); | ||
| const attachment = uploadMessage.attachments?.find((item) => item.title === testImageName); | ||
| expect(attachment).to.be.an('object'); | ||
|
|
||
| const fileUrl = (attachment as { title_link?: string }).title_link; | ||
| expect(fileUrl).to.be.a('string'); | ||
|
|
||
| const originalBuffer = await downloadBuffer(fileUrl as string, userCredentials); | ||
| const originalMetadata = await sharp(originalBuffer).metadata(); | ||
|
|
||
| expect(originalMetadata.width).to.equal(719); | ||
| expect(originalMetadata.height).to.equal(479); | ||
| }); | ||
| }); | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question, do we need the
debounceforupdateSetting?Or
await updateSetting('FileUpload_RotateImages', true, false);would work as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, i think using
updateSetting('FileUpload_RotateImages', true, false)could work, but could be a risk for flaky tests if the setting doesn't propagate before the file upload runs, wdyt?