@@ -32,3 +32,83 @@ class SanitizedFile; end
3232 end
3333 end
3434end
35+
36+ RSpec . describe Cloudinary ::PreloadedFile do
37+ let ( :test_api_secret ) { "X7qLTrsES31MzxxkxPPA-pAGGfU" }
38+
39+ before do
40+ Cloudinary . config . update ( :api_secret => test_api_secret )
41+ end
42+
43+ describe "folder support" do
44+ it "should allow to use folders in PreloadedFile" do
45+ signature = Cloudinary ::Utils . api_sign_request ( { :public_id => "folder/file" , :version => "1234" } , Cloudinary . config . api_secret )
46+ preloaded = Cloudinary ::PreloadedFile . new ( "image/upload/v1234/folder/file.jpg#" + signature )
47+ expect ( preloaded ) . to be_valid
48+ [
49+ [ :filename , 'folder/file.jpg' ] ,
50+ [ :version , '1234' ] ,
51+ [ :public_id , 'folder/file' ] ,
52+ [ :signature , signature ] ,
53+ [ :resource_type , 'image' ] ,
54+ [ :type , 'upload' ] ,
55+ [ :format , 'jpg' ]
56+ ] . each do |attr , value |
57+ expect ( preloaded . send ( attr ) ) . to eq ( value )
58+ end
59+ end
60+ end
61+
62+ describe "signature verification" do
63+ let ( :public_id ) { 'tests/logo.png' }
64+ let ( :test_version ) { 1234 }
65+
66+ it "should correctly verify signature with proper parameter order" do
67+ # PreloadedFile extracts public_id by removing the format extension
68+ # So if filename is "tests/logo.png", public_id becomes "tests/logo"
69+ filename_with_format = public_id
70+ public_id_without_format = "tests/logo" # public_id without .png extension
71+
72+ # Generate a valid signature using the public_id without extension
73+ # The version parsed from preloaded string will be a string, so we use string here too
74+ version_string = test_version . to_s
75+ expected_signature = Cloudinary ::Utils . api_sign_request (
76+ { :public_id => public_id_without_format , :version => version_string } ,
77+ test_api_secret ,
78+ nil ,
79+ 1 # verify_api_response_signature uses version 1
80+ )
81+
82+ # Create a preloaded file string
83+ preloaded_string = "image/upload/v#{ version_string } /#{ filename_with_format } ##{ expected_signature } "
84+ preloaded_file = Cloudinary ::PreloadedFile . new ( preloaded_string )
85+
86+ expect ( preloaded_file ) . to be_valid
87+ end
88+
89+ it "should fail verification with incorrect signature" do
90+ wrong_signature = "wrongsignature"
91+ preloaded_string = "image/upload/v#{ test_version } /#{ public_id } ##{ wrong_signature } "
92+ preloaded_file = Cloudinary ::PreloadedFile . new ( preloaded_string )
93+
94+ expect ( preloaded_file ) . not_to be_valid
95+ end
96+
97+ it "should handle raw resource type correctly" do
98+ raw_filename = "document.pdf"
99+ version_string = test_version . to_s
100+ raw_signature = Cloudinary ::Utils . api_sign_request (
101+ { :public_id => raw_filename , :version => version_string } ,
102+ test_api_secret ,
103+ nil ,
104+ 1
105+ )
106+
107+ preloaded_string = "raw/upload/v#{ version_string } /#{ raw_filename } ##{ raw_signature } "
108+ preloaded_file = Cloudinary ::PreloadedFile . new ( preloaded_string )
109+
110+ expect ( preloaded_file ) . to be_valid
111+ expect ( preloaded_file . resource_type ) . to eq ( 'raw' )
112+ end
113+ end
114+ end
0 commit comments