added backup/restore feature
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
require "rails_helper"
|
||||
require "stringio"
|
||||
require "zlib"
|
||||
|
||||
RSpec.describe "Backups" do
|
||||
describe "unauthenticated access" do
|
||||
it "redirects backup pages to identification" do
|
||||
get backup_path
|
||||
|
||||
expect(response).to redirect_to(identify_path)
|
||||
end
|
||||
|
||||
it "does not download backups" do
|
||||
get download_backup_path
|
||||
|
||||
expect(response).to redirect_to(identify_path)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /backup" do
|
||||
before { identify_user }
|
||||
|
||||
it "renders the backup and restore page" do
|
||||
get backup_path
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(response.body).to include("Backup / Restore")
|
||||
expect(response.body).to include("Download backup")
|
||||
expect(response.body).to include("Restore backup")
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /backup/download" do
|
||||
before { identify_user }
|
||||
|
||||
it "downloads app data as gzip-compressed YAML" do
|
||||
user = create(:user, name: "Driver", email: "driver@example.com")
|
||||
vehicle = create(:vehicle, vin: "ABC123", licence_plate: "FLT1234")
|
||||
create(:maintenance_schedule, vehicle: vehicle, maintenance_type: "Oil Change")
|
||||
create(:fuel_entry, vehicle: vehicle, updated_by_user: user, odometer: 10_500)
|
||||
create(:maintenance_entry, vehicle: vehicle, updated_by_user: user, name: "Oil Change", odometer: 10_600, ensure_matching_schedule: false)
|
||||
AccessKey.current
|
||||
UserSession.create_for!(user)
|
||||
|
||||
get download_backup_path
|
||||
|
||||
backup = YAML.safe_load(gunzip(response.body), aliases: false)
|
||||
vehicle_data = backup.fetch("vehicles").first
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(response.media_type).to eq("application/gzip")
|
||||
expect(response.headers.fetch("Content-Disposition")).to include(".yml.gz")
|
||||
expect(backup.fetch("format")).to eq("car_tracker_backup")
|
||||
expect(backup.fetch("version")).to eq(1)
|
||||
expect(vehicle_data.fetch("vin")).to eq("ABC123")
|
||||
expect(vehicle_data.fetch("fuel_entries").first.fetch("updated_by_user").fetch("email")).to eq("driver@example.com")
|
||||
expect(gunzip(response.body)).not_to include("access_keys")
|
||||
expect(gunzip(response.body)).not_to include("user_sessions")
|
||||
end
|
||||
end
|
||||
|
||||
describe "dashboard link" do
|
||||
before { identify_user }
|
||||
|
||||
it "shows backup and restore on the main dashboard" do
|
||||
get vehicles_path
|
||||
|
||||
expect(response.body).to include("Backup / Restore")
|
||||
expect(response.body).to include(backup_path)
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /backup/restore" do
|
||||
before { identify_user }
|
||||
|
||||
it "merges backup data into existing records" do
|
||||
existing_user = create(:user, name: "Old Name", email: "driver@example.com")
|
||||
existing_vehicle = create(:vehicle, vin: "ABC123", licence_plate: "OLD123", make: "Old", current_odometer: 1_000)
|
||||
existing_schedule = create(:maintenance_schedule, vehicle: existing_vehicle, maintenance_type: "Oil Change", mileage_interval: 3_000)
|
||||
|
||||
expect do
|
||||
post restore_backup_path, params: {
|
||||
mode: "merge",
|
||||
backup_file: uploaded_backup(backup_payload)
|
||||
}
|
||||
end.to change(FuelEntry, :count).by(1)
|
||||
.and change(MaintenanceEntry, :count).by(1)
|
||||
|
||||
expect(response).to redirect_to(backup_path)
|
||||
expect(existing_user.reload.name).to eq("Driver")
|
||||
expect(existing_vehicle.reload.make).to eq("Ford")
|
||||
expect(existing_vehicle.licence_plate).to eq("FLT1234")
|
||||
expect(existing_vehicle.current_odometer).to eq(10_600)
|
||||
expect(existing_schedule.reload.mileage_interval).to eq(5_000)
|
||||
expect(Vehicle.count).to eq(1)
|
||||
expect(User.count).to eq(2)
|
||||
end
|
||||
|
||||
it "does not duplicate matching entries during merge" do
|
||||
post restore_backup_path, params: { mode: "merge", backup_file: uploaded_backup(backup_payload) }
|
||||
post restore_backup_path, params: { mode: "merge", backup_file: uploaded_backup(backup_payload) }
|
||||
|
||||
expect(Vehicle.count).to eq(1)
|
||||
expect(FuelEntry.count).to eq(1)
|
||||
expect(MaintenanceEntry.count).to eq(1)
|
||||
end
|
||||
|
||||
it "replaces existing app data" do
|
||||
create(:vehicle, vin: "OLDVIN", licence_plate: "OLD999")
|
||||
create(:user, email: "old@example.com")
|
||||
AccessKey.current
|
||||
|
||||
post restore_backup_path, params: {
|
||||
mode: "replace",
|
||||
backup_file: uploaded_backup(backup_payload)
|
||||
}
|
||||
|
||||
expect(response).to redirect_to(backup_path)
|
||||
expect(Vehicle.pluck(:vin)).to eq([ "ABC123" ])
|
||||
expect(User.pluck(:email)).to contain_exactly("driver@example.com")
|
||||
expect(AccessKey.count).to eq(1)
|
||||
end
|
||||
|
||||
it "rejects invalid gzip uploads" do
|
||||
expect do
|
||||
post restore_backup_path, params: {
|
||||
mode: "merge",
|
||||
backup_file: uploaded_file("not gzip")
|
||||
}
|
||||
end.not_to change(Vehicle, :count)
|
||||
|
||||
expect(response).to redirect_to(backup_path)
|
||||
expect(flash[:alert]).to include("gzip-compressed YAML")
|
||||
end
|
||||
|
||||
it "rejects unsupported backup versions" do
|
||||
payload = backup_payload.merge("version" => 0)
|
||||
|
||||
post restore_backup_path, params: {
|
||||
mode: "merge",
|
||||
backup_file: uploaded_backup(payload)
|
||||
}
|
||||
|
||||
expect(response).to redirect_to(backup_path)
|
||||
expect(flash[:alert]).to include("does not have an available migration path")
|
||||
end
|
||||
end
|
||||
|
||||
def backup_payload
|
||||
{
|
||||
"format" => "car_tracker_backup",
|
||||
"version" => 1,
|
||||
"exported_at" => Time.current.iso8601,
|
||||
"vehicles" => [
|
||||
{
|
||||
"vin" => "ABC123",
|
||||
"licence_plate" => "FLT1234",
|
||||
"make" => "Ford",
|
||||
"model" => "F-150",
|
||||
"year" => Date.current.year,
|
||||
"color" => "White",
|
||||
"current_odometer" => 10_000,
|
||||
"fuel_tank_size" => "26.0",
|
||||
"active" => true,
|
||||
"maintenance_schedules" => [
|
||||
{
|
||||
"maintenance_type" => "Oil Change",
|
||||
"mileage_interval" => 5_000,
|
||||
"time_interval" => 180,
|
||||
"baseline_odometer" => 10_000,
|
||||
"baseline_date" => Date.current.iso8601,
|
||||
"active" => true,
|
||||
"last_status" => nil,
|
||||
"last_status_changed_at" => nil
|
||||
}
|
||||
],
|
||||
"fuel_entries" => [
|
||||
{
|
||||
"date" => Date.current.iso8601,
|
||||
"odometer" => 10_500,
|
||||
"gallons_pumped" => "18.25",
|
||||
"price_paid" => "47.62",
|
||||
"updated_by_user" => {
|
||||
"name" => "Driver",
|
||||
"email" => "driver@example.com"
|
||||
}
|
||||
}
|
||||
],
|
||||
"maintenance_entries" => [
|
||||
{
|
||||
"name" => "Oil Change",
|
||||
"notes" => "Oil change performed.",
|
||||
"date" => Date.current.iso8601,
|
||||
"odometer" => 10_600,
|
||||
"cost" => "89.95",
|
||||
"updated_by_user" => {
|
||||
"name" => "Driver",
|
||||
"email" => "driver@example.com"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
def uploaded_backup(payload)
|
||||
uploaded_file(gzip(payload.to_yaml))
|
||||
end
|
||||
|
||||
def uploaded_file(content)
|
||||
file = Tempfile.new([ "backup", ".yml.gz" ])
|
||||
file.binmode
|
||||
file.write(content)
|
||||
file.rewind
|
||||
Rack::Test::UploadedFile.new(file.path, "application/gzip")
|
||||
end
|
||||
|
||||
def gzip(content)
|
||||
buffer = StringIO.new
|
||||
Zlib::GzipWriter.wrap(buffer) { |gzip| gzip.write(content) }
|
||||
buffer.string
|
||||
end
|
||||
|
||||
def gunzip(content)
|
||||
Zlib::GzipReader.wrap(StringIO.new(content), &:read)
|
||||
end
|
||||
|
||||
def identify_user(user = create(:user))
|
||||
@identified_user = user
|
||||
access_key = AccessKey.current
|
||||
post session_path, params: { name: user.name, email: user.email, key: access_key.raw_key }
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user