Skip to content

Commit

Permalink
fix upload with empty path (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
chandra-tacc authored Feb 11, 2025
1 parent 83dabae commit 0cd2c59
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
82 changes: 82 additions & 0 deletions client/src/hooks/datafiles/mutations/useUpload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { uploadUtil } from './useUpload';
import { apiClient } from 'utils/apiClient';
import Cookies from 'js-cookie';
import { vi, Mock } from 'vitest';

vi.mock('utils/apiClient');
Cookies.get = vi.fn().mockImplementation(() => 'test-cookie');

describe('uploadUtil', () => {
const api = 'tapis';
const scheme = 'private';
const system = 'apcd.submissions';
const file = new FormData();
file.append(
'uploaded_file',
new Blob(['test content'], { type: 'text/plain' })
);

beforeEach(() => {
vi.clearAllMocks();
(Cookies.get as Mock).mockReturnValue('mockCsrfToken');
});

it('should construct the correct URL when path is an empty string', async () => {
vi.mocked(apiClient.post).mockResolvedValue({
data: { file: 'mockFile', path: '' },
});

await uploadUtil({ api, scheme, system, path: '', file });

expect(apiClient.post).toHaveBeenCalledWith(
'/api/datafiles/tapis/upload/private/apcd.submissions/',
expect.any(FormData),
expect.objectContaining({
headers: { 'X-CSRFToken': 'mockCsrfToken' },
withCredentials: true,
})
);
});

it('should not call post when path is "/"', async () => {
vi.mocked(apiClient.post).mockResolvedValue({
data: { file: 'mockFile', path: '' },
});
await uploadUtil({ api, scheme, system, path: '/', file });
expect(apiClient.post).not.toHaveBeenCalled();
});

it('should construct the correct URL when path is regular folder', async () => {
vi.mocked(apiClient.post).mockResolvedValue({
data: { file: 'mockFile', path: '/subdir' },
});

await uploadUtil({ api, scheme, system, path: 'subdir', file });

expect(apiClient.post).toHaveBeenCalledWith(
'/api/datafiles/tapis/upload/private/apcd.submissions/subdir/',
expect.any(FormData),
expect.objectContaining({
headers: { 'X-CSRFToken': 'mockCsrfToken' },
withCredentials: true,
})
);
});

it('should normalize multiple slashes in the URL', async () => {
vi.mocked(apiClient.post).mockResolvedValue({
data: { file: 'mockFile', path: '/nested/path' },
});

await uploadUtil({ api, scheme, system, path: 'nested//path', file });

expect(apiClient.post).toHaveBeenCalledWith(
'/api/datafiles/tapis/upload/private/apcd.submissions/nested/path/',
expect.any(FormData),
expect.objectContaining({
headers: { 'X-CSRFToken': 'mockCsrfToken' },
withCredentials: true,
})
);
});
});
2 changes: 1 addition & 1 deletion client/src/hooks/datafiles/mutations/useUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function uploadUtil({
const fileField = file.get('uploaded_file') as Blob;
formData.append('uploaded_file', fileField);
let url = `/api/datafiles/${api}/upload/${scheme}/${system}/${apiPath}/`;
url.replace(/\/{2,}/g, '/');
url = url.replace(/\/{2,}/g, '/');
const response = await apiClient.post(url, formData, {
headers: {
'X-CSRFToken': Cookies.get('csrftoken') || '',
Expand Down

0 comments on commit 0cd2c59

Please sign in to comment.