Skip to content

Commit 376ad59

Browse files
committed
Harden file metadata paths and add coverage
1 parent ecbba8c commit 376ad59

File tree

4 files changed

+82
-16
lines changed

4 files changed

+82
-16
lines changed
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# frozen_string_literal: true
22

33
# Client for file metadata operations.
4+
require 'uri'
45
class Uploadcare::FileMetadataClient < Uploadcare::RestClient
56
# Retrieves all metadata associated with a specific file by UUID.
67
# @param uuid [String] The UUID of the file.
78
# @return [Hash] A hash containing all metadata key-value pairs for the file.
89
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/_fileMetadata
910
def index(uuid:, request_options: {})
10-
get(path: "/files/#{uuid}/metadata/", params: {}, headers: {}, request_options: request_options)
11+
encoded_uuid = URI.encode_www_form_component(uuid)
12+
get(path: "/files/#{encoded_uuid}/metadata/", params: {}, headers: {}, request_options: request_options)
1113
end
1214

1315
# Gets the value of a specific metadata key for a file by UUID
@@ -16,7 +18,9 @@ def index(uuid:, request_options: {})
1618
# @return [String] The value of the metadata key
1719
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/fileMetadata
1820
def show(uuid:, key:, request_options: {})
19-
get(path: "/files/#{uuid}/metadata/#{key}/", params: {}, headers: {}, request_options: request_options)
21+
encoded_uuid = URI.encode_www_form_component(uuid)
22+
encoded_key = URI.encode_www_form_component(key)
23+
get(path: "/files/#{encoded_uuid}/metadata/#{encoded_key}/", params: {}, headers: {}, request_options: request_options)
2024
end
2125

2226
# Updates or creates a metadata key for a specific file by UUID
@@ -26,7 +30,9 @@ def show(uuid:, key:, request_options: {})
2630
# @return [String] The value of the updated or added metadata key
2731
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/updateFileMetadataKey
2832
def update(uuid:, key:, value:, request_options: {})
29-
put(path: "/files/#{uuid}/metadata/#{key}/", params: value, headers: {}, request_options: request_options)
33+
encoded_uuid = URI.encode_www_form_component(uuid)
34+
encoded_key = URI.encode_www_form_component(key)
35+
put(path: "/files/#{encoded_uuid}/metadata/#{encoded_key}/", params: value, headers: {}, request_options: request_options)
3036
end
3137

3238
# Deletes a specific metadata key for a file by UUID
@@ -35,6 +41,8 @@ def update(uuid:, key:, value:, request_options: {})
3541
# @return [Nil] Returns nil on successful deletion
3642
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/deleteFileMetadata
3743
def delete(uuid:, key:, request_options: {})
38-
super(path: "/files/#{uuid}/metadata/#{key}/", params: {}, headers: {}, request_options: request_options)
44+
encoded_uuid = URI.encode_www_form_component(uuid)
45+
encoded_key = URI.encode_www_form_component(key)
46+
super(path: "/files/#{encoded_uuid}/metadata/#{encoded_key}/", params: {}, headers: {}, request_options: request_options)
3947
end
4048
end

