|
| 1 | +import { formatAttachmentUrl } from './formatAttachmentUrl'; |
| 2 | +import { store as reduxStore } from '../../store/auxStore'; |
| 3 | + |
| 4 | +jest.mock('../../store/auxStore', () => ({ |
| 5 | + store: { |
| 6 | + getState: jest.fn() |
| 7 | + } |
| 8 | +})); |
| 9 | + |
| 10 | +const mockedGetState = reduxStore.getState as jest.Mock; |
| 11 | + |
| 12 | +const SERVER = 'https://open.rocket.chat'; |
| 13 | +const USER_ID = 'userId'; |
| 14 | +const TOKEN = 'token'; |
| 15 | + |
| 16 | +const mockSettings = ({ protectFiles = false, cdnPrefix = '' } = {}) => |
| 17 | + mockedGetState.mockReturnValue({ settings: { FileUpload_ProtectFiles: protectFiles, CDN_PREFIX: cdnPrefix } }); |
| 18 | + |
| 19 | +describe('formatAttachmentUrl', () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + // The server builds the path with `encodeURI(file.name)`, which leaves `#` raw — the url would otherwise be cut |
| 25 | + // at the fragment and the server would receive `/file-upload/1/a%20video%20`. |
| 26 | + describe('raw `#` in the filename', () => { |
| 27 | + const rawUrl = '/file-upload/1/a%20video%20#2.mov'; |
| 28 | + const escapedUrl = `${SERVER}/file-upload/1/a%20video%20%232.mov`; |
| 29 | + |
| 30 | + test('escapes it on a relative url', () => { |
| 31 | + mockSettings(); |
| 32 | + expect(formatAttachmentUrl(rawUrl, USER_ID, TOKEN, SERVER)).toBe(escapedUrl); |
| 33 | + }); |
| 34 | + |
| 35 | + test('escapes it on an absolute url', () => { |
| 36 | + mockSettings(); |
| 37 | + expect(formatAttachmentUrl(`${SERVER}${rawUrl}`, USER_ID, TOKEN, SERVER)).toBe(escapedUrl); |
| 38 | + }); |
| 39 | + |
| 40 | + test('keeps the whole path when the auth params are appended', () => { |
| 41 | + mockSettings({ protectFiles: true }); |
| 42 | + expect(formatAttachmentUrl(rawUrl, USER_ID, TOKEN, SERVER)).toBe(`${escapedUrl}?rc_token=${TOKEN}&rc_uid=${USER_ID}`); |
| 43 | + }); |
| 44 | + |
| 45 | + test('escapes it on a url that already carries the auth params', () => { |
| 46 | + mockSettings(); |
| 47 | + expect( |
| 48 | + formatAttachmentUrl(`${SERVER}/file-upload/1/video#2.mov?rc_token=${TOKEN}&rc_uid=${USER_ID}`, USER_ID, TOKEN, SERVER) |
| 49 | + ).toBe(`${SERVER}/file-upload/1/video%232.mov?rc_token=${TOKEN}&rc_uid=${USER_ID}`); |
| 50 | + }); |
| 51 | + |
| 52 | + test('escapes it behind a cdn prefix', () => { |
| 53 | + mockSettings({ cdnPrefix: 'https://cdn.example.com/' }); |
| 54 | + expect(formatAttachmentUrl(rawUrl, USER_ID, TOKEN, SERVER)).toBe( |
| 55 | + 'https://cdn.example.com/file-upload/1/a%20video%20%232.mov' |
| 56 | + ); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + test('leaves an already-escaped `#` untouched', () => { |
| 61 | + mockSettings(); |
| 62 | + expect(formatAttachmentUrl('/file-upload/1/a%20video%20%232.mov', USER_ID, TOKEN, SERVER)).toBe( |
| 63 | + `${SERVER}/file-upload/1/a%20video%20%232.mov` |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + // An external url is not a file-upload path, so a `#` there can be a genuine fragment. |
| 68 | + test('returns an external original url verbatim', () => { |
| 69 | + mockSettings(); |
| 70 | + const externalUrl = 'https://example.com/page#section'; |
| 71 | + expect(formatAttachmentUrl(`${SERVER}/file-upload/1/file.mov`, USER_ID, TOKEN, SERVER, externalUrl)).toBe(externalUrl); |
| 72 | + }); |
| 73 | + |
| 74 | + test('returns a base64 data uri untouched', () => { |
| 75 | + mockSettings(); |
| 76 | + const base64 = 'data:image/png;base64,ABC123'; |
| 77 | + expect(formatAttachmentUrl(base64, USER_ID, TOKEN, SERVER)).toBe(base64); |
| 78 | + }); |
| 79 | + |
| 80 | + test('returns a local file uri untouched', () => { |
| 81 | + mockSettings(); |
| 82 | + const fileUri = 'file:///var/app/Documents/server/msg1/video.mov'; |
| 83 | + expect(formatAttachmentUrl(fileUri, USER_ID, TOKEN, SERVER)).toBe(fileUri); |
| 84 | + }); |
| 85 | +}); |
0 commit comments