|
1 | 1 | require "rails_helper" |
2 | 2 |
|
3 | 3 | RSpec.describe "Attachments", type: :request do |
| 4 | + include ActionDispatch::TestProcess |
| 5 | + |
| 6 | + let(:admin_user) do |
| 7 | + User.create!( |
| 8 | + first_name: "Admin", |
| 9 | + last_name: "User", |
| 10 | + email: "admin@example.com", |
| 11 | + password: "password", |
| 12 | + roles: {"admin" => true} |
| 13 | + ) |
| 14 | + end |
| 15 | + |
| 16 | + let(:post_record) do |
| 17 | + Post.create!(name: "Hello", body: "World", user: admin_user) |
| 18 | + end |
| 19 | + |
| 20 | + def build_upload(content:, original_filename:, content_type:) |
| 21 | + tmp = Tempfile.new(["upload", File.extname(original_filename)]) |
| 22 | + tmp.binmode |
| 23 | + tmp.write(content) |
| 24 | + tmp.rewind |
| 25 | + |
| 26 | + Rack::Test::UploadedFile.new(tmp.path, content_type, original_filename: original_filename) |
| 27 | + end |
| 28 | + |
| 29 | + it "denies has_one_attached upload when upload_cover_photo? is false" do |
| 30 | + sign_in admin_user |
| 31 | + |
| 32 | + post_record.cover_photo.attach( |
| 33 | + io: StringIO.new("old"), |
| 34 | + filename: "old.txt", |
| 35 | + content_type: "text/plain" |
| 36 | + ) |
| 37 | + |
| 38 | + allow_any_instance_of(Avo::Services::AuthorizationService) |
| 39 | + .to receive(:authorize_action) |
| 40 | + .with("upload_cover_photo?", record: post_record, raise_exception: false) |
| 41 | + .and_return(false) |
| 42 | + |
| 43 | + upload = build_upload(content: "new", original_filename: "new.txt", content_type: "text/plain") |
| 44 | + old_blob_id = post_record.reload.cover_photo.blob_id |
| 45 | + old_checksum = post_record.cover_photo.blob.checksum |
| 46 | + |
| 47 | + expect { |
| 48 | + post "/admin/avo_api/resources/posts/#{post_record.to_param}/attachments", |
| 49 | + params: {file: upload, filename: "new.txt", attachment_key: "cover_photo"}, |
| 50 | + headers: {"ACCEPT" => "application/json"} |
| 51 | + }.not_to change { ActiveStorage::Blob.count } |
| 52 | + |
| 53 | + post_record.reload |
| 54 | + expect(post_record.cover_photo).to be_attached |
| 55 | + expect(post_record.cover_photo.blob_id).to eq(old_blob_id) |
| 56 | + expect(post_record.cover_photo.blob.checksum).to eq(old_checksum) |
| 57 | + |
| 58 | + expect(response).to have_http_status(:forbidden) |
| 59 | + end |
| 60 | + |
| 61 | + it "denies has_many_attached upload when upload_attachments? is false" do |
| 62 | + sign_in admin_user |
| 63 | + |
| 64 | + allow_any_instance_of(Avo::Services::AuthorizationService) |
| 65 | + .to receive(:authorize_action) |
| 66 | + .with("upload_attachments?", record: post_record, raise_exception: false) |
| 67 | + .and_return(false) |
| 68 | + |
| 69 | + upload = build_upload(content: "one", original_filename: "one.txt", content_type: "text/plain") |
| 70 | + |
| 71 | + expect { |
| 72 | + post "/admin/avo_api/resources/posts/#{post_record.to_param}/attachments", |
| 73 | + params: {file: upload, filename: "one.txt", attachment_key: "attachments"}, |
| 74 | + headers: {"ACCEPT" => "application/json"} |
| 75 | + }.not_to change { ActiveStorage::Blob.count } |
| 76 | + |
| 77 | + expect(post_record.reload.attachments.count).to eq(0) |
| 78 | + expect(response).to have_http_status(:forbidden) |
| 79 | + end |
| 80 | + |
| 81 | + it "denies key/Trix-style upload when no attachment association is resolved" do |
| 82 | + sign_in admin_user |
| 83 | + |
| 84 | + allow_any_instance_of(Avo::Services::AuthorizationService) |
| 85 | + .to receive(:authorize_action) |
| 86 | + .with("update?", record: post_record, raise_exception: false) |
| 87 | + .and_return(false) |
| 88 | + |
| 89 | + upload = build_upload(content: "trix", original_filename: "trix.txt", content_type: "text/plain") |
| 90 | + |
| 91 | + expect { |
| 92 | + post "/admin/avo_api/resources/posts/#{post_record.to_param}/attachments", |
| 93 | + params: {file: upload, filename: "trix.txt", attachment_key: "missing_field", key: "trixKey"}, |
| 94 | + headers: {"ACCEPT" => "application/json"} |
| 95 | + }.not_to change { ActiveStorage::Blob.count } |
| 96 | + |
| 97 | + expect(response).to have_http_status(:forbidden) |
| 98 | + end |
4 | 99 | end |
0 commit comments