aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/test/controllers/blobs_controller_test.rb
blob: 9bf2641de6b66f62ec6c4101ff87533a63156078 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

require "test_helper"
require "database/setup"

class ActiveStorage::BlobsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @blob = create_file_blob filename: "racecar.jpg"
  end

  test "showing blob with invalid signed ID" do
    get rails_service_blob_url("invalid", "racecar.jpg")
    assert_response :not_found
  end

  test "showing blob utilizes browser caching" do
    get rails_blob_url(@blob)

    assert_redirected_to(/racecar\.jpg/)
    assert_equal "max-age=300, private", @response.headers["Cache-Control"]
  end
end

if SERVICE_CONFIGURATIONS[:s3] && SERVICE_CONFIGURATIONS[:s3][:access_key_id].present?
  class ActiveStorage::S3BlobsControllerTest < ActionDispatch::IntegrationTest
    setup do
      @old_service = ActiveStorage::Blob.service
      ActiveStorage::Blob.service = ActiveStorage::Service.configure(:s3, SERVICE_CONFIGURATIONS)
    end

    teardown do
      ActiveStorage::Blob.service = @old_service
    end

    test "allow redirection to the different host" do
      blob = create_file_blob filename: "racecar.jpg"

      assert_nothing_raised { get rails_blob_url(blob) }
      assert_response :redirect
      assert_no_match @request.host, @response.headers["Location"]
    ensure
      blob.purge
    end
  end
else
  puts "Skipping S3 redirection tests because no S3 configuration was supplied"
end