mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
AirTrail sync and export, GPS tracks, airport database with autocomplete
AirTrail (https://github.com/johanohly/AirTrail): - File export (Flights → Export for AirTrail) in AirTrail's import format, validated against its schema; same-day repeat legs get leg numbers so AirTrail's de-duplication doesn't merge them. - Automatic sync through AirTrail's REST API (Profile → AirTrail sync): per-user connection with an encrypted API key and scope check; flights are created/updated/deleted in background jobs; a full sync first links flights already in AirTrail (e.g. from a file import) instead of duplicating them; airports are resolved and cached, with manual mapping for unknown codes. - GPS tracks: GPX, KML and CSV track logs are converted to AirTrail's track format for both sync and export; the flight page shows a summary. - Aircraft ICAO type (e.g. C150) for AirTrail's aircraft type. Airport database (OurAirports, the source AirTrail uses): - Managed like AirTrail: downloaded in the background on first start, updated in place from Admin → Airports (adds, changes, removes; ids stable), with admin-managed custom airports that updates never touch. - Each airport carries AirTrail's code, so logged codes translate to AirTrail's exactly (1B9 → K1B9, 4MB → K5M3). - From/To autocomplete on new flights (airports this aircraft has used rank first); From is pre-filled with the last flight's destination; codes are normalized to uppercase. - Flights → Standardize airport codes: rewrite logged codes to the standard code, and fix codes not in the database (typos such as 0R8 → 08R are suggested). Also: admin pages grouped under an Admin dropdown menu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0c4803d717
commit
5bf682502b
68 files changed
+2667
-10
No files matched your search
@@ -0,0 +1,78 @@
|
||||
require "test_helper"
|
||||
|
||||
class AdminAirportsTest < ActionDispatch::IntegrationTest
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
CSV_BODY = <<~CSV.freeze
|
||||
"id","ident","type","name","latitude_deg","longitude_deg","elevation_ft","continent","iso_country","iso_region","municipality","scheduled_service","icao_code","iata_code","gps_code","local_code","home_link","wikipedia_link","keywords"
|
||||
1,"KOQU","medium_airport","Quonset State Airport",41.597,-71.412,18,"NA","US","US-RI","North Kingstown","no","KOQU","NCO","KOQU","OQU",,,
|
||||
2,"K1B9","small_airport","Mansfield Municipal Airport",42.0,-71.19,122,"NA","US","US-MA","Mansfield","no",,,"1B9","1B9",,,
|
||||
CSV
|
||||
|
||||
setup do
|
||||
@admin = create_user("admin@example.com", admin: true)
|
||||
stub_request(:get, AirportData::SOURCE_URL).to_return(status: 200, body: CSV_BODY)
|
||||
end
|
||||
|
||||
test "admin updates from OurAirports in the background and sees the result" do
|
||||
sign_in @admin
|
||||
get admin_airports_path
|
||||
assert_response :success
|
||||
assert_select "details[data-controller='dropdown'] summary", text: /Admin/
|
||||
assert_select "details a[href='#{admin_airports_path}']", text: "Airports" # in the Admin menu
|
||||
assert_select "details a[href='#{admin_users_path}']", text: "Users"
|
||||
|
||||
perform_enqueued_jobs { post refresh_admin_airports_path }
|
||||
assert_redirected_to admin_airports_path
|
||||
assert_equal 2, Airport.count
|
||||
update = AirportUpdate.last
|
||||
assert_equal [ "done", 2, @admin ], [ update.status, update.created_count, update.requested_by ]
|
||||
follow_redirect!
|
||||
assert_match "2 added, 0 updated, 0 removed", response.body
|
||||
end
|
||||
|
||||
test "failed downloads are recorded" do
|
||||
stub_request(:get, AirportData::SOURCE_URL).to_return(status: 503)
|
||||
sign_in @admin
|
||||
perform_enqueued_jobs { post refresh_admin_airports_path }
|
||||
assert_equal "failed", AirportUpdate.last.status
|
||||
follow_redirect!
|
||||
assert_match "503", response.body
|
||||
end
|
||||
|
||||
test "first start populates an empty table once" do
|
||||
assert_enqueued_jobs(1, only: AirportUpdateJob) { AirportUpdateJob.ensure_populated }
|
||||
assert_no_enqueued_jobs(only: AirportUpdateJob) { AirportUpdateJob.ensure_populated } # already queued
|
||||
perform_enqueued_jobs
|
||||
assert_equal 2, Airport.count
|
||||
assert_no_enqueued_jobs(only: AirportUpdateJob) { AirportUpdateJob.ensure_populated } # has data
|
||||
end
|
||||
|
||||
test "admin manages custom airports; only custom ones are editable" do
|
||||
sign_in @admin
|
||||
get admin_airports_path(q: "0R8")
|
||||
assert_select "a[href='#{new_admin_airport_path(ident: '0R8')}']"
|
||||
post admin_airports_path, params: { airport: { ident: "0r8", name: "Grass Strip", kind: "small_airport", region: "US-RI" } }
|
||||
strip = Airport.find_by!(ident: "0R8")
|
||||
assert strip.custom?
|
||||
assert_equal "0R8", strip.airtrail_code
|
||||
patch admin_airport_path(strip), params: { airport: { name: "Grass Strip North" } }
|
||||
assert_equal "Grass Strip North", strip.reload.name
|
||||
|
||||
AirportData.sync!(StringIO.new(CSV_BODY))
|
||||
get edit_admin_airport_path(Airport.find_by!(ident: "KOQU"))
|
||||
assert_response :not_found
|
||||
delete admin_airport_path(strip)
|
||||
assert_not Airport.exists?(strip.id)
|
||||
end
|
||||
|
||||
test "non-admins can't reach it" do
|
||||
sign_in create_user
|
||||
get root_path
|
||||
assert_select "summary", text: /Admin/, count: 0
|
||||
get admin_airports_path
|
||||
assert_redirected_to root_path
|
||||
post refresh_admin_airports_path
|
||||
assert_equal 0, AirportUpdate.count
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
require "test_helper"
|
||||
|
||||
class AirTrailExportFlowTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@pilot = create_user("ricky@example.com")
|
||||
@a = create_aircraft(pilot: @pilot, owner: create_user, tail_number: "N12345")
|
||||
@a.flight_logs.create!(date: Date.current, departure_location: "KOQU", destination_location: "KBID", tach_time: 0.5)
|
||||
sign_in @pilot
|
||||
end
|
||||
|
||||
test "pilot downloads the AirTrail file" do
|
||||
get aircraft_flight_logs_path(@a)
|
||||
assert_select "a[href='#{new_aircraft_air_trail_export_path(@a)}']", text: "Export for AirTrail"
|
||||
get new_aircraft_air_trail_export_path(@a)
|
||||
assert_response :success
|
||||
assert_select "input[name='username'][value='ricky']"
|
||||
assert_select "form[data-turbo='false']"
|
||||
|
||||
post aircraft_air_trail_export_path(@a), params: { username: "ricky", aircraft_type: "C150", scope: "all" }
|
||||
assert_response :success
|
||||
assert_equal "application/json", response.media_type
|
||||
assert_match(/attachment; filename="n12345-airtrail-.*\.json"/, response.headers["Content-Disposition"])
|
||||
assert_equal "KBID", JSON.parse(response.body)["flights"].first["to"]["code"]
|
||||
end
|
||||
|
||||
test "invalid username re-renders the form" do
|
||||
post aircraft_air_trail_export_path(@a), params: { username: "x" }
|
||||
assert_response :unprocessable_entity
|
||||
assert_match "3–20 letters", response.body
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,70 @@
|
||||
require "test_helper"
|
||||
|
||||
class AirTrailSyncFlowTest < ActionDispatch::IntegrationTest
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
setup do
|
||||
@fake = FakeAirTrail.new(self)
|
||||
@user = create_user
|
||||
@a = create_aircraft(owner: @user, tail_number: "N12345", tach_time: 100)
|
||||
sign_in @user
|
||||
end
|
||||
|
||||
def connect(key: "at_secret")
|
||||
post air_trail_connection_path, params: { air_trail_connection: { base_url: "#{FakeAirTrail::BASE}/", api_key: key, enabled: "1" } }
|
||||
end
|
||||
|
||||
test "connect, see status, and flights sync automatically when logged and deleted" do
|
||||
get air_trail_connection_path
|
||||
assert_response :success
|
||||
connect
|
||||
assert_redirected_to air_trail_connection_path
|
||||
follow_redirect!
|
||||
assert_match "Connected to AirTrail as ricky", response.body
|
||||
assert_equal "https://airtrail.test", @user.air_trail_connection.base_url
|
||||
|
||||
perform_enqueued_jobs do
|
||||
post aircraft_flight_logs_path(@a), params: { flight_log: { date: Date.current, tach_out: 100, tach_in: 101.2,
|
||||
departure_location: "KOQU", destination_location: "KBID" } }
|
||||
end
|
||||
flight = @a.flight_logs.last
|
||||
sync = flight.air_trail_syncs.first
|
||||
assert_equal "synced", sync.status
|
||||
assert @fake.flights.key?(sync.remote_id)
|
||||
|
||||
get aircraft_flight_log_path(@a, flight)
|
||||
assert_select "span.badge-green", text: "Synced"
|
||||
|
||||
perform_enqueued_jobs { delete aircraft_flight_log_path(@a, flight) }
|
||||
assert_not @fake.flights.key?(sync.remote_id)
|
||||
end
|
||||
|
||||
test "missing scopes and bad keys are reported" do
|
||||
@fake.scopes = %w[profile.read flight.create.own]
|
||||
connect
|
||||
follow_redirect!
|
||||
assert_match "missing scopes", response.body
|
||||
assert_match "tracks.write", response.body
|
||||
|
||||
@user.air_trail_connection.destroy!
|
||||
connect(key: "wrong")
|
||||
follow_redirect!
|
||||
assert_match "Invalid API key", response.body
|
||||
end
|
||||
|
||||
test "updating keeps the stored key when left blank; sync all runs a backfill; disconnect" do
|
||||
connect
|
||||
patch air_trail_connection_path, params: { air_trail_connection: { base_url: FakeAirTrail::BASE, api_key: "" } }
|
||||
assert_equal "at_secret", @user.air_trail_connection.reload.api_key
|
||||
|
||||
assert_enqueued_with(job: AirTrailBackfillJob) { post sync_air_trail_connection_path }
|
||||
delete air_trail_connection_path
|
||||
assert_nil @user.reload.air_trail_connection
|
||||
end
|
||||
|
||||
test "flights by users without a connection don't enqueue anything" do
|
||||
assert_no_enqueued_jobs(only: AirTrailPushJob) do
|
||||
@a.flight_logs.create!(date: Date.current, tach_time: 1, user: @user, departure_location: "KOQU", destination_location: "KBID")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -82,6 +82,38 @@ class PagesTest < ActionDispatch::IntegrationTest
|
||||
assert_select "a[href='https://drs.faa.gov/browse/excelExternalWindow/ABC.0001']", minimum: 2 # status + history
|
||||
end
|
||||
|
||||
test "new flight pre-fills From with the last flight's destination and offers airport search" do
|
||||
@a.flight_logs.create!(date: Date.current - 1, tach_time: 1.0, departure_location: "KOQU", destination_location: "kbid")
|
||||
get new_aircraft_flight_log_path(@a)
|
||||
assert_select "input[name='flight_log[departure_location]'][value='KBID']"
|
||||
assert_select "div[data-controller='airport-autocomplete'][data-airport-autocomplete-url-value='#{airport_search_path(aircraft_id: @a.id)}']", count: 2
|
||||
|
||||
AirportData.sync!(StringIO.new("ident,type,name,latitude_deg,longitude_deg,elevation_ft,iso_country,iso_region,municipality,gps_code,iata_code,local_code\nKBID,small_airport,Block Island State Airport,41.1,-71.5,108,US,US-RI,Block Island,KBID,BID,BID\n88LL,small_airport,Block Airport,41.9,-89.1,800,US,US-IL,Pearl City,88LL,,88LL\n"))
|
||||
get airport_search_path(q: "block", aircraft_id: @a.id)
|
||||
assert_equal %w[KBID 88LL], response.parsed_body.map { _1["code"] } # flown-to airport first
|
||||
end
|
||||
|
||||
test "owner standardizes airport codes from the Flights tab" do
|
||||
AirportData.sync!(StringIO.new("ident,type,name,latitude_deg,longitude_deg,elevation_ft,iso_country,iso_region,municipality,gps_code,iata_code,local_code\nK1B9,small_airport,Mansfield Municipal Airport,42,-71,122,US,US-MA,Mansfield,1B9,,1B9\n"))
|
||||
@a.flight_logs.create!(date: Date.current, tach_time: 1, departure_location: "1B9", destination_location: "1B9")
|
||||
get aircraft_flight_logs_path(@a)
|
||||
assert_select "a[href='#{aircraft_airport_codes_path(@a)}']"
|
||||
get aircraft_airport_codes_path(@a)
|
||||
assert_select "input[name='codes[]'][value='1B9'][checked]"
|
||||
patch aircraft_airport_codes_path(@a), params: { codes: [ "1B9" ] }
|
||||
assert_redirected_to aircraft_flight_logs_path(@a)
|
||||
assert_equal [ "K1B9", "K1B9" ], @a.flight_logs.last.slice(:departure_location, :destination_location).values
|
||||
|
||||
# a typo'd code: suggestion pre-filled, fixed only when ticked
|
||||
@a.flight_logs.create!(date: Date.current, tach_time: 1, departure_location: "K1B9", destination_location: "1K9B")
|
||||
get aircraft_airport_codes_path(@a)
|
||||
assert_select "input[name='fixes[1K9B]']"
|
||||
patch aircraft_airport_codes_path(@a), params: { fixes: { "1K9B" => "1B9" } } # not ticked
|
||||
assert_equal "1K9B", @a.flight_logs.last.destination_location
|
||||
patch aircraft_airport_codes_path(@a), params: { fix: [ "1K9B" ], fixes: { "1K9B" => "1B9" } }
|
||||
assert_equal "K1B9", @a.flight_logs.last.destination_location
|
||||
end
|
||||
|
||||
test "flights tab shows flying stats" do
|
||||
get aircraft_flight_logs_path(@a)
|
||||
assert_select "h2", text: "Flight hours by month"
|
||||
|
||||
Reference in new issue
Block a user