File: test_files.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 (234 lines) | stat: -rw-r--r-- 6,365 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
require File.expand_path '../../test_helper', __dir__

# Test class for Blob Collection
class TestFiles < Minitest::Test
  def setup
    @service = Fog::AzureRM::Storage.new(storage_account_credentials)
    @directory = directory(@service)
    @files = @directory.files

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

  def test_collection_methods
    methods = [
      :all,
      :each,
      :each_file_this_page,
      :get,
      :get_url,
      :get_http_url,
      :get_https_url,
      :head,
      :new
    ]
    methods.each do |method|
      assert_respond_to @files, method
    end
  end

  def test_collection_attributes
    attributes = [
      :directory,
      :delimiter,
      :marker,
      :max_results,
      :next_marker,
      :prefix
    ]
    attributes.each do |attribute|
      assert_respond_to @files, attribute
    end
  end

  def test_all_method
    @service.stub :get_container_properties, @container do
      @service.stub :list_blobs, @blob_list do
        files = @files.all
        assert_instance_of Fog::AzureRM::Storage::Files, files
        assert_equal @blob_list[:blobs].size, files.size
      end
    end
  end

  def test_all_method_without_directory_exception
    assert_raises(ArgumentError) do
      @files.attributes.delete(:directory)
      @files.all
    end
  end

  def test_all_method_container_not_found
    @directory.collection.stub :get, nil do
      assert @files.all.nil?
    end
  end

  def test_each_method_with_all
    @blob_list[:next_marker] = nil
    @blob_list[:marker] = nil
    @service.stub :get_container_properties, @container do
      @service.stub :list_blobs, @blob_list do
        @files.next_marker = nil
        j = 0
        @files.each do |file|
          assert_instance_of Fog::AzureRM::Storage::File, file
          j += 1
        end
        assert_equal @blob_list[:blobs].size, j
      end
    end
  end

  def test_each_method_with_parts
    assert_instance_of Fog::AzureRM::Storage::Files, @files.each

    multiple_values = lambda do |_container_name, options|
      if options[:marker] == 'marker'
        return {
          next_marker: nil,
          blobs: @blob_list[:blobs][2, 2]
        }
      end
      return {
        next_marker: 'marker',
        blobs: @blob_list[:blobs][0, 2]
      }
    end

    @service.stub :get_container_properties, @container do
      @service.stub :list_blobs, multiple_values do
        j = 0
        @files.each do |file|
          assert_instance_of Fog::AzureRM::Storage::File, file
          j += 1
        end
        assert_equal @blob_list[:blobs].size, j
      end
    end
  end

  def test_get_method_success
    content = 'data'
    @service.stub :get_blob, [@blob, content] do
      file = @files.get('test_blob')
      assert_instance_of Fog::AzureRM::Storage::File, file
      assert_equal content, file.body
    end
  end

  def test_get_method_without_directory_exception
    assert_raises(ArgumentError) do
      @files.attributes.delete(:directory)
      @files.get('test_blob')
    end
  end

  def test_get_method_blob_not_found
    exception = ->(*) { raise 'NotFound' }
    @service.stub :get_blob, exception do
      assert @files.get('test_blob').nil?
    end
  end

  def test_get_method_http_exception
    http_exception = ->(*) { raise Azure::Core::Http::HTTPError.new(@mocked_response) }
    @service.stub :get_blob, http_exception do
      assert_raises(Azure::Core::Http::HTTPError) do
        @files.get('test_blob')
      end
    end
  end

  def test_get_url_method_success
    @files.stub :get_https_url, @blob_https_url do
      assert_equal @blob_https_url, @files.get_url('test_blob', Time.now + 3600)
    end

    options = { scheme: 'http' }
    http_url = @blob_https_url.gsub('https:', 'http:')
    @files.stub :get_http_url, http_url do
      assert_equal http_url, @files.get_url('test_blob', Time.now + 3600, options)
    end
  end

  def test_get_url_method_without_directory_exception
    assert_raises(ArgumentError) do
      @files.attributes.delete(:directory)
      @files.get_url('test_blob', Time.now + 3600)
    end
  end

  def test_get_http_url_method_success
    http_url = @blob_https_url.gsub('https:', 'http:')
    @service.stub :get_blob_http_url, http_url do
      assert_equal http_url, @files.get_http_url('test_blob', Time.now + 3600)
    end
  end

  def test_get_http_url_method_without_directory_exception
    assert_raises(ArgumentError) do
      @files.attributes.delete(:directory)
      @files.get_http_url('test_blob', Time.now + 3600)
    end
  end

  def test_get_https_url_method_success
    @service.stub :get_blob_https_url, @blob_https_url do
      assert_equal @blob_https_url, @files.get_https_url('test_blob', Time.now + 3600)
    end
  end

  def test_get_https_url_method_without_directory_exception
    assert_raises(ArgumentError) do
      @files.attributes.delete(:directory)
      @files.get_https_url('test_blob', Time.now + 3600)
    end
  end

  def test_head_method_success
    @service.stub :get_blob_properties, @blob do
      file = @files.head('test_blob')
      assert_instance_of Fog::AzureRM::Storage::File, file
      assert file.attributes[:body].nil?
    end
  end

  def test_head_method_without_directory_exception
    assert_raises(ArgumentError) do
      @files.attributes.delete(:directory)
      @files.head('test_blob')
    end
  end

  def test_head_method_blob_not_found
    exception = ->(*) { raise 'NotFound' }
    @service.stub :get_blob_properties, exception do
      assert @files.head('test_blob').nil?
    end
  end

  def test_head_method_http_exception
    http_exception = ->(*) { raise Azure::Core::Http::HTTPError.new(@mocked_response) }
    @service.stub :get_blob_properties, http_exception do
      assert_raises(Azure::Core::Http::HTTPError) do
        @files.head('test_blob')
      end
    end
  end

  def test_new_method_success
    assert_instance_of Fog::AzureRM::Storage::File, @files.new
  end

  def test_new_method_without_directory_exception
    assert_raises(ArgumentError) do
      @files.attributes.delete(:directory)
      @files.new
    end
  end
end