-
Notifications
You must be signed in to change notification settings - Fork 50
Fix: Prevent attachment to crash when asset-server is not available #8421
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
Open
CarolineDenis
wants to merge
13
commits into
main
Choose a base branch
from
issue-6851
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fda0db3
Fix:Add per-image fallback guard so the original can fall back to the…
CarolineDenis 45f3cf9
Add attachment server runtime state
CarolineDenis 6909f79
Handle unavailable attachment thumbnails
CarolineDenis cd9c7b8
Show attachment gallery outage warning
CarolineDenis 5842bdd
Feat: Disable attachment menu item when no server connexion
CarolineDenis 3924551
Feat: Record transitions between available and unavailable with times…
CarolineDenis b48fead
Feat: Confirm server health before changing global availability
CarolineDenis bdd4ff8
Fix: Disable attachment actions when the server is unavailable
CarolineDenis ea1b85e
Fix: Return the cleanup function to every subscriber
CarolineDenis c37c2fc
Fix: Add tooltip for side bar attachement disabled menu item
CarolineDenis ff90013
Test: Add frontend unit tests for server status
CarolineDenis 8e3853b
Test: Add frontend unit tests for unavailable gallery
CarolineDenis d9e3a31
Fix: Run the first health check immediately
CarolineDenis 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
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
97 changes: 97 additions & 0 deletions
97
specifyweb/frontend/js_src/lib/components/Attachments/__tests__/AttachmentsView.test.tsx
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,97 @@ | ||
| import React from 'react'; | ||
| import * as Router from 'react-router-dom'; | ||
|
|
||
| import { requireContext } from '../../../tests/helpers'; | ||
| import { mount } from '../../../tests/reactUtils'; | ||
| import { commonText } from '../../../localization/common'; | ||
| import { attachmentsText } from '../../../localization/attachments'; | ||
| import { SetMenuContext } from '../../Header/MenuContext'; | ||
| import { | ||
| attachmentSettingsPromise, | ||
| overrideAttachmentServerStatus, | ||
| } from '../attachments'; | ||
| import { AttachmentsView } from '..'; | ||
|
|
||
| /* | ||
| * Bypass the paginated attachment fetches (a pre-existing, unrelated bug in | ||
| * useAsyncState's real implementation crashes when exercised here); only the | ||
| * status-driven rendering is under test | ||
| */ | ||
| jest.mock('../../../hooks/useAsyncState', () => { | ||
| const ReactModule = require('react'); | ||
| return { | ||
| __esModule: true, | ||
| useAsyncState: () => ReactModule.useState(undefined), | ||
| usePromise: (promise: Promise<unknown>) => { | ||
| const [state, setState] = ReactModule.useState(undefined); | ||
| ReactModule.useEffect(() => { | ||
| let ignore = false; | ||
| promise.then((value: unknown) => { | ||
| if (!ignore) setState(value); | ||
| }); | ||
| return () => { | ||
| ignore = true; | ||
| }; | ||
| }, [promise]); | ||
| return [state, setState]; | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| requireContext(); | ||
|
|
||
| function TestAttachmentsView(): JSX.Element { | ||
| return ( | ||
| <Router.MemoryRouter | ||
| future={{ | ||
| v7_relativeSplatPath: true, | ||
| v7_startTransition: true, | ||
| }} | ||
| > | ||
| <SetMenuContext.Provider value={jest.fn()}> | ||
| <AttachmentsView /> | ||
| </SetMenuContext.Provider> | ||
| </Router.MemoryRouter> | ||
| ); | ||
| } | ||
|
|
||
| describe('AttachmentsView', () => { | ||
| beforeEach(async () => { | ||
| await attachmentSettingsPromise; | ||
| }); | ||
|
|
||
| test('replaces the gallery with a single unavailable message and disables Import', async () => { | ||
| overrideAttachmentServerStatus('unavailable'); | ||
|
|
||
| const { findByRole } = mount(<TestAttachmentsView />); | ||
|
|
||
| await findByRole('heading', { | ||
| name: attachmentsText.attachmentServerUnavailable(), | ||
| }); | ||
|
|
||
| const importButton = await findByRole('button', { | ||
| name: commonText.import(), | ||
| }); | ||
| expect(importButton).toBeDisabled(); | ||
| expect(importButton).toHaveAttribute( | ||
| 'title', | ||
| attachmentsText.attachmentServerUnavailable() | ||
| ); | ||
| }); | ||
|
|
||
| test('shows the gallery and an enabled Import button when available', async () => { | ||
| overrideAttachmentServerStatus('available'); | ||
|
|
||
| const { findByRole, queryByRole } = mount(<TestAttachmentsView />); | ||
|
|
||
| const importButton = await findByRole('button', { | ||
| name: commonText.import(), | ||
| }); | ||
| expect(importButton).toBeEnabled(); | ||
| expect( | ||
| queryByRole('heading', { | ||
| name: attachmentsText.attachmentServerUnavailable(), | ||
| }) | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }); |
120 changes: 120 additions & 0 deletions
120
...fyweb/frontend/js_src/lib/components/Attachments/__tests__/attachmentServerStatus.test.ts
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,120 @@ | ||
| import { act, renderHook, waitFor } from '@testing-library/react'; | ||
|
|
||
| import { overrideAjax } from '../../../tests/ajax'; | ||
| import { requireContext } from '../../../tests/helpers'; | ||
| import { Http } from '../../../utils/ajax/definitions'; | ||
| import { | ||
| attachmentSettingsPromise, | ||
| overrideAttachmentServerStatus, | ||
| overrideAttachmentSettings, | ||
| reportAttachmentServerFailure, | ||
| useAttachmentServerStatus, | ||
| } from '../attachments'; | ||
|
|
||
| requireContext(); | ||
|
|
||
| const mockReadUrl = '/mockAssetServer/fileget'; | ||
|
|
||
| const testSettings = { | ||
| collection: 'Test Collection', | ||
| delete: '/mockAssetServer/filedelete', | ||
| getmetadata: '/mockAssetServer/getmetadata', | ||
| read: mockReadUrl, | ||
| testkey: '/mockAssetServer/testkey', | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| token_required_for_get: false, | ||
| write: '/mockAssetServer/fileupload', | ||
| }; | ||
|
|
||
| // Silences (and lets tests assert on) the connection-loss/restoration logging | ||
| let consoleError: jest.SpiedFunction<typeof console.error>; | ||
| let consoleWarn: jest.SpiedFunction<typeof console.warn>; | ||
|
|
||
| beforeEach(async () => { | ||
| await attachmentSettingsPromise; | ||
| overrideAttachmentSettings(testSettings); | ||
| overrideAttachmentServerStatus('available'); | ||
| consoleError = jest | ||
| .spyOn(console, 'error') | ||
| .mockImplementation(() => undefined); | ||
| consoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| overrideAttachmentSettings(undefined); | ||
| consoleError.mockRestore(); | ||
| consoleWarn.mockRestore(); | ||
| }); | ||
|
|
||
| describe('reportAttachmentServerFailure', () => { | ||
| describe('when a health check confirms the server is reachable', () => { | ||
| overrideAjax(mockReadUrl, '', { responseCode: Http.OK }); | ||
|
|
||
| test('a single caller error does not mark the server unavailable', async () => { | ||
| const { result, unmount } = renderHook(() => useAttachmentServerStatus()); | ||
| expect(result.current).toBe('available'); | ||
|
|
||
| act(() => reportAttachmentServerFailure()); | ||
|
|
||
| await waitFor(() => expect(result.current).toBe('available')); | ||
| unmount(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when a health check confirms the server is unreachable', () => { | ||
| overrideAjax(mockReadUrl, '', { responseCode: Http.SERVER_ERROR }); | ||
|
|
||
| test('marks the server unavailable', async () => { | ||
| const { result, unmount } = renderHook(() => useAttachmentServerStatus()); | ||
| expect(result.current).toBe('available'); | ||
|
|
||
| act(() => reportAttachmentServerFailure()); | ||
|
|
||
| await waitFor(() => expect(result.current).toBe('unavailable')); | ||
| unmount(); | ||
| }); | ||
| }); | ||
|
|
||
| test('does nothing when no settings are configured', () => { | ||
| overrideAttachmentSettings(undefined); | ||
| expect(() => reportAttachmentServerFailure()).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('connection loss/restoration logging', () => { | ||
| overrideAjax(mockReadUrl, '', { responseCode: Http.SERVER_ERROR }); | ||
|
|
||
| test('logs connection loss exactly once, and only once further failures are reported', async () => { | ||
| const { result, unmount } = renderHook(() => useAttachmentServerStatus()); | ||
| expect(result.current).toBe('available'); | ||
|
|
||
| act(() => reportAttachmentServerFailure()); | ||
| await waitFor(() => expect(result.current).toBe('unavailable')); | ||
|
|
||
| expect(consoleError).toHaveBeenCalledTimes(1); | ||
| expect(consoleWarn).not.toHaveBeenCalled(); | ||
|
|
||
| act(() => reportAttachmentServerFailure()); | ||
| await waitFor(() => expect(consoleError).toHaveBeenCalledTimes(1)); | ||
|
|
||
| unmount(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('useAttachmentServerStatus polling', () => { | ||
| beforeEach(() => jest.useFakeTimers()); | ||
| afterEach(() => jest.useRealTimers()); | ||
|
|
||
| test('keeps the shared interval running until the last subscriber unmounts', () => { | ||
| const first = renderHook(() => useAttachmentServerStatus()); | ||
| const second = renderHook(() => useAttachmentServerStatus()); | ||
|
|
||
| expect(jest.getTimerCount()).toBe(1); | ||
|
|
||
| first.unmount(); | ||
| expect(jest.getTimerCount()).toBe(1); | ||
|
|
||
| second.unmount(); | ||
| expect(jest.getTimerCount()).toBe(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Make the test wait for the health check result.
result.currentis already'available'at Line 55. The assertion at Line 59 can pass beforereportAttachmentServerFailure()completes its asynchronous health check.Set the status to
'unknown'before rendering the hook. Then wait for it to become'available'after the failure report. This makes the test verify the successful health-check path.Proposed test change
test('a single caller error does not mark the server unavailable', async () => { + overrideAttachmentServerStatus('unknown'); const { result, unmount } = renderHook(() => useAttachmentServerStatus()); - expect(result.current).toBe('available'); + expect(result.current).toBe('unknown'); act(() => reportAttachmentServerFailure()); await waitFor(() => expect(result.current).toBe('available'));📝 Committable suggestion
🤖 Prompt for AI Agents