require "test_helper" require "base64" class MailImagesControllerTest < ActionDispatch::IntegrationTest PNG = Base64.decode64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==") test "admin can upload an image for the editor" do login_as users(:admin) follow_redirect! if response.redirect? upload = png_upload assert_difference -> { ActiveStorage::Blob.count }, 1 do post mail_images_path(project_code: "matchlivetv"), params: { file: upload } end assert_response :success json = JSON.parse(response.body) assert json["url"].present? assert_match(/active_storage/, json["url"]) end test "rejects non image files" do login_as users(:admin) follow_redirect! if response.redirect? file = Tempfile.new(["note", ".txt"]) file.write("not-an-image") file.rewind post mail_images_path(project_code: "matchlivetv"), params: { file: Rack::Test::UploadedFile.new(file.path, "text/plain") } assert_response :unprocessable_entity end private def png_upload file = Tempfile.new(["pixel", ".png"]) file.binmode file.write(PNG) file.rewind Rack::Test::UploadedFile.new(file.path, "image/png") end end