resizing 1.0.0.pre → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +61 -0
- data/Gemfile +6 -2
- data/README.md +59 -32
- data/lib/resizing/active_storage/service/resizing_service.rb +6 -2
- data/lib/resizing/active_storage/service.rb +9 -0
- data/lib/resizing/active_storage.rb +7 -0
- data/lib/resizing/carrier_wave/storage/file.rb +35 -16
- data/lib/resizing/carrier_wave/storage/remote.rb +7 -3
- data/lib/resizing/carrier_wave.rb +10 -11
- data/lib/resizing/client.rb +39 -41
- data/lib/resizing/configurable.rb +1 -1
- data/lib/resizing/configuration.rb +6 -16
- data/lib/resizing/http_clientable.rb +3 -3
- data/lib/resizing/mock_client.rb +6 -5
- data/lib/resizing/public_id.rb +5 -4
- data/lib/resizing/version.rb +1 -1
- data/lib/resizing.rb +16 -12
- data/resizing.gemspec +6 -5
- data/test/resizing/active_storage_service_test.rb +98 -0
- data/test/resizing/carrier_wave/storage/file_test.rb +149 -8
- data/test/resizing/carrier_wave/storage/remote_test.rb +75 -0
- data/test/resizing/carrier_wave_test.rb +99 -37
- data/test/resizing/client_test.rb +96 -11
- data/test/resizing/configurable_test.rb +82 -0
- data/test/resizing/configuration_test.rb +118 -2
- data/test/resizing/constants_test.rb +25 -0
- data/test/resizing/error_test.rb +73 -0
- data/test/resizing/http_clientable_test.rb +84 -0
- data/test/resizing/mock_client_test.rb +75 -0
- data/test/resizing/public_id_test.rb +1 -1
- data/test/resizing_module_test.rb +206 -0
- data/test/test_helper.rb +106 -8
- metadata +75 -27
- data/.circleci/config.yml +0 -73
- data/lib/resizing/video/client.rb +0 -116
- data/lib/resizing/video.rb +0 -8
- data/test/resizing/video/client_test.rb +0 -158
- data/test/vcr/video/metadata/success.yml +0 -47
- data/test/vcr/video/prepare/success.yml +0 -47
- data/test/vcr/video/upload_completed/success.yml +0 -47
- /data/{exe → bin}/console +0 -0
- /data/{exe → bin}/generate-changelog +0 -0
- /data/{exe → bin}/setup +0 -0
|
@@ -27,12 +27,8 @@ module Resizing
|
|
|
27
27
|
def initialize(*attrs)
|
|
28
28
|
case attr = attrs.first
|
|
29
29
|
when Hash
|
|
30
|
-
if attr[:project_id].nil? || attr[:secret_token].nil?
|
|
31
|
-
|
|
32
|
-
end
|
|
33
|
-
if attr[:host].present?
|
|
34
|
-
raise_configiration_error
|
|
35
|
-
end
|
|
30
|
+
raise_configiration_error if attr[:project_id].nil? || attr[:secret_token].nil?
|
|
31
|
+
raise_configiration_error if attr[:host].present?
|
|
36
32
|
|
|
37
33
|
initialize_by_hash attr
|
|
38
34
|
return
|
|
@@ -41,11 +37,6 @@ module Resizing
|
|
|
41
37
|
raise_configiration_error
|
|
42
38
|
end
|
|
43
39
|
|
|
44
|
-
def host
|
|
45
|
-
Kernel.warn "[DEPRECATED] The Configuration#host is deprecated. Use Configuration#image_host."
|
|
46
|
-
self.image_host
|
|
47
|
-
end
|
|
48
|
-
|
|
49
40
|
def generate_auth_header
|
|
50
41
|
current_timestamp = Time.now.to_i
|
|
51
42
|
data = [current_timestamp, secret_token].join('|')
|
|
@@ -102,13 +93,11 @@ module Resizing
|
|
|
102
93
|
raise ConfigurationError, 'need hash and some keys like :image_host, video_host, :project_id, :secret_token'
|
|
103
94
|
end
|
|
104
95
|
|
|
96
|
+
# rubocop:disable Metrics/AbcSize
|
|
105
97
|
def initialize_by_hash(attr)
|
|
106
|
-
|
|
107
|
-
if attr[:host].present?
|
|
108
|
-
Kernel.warn "[DEPRECATED] The host on configration is deprecated. Use image_host, video_host" if attr[:host].present?
|
|
109
|
-
@image_host ||= attr[:host].dup.freeze || DEFAULT_HOST # for backward compatible
|
|
110
|
-
end
|
|
98
|
+
raise 'The host on configuration is deprecated. Use image_host, video_host' if attr[:host].present?
|
|
111
99
|
|
|
100
|
+
@image_host = attr[:image_host].dup.freeze || DEFAULT_IMAGE_HOST
|
|
112
101
|
@video_host = attr[:video_host].dup.freeze || DEFAULT_VIDEO_HOST
|
|
113
102
|
@project_id = attr[:project_id].dup.freeze
|
|
114
103
|
@secret_token = attr[:secret_token].dup.freeze
|
|
@@ -116,5 +105,6 @@ module Resizing
|
|
|
116
105
|
@response_timeout = attr[:response_timeout] || DEFAULT_RESPONSE_TIMEOUT
|
|
117
106
|
@enable_mock = attr[:enable_mock] || false
|
|
118
107
|
end
|
|
108
|
+
# rubocop:enable Metrics/AbcSize
|
|
119
109
|
end
|
|
120
110
|
end
|
|
@@ -12,14 +12,14 @@ module Resizing
|
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
def handle_faraday_error
|
|
15
|
+
def handle_faraday_error
|
|
16
16
|
yield
|
|
17
17
|
rescue Faraday::TimeoutError => e
|
|
18
18
|
handle_timeout_error e
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def handle_timeout_error
|
|
22
|
-
raise APIError
|
|
21
|
+
def handle_timeout_error(error)
|
|
22
|
+
raise APIError, "TimeoutError: #{error.inspect}"
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
end
|
data/lib/resizing/mock_client.rb
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
module Resizing
|
|
4
4
|
class MockClient
|
|
5
|
-
def post(
|
|
5
|
+
def post(_file_or_binary, _options = {})
|
|
6
6
|
r = load_yaml('test/vcr/client/post.yml')
|
|
7
7
|
JSON.parse(r['string'])
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
def put(name,
|
|
10
|
+
def put(name, _file_or_binary, _options)
|
|
11
11
|
r = load_yaml('test/vcr/client/put.yml')
|
|
12
12
|
result = JSON.parse(r['string'])
|
|
13
13
|
# replace name, public_id and version by name argument
|
|
@@ -26,7 +26,7 @@ module Resizing
|
|
|
26
26
|
result
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def metadata(name)
|
|
29
|
+
def metadata(name, _options = {})
|
|
30
30
|
r = load_yaml('test/vcr/client/metadata.yml')
|
|
31
31
|
result = JSON.parse(r['string'])
|
|
32
32
|
# replace name and public_id by name argument
|
|
@@ -34,15 +34,16 @@ module Resizing
|
|
|
34
34
|
result['public_id'].gsub!(/bfdaf2b3-7ec5-41f4-9caa-d53247dd9666/, name)
|
|
35
35
|
result
|
|
36
36
|
end
|
|
37
|
+
|
|
37
38
|
private
|
|
38
39
|
|
|
39
|
-
def load_yaml
|
|
40
|
+
def load_yaml(filename)
|
|
40
41
|
path = "#{library_root}/#{filename}"
|
|
41
42
|
YAML.load_file(path)['http_interactions'].first['response']['body']
|
|
42
43
|
end
|
|
43
44
|
|
|
44
45
|
def library_root
|
|
45
|
-
@library_root ||= File.expand_path('
|
|
46
|
+
@library_root ||= File.expand_path('../..', __dir__)
|
|
46
47
|
end
|
|
47
48
|
end
|
|
48
49
|
end
|
data/lib/resizing/public_id.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Resizing
|
|
2
4
|
class PublicId
|
|
3
|
-
def initialize
|
|
5
|
+
def initialize(public_id)
|
|
4
6
|
@public_id = public_id
|
|
5
7
|
parsed
|
|
6
8
|
end
|
|
@@ -38,13 +40,12 @@ module Resizing
|
|
|
38
40
|
|
|
39
41
|
def parsed
|
|
40
42
|
return nil if @public_id.nil?
|
|
43
|
+
|
|
41
44
|
unless defined? @parsed
|
|
42
45
|
@parsed = Resizing.separate_public_id(@public_id)
|
|
43
|
-
raise "type error #{@public_id}" if @parsed
|
|
46
|
+
raise "type error #{@public_id}" if @parsed.nil?
|
|
44
47
|
end
|
|
45
48
|
@parsed
|
|
46
49
|
end
|
|
47
|
-
|
|
48
|
-
private
|
|
49
50
|
end
|
|
50
51
|
end
|
data/lib/resizing/version.rb
CHANGED
data/lib/resizing.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'resizing/version'
|
|
4
4
|
require 'faraday'
|
|
5
|
+
require 'faraday/multipart'
|
|
5
6
|
require 'json'
|
|
6
7
|
|
|
7
8
|
module Resizing
|
|
@@ -14,16 +15,19 @@ module Resizing
|
|
|
14
15
|
autoload :CarrierWave, 'resizing/carrier_wave'
|
|
15
16
|
autoload :PublicId, 'resizing/public_id'
|
|
16
17
|
autoload :Video, 'resizing/video'
|
|
18
|
+
autoload :ActiveStorage, 'resizing/active_storage'
|
|
17
19
|
|
|
18
20
|
class Error < StandardError; end
|
|
19
21
|
class ConfigurationError < Error; end
|
|
20
|
-
|
|
22
|
+
|
|
23
|
+
class APIError < Error
|
|
21
24
|
def decoded_body
|
|
22
25
|
@decoded_body ||= {}
|
|
23
26
|
end
|
|
24
27
|
|
|
25
28
|
def decoded_body=(value)
|
|
26
29
|
raise ArgumentError, 'The decoded_body is expected to be passed a Hash.' unless value.is_a? Hash
|
|
30
|
+
|
|
27
31
|
@decoded_body = value
|
|
28
32
|
end
|
|
29
33
|
end
|
|
@@ -48,39 +52,39 @@ module Resizing
|
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
def self.post(file_or_binary, options)
|
|
51
|
-
|
|
55
|
+
client.post file_or_binary, options
|
|
52
56
|
end
|
|
53
57
|
|
|
54
58
|
def self.put(name, file_or_binary, options)
|
|
55
|
-
|
|
59
|
+
client.put name, file_or_binary, options
|
|
56
60
|
end
|
|
57
61
|
|
|
58
62
|
def self.delete(name)
|
|
59
|
-
|
|
63
|
+
client.delete name
|
|
60
64
|
end
|
|
61
65
|
|
|
62
66
|
def self.metadata(name, options)
|
|
63
|
-
|
|
67
|
+
client.metadata name, options
|
|
64
68
|
end
|
|
65
69
|
|
|
66
70
|
# TODO: refactoring
|
|
67
71
|
#
|
|
68
72
|
# identifier:
|
|
69
|
-
# public_id: /projects
|
|
70
|
-
# identifier:
|
|
71
|
-
# project_id:
|
|
72
|
-
# image_id:
|
|
73
|
-
# version:
|
|
73
|
+
# public_id: /projects/<project_id>/upload/images/<image_id>/v<version>
|
|
74
|
+
# identifier: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
75
|
+
# project_id: ^^^^^^^^^^^^^
|
|
76
|
+
# image_id: ^^^^^^^^^^
|
|
77
|
+
# version: ^^^^^^^^
|
|
74
78
|
def self.generate_identifier
|
|
75
79
|
Resizing.configure.generate_identifier
|
|
76
80
|
end
|
|
77
81
|
|
|
78
|
-
def self.separate_public_id
|
|
82
|
+
def self.separate_public_id(public_id)
|
|
79
83
|
public_id.match('/projects/(?<project_id>[0-9a-f-]+)/upload/images/(?<image_id>[^/]+)(/v(?<version>[^/]+))?')
|
|
80
84
|
end
|
|
81
85
|
|
|
82
86
|
def self.client
|
|
83
|
-
if
|
|
87
|
+
if configure.enable_mock
|
|
84
88
|
Resizing::MockClient.new
|
|
85
89
|
else
|
|
86
90
|
Resizing::Client.new
|
data/resizing.gemspec
CHANGED
|
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
|
12
12
|
spec.description = 'Client and utilities for Resizing '
|
|
13
13
|
spec.homepage = 'https://github.com/jksy/resizing-gem'
|
|
14
14
|
spec.license = 'MIT'
|
|
15
|
-
spec.required_ruby_version = Gem::Requirement.new('>=
|
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 3.0.0')
|
|
16
16
|
|
|
17
17
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
|
18
18
|
|
|
@@ -25,16 +25,17 @@ Gem::Specification.new do |spec|
|
|
|
25
25
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
26
26
|
`git ls-files -z`.split("\x0")
|
|
27
27
|
end
|
|
28
|
-
spec.bindir = 'exe'
|
|
29
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
30
28
|
spec.require_paths = ['lib']
|
|
31
29
|
spec.add_runtime_dependency 'faraday', '~> 1.0'
|
|
32
|
-
spec.add_development_dependency 'rails',
|
|
30
|
+
spec.add_development_dependency 'rails', ">= 6.0", '< 7.1'
|
|
33
31
|
spec.add_development_dependency 'carrierwave', '~> 1.3.2'
|
|
34
32
|
spec.add_development_dependency 'fog-aws'
|
|
35
|
-
spec.add_development_dependency 'minitest'
|
|
33
|
+
spec.add_development_dependency 'minitest', '~> 5.16'
|
|
36
34
|
spec.add_development_dependency 'minitest-ci'
|
|
35
|
+
spec.add_development_dependency 'mysql2'
|
|
37
36
|
spec.add_development_dependency 'rubocop'
|
|
37
|
+
spec.add_development_dependency 'simplecov'
|
|
38
|
+
spec.add_development_dependency 'simplecov-cobertura'
|
|
38
39
|
spec.add_development_dependency 'timecop'
|
|
39
40
|
spec.add_development_dependency 'vcr'
|
|
40
41
|
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
module ActiveStorage
|
|
7
|
+
module Service
|
|
8
|
+
class ResizingServiceTest < Minitest::Test
|
|
9
|
+
def setup
|
|
10
|
+
@service = Resizing::ActiveStorage::Service::ResizingService.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def teardown
|
|
14
|
+
# NOP
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_service_initialization
|
|
18
|
+
assert_instance_of Resizing::ActiveStorage::Service::ResizingService, @service
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_upload_raises_not_implemented_error
|
|
22
|
+
assert_raises NotImplementedError do
|
|
23
|
+
@service.upload('test_key', StringIO.new('test data'))
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_download_raises_not_implemented_error
|
|
28
|
+
assert_raises NotImplementedError do
|
|
29
|
+
@service.download('test_key')
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_download_chunk_raises_not_implemented_error
|
|
34
|
+
assert_raises NotImplementedError do
|
|
35
|
+
@service.download_chunk('test_key', 0..99)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_delete_raises_not_implemented_error
|
|
40
|
+
assert_raises NotImplementedError do
|
|
41
|
+
@service.delete('test_key')
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_exist_raises_not_implemented_error
|
|
46
|
+
assert_raises NotImplementedError do
|
|
47
|
+
@service.exist?('test_key')
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_url_for_direct_upload_raises_not_implemented_error
|
|
52
|
+
assert_raises NotImplementedError do
|
|
53
|
+
@service.url_for_direct_upload(
|
|
54
|
+
'test_key',
|
|
55
|
+
expires_in: 3600,
|
|
56
|
+
content_type: 'image/png',
|
|
57
|
+
conteont_length: 1024,
|
|
58
|
+
checksum: 'test_checksum'
|
|
59
|
+
)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_headers_for_direct_upload_raises_not_implemented_error
|
|
64
|
+
assert_raises NotImplementedError do
|
|
65
|
+
@service.headers_for_direct_upload(
|
|
66
|
+
'test_key',
|
|
67
|
+
content_type: 'image/png',
|
|
68
|
+
checksum: 'test_checksum'
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_private_url_raises_not_implemented_error
|
|
74
|
+
assert_raises NotImplementedError do
|
|
75
|
+
@service.send(
|
|
76
|
+
:private_url,
|
|
77
|
+
'test_key',
|
|
78
|
+
expires_in: 3600,
|
|
79
|
+
filename: 'test.png',
|
|
80
|
+
content_type: 'image/png',
|
|
81
|
+
disposition: :attachment
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def test_public_url_raises_not_implemented_error
|
|
87
|
+
assert_raises NotImplementedError do
|
|
88
|
+
@service.send(
|
|
89
|
+
:public_url,
|
|
90
|
+
'test_key',
|
|
91
|
+
filename: 'test.png'
|
|
92
|
+
)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -7,22 +7,163 @@ module Resizing
|
|
|
7
7
|
module Storage
|
|
8
8
|
class FileTest < Minitest::Test
|
|
9
9
|
def setup
|
|
10
|
-
|
|
10
|
+
@configuration_template = {
|
|
11
|
+
image_host: 'http://192.168.56.101:5000',
|
|
12
|
+
video_host: 'http://192.168.56.101:5000',
|
|
13
|
+
project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
|
|
14
|
+
secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
|
|
15
|
+
open_timeout: 10,
|
|
16
|
+
response_timeout: 20
|
|
17
|
+
}
|
|
18
|
+
Resizing.configure = @configuration_template
|
|
11
19
|
end
|
|
12
20
|
|
|
13
21
|
def teardown
|
|
14
22
|
# NOP
|
|
15
23
|
end
|
|
16
24
|
|
|
17
|
-
def
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
def test_initialize_without_identifier
|
|
26
|
+
model = TestModel.new
|
|
27
|
+
uploader = model.resizing_picture
|
|
28
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
29
|
+
|
|
30
|
+
assert_instance_of Resizing::CarrierWave::Storage::File, file
|
|
31
|
+
assert file.public_id.empty?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def test_initialize_with_identifier
|
|
35
|
+
model = TestModel.new
|
|
36
|
+
uploader = model.resizing_picture
|
|
37
|
+
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
38
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
39
|
+
|
|
40
|
+
assert_instance_of Resizing::CarrierWave::Storage::File, file
|
|
41
|
+
assert_equal identifier, file.public_id.to_s
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def test_retrieve_sets_public_id
|
|
45
|
+
model = TestModel.new
|
|
46
|
+
uploader = model.resizing_picture
|
|
47
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
48
|
+
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
49
|
+
|
|
50
|
+
file.retrieve(identifier)
|
|
51
|
+
|
|
52
|
+
assert_equal identifier, file.public_id.to_s
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def test_delete_does_nothing_when_public_id_empty
|
|
56
|
+
model = TestModel.new
|
|
57
|
+
uploader = model.resizing_picture
|
|
58
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
59
|
+
|
|
60
|
+
# Should not raise any error and should return early
|
|
61
|
+
result = file.delete
|
|
62
|
+
assert_nil result
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_current_path_returns_nil_for_new_model
|
|
66
|
+
model = TestModel.new
|
|
67
|
+
uploader = model.resizing_picture
|
|
68
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
69
|
+
|
|
70
|
+
assert_nil file.current_path
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def test_path_alias
|
|
74
|
+
model = TestModel.new
|
|
75
|
+
uploader = model.resizing_picture
|
|
76
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
77
|
+
|
|
78
|
+
assert_nil file.current_path
|
|
79
|
+
assert_nil file.path
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_authenticated_url_returns_nil
|
|
83
|
+
model = TestModel.new
|
|
84
|
+
uploader = model.resizing_picture
|
|
85
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
86
|
+
|
|
87
|
+
assert_nil file.authenticated_url
|
|
22
88
|
end
|
|
23
89
|
|
|
24
|
-
def
|
|
25
|
-
|
|
90
|
+
def test_authenticated_url_with_options_returns_nil
|
|
91
|
+
model = TestModel.new
|
|
92
|
+
uploader = model.resizing_picture
|
|
93
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
94
|
+
|
|
95
|
+
assert_nil file.authenticated_url(expires_in: 3600)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def test_extension_raises_not_implemented_error
|
|
99
|
+
model = TestModel.new
|
|
100
|
+
uploader = model.resizing_picture
|
|
101
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
102
|
+
|
|
103
|
+
assert_raises(NotImplementedError) do
|
|
104
|
+
file.extension
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def test_name_returns_image_id_from_public_id
|
|
109
|
+
model = TestModel.new
|
|
110
|
+
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
111
|
+
# Use write_attribute to set the column directly
|
|
112
|
+
model.send(:write_attribute, :resizing_picture, identifier)
|
|
113
|
+
uploader = model.resizing_picture
|
|
114
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
115
|
+
|
|
116
|
+
# name returns image_id (UUID) from public_id
|
|
117
|
+
assert_equal '14ea7aac-a194-4330-931f-6b562aec413d', file.name
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def test_store_uploads_file_and_sets_public_id
|
|
121
|
+
VCR.use_cassette 'carrier_wave_test/save', record: :once do
|
|
122
|
+
model = TestModel.new
|
|
123
|
+
uploader = model.resizing_picture
|
|
124
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
125
|
+
|
|
126
|
+
source_file = ::File.open('test/data/images/sample1.jpg', 'r')
|
|
127
|
+
uploaded_file = ActionDispatch::Http::UploadedFile.new(
|
|
128
|
+
filename: ::File.basename(source_file.path),
|
|
129
|
+
type: 'image/jpeg',
|
|
130
|
+
tempfile: source_file
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
result = file.store(uploaded_file)
|
|
134
|
+
|
|
135
|
+
assert result
|
|
136
|
+
refute file.public_id.empty?
|
|
137
|
+
assert_equal 'image/jpeg', file.content_type
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def test_store_with_file_object
|
|
142
|
+
VCR.use_cassette 'carrier_wave_test/save', record: :once do
|
|
143
|
+
model = TestModel.new
|
|
144
|
+
uploader = model.resizing_picture
|
|
145
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader)
|
|
146
|
+
|
|
147
|
+
source_file = ::File.open('test/data/images/sample1.jpg', 'r')
|
|
148
|
+
|
|
149
|
+
result = file.store(source_file)
|
|
150
|
+
|
|
151
|
+
assert result
|
|
152
|
+
refute file.public_id.empty?
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def test_delete_with_valid_public_id
|
|
157
|
+
VCR.use_cassette 'carrier_wave_test/remove_resizing_picture' do
|
|
158
|
+
model = TestModel.new
|
|
159
|
+
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
160
|
+
model.send(:write_attribute, :resizing_picture, identifier)
|
|
161
|
+
uploader = model.resizing_picture
|
|
162
|
+
file = Resizing::CarrierWave::Storage::File.new(uploader, identifier)
|
|
163
|
+
|
|
164
|
+
# This should call delete on Resizing API
|
|
165
|
+
file.delete
|
|
166
|
+
end
|
|
26
167
|
end
|
|
27
168
|
end
|
|
28
169
|
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
module Resizing
|
|
6
|
+
module CarrierWave
|
|
7
|
+
module Storage
|
|
8
|
+
class RemoteTest < Minitest::Test
|
|
9
|
+
def setup
|
|
10
|
+
@configuration_template = {
|
|
11
|
+
image_host: 'http://192.168.56.101:5000',
|
|
12
|
+
video_host: 'http://192.168.56.101:5000',
|
|
13
|
+
project_id: 'e06e710d-f026-4dcf-b2c0-eab0de8bb83f',
|
|
14
|
+
secret_token: 'ewbym2r1pk49x1d2lxdbiiavnqp25j2kh00hsg3koy0ppm620x5mhlmgl3rq5ci8',
|
|
15
|
+
open_timeout: 10,
|
|
16
|
+
response_timeout: 20
|
|
17
|
+
}
|
|
18
|
+
Resizing.configure = @configuration_template
|
|
19
|
+
|
|
20
|
+
model = TestModel.new
|
|
21
|
+
@uploader = model.resizing_picture
|
|
22
|
+
@storage = Remote.new(@uploader)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def teardown
|
|
26
|
+
# NOP
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_storage_has_required_methods
|
|
30
|
+
assert_respond_to @storage, :store!
|
|
31
|
+
assert_respond_to @storage, :remove!
|
|
32
|
+
assert_respond_to @storage, :cache!
|
|
33
|
+
assert_respond_to @storage, :retrieve_from_cache!
|
|
34
|
+
assert_respond_to @storage, :delete_dir!
|
|
35
|
+
assert_respond_to @storage, :clean_cache!
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_retrieve_from_cache_returns_nil
|
|
39
|
+
result = @storage.retrieve_from_cache!('identifier')
|
|
40
|
+
assert_nil result
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_delete_dir_does_nothing
|
|
44
|
+
# Should not raise any exception
|
|
45
|
+
assert_nil @storage.delete_dir!('/path/to/dir')
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def test_clean_cache_does_nothing
|
|
49
|
+
# Should not raise any exception
|
|
50
|
+
assert_nil @storage.clean_cache!(3600)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_storage_initialization
|
|
54
|
+
assert_instance_of Remote, @storage
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def test_retrieve_returns_nil_for_blank_identifier
|
|
58
|
+
result = @storage.retrieve!(nil)
|
|
59
|
+
assert_nil result
|
|
60
|
+
|
|
61
|
+
result = @storage.retrieve!('')
|
|
62
|
+
assert_nil result
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_retrieve_returns_file_for_valid_identifier
|
|
66
|
+
identifier = '/projects/e06e710d-f026-4dcf-b2c0-eab0de8bb83f/upload/images/14ea7aac-a194-4330-931f-6b562aec413d/v_8c5lEhDB5RT3PZp1Fn5PYGm9YVx_x0e'
|
|
67
|
+
result = @storage.retrieve!(identifier)
|
|
68
|
+
|
|
69
|
+
assert_instance_of Resizing::CarrierWave::Storage::File, result
|
|
70
|
+
assert_equal identifier, result.public_id.to_s
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|