File: test_objects.rb

package info (click to toggle)
ruby-fog-google 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,568 kB
  • sloc: ruby: 16,775; makefile: 3
file content (225 lines) | stat: -rw-r--r-- 7,057 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
require "helpers/integration_test_helper"
require "integration/storage/storage_shared"
require "securerandom"
require "base64"
require "tempfile"
require "net/http"

class TestStorageRequests < StorageShared
  def test_put_object_string
    object_name = new_object_name
    @client.put_object(some_bucket_name, object_name, some_temp_file)

    object = @client.get_object(some_bucket_name, object_name)

    assert_equal(object_name, object[:name])
    assert_equal(temp_file_content, object[:body])
  end

  def test_put_object_nil
    assert_raises(ArgumentError) do
      @client.put_object(some_bucket_name, new_object_name, nil)
    end
  end

  def test_put_object_file
    object_name = new_object_name
    expected_body = "A file body"
    @client.put_object(some_bucket_name, object_name, expected_body)

    object = @client.get_object(some_bucket_name, object_name)
    assert_equal(object_name, object[:name])
    assert_equal(expected_body, object[:body])
  end

  def test_put_object_paperclip
    object_name = new_object_name
    paperclip_file = OpenStruct.new(:path => some_temp_file(binary_file_content),
                                    :content_type => "image/png")
    @client.put_object(some_bucket_name, object_name, paperclip_file, :content_type => "image/png")

    object = @client.get_object(some_bucket_name, object_name)

    assert_equal(object_name, object[:name])
    assert_equal(Encoding::ASCII_8BIT, object[:body].encoding)
    assert_equal(binary_file_content, object[:body])
    assert_equal("image/png", object[:content_type])
  end

  def test_put_object_contradictory_content_type
    object_name = new_object_name
    file = OpenStruct.new(:path => some_temp_file,
                          :content_type => "text/plain")
    @client.put_object(some_bucket_name, object_name, file, :content_type => "image/png")

    object = @client.get_object(some_bucket_name, object_name)

    assert_equal(object_name, object[:name])
    assert_equal("image/png", object[:content_type])
  end

  def test_put_object_predefined_acl
    @client.put_object(some_bucket_name, new_object_name, some_temp_file,
                       :predefined_acl => "publicRead")
  end

  def test_put_object_invalid_predefined_acl
    assert_raises(Google::Apis::ClientError) do
      @client.put_object(some_bucket_name, new_object_name, some_temp_file,
                         :predefined_acl => "invalidAcl")
    end
  end

  def test_get_object
    object = @client.get_object(some_bucket_name, some_object_name)
    assert_equal(temp_file_content, object[:body])
  end

  def test_delete_object
    object_name = new_object_name
    @client.put_object(some_bucket_name, object_name, some_temp_file)
    @client.delete_object(some_bucket_name, object_name)

    assert_raises(Google::Apis::ClientError) do
      @client.get_object(some_bucket_name, object_name)
    end
  end

  def test_object_metadata
    object = @client.get_object_metadata(some_bucket_name, some_object_name)
    assert_equal(temp_file_content.length, object.size)
    assert_equal(some_bucket_name, object.bucket)
  end

  def test_copy_object
    target_object_name = new_object_name

    @client.copy_object(some_bucket_name, some_object_name,
                        some_bucket_name, target_object_name)

    object = @client.get_object(some_bucket_name, target_object_name)

    assert_equal(temp_file_content, object[:body])
  end

  def test_copy_object_predefined_acl
    target_object_name = new_object_name

    res = @client.copy_object(some_bucket_name, some_object_name,
                              some_bucket_name, target_object_name, destination_predefined_acl: "publicRead")

    result = @client.get_object(some_bucket_name, target_object_name)

    response = Net::HTTP.get_response(URI(result[:self_link]))

    assert_kind_of(Net::HTTPOK, response)
  end

  def test_copy_object_with_request_options
    assert_raises(Google::Apis::AuthorizationError) do
      target_object_name = new_object_name

      @client.copy_object(some_bucket_name, some_object_name,
                          some_bucket_name, target_object_name, authorization: false)
    end
  end

  def test_copy_object_with_object_property
    target_object_name = new_object_name

    @client.copy_object(some_bucket_name, some_object_name,
                        some_bucket_name, target_object_name, content_type: 'text/plain')

    object = @client.get_object(some_bucket_name, target_object_name)

    assert_equal("text/plain", object[:content_type])
  end

  def test_list_objects
    expected_object = some_object_name

    result = @client.list_objects(some_bucket_name)
    if result.items.nil?
      raise StandardError.new("no objects found")
    end

    contained = result.items.any? { |object| object.name == expected_object }
    assert_equal(true, contained, "expected object not present")
  end

  def test_put_object_acl
    object_name = new_object_name
    @client.put_object(some_bucket_name, object_name, some_temp_file)

    acl = {
      :entity => "allUsers",
      :role => "READER"
    }
    @client.put_object_acl(some_bucket_name, object_name, acl)
  end

  def test_list_object_acl
    object_name = new_object_name
    acls = [
      {
        :entity => "allUsers",
        :role => "READER"
      },
      {
        :entity => "allAuthenticatedUsers",
        :role => "READER"
      }
    ]
    @client.put_object(some_bucket_name, object_name, some_temp_file, :acl => acls)

    result = @client.list_object_acl(some_bucket_name, object_name)
    if result.items.nil?
      raise StandardError.new("no object access controls found")
    end

    assert_operator(acls.length, :<=, result.items.length,
                    "expected at least #{acls.length} ACL items")

    expected_acls = Hash[acls.map { |acl| [acl[:entity], acl[:role]] }]
    num_found = 0

    result.items.each do |actual_acl|
      if expected_acls[actual_acl.entity] == actual_acl.role
        num_found += 1
      end
    end

    assert_equal(acls.length, num_found, "only found #{num_found} of #{acls.length} expected ACLs")
  end

  def test_get_object_acl
    object_name = new_object_name
    @client.put_object(some_bucket_name, object_name, some_temp_file)

    data = {
      :entity => "allUsers",
      :role => "READER"
    }
    @client.put_object_acl(some_bucket_name, object_name, data)

    result = @client.get_object_acl(some_bucket_name, object_name, "allUsers")
    if result.nil?
      raise StandardError.new("expected object access control not found")
    end
  end

  def test_get_object_https_url
    result = @client.get_object_https_url(some_bucket_name, some_object_name, 0)
    assert_operator(result.length, :>, 0)
  end

  def test_get_object_http_url
    result = @client.get_object_http_url(some_bucket_name, some_object_name, 0)
    assert_operator(result.length, :>, 0)
  end

  def test_put_object_url
    result = @client.put_object_url(some_bucket_name, new_object_name, 0)
    assert_operator(result.length, :>, 0)
  end
end