Files
simple-aircraft-manager-rails/test/integration/flight_import_flow_test.rb
T
rickyandClaude Opus 5.5 4d3dc7150a Add spreadsheet flight import; fix oil/fuel consumption charts
Flight import (owners, Flights tab → Import from spreadsheet):
- Upload .xlsx or .csv, preview, then confirm. Columns are matched by
  header name (Date, From, To, Tach, Hobbs, Oil, Fuel, Flight Time).
- Each flight's time is derived from the previous row's meter reading;
  rows are kept in file order so typos (tach going backwards) are
  flagged and skipped while the next good row absorbs the time.
- Unchanged tach imports as a 0.0 hr entry so fuel/oil isn't lost;
  "Full" fuel is warned about; re-imports skip existing rows.
- History only: component hours are untouched unless the owner opts in
  to advancing the aircraft's meters to the last reading.
- Upload redirects to a GET preview (Turbo ignores 200 renders after a
  form POST, which made the button appear to do nothing).

Consumption charts:
- Replace per-fill math (qty ÷ hours since last add), which spiked to
  80 gal/hr on partial fills and top-offs, with a rolling rate (total
  added ÷ hours over a trailing 10 h fuel / 25 h oil window).
- Headline shows recent (last ~50/100 hrs) and lifetime rates; straight
  chart lines instead of splines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 19:23:47 -05:00

58 lines
2.4 KiB
Ruby

require "test_helper"
class FlightImportFlowTest < ActionDispatch::IntegrationTest
setup do
@owner = create_user
@pilot = create_user
@aircraft = create_aircraft(owner: @owner, pilot: @pilot, tach_time: 7270)
end
test "owner uploads, previews, and confirms" do
sign_in @owner
get new_aircraft_flight_import_path(@aircraft)
assert_response :success
post aircraft_flight_import_path(@aircraft), params: { file: fixture_file_upload("flights.csv", "text/csv") }
# Turbo ignores a 200 render after a form POST, so upload must redirect to the preview
assert_response :redirect
assert_match %r{/flight_import\?}, response.location
follow_redirect!
assert_response :success
assert_includes response.body, "Tach went backwards"
assert_includes response.body, "Import 12 Flights"
token = response.body[/name="token"[^>]*value="([^"]+)"/, 1]
assert token
post confirm_aircraft_flight_import_path(@aircraft), params: { token: token, filename: "flights.csv", update_aircraft_hours: "1" }
assert_redirected_to aircraft_flight_logs_path(@aircraft)
assert_equal 12, @aircraft.flight_logs.count
assert_equal BigDecimal("7290.3"), @aircraft.reload.tach_time
# token is single-use
post confirm_aircraft_flight_import_path(@aircraft), params: { token: token, filename: "flights.csv" }
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
assert_equal 12, @aircraft.flight_logs.count
end
test "rejects path traversal tokens and wrong file types" do
sign_in @owner
post confirm_aircraft_flight_import_path(@aircraft), params: { token: "../../config/master.key", filename: "x.csv" }
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
post aircraft_flight_import_path(@aircraft), params: { file: fixture_file_upload("flights.csv", "text/plain") .tap { |f| def f.original_filename = "evil.rb" } }
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
end
test "preview with a bogus token sends you back to upload" do
sign_in @owner
get aircraft_flight_import_path(@aircraft, token: "nope.csv")
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
end
test "pilots cannot import" do
sign_in @pilot
post aircraft_flight_import_path(@aircraft), params: { file: fixture_file_upload("flights.csv", "text/csv") }
assert_response :redirect
assert_equal 0, @aircraft.flight_logs.count
end
end