require 'spec_helper'

describe 'Copying Files', type: :feature do
  let(:image)    { File.open('spec/fixtures/image.png', 'r') }
  let(:original) { FeatureUploader.new }

  it 'copies an existing file to the specified path' do
    original.store!(image)
    original.retrieve_from_store!('image.png')

    original.file.copy_to('uploads/image2.png')

    copy = FeatureUploader.new
    copy.retrieve_from_store!('image2.png')

    original_attributes = original.file.attributes
    original_attributes.reject! { |k,v| k == :last_modified }

    copy_attributes = copy.file.attributes
    copy_attributes.reject! { |k,v| k == :last_modified }

    expect(copy_attributes).to eq(original_attributes)

    image.close
    original.file.delete
    copy.file.delete
  end
end


require 'spec_helper'

describe 'Copying Files', type: :feature do
  let(:image)    { File.open('spec/fixtures/image.png', 'r') }
  let(:original) { FeatureUploader.new }

  it 'copies an existing file to the specified path' do
    original.store!(image)
    original.retrieve_from_store!('image.png')

    original.file.copy_to('uploads/image2.png')

    copy = FeatureUploader.new
    copy.retrieve_from_store!('image2.png')

    original_attributes = original.file.attributes
    original_attributes.reject! { |k,v| k == :last_modified }

    copy_attributes = copy.file.attributes
    copy_attributes.reject! { |k,v| k == :last_modified }

    copy_acl_grants = copy.file.file.acl.grants
    original_acl_grants = original.file.file.acl.grants

    expect(copy_attributes).to eq(original_attributes)
    expect(copy_acl_grants).to eq(original_acl_grants)

    image.close
    original.file.delete
    copy.file.delete
  end
end


Test for matching ACL grants on copied file
Test for matching ACL grants on copied file

Until now we didn't ensure that the files generated with
CarrierWave::Storage::AWSFile#copy_to were given the same ACL
(Access Control List) as the original files.

As explained in https://github.com/sorentwo/carrierwave-aws/issues/81#issue-154100161
the behavior of #copy_to in CarrierWave is use the `fog_public` configuration
option and make files `public-read` (world-accessible) if it is true.

carrierwave-aws uses a different configuration option (`aws_acl`) which
explicitely defines the default ACL for all uploaded files (i.e. `public-read`)
so it makes sense to compare the copied file with the originally uploaded one.

An alternative test method would be to always check that copied files include
an ACL that matches the `aws_acl` but I cannot for the life of me figure out a
way to get aws-sdk-ruby to output the shorthand `acl` (i.e. `public-read` and
not a list of grants) so for now I'm simply checking that the ACL grants for the
copy are equal to those of the original.
