|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "rails_helper" |
| 5 | + |
| 6 | +RSpec.describe ImageProcessable, type: :controller do |
| 7 | + controller(ApplicationController) do |
| 8 | + include ImageProcessable |
| 9 | + |
| 10 | + # Skip authentication for tests |
| 11 | + skip_before_action :require_login |
| 12 | + |
| 13 | + define_method(:create) do |
| 14 | + processed_params = process_image_params(params[:item], :photo, :avatar) |
| 15 | + |
| 16 | + # For testing purposes, we don't re-raise image processing errors |
| 17 | + # The method should handle them gracefully by setting fields to nil |
| 18 | + render json: {params: processed_params}, status: :ok |
| 19 | + end |
| 20 | + |
| 21 | + define_method(:upload) do |
| 22 | + processed_io = process_image(params[:file]) |
| 23 | + render json: {success: true, processed: processed_io.present?} |
| 24 | + rescue ApplicationErrors::NotAnImageError, |
| 25 | + ApplicationErrors::ImageProcessingError => e |
| 26 | + handle_image_error(e) |
| 27 | + end |
| 28 | + |
| 29 | + define_method(:validate) do |
| 30 | + validate_image!(params[:file]) |
| 31 | + render json: {valid: true} |
| 32 | + rescue ApplicationErrors::NotAnImageError => e |
| 33 | + render json: {valid: false, error: e.message}, |
| 34 | + status: :unprocessable_entity |
| 35 | + end |
| 36 | + |
| 37 | + # Test methods for rescue_from handlers |
| 38 | + define_method(:test_not_an_image) do |
| 39 | + raise ApplicationErrors::NotAnImageError.new("Not an image") |
| 40 | + rescue ApplicationErrors::NotAnImageError => e |
| 41 | + handle_image_error(e) |
| 42 | + end |
| 43 | + |
| 44 | + define_method(:test_processing_error) do |
| 45 | + raise ApplicationErrors::ImageProcessingError.new("Processing error") |
| 46 | + rescue ApplicationErrors::ImageProcessingError => e |
| 47 | + handle_image_error(e) |
| 48 | + end |
| 49 | + |
| 50 | + # Test method for handle_image_error |
| 51 | + define_method(:test_error) do |
| 52 | + raise params[:error_type].constantize.new(params[:message]) |
| 53 | + rescue ApplicationErrors::NotAnImageError, |
| 54 | + ApplicationErrors::ImageProcessingError => e |
| 55 | + handle_image_error(e) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + before do |
| 60 | + routes.draw do |
| 61 | + post "create" => "anonymous#create" |
| 62 | + post "upload" => "anonymous#upload" |
| 63 | + post "validate" => "anonymous#validate" |
| 64 | + post "test_error" => "anonymous#test_error" |
| 65 | + post "test_not_an_image" => "anonymous#test_not_an_image" |
| 66 | + post "test_processing_error" => "anonymous#test_processing_error" |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe "#process_image_params" do |
| 71 | + let(:valid_image) do |
| 72 | + fixture_file_upload("spec/fixtures/files/test_image.jpg", "image/jpeg") |
| 73 | + end |
| 74 | + |
| 75 | + let(:invalid_file) do |
| 76 | + fixture_file_upload("spec/fixtures/files/test.txt", "text/plain") |
| 77 | + end |
| 78 | + |
| 79 | + context "when processing valid image fields" do |
| 80 | + it "processes single image field successfully" do |
| 81 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 82 | + allow(PhotoProcessingService).to receive(:process_upload).and_return( |
| 83 | + StringIO.new("processed_image_data") |
| 84 | + ) |
| 85 | + |
| 86 | + post :create, params: {item: {photo: valid_image, name: "Test"}} |
| 87 | + |
| 88 | + expect(response).to have_http_status(:ok) |
| 89 | + parsed_response = JSON.parse(response.body) |
| 90 | + expect(parsed_response["params"]["name"]).to eq("Test") |
| 91 | + end |
| 92 | + |
| 93 | + it "processes multiple image fields" do |
| 94 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 95 | + allow(PhotoProcessingService).to receive(:process_upload).and_return( |
| 96 | + StringIO.new("processed_image_data") |
| 97 | + ) |
| 98 | + |
| 99 | + post :create, params: { |
| 100 | + item: { |
| 101 | + photo: valid_image, |
| 102 | + avatar: valid_image, |
| 103 | + name: "Test" |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + expect(response).to have_http_status(:ok) |
| 108 | + expect(PhotoProcessingService).to have_received(:process_upload).twice |
| 109 | + end |
| 110 | + |
| 111 | + it "skips blank image fields" do |
| 112 | + allow(PhotoProcessingService).to receive(:valid_image?) |
| 113 | + allow(PhotoProcessingService).to receive(:process_upload) |
| 114 | + |
| 115 | + post :create, params: {item: {photo: nil, avatar: "", name: "Test"}} |
| 116 | + |
| 117 | + expect(response).to have_http_status(:ok) |
| 118 | + expect(PhotoProcessingService).not_to have_received(:valid_image?) |
| 119 | + expect(PhotoProcessingService).not_to have_received(:process_upload) |
| 120 | + end |
| 121 | + |
| 122 | + it "skips non-file parameters" do |
| 123 | + allow(PhotoProcessingService).to receive(:valid_image?) |
| 124 | + allow(PhotoProcessingService).to receive(:process_upload) |
| 125 | + |
| 126 | + post :create, params: {item: {photo: "just_a_string", name: "Test"}} |
| 127 | + |
| 128 | + expect(response).to have_http_status(:ok) |
| 129 | + expect(PhotoProcessingService).not_to have_received(:valid_image?) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + context "when processing invalid images" do |
| 134 | + it "sets field to nil when image is invalid" do |
| 135 | + allow(PhotoProcessingService).to receive(:valid_image?) |
| 136 | + .and_return(false) |
| 137 | + |
| 138 | + post :create, params: {item: {photo: invalid_file, name: "Test"}} |
| 139 | + |
| 140 | + expect(response).to have_http_status(:ok) |
| 141 | + parsed_response = JSON.parse(response.body) |
| 142 | + expect(parsed_response["params"]["photo"]).to be_nil |
| 143 | + end |
| 144 | + |
| 145 | + it "sets field to nil when processing fails" do |
| 146 | + allow(PhotoProcessingService).to receive(:valid_image?) |
| 147 | + .and_return(true) |
| 148 | + allow(PhotoProcessingService).to receive(:process_upload) |
| 149 | + .and_return(nil) |
| 150 | + |
| 151 | + post :create, params: {item: {photo: valid_image, name: "Test"}} |
| 152 | + |
| 153 | + expect(response).to have_http_status(:ok) |
| 154 | + parsed_response = JSON.parse(response.body) |
| 155 | + expect(parsed_response["params"]["photo"]).to be_nil |
| 156 | + end |
| 157 | + |
| 158 | + it "handles ApplicationErrors::NotAnImageError" do |
| 159 | + allow(PhotoProcessingService).to receive(:valid_image?) |
| 160 | + .and_return(false) |
| 161 | + |
| 162 | + expect { |
| 163 | + post :create, params: {item: {photo: invalid_file}} |
| 164 | + }.not_to raise_error |
| 165 | + |
| 166 | + expect(response).to have_http_status(:ok) |
| 167 | + end |
| 168 | + |
| 169 | + it "handles ApplicationErrors::ImageProcessingError" do |
| 170 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 171 | + allow(PhotoProcessingService).to receive(:process_upload).and_raise( |
| 172 | + ApplicationErrors::ImageProcessingError.new("Processing failed") |
| 173 | + ) |
| 174 | + |
| 175 | + expect { |
| 176 | + post :create, params: {item: {photo: valid_image}} |
| 177 | + }.not_to raise_error |
| 178 | + |
| 179 | + expect(response).to have_http_status(:ok) |
| 180 | + end |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + describe "#process_image" do |
| 185 | + let(:valid_image) do |
| 186 | + fixture_file_upload("spec/fixtures/files/test_image.jpg", "image/jpeg") |
| 187 | + end |
| 188 | + |
| 189 | + context "when image is valid" do |
| 190 | + it "returns processed image IO" do |
| 191 | + processed_io = StringIO.new("processed_data") |
| 192 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 193 | + allow(PhotoProcessingService).to receive(:process_upload) |
| 194 | + .and_return(processed_io) |
| 195 | + |
| 196 | + post :upload, params: {file: valid_image} |
| 197 | + |
| 198 | + expect(response).to have_http_status(:ok) |
| 199 | + parsed_response = JSON.parse(response.body) |
| 200 | + expect(parsed_response["success"]).to be true |
| 201 | + expect(parsed_response["processed"]).to be true |
| 202 | + end |
| 203 | + end |
| 204 | + |
| 205 | + context "when image is invalid" do |
| 206 | + it "raises NotAnImageError" do |
| 207 | + allow(PhotoProcessingService).to receive(:valid_image?) |
| 208 | + .and_return(false) |
| 209 | + |
| 210 | + post :upload, params: {file: valid_image} |
| 211 | + |
| 212 | + expect(response).to redirect_to(root_path) |
| 213 | + msg = I18n.t("errors.messages.invalid_image_format") |
| 214 | + expect(flash[:alert]).to eq(msg) |
| 215 | + end |
| 216 | + end |
| 217 | + |
| 218 | + context "when processing fails" do |
| 219 | + it "raises ImageProcessingError when process_upload returns nil" do |
| 220 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 221 | + allow(PhotoProcessingService).to receive(:process_upload) |
| 222 | + .and_return(nil) |
| 223 | + |
| 224 | + post :upload, params: {file: valid_image} |
| 225 | + |
| 226 | + expect(response).to redirect_to(root_path) |
| 227 | + msg = I18n.t("errors.messages.image_processing_failed") |
| 228 | + expect(flash[:alert]).to eq(msg) |
| 229 | + end |
| 230 | + |
| 231 | + it "handles Vips::Error and logs the error" do |
| 232 | + vips_error = Vips::Error.new("Vips processing failed") |
| 233 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 234 | + allow(PhotoProcessingService).to receive(:process_upload) |
| 235 | + .and_raise(vips_error) |
| 236 | + allow(Rails.logger).to receive(:error) |
| 237 | + |
| 238 | + post :upload, params: {file: valid_image} |
| 239 | + |
| 240 | + error_msg = "Image processing failed: Vips processing failed" |
| 241 | + expect(Rails.logger).to have_received(:error).with(error_msg) |
| 242 | + expect(response).to redirect_to(root_path) |
| 243 | + expect(flash[:alert]).to include("Vips processing failed") |
| 244 | + end |
| 245 | + end |
| 246 | + end |
| 247 | + |
| 248 | + describe "#validate_image!" do |
| 249 | + let(:valid_image) do |
| 250 | + fixture_file_upload("spec/fixtures/files/test_image.jpg", "image/jpeg") |
| 251 | + end |
| 252 | + |
| 253 | + it "does not raise error for valid image" do |
| 254 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 255 | + |
| 256 | + post :validate, params: {file: valid_image} |
| 257 | + |
| 258 | + expect(response).to have_http_status(:ok) |
| 259 | + parsed_response = JSON.parse(response.body) |
| 260 | + expect(parsed_response["valid"]).to be true |
| 261 | + end |
| 262 | + |
| 263 | + it "raises NotAnImageError for invalid image" do |
| 264 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(false) |
| 265 | + |
| 266 | + post :validate, params: {file: valid_image} |
| 267 | + |
| 268 | + expect(response).to have_http_status(:unprocessable_entity) |
| 269 | + parsed_response = JSON.parse(response.body) |
| 270 | + expect(parsed_response["valid"]).to be false |
| 271 | + msg = I18n.t("errors.messages.invalid_image_format") |
| 272 | + expect(parsed_response["error"]).to eq(msg) |
| 273 | + end |
| 274 | + end |
| 275 | + |
| 276 | + describe "#handle_image_error" do |
| 277 | + context "with HTML format" do |
| 278 | + it "sets flash alert and redirects to root" do |
| 279 | + post :test_error, params: { |
| 280 | + error_type: "ApplicationErrors::NotAnImageError", |
| 281 | + message: "Invalid image" |
| 282 | + } |
| 283 | + |
| 284 | + expect(response).to redirect_to(root_path) |
| 285 | + expect(flash[:alert]).to eq("Invalid image") |
| 286 | + end |
| 287 | + |
| 288 | + it "redirects to fallback location" do |
| 289 | + request.env["HTTP_REFERER"] = "/previous_page" |
| 290 | + |
| 291 | + post :test_error, params: { |
| 292 | + error_type: |
| 293 | + "ApplicationErrors::ImageProcessingError", |
| 294 | + message: "Processing failed" |
| 295 | + } |
| 296 | + |
| 297 | + expect(response).to redirect_to("/previous_page") |
| 298 | + expect(flash[:alert]).to eq("Processing failed") |
| 299 | + end |
| 300 | + end |
| 301 | + |
| 302 | + context "with turbo_stream format" do |
| 303 | + it "sets flash.now and redirects with see_other status" do |
| 304 | + post :test_error, params: { |
| 305 | + error_type: "ApplicationErrors::NotAnImageError", |
| 306 | + message: "Invalid image" |
| 307 | + }, format: :turbo_stream |
| 308 | + |
| 309 | + expect(response).to have_http_status(:see_other) |
| 310 | + expect(response).to redirect_to(root_path) |
| 311 | + expect(flash.now[:alert]).to eq("Invalid image") |
| 312 | + end |
| 313 | + end |
| 314 | + end |
| 315 | + |
| 316 | + describe "rescue_from handlers" do |
| 317 | + it "rescues from NotAnImageError" do |
| 318 | + post :test_not_an_image |
| 319 | + |
| 320 | + expect(response).to redirect_to(root_path) |
| 321 | + expect(flash[:alert]).to eq("Not an image") |
| 322 | + end |
| 323 | + |
| 324 | + it "rescues from ImageProcessingError" do |
| 325 | + post :test_processing_error |
| 326 | + |
| 327 | + expect(response).to redirect_to(root_path) |
| 328 | + expect(flash[:alert]).to eq("Processing error") |
| 329 | + end |
| 330 | + end |
| 331 | + |
| 332 | + describe "integration with ActionController::Parameters" do |
| 333 | + let(:valid_image) do |
| 334 | + fixture_file_upload("spec/fixtures/files/test_image.jpg", "image/jpeg") |
| 335 | + end |
| 336 | + |
| 337 | + it "works with strong parameters" do |
| 338 | + allow(PhotoProcessingService).to receive(:valid_image?).and_return(true) |
| 339 | + allow(PhotoProcessingService).to receive(:process_upload).and_return( |
| 340 | + StringIO.new("processed_image_data") |
| 341 | + ) |
| 342 | + |
| 343 | + controller.params = ActionController::Parameters.new( |
| 344 | + item: {photo: valid_image, name: "Test"} |
| 345 | + ) |
| 346 | + |
| 347 | + result = controller.send(:process_image_params, |
| 348 | + controller.params[:item], :photo) |
| 349 | + |
| 350 | + expect(result).to be_a(ActionController::Parameters) |
| 351 | + expect(result[:name]).to eq("Test") |
| 352 | + end |
| 353 | + end |
| 354 | +end |
0 commit comments