mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
feat(airtrail): use the AirTrail /api/v1 routes, and keep the legacy routes
AirTrail 3.13 adds /api/v1 and marks the /api/flight/* routes as deprecated. TREK now asks the public discovery document GET /api/v1 and uses v1 when the instance has it. It uses the legacy routes for an older instance. The client methods keep their signatures, and the sync hash stays the same, so a linked flight does not sync again when an instance changes to v1. The probe cache holds only a clear answer: 200 with the discovery document gives v1, and 404 gives legacy. The cache is per AirTrail URL, for 10 minutes. A timeout, a 5xx or a network error fails that call only. A 401, a 403 or a page that is not the discovery document uses legacy for that call only. A 404 for one flight makes the client ask the probe again before it reports the flight as deleted. A missing scope gives AirtrailScopeError, which names the scope. It does not unlink a flight. The API key hint and the help step list the scopes for read and for write-back, in all languages. Each new request goes through safeFetch, with the SSRF guard, the timeout and the size cap. The flight list reads pages of 100 and fails above 50 pages. A failed list unlinks nothing. airtrail-v1.ts translates between the v1 shapes and the legacy shapes. A write-back looks up the airport, airline and aircraft ids, and changes each local time to a UTC instant in the zone of its own airport. It writes each instant in the +00:00 form of the legacy save, because AirTrail stores the string as it gets it and the sync hash reads that string. The tests cover both APIs over HTTP, the probe cache rules, the sync hash on a switch and on a save, the page limit, the scope error, and the UTC conversion across midnight, the date line and daylight-saving changes.
This commit is contained in:
@@ -4,7 +4,11 @@ import type { ReservationsRepository } from '../../../../src/db/repositories/Res
|
||||
import type { AddonsService } from '../../../../src/nest/addons/addons.service';
|
||||
import { AirtrailLinkService } from '../../../../src/nest/integrations/airtrail-link.service';
|
||||
import { AirtrailSyncService } from '../../../../src/nest/integrations/airtrail-sync.service';
|
||||
import { AirtrailAuthError } from '../../../../src/nest/integrations/airtrail.client';
|
||||
import {
|
||||
AirtrailAuthError,
|
||||
AirtrailRequestError,
|
||||
AirtrailScopeError,
|
||||
} from '../../../../src/nest/integrations/airtrail.client';
|
||||
import type { AirtrailClient } from '../../../../src/nest/integrations/airtrail.client';
|
||||
import type { AirtrailService } from '../../../../src/nest/integrations/airtrail.service';
|
||||
import type { RealtimeService } from '../../../../src/nest/realtime/realtime.service';
|
||||
@@ -184,6 +188,14 @@ describe('pushReservationToAirtrail write gate (#1240)', () => {
|
||||
expect(detachSpy).toHaveBeenCalledWith(5);
|
||||
});
|
||||
|
||||
it('keeps the link when the key only lacks a scope, because the key still works', async () => {
|
||||
isAirtrailWriteEnabled.mockReturnValue(true);
|
||||
getFlight.mockRejectedValue(new AirtrailScopeError('flight.read.own'));
|
||||
await svc.link.pushReservationToAirtrail(5, 9);
|
||||
expect(saveFlight).not.toHaveBeenCalled();
|
||||
expect(detachSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('detaches when the flight is gone from AirTrail — the same as a remote delete', async () => {
|
||||
isAirtrailWriteEnabled.mockReturnValue(true);
|
||||
getFlight.mockResolvedValue(null);
|
||||
@@ -234,6 +246,19 @@ describe('inbound sync multi-leg guard (#1535)', () => {
|
||||
expect(changed).toBe(1);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['the page limit', new AirtrailRequestError('AirTrail has more than 5000 flights')],
|
||||
['a missing scope', new AirtrailScopeError('flight.read.own')],
|
||||
['a 5xx', new AirtrailRequestError('AirTrail list failed (HTTP 502)', 502)],
|
||||
])('detaches nothing when the list fails on %s', async (_, error) => {
|
||||
withLinkedRow(2);
|
||||
listFlights.mockRejectedValue(error);
|
||||
const { changed } = await svc.sync.runAirtrailSyncForUser(7);
|
||||
expect(updateReservation).not.toHaveBeenCalled();
|
||||
expect(detachSpy).not.toHaveBeenCalled();
|
||||
expect(changed).toBe(0);
|
||||
});
|
||||
|
||||
it('leaves a disconnected owner alone rather than detaching their rows', async () => {
|
||||
withLinkedRow(2);
|
||||
getAirtrailCredentials.mockReturnValue(null);
|
||||
|
||||
Reference in New Issue
Block a user