ruby-thumbor 1.3.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,471 +0,0 @@
1
- require 'spec_helper'
2
- require 'json'
3
- require 'ruby-thumbor'
4
- require 'util/thumbor'
5
-
6
- image_url = 'my.domain.com/some/image/url.jpg'
7
- image_md5 = 'f33af67e41168e80fcc5b00f8bd8061a'
8
- key = 'my-security-key'
9
-
10
- describe Thumbor::CryptoURL do
11
- subject { Thumbor::CryptoURL.new key }
12
-
13
- describe '#new' do
14
- it "should create a new instance passing key and keep it" do
15
- subject.computed_key.should == 'my-security-keym'
16
- end
17
- end
18
-
19
- describe '#url_for' do
20
-
21
- it "should return just the image hash if no arguments passed" do
22
- url = subject.url_for :image => image_url
23
- url.should == image_md5
24
- end
25
-
26
- it "should raise if no image passed" do
27
- expect { subject.url_for Hash.new }.to raise_error(RuntimeError)
28
- end
29
-
30
- it "should return proper url for width-only" do
31
- url = subject.url_for :image => image_url, :width => 300
32
- url.should == '300x0/' << image_md5
33
- end
34
-
35
- it "should return proper url for height-only" do
36
- url = subject.url_for :image => image_url, :height => 300
37
- url.should == '0x300/' << image_md5
38
- end
39
-
40
- it "should return proper url for width and height" do
41
- url = subject.url_for :image => image_url, :width => 200, :height => 300
42
- url.should == '200x300/' << image_md5
43
- end
44
-
45
- it "should return proper smart url" do
46
- url = subject.url_for :image => image_url, :width => 200, :height => 300, :smart => true
47
- url.should == '200x300/smart/' << image_md5
48
- end
49
-
50
- it "should return proper fit-in url" do
51
- url = subject.url_for :image => image_url, :width => 200, :height => 300, :fit_in => true
52
- url.should == 'fit-in/200x300/' << image_md5
53
- end
54
-
55
- it "should return proper flip url if no width and height" do
56
- url = subject.url_for :image => image_url, :flip => true
57
- url.should == '-0x0/' << image_md5
58
- end
59
-
60
- it "should return proper flop url if no width and height" do
61
- url = subject.url_for :image => image_url, :flop => true
62
- url.should == '0x-0/' << image_md5
63
- end
64
-
65
- it "should return proper flip-flop url if no width and height" do
66
- url = subject.url_for :image => image_url, :flip => true, :flop => true
67
- url.should == '-0x-0/' << image_md5
68
- end
69
-
70
- it "should return proper flip url if width" do
71
- url = subject.url_for :image => image_url, :width => 300, :flip => true
72
- url.should == '-300x0/' << image_md5
73
- end
74
-
75
- it "should return proper flop url if height" do
76
- url = subject.url_for :image => image_url, :height => 300, :flop => true
77
- url.should == '0x-300/' << image_md5
78
- end
79
-
80
- it "should return horizontal align" do
81
- url = subject.url_for :image => image_url, :halign => :left
82
- url.should == 'left/' << image_md5
83
- end
84
-
85
- it "should not return horizontal align if it is center" do
86
- url = subject.url_for :image => image_url, :halign => :center
87
- url.should == image_md5
88
- end
89
-
90
- it "should return vertical align" do
91
- url = subject.url_for :image => image_url, :valign => :top
92
- url.should == 'top/' << image_md5
93
- end
94
-
95
- it "should not return vertical align if it is middle" do
96
- url = subject.url_for :image => image_url, :valign => :middle
97
- url.should == image_md5
98
- end
99
-
100
- it "should return halign and valign properly" do
101
- url = subject.url_for :image => image_url, :halign => :left, :valign => :top
102
- url.should == 'left/top/' << image_md5
103
- end
104
-
105
- it "should return meta properly" do
106
- url = subject.url_for :image => image_url, :meta => true
107
- url.should == 'meta/' << image_md5
108
- end
109
-
110
- it "should return proper crop url" do
111
- url = subject.url_for :image => image_url, :crop => [10, 20, 30, 40]
112
- url.should == '10x20:30x40/' << image_md5
113
- end
114
-
115
- it "should ignore crop if all zeros" do
116
- url = subject.url_for :image => image_url, :crop => [0, 0, 0, 0]
117
- url.should == image_md5
118
- end
119
-
120
- it "should have smart after halign and valign" do
121
- url = subject.url_for :image => image_url, :halign => :left, :valign => :top, :smart => true
122
- url.should == 'left/top/smart/' << image_md5
123
- end
124
-
125
- it "should ignore filters if empty" do
126
- url = subject.url_for :image => image_url, :filters => []
127
- url.should == image_md5
128
- end
129
-
130
- it "should have trim without params" do
131
- url = subject.url_for :image => image_url, :trim => true
132
- url.should == 'trim/' << image_md5
133
- end
134
-
135
- it "should have trim with direction param" do
136
- url = subject.url_for :image => image_url, :trim => ['bottom-right']
137
- url.should == 'trim:bottom-right/' << image_md5
138
- end
139
-
140
- it "should have trim with direction and tolerance param" do
141
- url = subject.url_for :image => image_url, :trim => ['bottom-right', 15]
142
- url.should == 'trim:bottom-right:15/' << image_md5
143
- end
144
-
145
- it "should have the trim option as the first one" do
146
- url = subject.url_for :image => image_url, :smart => true, :trim => true
147
-
148
- url.should == 'trim/smart/f33af67e41168e80fcc5b00f8bd8061a'
149
- end
150
-
151
-
152
- it "should have the right crop when cropping horizontally and given a left center" do
153
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 40, :height => 50, :center => [0, 50]
154
- url.should == '0x0:80x100/40x50/' << image_md5
155
- end
156
-
157
- it "should have the right crop when cropping horizontally and given a right center" do
158
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 40, :height => 50, :center => [100, 50]
159
- url.should == '20x0:100x100/40x50/' << image_md5
160
- end
161
-
162
- it "should have the right crop when cropping horizontally and given the actual center" do
163
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 40, :height => 50, :center => [50, 50]
164
- url.should == '10x0:90x100/40x50/' << image_md5
165
- end
166
-
167
- it "should have the right crop when cropping vertically and given a top center" do
168
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 50, :height => 40, :center => [50, 0]
169
- url.should == '0x0:100x80/50x40/' << image_md5
170
- end
171
-
172
- it "should have the right crop when cropping vertically and given a bottom center" do
173
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 50, :height => 40, :center => [50, 100]
174
- url.should == '0x20:100x100/50x40/' << image_md5
175
- end
176
-
177
- it "should have the right crop when cropping vertically and given the actual center" do
178
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 50, :height => 40, :center => [50, 50]
179
- url.should == '0x10:100x90/50x40/' << image_md5
180
- end
181
-
182
- it "should have the no crop when not necessary" do
183
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 50, :height => 50, :center => [50, 0]
184
- url.should == '50x50/' << image_md5
185
- end
186
-
187
- it "should blow up with a bad center" do
188
- expect { subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 50, :height => 40, :center => 50 }.to raise_error(RuntimeError)
189
- end
190
-
191
- it "should have no crop with a missing original_height" do
192
- url = subject.url_for :image => image_url, :original_width => 100, :width => 50, :height => 40, :center => [50, 50]
193
- url.should == '50x40/' << image_md5
194
- end
195
-
196
- it "should have no crop with a missing original_width" do
197
- url = subject.url_for :image => image_url, :original_height => 100, :width => 50, :height => 40, :center => [50, 50]
198
- url.should == '50x40/' << image_md5
199
- end
200
-
201
- it "should have no crop with out a width and height" do
202
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :center => [50, 50]
203
- url.should == image_md5
204
- end
205
-
206
- it "should use the original width with a missing width" do
207
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :height => 80, :center => [50, 50]
208
- url.should == '0x10:100x90/0x80/' << image_md5
209
- end
210
-
211
- it "should use the original height with a missing height" do
212
- url = subject.url_for :image => image_url,:original_width => 100, :original_height => 100, :width => 80, :center => [50, 50]
213
- url.should == '10x0:90x100/80x0/' << image_md5
214
- end
215
-
216
- it "should have the right crop with a negative width" do
217
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => -50, :height => 40, :center => [50, 50]
218
- url.should == '0x10:100x90/-50x40/' << image_md5
219
- end
220
-
221
- it "should have the right crop with a negative height" do
222
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => 50, :height => -40, :center => [50, 50]
223
- url.should == '0x10:100x90/50x-40/' << image_md5
224
- end
225
-
226
- it "should have the right crop with a negative height and width" do
227
- url = subject.url_for :image => image_url, :original_width => 100, :original_height => 100, :width => -50, :height => -40, :center => [50, 50]
228
- url.should == '0x10:100x90/-50x-40/' << image_md5
229
- end
230
- end
231
-
232
- describe '#generate' do
233
- it "should generate a proper url when only an image url is specified" do
234
- url = subject.generate :image => image_url
235
-
236
- url.should == "/964rCTkAEDtvjy_a572k7kRa0SU=/#{image_url}"
237
- end
238
-
239
- it "should create a new instance passing key and keep it" do
240
- url = subject.generate :width => 300, :height => 200, :image => image_url
241
-
242
- url.should == '/TQfyd3H36Z3srcNcLOYiM05YNO8=/300x200/my.domain.com/some/image/url.jpg'
243
- end
244
-
245
- it "should create a new instance passing key and keep it" do
246
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url
247
-
248
- url.should == '/YBQEWd3g_WRMnVEG73zfzcr8Zj0=/meta/300x200/my.domain.com/some/image/url.jpg'
249
- end
250
-
251
- it "should create a new instance passing key and keep it" do
252
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true
253
-
254
- url.should == '/jP89J0qOWHgPlm_lOA28GtOh5GU=/meta/300x200/smart/my.domain.com/some/image/url.jpg'
255
- end
256
-
257
- it "should create a new instance passing key and keep it" do
258
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :fit_in => true
259
-
260
- url.should == '/zrrOh_TtTs4kiLLEQq1w4bcTYdc=/meta/fit-in/300x200/smart/my.domain.com/some/image/url.jpg'
261
- end
262
-
263
- it "should create a new instance passing key and keep it" do
264
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :fit_in => true, :flip => true
265
-
266
- url.should == '/4t1XK1KH43cOb1QJ9tU00-W2_k8=/meta/fit-in/-300x200/smart/my.domain.com/some/image/url.jpg'
267
- end
268
-
269
- it "should create a new instance passing key and keep it" do
270
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :fit_in => true, :flip => true, :flop => true
271
-
272
- url.should == '/HJnvjZU69PkPOhyZGu-Z3Uc_W_A=/meta/fit-in/-300x-200/smart/my.domain.com/some/image/url.jpg'
273
- end
274
-
275
- it "should create a new instance passing key and keep it" do
276
- url = subject.generate :filters => ["quality(20)", "brightness(10)"], :image => image_url
277
-
278
- url.should == '/q0DiFg-5-eFZIqyN3lRoCvg2K0s=/filters:quality(20):brightness(10)/my.domain.com/some/image/url.jpg'
279
- end
280
- end
281
-
282
- describe "#generate :old => true" do
283
-
284
- it "should create a new instance passing key and keep it" do
285
- url = subject.generate :width => 300, :height => 200, :image => image_url, :old => true
286
-
287
- url.should == '/qkLDiIbvtiks0Up9n5PACtmpOfX6dPXw4vP4kJU-jTfyF6y1GJBJyp7CHYh1H3R2/' << image_url
288
- end
289
-
290
- it "should allow thumbor to decrypt it properly" do
291
- url = subject.generate :width => 300, :height => 200, :image => image_url, :old => true
292
-
293
- encrypted = url.split('/')[1]
294
-
295
- decrypted = decrypt_in_thumbor(encrypted)
296
-
297
- decrypted["horizontal_flip"].should == false
298
- decrypted["vertical_flip"].should == false
299
- decrypted["smart"].should == false
300
- decrypted["meta"].should == false
301
- decrypted["fit_in"].should == false
302
- decrypted["crop"]["left"].should == 0
303
- decrypted["crop"]["top"].should == 0
304
- decrypted["crop"]["right"].should == 0
305
- decrypted["crop"]["bottom"].should == 0
306
- decrypted["valign"].should == 'middle'
307
- decrypted["halign"].should == 'center'
308
- decrypted["image_hash"].should == image_md5
309
- decrypted["width"].should == 300
310
- decrypted["height"].should == 200
311
-
312
- end
313
-
314
- it "should allow thumbor to decrypt it properly with meta" do
315
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :old => true
316
-
317
- encrypted = url.split('/')[1]
318
-
319
- decrypted = decrypt_in_thumbor(encrypted)
320
-
321
- decrypted["meta"].should == true
322
- decrypted["image_hash"].should == image_md5
323
- decrypted["width"].should == 300
324
- decrypted["height"].should == 200
325
-
326
- end
327
-
328
- it "should allow thumbor to decrypt it properly with smart" do
329
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :old => true
330
-
331
- encrypted = url.split('/')[1]
332
-
333
- decrypted = decrypt_in_thumbor(encrypted)
334
-
335
- decrypted["meta"].should == true
336
- decrypted["smart"].should == true
337
- decrypted["image_hash"].should == image_md5
338
- decrypted["width"].should == 300
339
- decrypted["height"].should == 200
340
-
341
- end
342
-
343
- it "should allow thumbor to decrypt it properly with fit-in" do
344
- url = subject.generate :width => 300, :height => 200, :fit_in => true, :image => image_url, :old => true
345
-
346
- encrypted = url.split('/')[1]
347
-
348
- decrypted = decrypt_in_thumbor(encrypted)
349
-
350
- decrypted["fit_in"].should == true
351
- decrypted["image_hash"].should == image_md5
352
- decrypted["width"].should == 300
353
- decrypted["height"].should == 200
354
-
355
- end
356
-
357
- it "should allow thumbor to decrypt it properly with flip" do
358
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :flip => true, :old => true
359
-
360
- encrypted = url.split('/')[1]
361
-
362
- decrypted = decrypt_in_thumbor(encrypted)
363
-
364
- decrypted["meta"].should == true
365
- decrypted["smart"].should == true
366
- decrypted["image_hash"].should == image_md5
367
- decrypted["width"].should == 300
368
- decrypted["height"].should == 200
369
- decrypted["flip_horizontally"] == true
370
-
371
- end
372
-
373
- it "should allow thumbor to decrypt it properly with flop" do
374
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :flip => true, :flop => true, :old => true
375
-
376
- encrypted = url.split('/')[1]
377
-
378
- decrypted = decrypt_in_thumbor(encrypted)
379
-
380
- decrypted["meta"].should == true
381
- decrypted["smart"].should == true
382
- decrypted["image_hash"].should == image_md5
383
- decrypted["width"].should == 300
384
- decrypted["height"].should == 200
385
- decrypted["flip_horizontally"] == true
386
- decrypted["flip_vertically"] == true
387
-
388
- end
389
-
390
- it "should allow thumbor to decrypt it properly with halign" do
391
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :flip => true, :flop => true,
392
- :halign => :left, :old => true
393
-
394
- encrypted = url.split('/')[1]
395
-
396
- decrypted = decrypt_in_thumbor(encrypted)
397
-
398
- decrypted["meta"].should == true
399
- decrypted["smart"].should == true
400
- decrypted["image_hash"].should == image_md5
401
- decrypted["width"].should == 300
402
- decrypted["height"].should == 200
403
- decrypted["flip_horizontally"] == true
404
- decrypted["flip_vertically"] == true
405
- decrypted["halign"] == "left"
406
-
407
- end
408
-
409
- it "should allow thumbor to decrypt it properly with valign" do
410
- url = subject.generate :width => 300, :height => 200, :meta => true, :image => image_url, :smart => true, :flip => true, :flop => true,
411
- :halign => :left, :valign => :top, :old => true
412
-
413
- encrypted = url.split('/')[1]
414
-
415
- decrypted = decrypt_in_thumbor(encrypted)
416
-
417
- decrypted["meta"].should == true
418
- decrypted["smart"].should == true
419
- decrypted["image_hash"].should == image_md5
420
- decrypted["width"].should == 300
421
- decrypted["height"].should == 200
422
- decrypted["flip_horizontally"] == true
423
- decrypted["flip_vertically"] == true
424
- decrypted["halign"] == "left"
425
- decrypted["valign"] == "top"
426
-
427
- end
428
-
429
- it "should allow thumbor to decrypt it properly with cropping" do
430
- url = subject.generate :width => 300, :height => 200, :image => image_url, :crop => [10, 20, 30, 40], :old => true
431
-
432
- encrypted = url.split('/')[1]
433
-
434
- decrypted = decrypt_in_thumbor(encrypted)
435
-
436
- decrypted["horizontal_flip"].should == false
437
- decrypted["vertical_flip"].should == false
438
- decrypted["smart"].should == false
439
- decrypted["meta"].should == false
440
- decrypted["crop"]["left"].should == 10
441
- decrypted["crop"]["top"].should == 20
442
- decrypted["crop"]["right"].should == 30
443
- decrypted["crop"]["bottom"].should == 40
444
- decrypted["valign"].should == 'middle'
445
- decrypted["halign"].should == 'center'
446
- decrypted["image_hash"].should == image_md5
447
- decrypted["width"].should == 300
448
- decrypted["height"].should == 200
449
-
450
- end
451
-
452
- it "should allow thumbor to decrypt it properly with filters" do
453
- url = subject.generate :filters => ["quality(20)", "brightness(10)"], :image => image_url, :old => true
454
-
455
- encrypted = url.split('/')[1]
456
-
457
- decrypted = decrypt_in_thumbor(encrypted)
458
-
459
- decrypted["filters"].should == "quality(20):brightness(10)"
460
- end
461
- end
462
-
463
- describe "without security key" do
464
- subject { Thumbor::CryptoURL.new nil }
465
- it "should generate a unsafe url" do
466
- url = subject.generate :image => image_url
467
-
468
- url.should == "/unsafe/#{image_url}"
469
- end
470
- end
471
- end
data/spec/util/thumbor.rb DELETED
@@ -1,7 +0,0 @@
1
- def decrypt_in_thumbor(str)
2
- command = "python -c 'from thumbor.crypto import Cryptor; cr = Cryptor(\"my-security-keymy\"); print cr.decrypt(\"" << str << "\")'"
3
- result = Array.new
4
- IO.popen(command) { |f| result.push(f.gets) }
5
- result = result.join('').strip
6
- JSON.parse(result.gsub('"', "@@@").gsub("'", '"').gsub("@@@", '\\"').gsub('True', 'true').gsub('False', 'false').gsub('None', 'null'))
7
- end