lib/uploadcare/resources/file_metadata.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def initialize(attributes = {}, config = Uploadcare.configuration)
1111
# Retrieves metadata for the file
1212
# @return [Hash] The metadata keys and values for the file
1313
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/_fileMetadata
14-
# TODO - Remove uuid if the opeartion is being perfomed on same file
15-
def index(uuid:, request_options: {})
16-
response = Uploadcare::Result.unwrap(@file_metadata_client.index(uuid: uuid, request_options: request_options))
14+
def index(uuid: nil, request_options: {})
15+
response = Uploadcare::Result.unwrap(@file_metadata_client.index(uuid: uuid || @uuid,
16+
request_options: request_options))
1717
@metadata = response if response.is_a?(Hash)
1818
self
1919
end
@@ -36,27 +36,26 @@ def to_h
3636
# Updates metadata key's value
3737
# @return [String] The updated value of the metadata key
3838
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/updateFileMetadataKey
39-
# TODO - Remove uuid if the opeartion is being perfomed on same file
40-
def update(uuid:, key:, value:, request_options: {})
41-
Uploadcare::Result.unwrap(@file_metadata_client.update(uuid: uuid, key: key, value: value,
39+
def update(uuid: nil, key:, value:, request_options: {})
40+
Uploadcare::Result.unwrap(@file_metadata_client.update(uuid: uuid || @uuid, key: key, value: value,
4241
request_options: request_options))
4342
end
4443

4544
# Retrieves the value of a specific metadata key for the file
4645
# @param key [String] The metadata key to retrieve
4746
# @return [String] The value of the metadata key
4847
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/fileMetadata
49-
# TODO - Remove uuid if the opeartion is being perfomed on same file
50-
def show(uuid:, key:, request_options: {})
51-
Uploadcare::Result.unwrap(@file_metadata_client.show(uuid: uuid, key: key, request_options: request_options))
48+
def show(uuid: nil, key:, request_options: {})
49+
Uploadcare::Result.unwrap(@file_metadata_client.show(uuid: uuid || @uuid, key: key,
50+
request_options: request_options))
5251
end
5352

5453
# Deletes a specific metadata key for the file
5554
# @param key [String] The metadata key to delete
5655
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File-metadata/operation/deleteFileMetadata
57-
# TODO - Remove uuid if the opeartion is being perfomed on same file
58-
def delete(uuid:, key:, request_options: {})
59-
Uploadcare::Result.unwrap(@file_metadata_client.delete(uuid: uuid, key: key, request_options: request_options))
56+
def delete(uuid: nil, key:, request_options: {})
57+
Uploadcare::Result.unwrap(@file_metadata_client.delete(uuid: uuid || @uuid, key: key,
58+
request_options: request_options))
6059
end
6160

6261
# Get file's metadata keys and values

spec/uploadcare/clients/file_metadata_client_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'spec_helper'
4+
require 'uri'
45

56
RSpec.describe Uploadcare::FileMetadataClient do
67
subject(:client) { described_class.new }
@@ -68,4 +69,22 @@
6869
expect(response.success).to be_nil
6970
end
7071
end
72+
73+
describe 'URL encoding' do
74+
let(:encoded_uuid) { URI.encode_www_form_component(uuid) }
75+
let(:encoded_key) { URI.encode_www_form_component(key) }
76+
77+
before do
78+
stub_request(:get, "https://api.uploadcare.com/files/#{encoded_uuid}/metadata/#{encoded_key}/")
79+
.to_return(status: 200, body: value.to_json, headers: { 'Content-Type' => 'application/json' })
80+
end
81+
82+
let(:uuid) { 'file~uuid' }
83+
let(:key) { 'custom key' }
84+
85+
it 'encodes uuid and key in metadata paths' do
86+
response = client.show(uuid: uuid, key: key)
87+
expect(response.success).to eq(value)
88+
end
89+
end
7190
end

spec/uploadcare/resources/file_metadata_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
result = file_metadata.index(uuid: uuid)
2020
expect(result).to be_a(described_class)
2121
end
22+
23+
it 'uses existing uuid when not provided' do
24+
file_metadata.instance_variable_set(:@uuid, uuid)
25+
allow_any_instance_of(Uploadcare::FileMetadataClient).to receive(:index)
26+
.with(uuid: uuid, request_options: {})
27+
.and_return(response_body)
28+
29+
result = file_metadata.index
30+
expect(result).to be_a(described_class)
31+
end
2232
end
2333

2434
describe '#[]' do
@@ -63,6 +73,16 @@
6373
result = file_metadata.show(uuid: uuid, key: key)
6474
expect(result).to eq(value)
6575
end
76+
77+
it 'uses existing uuid when not provided' do
78+
file_metadata.instance_variable_set(:@uuid, uuid)
79+
allow_any_instance_of(Uploadcare::FileMetadataClient).to receive(:show)
80+
.with(uuid: uuid, key: key, request_options: {})
81+
.and_return(value)
82+
83+
result = file_metadata.show(key: key)
84+
expect(result).to eq(value)
85+
end
6686
end
6787

6888
describe '#update' do
@@ -74,6 +94,16 @@
7494
result = file_metadata.update(uuid: uuid, key: key, value: value)
7595
expect(result).to eq(value)
7696
end
97+
98+
it 'uses existing uuid when not provided' do
99+
file_metadata.instance_variable_set(:@uuid, uuid)
100+
allow_any_instance_of(Uploadcare::FileMetadataClient).to receive(:update)
101+
.with(uuid: uuid, key: key, value: value, request_options: {})
102+
.and_return(value)
103+
104+
result = file_metadata.update(key: key, value: value)
105+
expect(result).to eq(value)
106+
end
77107
end
78108

79109
describe '#delete' do
@@ -85,6 +115,16 @@
85115
result = file_metadata.delete(uuid: uuid, key: key)
86116
expect(result).to be_nil
87117
end
118+
119+
it 'uses existing uuid when not provided' do
120+
file_metadata.instance_variable_set(:@uuid, uuid)
121+
allow_any_instance_of(Uploadcare::FileMetadataClient).to receive(:delete)
122+
.with(uuid: uuid, key: key, request_options: {})
123+
.and_return(nil)
124+
125+
result = file_metadata.delete(key: key)
126+
expect(result).to be_nil
127+
end
88128
end
89129
describe '.index' do
90130
it 'retrieves all metadata keys and values' do

0 commit comments

Comments
 (0)