working on image feature

This commit is contained in:
2026-10-01 09:56:32 -06:00
parent 7d4e446d4a
commit 23d8154f17
26 changed files with 1259 additions and 57 deletions
+84 -2
View File
@@ -1,5 +1,8 @@
require "rails_helper"
require "base64"
require "digest/md5"
require "stringio"
require "tempfile"
require "zlib"
RSpec.describe "Backups" do
@@ -37,7 +40,8 @@ RSpec.describe "Backups" 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)
fuel_entry = create(:fuel_entry, vehicle: vehicle, updated_by_user: user, odometer: 10_500)
fuel_entry.images.attach(image_attachment("receipt.png"))
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)
@@ -51,11 +55,34 @@ RSpec.describe "Backups" do
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(backup.fetch("version")).to eq(2)
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")
expect(gunzip(response.body)).not_to include("data_base64")
end
end
describe "GET /backup/download_with_images" do
before { identify_user }
it "downloads app data and image content" do
user = create(:user, name: "Driver", email: "driver@example.com")
vehicle = create(:vehicle, vin: "ABC123", licence_plate: "FLT1234")
fuel_entry = create(:fuel_entry, vehicle: vehicle, updated_by_user: user, odometer: 10_500)
fuel_entry.images.attach(image_attachment("receipt.png"))
get download_with_images_backup_path
backup = YAML.safe_load(gunzip(response.body), aliases: false)
image_data = backup.fetch("vehicles").first.fetch("fuel_entries").first.fetch("images").first
expect(response).to have_http_status(:ok)
expect(response.headers.fetch("Content-Disposition")).to include("with-images")
expect(image_data.fetch("filename")).to eq("receipt.png")
expect(image_data.fetch("content_type")).to eq("image/png")
expect(Base64.strict_decode64(image_data.fetch("data_base64"))).to eq(png_bytes)
end
end
@@ -144,6 +171,35 @@ RSpec.describe "Backups" do
expect(response).to redirect_to(backup_path)
expect(flash[:alert]).to include("does not have an available migration path")
end
it "restores image-inclusive backups" do
payload = backup_payload_with_images
expect do
post restore_backup_path, params: {
mode: "replace",
backup_file: uploaded_backup(payload)
}
end.to change(FuelEntry, :count).by(1)
expect(response).to redirect_to(backup_path)
expect(FuelEntry.last.images.count).to eq(1)
expect(FuelEntry.last.images.first.filename.to_s).to eq("receipt.png")
end
it "deduplicates imported images by filename and checksum during merge" do
post restore_backup_path, params: {
mode: "merge",
backup_file: uploaded_backup(backup_payload_with_images)
}
post restore_backup_path, params: {
mode: "merge",
backup_file: uploaded_backup(backup_payload_with_images)
}
expect(FuelEntry.count).to eq(1)
expect(FuelEntry.last.images.count).to eq(1)
end
end
def backup_payload
@@ -216,6 +272,32 @@ RSpec.describe "Backups" do
Rack::Test::UploadedFile.new(file.path, "application/gzip")
end
def backup_payload_with_images
payload = backup_payload.deep_dup.merge("version" => 2)
payload.fetch("vehicles").first.fetch("fuel_entries").first["images"] = [
{
"filename" => "receipt.png",
"content_type" => "image/png",
"checksum" => Digest::MD5.base64digest(png_bytes),
"byte_size" => png_bytes.bytesize,
"data_base64" => Base64.strict_encode64(png_bytes)
}
]
payload
end
def image_attachment(filename)
{
io: StringIO.new(png_bytes),
filename: filename,
content_type: "image/png"
}
end
def png_bytes
Base64.decode64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=")
end
def gzip(content)
buffer = StringIO.new
Zlib::GzipWriter.wrap(buffer) { |gzip| gzip.write(content) }