diff options
author | Julien Veyssier <eneiluj@posteo.net> | 2022-08-24 16:58:50 +0300 |
---|---|---|
committer | Julien Veyssier <eneiluj@posteo.net> | 2022-09-06 15:44:53 +0300 |
commit | c18a030f076f123a5d5172e1b6ece6be27d92c3f (patch) | |
tree | b9be1416e603d7f53020dae7a43a76ffdd0a7ed4 /src | |
parent | 22f70c954111109c5dd76d76dcefa8632d9eaa66 (diff) |
adjust frontend tests
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/services/AttachmentResolver.js | 2 | ||||
-rw-r--r-- | src/tests/services/AttachmentResolver.spec.js | 80 |
2 files changed, 53 insertions, 29 deletions
diff --git a/src/services/AttachmentResolver.js b/src/services/AttachmentResolver.js index 10b9fa083..096cbbc68 100644 --- a/src/services/AttachmentResolver.js +++ b/src/services/AttachmentResolver.js @@ -47,7 +47,7 @@ export default class AttachmentResolver { /* * Resolve a given src. * @param { string } the original src in the node. - * @returns { Array<string> } - resolved urls to try. + * @returns { Array<Array> } - resolved candidates to try. * * Currently returns either one or two urls. */ diff --git a/src/tests/services/AttachmentResolver.spec.js b/src/tests/services/AttachmentResolver.spec.js index 8ac76501c..b3d81eadd 100644 --- a/src/tests/services/AttachmentResolver.spec.js +++ b/src/tests/services/AttachmentResolver.spec.js @@ -22,8 +22,9 @@ describe('Image resolver', () => { it('handles text:// urls via Text API', () => { const src = 'text://image?imageFileName=group%20pic.jpg' const resolver = new AttachmentResolver({ session }) - const [url] = resolver.resolve(src) - expect(url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg') }) it('handles text:// urls with token via Text API', () => { @@ -32,8 +33,9 @@ describe('Image resolver', () => { session, shareToken: 'myShareToken', }) - const [url] = resolver.resolve(src) - expect(url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg&shareToken=myShareToken') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg&shareToken=myShareToken') }) it('uses user auth over token auth', () => { @@ -43,15 +45,20 @@ describe('Image resolver', () => { user, shareToken: 'myShareToken', }) - const [url] = resolver.resolve(src) - expect(url).not.toContain('myShareToken') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).not.toContain('myShareToken') }) it('handles .attachments urls via Text API', () => { const src = `.attachments.${session.documentId}/group%20pic.jpg` const resolver = new AttachmentResolver({ session }) - const [url] = resolver.resolve(src) - expect(url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg') + const [candidate, fallbackCandidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg') + expect(fallbackCandidate.type).toBe('media') + expect(fallbackCandidate.url).toBe('/nc-webroot/apps/text/mediaPreview?documentId=4173&sessionId=456&sessionToken=mySessionToken&mediaFileName=group%20pic.jpg') + expect(fallbackCandidate.name).toBe('group pic.jpg') }) it('handles .attachments urls with token via Text API', () => { @@ -60,8 +67,12 @@ describe('Image resolver', () => { session, shareToken, }) - const [url] = resolver.resolve(src) - expect(url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg&shareToken=myShareToken') + const [candidate, fallbackCandidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg&shareToken=myShareToken') + expect(fallbackCandidate.type).toBe('media') + expect(fallbackCandidate.url).toBe('/nc-webroot/apps/text/mediaPreview?documentId=4173&sessionId=456&sessionToken=mySessionToken&mediaFileName=group%20pic.jpg&shareToken=myShareToken') + expect(fallbackCandidate.name).toBe('group pic.jpg') }) it('leaves urls unchanged if they can be loaded directly', () => { @@ -74,23 +85,26 @@ describe('Image resolver', () => { ] const resolver = new AttachmentResolver({ }) for (const src of sources) { - const [url] = resolver.resolve(src) - expect(url).toBe(src) + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe(src) } }) it('uses fileId for preview', () => { const src = '/Media/photo.jpeg?fileId=7#mimetype=image%2Fjpeg&hasPreview=true' const resolver = new AttachmentResolver({ user }) - const [url] = resolver.resolve(src) - expect(url).toContain('/core/preview?fileId=7') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toContain('/core/preview?fileId=7') }) it('uses .png endpoint if no fileId is given', () => { const src = '/Media/photo.jpeg?mimetype=image%2Fjpeg&hasPreview=true' const resolver = new AttachmentResolver({ user }) - const [url] = resolver.resolve(src) - expect(url).toBe('/nc-webroot/core/preview.png?file=%2FMedia%2Fphoto.jpeg&x=1024&y=1024&a=true') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('/nc-webroot/core/preview.png?file=%2FMedia%2Fphoto.jpeg&x=1024&y=1024&a=true') }) it('retrieves public preview by path', () => { @@ -98,16 +112,22 @@ describe('Image resolver', () => { const resolver = new AttachmentResolver({ shareToken: 'SHARE_TOKEN' }) - const [url] = resolver.resolve(src) - expect(url).toBe('/nc-webroot/apps/files_sharing/publicpreview/SHARE_TOKEN?file=%2FMedia%2Fphoto.jpeg&x=1024&y=1024&a=true') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('/nc-webroot/apps/files_sharing/publicpreview/SHARE_TOKEN?file=%2FMedia%2Fphoto.jpeg&x=1024&y=1024&a=true') }) it('handles old .attachments urls via webdav with text API fallback', () => { const src = `.attachments.${session.documentId + 1}/group%20pic.jpg` const resolver = new AttachmentResolver({ session, user, currentDirectory }) - const [url, fallback] = resolver.resolve(src) - expect(url).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4174/group%20pic.jpg') - expect(fallback).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg') + const [candidate, fallbackCandidate, secondFallback] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4174/group%20pic.jpg') + expect(fallbackCandidate.type).toBe('image') + expect(fallbackCandidate.url).toBe('/nc-webroot/apps/text/image?documentId=4173&sessionId=456&sessionToken=mySessionToken&imageFileName=group%20pic.jpg') + expect(secondFallback.type).toBe('media') + expect(secondFallback.url).toBe('/nc-webroot/apps/text/mediaPreview?documentId=4173&sessionId=456&sessionToken=mySessionToken&mediaFileName=group%20pic.jpg') + expect(secondFallback.name).toBe('group pic.jpg') }) describe('missing session', () => { @@ -119,8 +139,9 @@ describe('Image resolver', () => { user, currentDirectory, }) - const [url] = resolver.resolve(src) - expect(url).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4173/group%20pic.jpg') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4173/group%20pic.jpg') }) it('resolves text:// urls as share token download', () => { @@ -130,16 +151,19 @@ describe('Image resolver', () => { shareToken, currentDirectory, }) - const [url] = resolver.resolve(src) - expect(url).toBe('/nc-webroot/s/myShareToken/download?path=%2FparentDir%2F.attachments.4173&files=group%20pic.jpg') + const [candidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('/nc-webroot/s/myShareToken/download?path=%2FparentDir%2F.attachments.4173&files=group%20pic.jpg') }) it('handles .attachments urls for other fileIds via webdav with webdav fallback', () => { const src = `.attachments.${session.documentId + 1}/group%20pic.jpg` const resolver = new AttachmentResolver({ user, currentDirectory, fileId }) - const [url, fallback] = resolver.resolve(src) - expect(url).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4174/group%20pic.jpg') - expect(fallback).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4173/group%20pic.jpg') + const [candidate, fallbackCandidate] = resolver.resolve(src) + expect(candidate.type).toBe('image') + expect(candidate.url).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4174/group%20pic.jpg') + expect(fallbackCandidate.type).toBe('image') + expect(fallbackCandidate.url).toBe('http://localhost/nc-webroot/remote.php/dav/files/user-uid/parentDir/.attachments.4173/group%20pic.jpg') }) }) |