|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../test_helper" |
| 4 | + |
| 5 | +class SigningTest < Minitest::Test |
| 6 | + def setup |
| 7 | + @client = Imagekit::Client.new( |
| 8 | + private_key: "dummy-key", |
| 9 | + password: "test_password" |
| 10 | + ) |
| 11 | + end |
| 12 | + |
| 13 | + def test_should_generate_a_signed_url_when_signed_is_true_without_expires_in |
| 14 | + url = @client.helper.build_url( |
| 15 | + Imagekit::Models::SrcOptions.new( |
| 16 | + src: "sdk-testing-files/future-search.png", |
| 17 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 18 | + signed: true |
| 19 | + ) |
| 20 | + ) |
| 21 | + |
| 22 | + expected = "https://ik.imagekit.io/demo/sdk-testing-files/future-search.png?ik-s=32dbbbfc5f945c0403c71b54c38e76896ef2d6b0" |
| 23 | + assert_equal(expected, url) |
| 24 | + end |
| 25 | + |
| 26 | + def test_should_generate_a_signed_url_when_signed_is_true_with_expires_in |
| 27 | + url = @client.helper.build_url( |
| 28 | + Imagekit::Models::SrcOptions.new( |
| 29 | + src: "sdk-testing-files/future-search.png", |
| 30 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 31 | + signed: true, |
| 32 | + expires_in: 3600.0 |
| 33 | + ) |
| 34 | + ) |
| 35 | + |
| 36 | + # Expect ik-t exist in the URL. We don't assert signature because it will keep changing. |
| 37 | + assert_includes(url, "ik-t") |
| 38 | + end |
| 39 | + |
| 40 | + def test_should_generate_a_signed_url_when_expires_in_is_above_0_and_even_if_signed_is_false |
| 41 | + url = @client.helper.build_url( |
| 42 | + Imagekit::Models::SrcOptions.new( |
| 43 | + src: "sdk-testing-files/future-search.png", |
| 44 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 45 | + signed: false, |
| 46 | + expires_in: 3600.0 |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + # Expect ik-t exist in the URL. We don't assert signature because it will keep changing. |
| 51 | + assert_includes(url, "ik-t") |
| 52 | + end |
| 53 | + |
| 54 | + def test_should_generate_signed_url_with_special_characters_in_filename |
| 55 | + url = @client.helper.build_url( |
| 56 | + Imagekit::Models::SrcOptions.new( |
| 57 | + src: "sdk-testing-files/हिन्दी.png", |
| 58 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 59 | + signed: true |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + expected = "https://ik.imagekit.io/demo/sdk-testing-files/%E0%A4%B9%E0%A4%BF%E0%A4%A8%E0%A5%8D%E0%A4%A6%E0%A5%80.png?ik-s=3fff2f31da1f45e007adcdbe95f88c8c330e743c" |
| 64 | + assert_equal(expected, url) |
| 65 | + end |
| 66 | + |
| 67 | + def test_should_generate_signed_url_with_text_overlay_containing_special_characters |
| 68 | + transformation = [ |
| 69 | + Imagekit::Models::Transformation.new( |
| 70 | + overlay: Imagekit::Models::TextOverlay.new( |
| 71 | + text: "हिन्दी", |
| 72 | + transformation: [ |
| 73 | + Imagekit::Models::TextOverlayTransformation.new( |
| 74 | + font_color: "red", |
| 75 | + font_size: "32", |
| 76 | + font_family: "sdk-testing-files/Poppins-Regular_Q15GrYWmL.ttf" |
| 77 | + ) |
| 78 | + ] |
| 79 | + ) |
| 80 | + ) |
| 81 | + ] |
| 82 | + |
| 83 | + url = @client.helper.build_url( |
| 84 | + Imagekit::Models::SrcOptions.new( |
| 85 | + src: "sdk-testing-files/हिन्दी.png", |
| 86 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 87 | + transformation: transformation, |
| 88 | + signed: true |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + expected = "https://ik.imagekit.io/demo/sdk-testing-files/%E0%A4%B9%E0%A4%BF%E0%A4%A8%E0%A5%8D%E0%A4%A6%E0%A5%80.png?tr=l-text,ie-4KS54KS%2F4KSo4KWN4KSm4KWA,fs-32,ff-sdk-testing-files@@Poppins-Regular_Q15GrYWmL.ttf,co-red,l-end&ik-s=705e41579d368caa6530a4375355325277fcfe5c" |
| 93 | + assert_equal(expected, url) |
| 94 | + end |
| 95 | + |
| 96 | + def test_should_generate_signed_url_with_text_overlay_and_special_characters_using_path_transformation_position |
| 97 | + transformation = [ |
| 98 | + Imagekit::Models::Transformation.new( |
| 99 | + overlay: Imagekit::Models::TextOverlay.new( |
| 100 | + text: "हिन्दी", |
| 101 | + transformation: [ |
| 102 | + Imagekit::Models::TextOverlayTransformation.new( |
| 103 | + font_color: "red", |
| 104 | + font_size: "32", |
| 105 | + font_family: "sdk-testing-files/Poppins-Regular_Q15GrYWmL.ttf" |
| 106 | + ) |
| 107 | + ] |
| 108 | + ) |
| 109 | + ) |
| 110 | + ] |
| 111 | + |
| 112 | + url = @client.helper.build_url( |
| 113 | + Imagekit::Models::SrcOptions.new( |
| 114 | + src: "sdk-testing-files/हिन्दी.png", |
| 115 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 116 | + transformation_position: :path, |
| 117 | + transformation: transformation, |
| 118 | + signed: true |
| 119 | + ) |
| 120 | + ) |
| 121 | + |
| 122 | + expected = "https://ik.imagekit.io/demo/tr:l-text,ie-4KS54KS%2F4KSo4KWN4KSm4KWA,fs-32,ff-sdk-testing-files@@Poppins-Regular_Q15GrYWmL.ttf,co-red,l-end/sdk-testing-files/%E0%A4%B9%E0%A4%BF%E0%A4%A8%E0%A5%8D%E0%A4%A6%E0%A5%80.png?ik-s=20958f6126fd67c90653f55a49f2b7bb938d9d1c" |
| 123 | + assert_equal(expected, url) |
| 124 | + end |
| 125 | + |
| 126 | + def test_should_generate_signed_url_with_query_parameters |
| 127 | + query_params = { |
| 128 | + "version" => "1.0", |
| 129 | + "cache" => "false" |
| 130 | + } |
| 131 | + |
| 132 | + url = @client.helper.build_url( |
| 133 | + Imagekit::Models::SrcOptions.new( |
| 134 | + src: "sdk-testing-files/future-search.png", |
| 135 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 136 | + query_parameters: query_params, |
| 137 | + signed: true |
| 138 | + ) |
| 139 | + ) |
| 140 | + |
| 141 | + expected = "https://ik.imagekit.io/demo/sdk-testing-files/future-search.png?cache=false&version=1.0&ik-s=03767bb6f0898c04e42f65714af65d937c696d66" |
| 142 | + assert_equal(expected, url) |
| 143 | + end |
| 144 | + |
| 145 | + def test_should_generate_signed_url_with_transformations_and_query_parameters |
| 146 | + transformation = [ |
| 147 | + Imagekit::Models::Transformation.new( |
| 148 | + width: 300.0, |
| 149 | + height: 200.0 |
| 150 | + ) |
| 151 | + ] |
| 152 | + |
| 153 | + query_params = { |
| 154 | + "version" => "2.0" |
| 155 | + } |
| 156 | + |
| 157 | + url = @client.helper.build_url( |
| 158 | + Imagekit::Models::SrcOptions.new( |
| 159 | + src: "sdk-testing-files/future-search.png", |
| 160 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 161 | + transformation: transformation, |
| 162 | + query_parameters: query_params, |
| 163 | + signed: true |
| 164 | + ) |
| 165 | + ) |
| 166 | + |
| 167 | + expected = "https://ik.imagekit.io/demo/sdk-testing-files/future-search.png?version=2.0&tr=w-300,h-200&ik-s=601d97a7834b7554f4dabf0d3fc3a219ceeb6b31" |
| 168 | + assert_equal(expected, url) |
| 169 | + end |
| 170 | + |
| 171 | + def test_should_not_sign_url_when_signed_is_false |
| 172 | + url = @client.helper.build_url( |
| 173 | + Imagekit::Models::SrcOptions.new( |
| 174 | + src: "sdk-testing-files/future-search.png", |
| 175 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 176 | + signed: false |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + expected = "https://ik.imagekit.io/demo/sdk-testing-files/future-search.png" |
| 181 | + assert_equal(expected, url) |
| 182 | + refute_includes(url, "ik-s=") |
| 183 | + refute_includes(url, "ik-t=") |
| 184 | + end |
| 185 | + |
| 186 | + def test_should_generate_signed_url_with_transformations_in_path_position_and_query_parameters |
| 187 | + transformation = [ |
| 188 | + Imagekit::Models::Transformation.new( |
| 189 | + width: 300.0, |
| 190 | + height: 200.0 |
| 191 | + ) |
| 192 | + ] |
| 193 | + |
| 194 | + query_params = { |
| 195 | + "version" => "2.0" |
| 196 | + } |
| 197 | + |
| 198 | + url = @client.helper.build_url( |
| 199 | + Imagekit::Models::SrcOptions.new( |
| 200 | + src: "sdk-testing-files/future-search.png", |
| 201 | + url_endpoint: "https://ik.imagekit.io/demo/", |
| 202 | + transformation: transformation, |
| 203 | + transformation_position: :path, |
| 204 | + query_parameters: query_params, |
| 205 | + signed: true |
| 206 | + ) |
| 207 | + ) |
| 208 | + |
| 209 | + expected = "https://ik.imagekit.io/demo/tr:w-300,h-200/sdk-testing-files/future-search.png?version=2.0&ik-s=dd1ee8f83d019bc59fd57a5fc4674a11eb8a3496" |
| 210 | + assert_equal(expected, url) |
| 211 | + end |
| 212 | +end |
0 commit comments