Check for upload service before by status 204

This commit is contained in:
Alan Grainger
2026-10-07 15:50:34 +13:00
parent 5f2edc5f74
commit 3bade3c34b
11 changed files with 178 additions and 25 deletions
+60
View File
@@ -0,0 +1,60 @@
import { describe, it, expect } from 'vitest'
import { renderPage } from '@ipp/core'
import { h } from 'preact'
import { Gallery, GalleryProps } from '../src/view/gallery'
/*
The "Add photos" button is rendered hidden, with the upload service's
healthcheck URL on it, when the service is assumed to be at a path on IPP's
hostname rather than configured. The client reveals it once the healthcheck
answers. When the header holds nothing else it is hidden too, so a gallery
with no title and no download button does not show an empty bar.
*/
function galleryProps (overrides: Partial<GalleryProps> = {}): GalleryProps {
return {
items: [{ id: 'a', type: 'IMAGE', previewUrl: '/p', thumbnailUrl: '/t', downloadFilename: 'photo.jpg' }],
title: 'Test gallery',
description: '',
publicBaseUrl: 'https://example.com',
path: '/share/k',
showDownloadZip: false,
showTitle: false,
lightboxConfig: { showArrows: true, showDownload: true, mobileArrows: false, autoPlayVideos: false },
metadataConfig: { descriptionInCaption: false, descriptionInSidebar: false, sidebarHasContent: false, locationWebLink: false },
groupByDate: false,
...overrides
}
}
const header = (html: string) => html.match(/<header[^>]*>/)?.[0]
const button = (html: string) => html.match(/<a id="upload-link"[^>]*>/)?.[0]
describe('gallery upload button', () => {
it('is hidden with the healthcheck to probe when the upload service is assumed', () => {
const html = renderPage(h(Gallery, galleryProps({ uploadLink: '/upload/share/k', uploadHealthcheck: '/upload/healthcheck' })))
expect(button(html)).toContain(' hidden')
expect(button(html)).toContain('data-healthcheck="/upload/healthcheck"')
expect(header(html)).toContain(' hidden')
})
it('shows at once when the owner configured the upload URL', () => {
const html = renderPage(h(Gallery, galleryProps({ uploadLink: 'https://upload.example.com/share/k' })))
expect(button(html)).toContain('href="https://upload.example.com/share/k"')
expect(button(html)).not.toContain('hidden')
expect(button(html)).not.toContain('data-healthcheck')
expect(header(html)).toBe('<header id="header">')
})
it('keeps the header visible when it holds other content', () => {
const html = renderPage(h(Gallery, galleryProps({ showTitle: true, uploadLink: '/upload/share/k', uploadHealthcheck: '/upload/healthcheck' })))
expect(header(html)).toBe('<header id="header">')
expect(button(html)).toContain(' hidden')
})
it('renders no button and no header when the share takes no uploads', () => {
const html = renderPage(h(Gallery, galleryProps({ uploadHealthcheck: '/upload/healthcheck' })))
expect(button(html)).toBeUndefined()
expect(header(html)).toBeUndefined()
})
})
+25 -3
View File
@@ -1,6 +1,6 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { KeyType, loadConfig, SharedLink } from '@ipp/core'
import { uploadLink } from '../src/share'
import { uploadHealthcheck, uploadLink } from '../src/share'
function setConfig (config: unknown) {
process.env.CONFIG = JSON.stringify(config)
@@ -23,8 +23,26 @@ describe('uploadLink', () => {
loadConfig()
})
it('is absent while ipp.uploadUrl is unset', () => {
it('assumes /upload on this hostname while ipp.uploadUrl is unset, and checks it is there', () => {
expect(uploadLink(share())).toBe('/upload/share/the-key')
expect(uploadHealthcheck()).toBe('/upload/healthcheck')
})
it('is absent when ipp.uploadUrl is empty', () => {
setConfig({ ipp: { uploadUrl: '' } })
expect(uploadLink(share())).toBeUndefined()
expect(uploadHealthcheck()).toBeUndefined()
})
it('takes another path on this hostname, without a trailing slash', () => {
setConfig({ ipp: { uploadUrl: '/photos-in/' } })
expect(uploadLink(share())).toBe('/photos-in/share/the-key')
expect(uploadHealthcheck()).toBe('/photos-in/healthcheck')
})
it('trusts an absolute URL without a check', () => {
setConfig({ ipp: { uploadUrl: 'https://upload.example.com' } })
expect(uploadHealthcheck()).toBeUndefined()
})
it('links a key share by its key, without a doubled slash', () => {
@@ -43,9 +61,13 @@ describe('uploadLink', () => {
expect(uploadLink(share({ allowUpload: undefined }))).toBeUndefined()
})
it('ignores a value that is not an http(s) URL', () => {
it('ignores a value that is neither an http(s) URL nor a path', () => {
setConfig({ ipp: { uploadUrl: 'javascript:alert(1)' } })
expect(uploadLink(share())).toBeUndefined()
setConfig({ ipp: { uploadUrl: '//evil.example.com' } })
expect(uploadLink(share())).toBeUndefined()
setConfig({ ipp: { uploadUrl: '/' } })
expect(uploadLink(share())).toBeUndefined()
setConfig({ ipp: { uploadUrl: 42 } })
expect(uploadLink(share())).toBeUndefined()
})