-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Nityam/fix private assets authorization #3968
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
raclim
merged 3 commits into
processing:develop
from
Nixxx19:nityam/fix-private-assets-authorization
Mar 9, 2026
+221
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
213 changes: 213 additions & 0 deletions
213
server/controllers/project.controller/__test__/getProjectAsset.test.js
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,213 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
| import { Request, Response } from 'jest-express'; | ||
| import axios from 'axios'; | ||
| import mime from 'mime'; | ||
| import Project from '../../../models/project'; | ||
| import { getProjectAsset } from '../../project.controller'; | ||
| import { resolvePathToFile } from '../../../utils/filePath'; | ||
|
|
||
| jest.mock('../../../models/project'); | ||
| jest.mock('../../../utils/filePath'); | ||
| jest.mock('mime'); | ||
| jest.mock('axios'); | ||
|
|
||
| describe('project.controller', () => { | ||
| describe('getProjectAsset()', () => { | ||
| let request; | ||
| let response; | ||
|
|
||
| beforeEach(() => { | ||
| request = new Request(); | ||
| response = new Response(); | ||
| response.send = jest.fn(); | ||
| response.status = jest.fn().mockReturnThis(); | ||
| response.set = jest.fn().mockReturnThis(); | ||
| request.setParams({ project_id: 'project-123', 0: 'image.png' }); | ||
| Project.findOne = jest.fn().mockReturnValue({ | ||
| populate: jest.fn().mockReturnThis(), | ||
| exec: jest.fn() | ||
| }); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| request.resetMocked(); | ||
| response.resetMocked(); | ||
| }); | ||
|
|
||
| it('returns 404 if project does not exist', async () => { | ||
| Project.findOne().populate().exec.mockResolvedValue(null); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(response.status).toHaveBeenCalledWith(404); | ||
| expect(response.send).toHaveBeenCalledWith({ | ||
| message: 'Project with that id does not exist' | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 403 if private project is accessed by unauthenticated user', async () => { | ||
| const project = { | ||
| _id: 'project-123', | ||
| visibility: 'Private', | ||
| user: { _id: { equals: jest.fn() } }, | ||
| files: [] | ||
| }; | ||
| request.user = undefined; | ||
|
|
||
| Project.findOne().populate().exec.mockResolvedValue(project); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(response.status).toHaveBeenCalledWith(403); | ||
| expect(response.send).toHaveBeenCalledWith({ | ||
| message: 'Project is private' | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 403 if private project is accessed by non-owner', async () => { | ||
| const ownerId = { equals: jest.fn().mockReturnValue(false) }; | ||
| const project = { | ||
| _id: 'project-123', | ||
| visibility: 'Private', | ||
| user: { _id: ownerId }, | ||
| files: [] | ||
| }; | ||
| request.user = { _id: 'other-user-id' }; | ||
|
|
||
| Project.findOne().populate().exec.mockResolvedValue(project); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(response.status).toHaveBeenCalledWith(403); | ||
| expect(response.send).toHaveBeenCalledWith({ | ||
| message: 'Project is private' | ||
| }); | ||
| expect(ownerId.equals).toHaveBeenCalledWith('other-user-id'); | ||
| }); | ||
|
|
||
| it('allows owner to access private project assets', async () => { | ||
| const ownerId = 'owner-123'; | ||
| const ownerIdObj = { equals: jest.fn().mockReturnValue(true) }; | ||
| const project = { | ||
| _id: 'project-123', | ||
| visibility: 'Private', | ||
| user: { _id: ownerIdObj }, | ||
| files: [] | ||
| }; | ||
| const resolvedFile = { | ||
| name: 'image.png', | ||
| content: Buffer.from('image content') | ||
| }; | ||
|
|
||
| request.user = { _id: ownerId }; | ||
| Project.findOne().populate().exec.mockResolvedValue(project); | ||
| resolvePathToFile.mockReturnValue(resolvedFile); | ||
| mime.getType.mockReturnValue('image/png'); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(ownerIdObj.equals).toHaveBeenCalledWith(ownerId); | ||
| expect(response.status).not.toHaveBeenCalledWith(403); | ||
| expect(response.set).toHaveBeenCalledWith('Content-Type', 'image/png'); | ||
| expect(response.send).toHaveBeenCalledWith(resolvedFile.content); | ||
| }); | ||
|
|
||
| it('allows anyone to access public project assets', async () => { | ||
| const project = { | ||
| _id: 'project-123', | ||
| visibility: 'Public', | ||
| user: { _id: { equals: jest.fn() } }, | ||
| files: [] | ||
| }; | ||
| const resolvedFile = { | ||
| name: 'image.png', | ||
| content: Buffer.from('image content') | ||
| }; | ||
|
|
||
| request.user = undefined; // unauthenticated | ||
| Project.findOne().populate().exec.mockResolvedValue(project); | ||
| resolvePathToFile.mockReturnValue(resolvedFile); | ||
| mime.getType.mockReturnValue('image/png'); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(response.status).not.toHaveBeenCalledWith(403); | ||
| expect(response.set).toHaveBeenCalledWith('Content-Type', 'image/png'); | ||
| expect(response.send).toHaveBeenCalledWith(resolvedFile.content); | ||
| }); | ||
|
|
||
| it('returns 404 if asset does not exist', async () => { | ||
| const project = { | ||
| _id: 'project-123', | ||
| visibility: 'Public', | ||
| user: { _id: { equals: jest.fn() } }, | ||
| files: [] | ||
| }; | ||
|
|
||
| Project.findOne().populate().exec.mockResolvedValue(project); | ||
| resolvePathToFile.mockReturnValue(null); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(response.status).toHaveBeenCalledWith(404); | ||
| expect(response.send).toHaveBeenCalledWith({ | ||
| message: 'Asset does not exist' | ||
| }); | ||
| }); | ||
|
|
||
| it('fetches and serves asset from URL when resolvedFile has url', async () => { | ||
| const project = { | ||
| _id: 'project-123', | ||
| visibility: 'Public', | ||
| user: { _id: { equals: jest.fn() } }, | ||
| files: [] | ||
| }; | ||
| const resolvedFile = { | ||
| name: 'image.png', | ||
| url: 'https://example.com/image.png' | ||
| }; | ||
| const imageData = Buffer.from('image data'); | ||
|
|
||
| Project.findOne().populate().exec.mockResolvedValue(project); | ||
| resolvePathToFile.mockReturnValue(resolvedFile); | ||
| mime.getType.mockReturnValue('image/png'); | ||
| axios.get.mockResolvedValue({ data: imageData }); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(axios.get).toHaveBeenCalledWith(resolvedFile.url, { | ||
| responseType: 'arraybuffer' | ||
| }); | ||
| expect(response.set).toHaveBeenCalledWith('Content-Type', 'image/png'); | ||
| expect(response.send).toHaveBeenCalledWith(imageData); | ||
| }); | ||
|
|
||
| it('returns 404 if fetching asset URL fails', async () => { | ||
| const project = { | ||
| _id: 'project-123', | ||
| visibility: 'Public', | ||
| user: { _id: { equals: jest.fn() } }, | ||
| files: [] | ||
| }; | ||
| const resolvedFile = { | ||
| name: 'image.png', | ||
| url: 'https://example.com/image.png' | ||
| }; | ||
|
|
||
| Project.findOne().populate().exec.mockResolvedValue(project); | ||
| resolvePathToFile.mockReturnValue(resolvedFile); | ||
| mime.getType.mockReturnValue('image/png'); | ||
| axios.get.mockRejectedValue(new Error('Network error')); | ||
|
|
||
| await getProjectAsset(request, response); | ||
|
|
||
| expect(response.status).toHaveBeenCalledWith(404); | ||
| expect(response.send).toHaveBeenCalledWith({ | ||
| message: 'Asset does not exist' | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
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.