· 7 years ago · Sep 22, 2018, 07:10 PM
1require 'test_helper'
2
3# This test class allows us to test CarrierWave specific behaviour, the application relies on.
4class CarrierWaveTest < ActiveSupport::TestCase
5
6 # We define an artificial ActiveRecord class using CarrierWave and fog.
7 class TestUser < ActiveRecord::Base
8 mount_uploader :avatar do
9 storage :fog
10 def store_dir
11 "tmp/test_users/#{model.id}"
12 end
13 end
14 end
15
16 def setup
17 # In setup we create the table TestUser class needs.
18 ActiveRecord::Base.connection.execute("CREATE TEMPORARY TABLE test_users (id INT NOT NULL AUTO_INCREMENT, avatar VARCHAR(255), PRIMARY KEY (id))")
19
20 # Create a mock version of AWS_S3_BUCKET, unless it already exists.
21 Fog.mock!
22 new_fog_connection.directories.create(:key => AWS_S3_BUCKET) unless get_fog_directory
23
24 # Check mock repository is empty (test is making this assumption).
25 assert_equal 0, get_file_count
26 end
27
28 def teardown
29 # Destroy Test User instances (to delete attached files - cleaning fog directory)
30 Fog.unmock!
31 TestUser.destroy_all
32
33 # Drop TestUser table, we don't need it anymore.
34 ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE test_users")
35 end
36
37 # With CarrierWave 0.6, the attached file is saved in real repository even if fog in mocked, if fog was unmock and used before.
38 test "CarrierWave writes in mock repository when fog is mocked, even if fog was unmocked before" do
39
40 # Use CarrierWave with an unmock fog
41 Fog.unmock!
42 original_count = get_file_count
43 create_test_user
44 files_in_real_repository = get_file_count
45 Fog.mock!
46 files_in_mock_repository = get_file_count
47
48 # We should find one additional file in real repository, and none in mock one
49 assert_equal [0, original_count + 1], [files_in_mock_repository, files_in_real_repository]
50
51 # Use CarrierWave with mock fog
52 create_test_user
53 files_in_mock_repository = get_file_count
54 Fog.unmock!
55 files_in_real_repository = get_file_count
56
57 # We should find one additional file in mock repository
58 assert_equal [1, original_count + 1], [files_in_mock_repository, files_in_real_repository]
59 end
60
61 # Return a new fog connection.
62 # In the class, we use a new fog connection for each operation to maximize isolation between each fog command.
63 def new_fog_connection
64 return Fog::Storage.new(
65 provider: 'AWS',
66 aws_access_key_id: AWS_ACCESS_KEY_ID,
67 aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
68 region: AWS_S3_REGION
69 )
70 end
71
72 # Return fog directory from a newly created connection.
73 def get_fog_directory
74 return new_fog_connection.directories.get(AWS_S3_BUCKET)
75 end
76
77 # Return file count in fog directory, using a newly created connection.
78 def get_file_count
79 return get_fog_directory.files.size
80 end
81
82 # Create TestUser instance, with an attached CarrierWave file.
83 def create_test_user
84 user = TestUser.new do |u|
85 u.avatar = File.open("test/fixtures/images/portray.jpg")
86 end
87 assert user.save
88 end
89end