File: test_file.rb

package info (click to toggle)
ruby-gitlab-fog-azure-rm 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 756 kB
  • sloc: ruby: 5,926; sh: 9; makefile: 4
file content (316 lines) | stat: -rw-r--r-- 8,446 bytes parent folder | download | duplicates (2)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
require File.expand_path '../../test_helper', __dir__

# Test class for Storage Container Model
class TestFile < Minitest::Test # rubocop:disable Metrics/ClassLength
  def setup
    @service = Fog::AzureRM::Storage.new(storage_account_credentials)
    @directory = directory(@service)
    @file = file(@service)
    @raw_cloud_blob = storage_blob

    @mocked_response = mocked_storage_http_error
    @blob = ApiStub::Models::Storage::File.blob
    @metadata = ApiStub::Models::Storage::File.blob_metadata
    @blob_list = ApiStub::Models::Storage::File.blob_list
    @blob_https_url = ApiStub::Models::Storage::File.blob_https_url
  end

  def test_model_methods
    methods = [
      :save,
      :body,
      :body=,
      :copy,
      :copy_from_uri,
      :destroy,
      :public?,
      :public_url,
      :url
    ]
    methods.each do |method|
      assert_respond_to @file, method
    end
  end

  def test_model_attributes
    attributes = [
      :key,
      :body,
      :directory,
      :accept_ranges,
      :content_length,
      :content_type,
      :content_md5,
      :content_encoding,
      :content_language,
      :cache_control,
      :content_disposition,
      :copy_completion_time,
      :copy_status,
      :copy_status_description,
      :copy_id,
      :copy_progress,
      :copy_source,
      :etag,
      :last_modified,
      :lease_duration,
      :lease_state,
      :lease_status,
      :sequence_number,
      :blob_type,
      :metadata
    ]
    attributes.each do |attribute|
      assert_respond_to @file, attribute
    end
  end

  def test_save_method_with_small_block_blob_success
    @file.body = 'd' * 1025 # SINGLE_BLOB_PUT_THRESHOLD is 32 * 1024 * 1024

    @service.stub :create_block_blob, @raw_cloud_blob do
      assert @file.save
    end
  end

  def test_save_method_with_large_block_blob_success
    @file.body = 'd' * (32 * 1024 * 1024 + 1) # SINGLE_BLOB_PUT_THRESHOLD is 32 * 1024 * 1024

    @service.stub :create_block_blob, @raw_cloud_blob do
      assert @file.save
    end
  end

  def test_save_method_with_page_blob_success
    options = { blob_type: 'PageBlob' }
    @file.metadata = @metadata
    @file.body = 'd' * 5 * 1024 * 1024 # MAXIMUM_CHUNK_SIZE is 4 * 1024 * 1024

    @service.stub :save_page_blob, true do
      @service.stub :get_blob_properties, @raw_cloud_blob do
        assert @file.save(options)
      end
    end
  end

  def test_save_method_with_not_update_body_success
    options = { update_body: false }
    @file.metadata = @metadata

    @service.stub :put_blob_metadata, true do
      @service.stub :put_blob_properties, true do
        @service.stub :get_blob_properties, @raw_cloud_blob do
          assert @file.save(options)
        end
      end
    end
  end

  def test_save_method_without_directory_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:directory)
      @file.save
    end
  end

  def test_save_method_without_key_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:key)
      @file.save
    end
  end

  def test_save_method_create_without_body_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:body)
      @file.save
    end
  end

  def test_get_body_method_local_success
    @file.attributes[:body] = 'body'
    assert_equal 'body', @file.body

    @file.attributes[:body] = nil
    @file.attributes[:last_modified] = nil
    assert_equal '', @file.body
  end

  def test_get_body_method_remote_success
    @file.attributes[:body] = nil
    remote_file = @file.dup
    body_content = 'data'
    @file.collection.stub :get, remote_file do
      remote_file.stub :body, body_content do
        body = @file.body
        assert_equal body_content, body
        assert_equal body_content, @file.attributes[:body]
      end
    end
  end

  def test_get_body_method_remote_not_found_success
    @file.attributes[:body] = nil
    @file.collection.stub :get, nil do
      body = @file.body
      assert_equal '', body
      assert_equal '', @file.attributes[:body]
    end
  end

  def test_set_body_method_success
    @file.body = 'new_body'
    assert_equal 'new_body', @file.attributes[:body]
  end

  def test_copy_method_success
    copy_id = @raw_cloud_blob.properties[:copy_id]
    copy_status = 'pending'

    @service.stub :get_blob_properties, @raw_cloud_blob do
      @service.stub :copy_blob, [copy_id, copy_status] do
        @service.stub :wait_blob_copy_operation_to_finish, true do
          target_file = @file.copy('target_container', 'target_blob')
          assert_instance_of Fog::AzureRM::Storage::File, target_file
        end
      end
    end
  end

  def test_copy_from_uri_method_success
    copy_id = @raw_cloud_blob.properties[:copy_id]
    copy_status = 'pending'

    @service.stub :get_blob_properties, @raw_cloud_blob do
      @service.stub :copy_blob_from_uri, [copy_id, copy_status] do
        @service.stub :wait_blob_copy_operation_to_finish, true do
          assert @file.copy_from_uri('source_uri')
        end
      end
    end
  end

  def test_copy_from_uri_method_without_directory_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:directory)
      @file.copy_from_uri('source_uri')
    end
  end

  def test_copy_from_uri_method_without_key_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:key)
      @file.copy_from_uri('source_uri')
    end
  end

  def test_destroy_method_success
    @service.stub :delete_blob, true do
      assert @file.destroy
      assert @file.attributes[:body].nil?
    end
  end

  def test_destroy_method_without_directory_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:directory)
      @file.destroy
    end
  end

  def test_destroy_method_without_key_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:key)
      @file.destroy
    end
  end

  def test_public_method_success
    @file.directory.acl = 'container'
    assert @file.public?

    @file.directory.acl = 'blob'
    assert @file.public?

    @file.directory.acl = nil
    assert !@file.public?
  end

  def test_public_method_without_directory_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:directory)
      @file.public?
    end
  end

  def test_public_url_method_success
    @file.stub :public?, false do
      assert @file.public_url.nil?
    end

    @file.stub :public?, true do
      @service.stub :get_blob_url, @blob_https_url do
        assert @file.public_url, @blob_https_url
      end
    end

    http_url = @blob_https_url.gsub('https:', 'http:')
    options = { scheme: 'https' }
    @file.stub :public?, true do
      @service.stub :get_blob_url, http_url do
        assert @file.public_url(options), http_url
      end
    end
  end

  def test_public_url_method_without_directory_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:directory)
      @file.public_url
    end
  end

  def test_public_url_method_without_key_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:key)
      @file.public_url
    end
  end

  def test_url_method_success
    @file.collection.stub :get_url, @blob_https_url do
      assert @file.url(Time.now + 3600), @blob_https_url
    end
  end

  def test_url_method_with_content_disposition
    @file.collection.stub :get_url, @blob_https_url, { content_disposition: 'attachment' } do
      assert @file.url(Time.now + 3600, content_disposition: 'attachment'), @blob_https_url
    end
  end

  def test_url_method_with_response_content_disposition
    fake = Minitest::Mock.new
    expiry = Time.now + 3600
    fake.expect :call, nil, [@file.key, expiry, { content_disposition: 'attachment', content_type: nil }]
    @file.collection.stub(:get_url, fake) do
      @file.url(expiry, { query: { 'response-content-disposition' => 'attachment' } })
    end
  end

  def test_url_method_with_response_content_type
    fake = Minitest::Mock.new
    expiry = Time.now + 3600
    fake.expect :call, nil, [@file.key, expiry, { content_disposition: nil, content_type: 'image/png' }]
    @file.collection.stub(:get_url, fake) do
      @file.url(expiry, { query: { 'response-content-type' => 'image/png' } })
    end
  end

  def test_url_method_without_key_exception
    assert_raises(ArgumentError) do
      @file.attributes.delete(:key)
      @file.url(Time.now + 3600)
    end
  end
end