PUT /availability/status vervangt POST /availability/admin/status; de poort sluit alleen het lezen van de status vrij
De status is één resource: de publieke GET en de PUT van de beheerder delen het pad; de PUT geeft de bijgewerkte resource terug. De vrijstelling van de poort geldt nu alleen voor GET en HEAD, zodat de PUT als beheerroute gewoon achter de poort blijft. De oude route verdwijnt zonder alias, dus API en admin-app moeten als één release uit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,5 +10,12 @@ export const availabilityHandlers = [
|
||||
}),
|
||||
),
|
||||
|
||||
http.post('*/Availability/admin/status', () => new HttpResponse(null, { status: 200 })),
|
||||
http.put('*/Availability/status', () =>
|
||||
HttpResponse.json({
|
||||
status: 'Available',
|
||||
checkedAt: new Date().toISOString(),
|
||||
message: '',
|
||||
isMasterControlled: false,
|
||||
}),
|
||||
),
|
||||
];
|
||||
|
||||
@@ -17,8 +17,12 @@ export function useAvailabilityStatus() {
|
||||
|
||||
export function useUpdateAvailability() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<void, Error, UpdateAvailabilityPayload>({
|
||||
mutationFn: (data) => api.post<void>('/api/v1/Availability/admin/status', data),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['availability', 'status'] }),
|
||||
return useMutation<AvailabilityResponse, Error, UpdateAvailabilityPayload>({
|
||||
mutationFn: (data) => api.put<AvailabilityResponse>('/api/v1/Availability/status', data),
|
||||
onSuccess: (updated) => {
|
||||
// The PUT answers with the updated resource (same shape as the GET): use it at once.
|
||||
queryClient.setQueryData(['availability', 'status'], updated);
|
||||
return queryClient.invalidateQueries({ queryKey: ['availability', 'status'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
@@ -60,6 +60,37 @@ describe('SettingsPage', () => {
|
||||
expect(maintenanceBtn.className).toContain('bg-primary');
|
||||
});
|
||||
|
||||
it('changes the status with PUT on the status resource (no admin route), sending the chosen status', async () => {
|
||||
let sent: { method: string; url: string; body: { newStatus: string } } | undefined;
|
||||
server.use(
|
||||
http.put(`${API_BASE}/api/v1/Availability/status`, async ({ request }) => {
|
||||
sent = {
|
||||
method: request.method,
|
||||
url: new URL(request.url).pathname,
|
||||
body: (await request.json()) as { newStatus: string },
|
||||
};
|
||||
return HttpResponse.json({
|
||||
status: 'Maintenance',
|
||||
checkedAt: new Date().toISOString(),
|
||||
message: '',
|
||||
isMasterControlled: false,
|
||||
});
|
||||
}),
|
||||
);
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
await screen.findByTestId('availability-mode-selector', {}, { timeout: 10000 });
|
||||
await userEvent.click(screen.getByTestId('mode-option-Maintenance'));
|
||||
await userEvent.click(screen.getByTestId('availability-save-button'));
|
||||
|
||||
await vi.waitFor(() => expect(sent).toBeDefined());
|
||||
expect(sent!.method).toBe('PUT');
|
||||
expect(sent!.url).toBe('/api/v1/Availability/status');
|
||||
expect(sent!.url).not.toContain('admin');
|
||||
expect(sent!.body.newStatus).toBe('Maintenance');
|
||||
});
|
||||
|
||||
it('saves availability and shows success toast', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
@@ -72,7 +103,7 @@ describe('SettingsPage', () => {
|
||||
|
||||
it('shows error toast when save fails', async () => {
|
||||
server.use(
|
||||
http.post(`${API_BASE}/api/v1/Availability/admin/status`, () =>
|
||||
http.put(`${API_BASE}/api/v1/Availability/status`, () =>
|
||||
HttpResponse.json({ title: 'Error' }, { status: 500 }),
|
||||
),
|
||||
);
|
||||
@@ -118,7 +149,7 @@ describe('SettingsPage', () => {
|
||||
|
||||
it('shows a master-controlled error toast on a 409 conflict from the server', async () => {
|
||||
server.use(
|
||||
http.post(`${API_BASE}/api/v1/Availability/admin/status`, () =>
|
||||
http.put(`${API_BASE}/api/v1/Availability/status`, () =>
|
||||
HttpResponse.json({ title: 'Conflict' }, { status: 409 }),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user