-
Notifications
You must be signed in to change notification settings - Fork 0
Libraries
Even though the process of generating safe image URLs is explained in the Security page, we'll try to provide libraries in each programming language to ease this process.
- libthumbor - Python library to enable easy usage of thumbor.
- node-thumbor - Node.js module to easily generate thumbor encrypted urls.
- ruby-thumbor - Ruby's gem to interact with thumbor server.
If you want to provide a library to enable easy usage of thumbor in your favorite programming language, please send an e-mail to [email protected] and we'll add it here.
Below are all the scenarios we think are worth testing automatically so you can guarantee compatibility with thumbor. Please note that this is not meant to be a replacement for TDD or for any other testing methodology you might want to use. These are just helper scenarios that we thought would help any library developers.
We sincerely advise you to have thumbor installed in your machine, so you can implement a method in your tests that has thumbor decrypt your URL and return a hash with the decrypted options. This way you can make sure your url formatting and encryption are working properly. Here's how it was implemented in Ruby:
def decrypt_in_thumbor(key, str)
#bash command to call thumbor's decrypt method
command = "python -c 'from thumbor.crypto import Crypto; cr = Crypto(\"" << key << "\"); print cr.decrypt(\"" << str << "\")'"
#execute it in the shell using ruby's popen mechanism
result = Array.new
IO.popen(command) { |f| result.push(f.gets) }
#convert the results from JSON to a Hash so we can assert on it.
result = result.join('').strip.gsub('"', "@@@").gsub("'", '"').gsub("@@@", '\\"').gsub('True', 'true').gsub('False', 'false') #replacements required to compply with JSON
JSON.parse(result)
end
You should be able to implement this easily in any modern programming language. It makes for very reliable tests.
Remember that these are in pseudo-code (BDD-like) language, and not in any programming language specifically.
These scenarios assume that you separate the logic of composing the url to be encrypted into a different "module", that is to be tested with the URL Testing Scenarios after these scenarios.
####Scenario 1 - Encryption of a known url results Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a width of 300 And a height of 200 When I ask my library for an encrypted URL Then I get '/l42l54VqaV_J-EcB5quNMP6CnsN9BX7htrh-QbPuDv0C7adUXX7LTo6DHm_woJtZ/my.server.com/some/path/to/image.jpg' as url
####Scenario 2 - Thumbor decryption of my library encryption Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a width of 300 And a height of 200 When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: horizontal_flip = False vertical_flip = False smart = False meta = False fit_in = False crop['left'] = 0 crop['top'] = 0 crop['right'] = 0 crop['bottom'] = 0 valign = "middle" halign = "center" width = 300 height = 200 image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 3 - Thumbor decryption of my library encryption with meta Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the meta flag When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: meta = True image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 4 - Thumbor decryption of my library encryption with smart Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the smart flag When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: smart = True image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 5 - Thumbor decryption of my library encryption with fit-in Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the fit-in flag When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: fit_in = True image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 6 - Thumbor decryption of my library encryption with flip Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the flip flag When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: flip_horizontally = True image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 7 - Thumbor decryption of my library encryption with flop Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the flop flag When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: flip_vertically = True image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 8 - Thumbor decryption of my library encryption with halign Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the horizontal alignment of 'right' When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: halign = 'right' image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 9 - Thumbor decryption of my library encryption with valign Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And the vertical alignment of 'top' When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: valign = 'top' image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 10 - Thumbor decryption of my library encryption with manual cropping Given A security key of 'my-security-key' And an image URL of "my.server.com/some/path/to/image.jpg" And a manual crop left-top point of (10, 20) And a manual crop right-bottom point of (30, 40) When I ask my library for an encrypted URL And I call the aforementioned 'decrypt_in_thumbor' method Then I get a decrypted dictionary that contains the following: crop['left'] = 10 crop['top'] = 20 crop['right'] = 30 crop['bottom'] = 40 image_hash = 84996242f65a4d864aceb125e1c4c5ba
####Scenario 1 - URL method returns the md5 hash if no options specified Given An image URL of "my.server.com/some/path/to/image.jpg" When I ask my library for an URL Then I get "84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 2 - URL method raises if no image url specified Given An image URL of "" or null When I ask my library for an URL Then I get an exception that says image URL is mandatory
####Scenario 3 - URL method returns proper width and zero height if only width specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a width of 300 When I ask my library for an URL Then I get "300x0/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 4 - URL method returns proper height and zero width if only height specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a height of 300 When I ask my library for an URL Then I get "0x300/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 5 - URL method returns proper height and width Given An image URL of "my.server.com/some/path/to/image.jpg" And a width of 200 And a height of 300 When I ask my library for an URL Then I get "200x300/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 6 - URL method returns proper smart url Given An image URL of "my.server.com/some/path/to/image.jpg" And a width of 200 And a height of 300 And the smart flag When I ask my library for an URL Then I get "200x300/smart/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 7 - URL method returns proper fit-in url Given An image URL of "my.server.com/some/path/to/image.jpg" And a width of 200 And a height of 300 And the fit-in flag When I ask my library for an URL Then I get "fit-in/200x300/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 8 - URL method returns proper flip when no width or height specified Given An image URL of "my.server.com/some/path/to/image.jpg" And the flip flag When I ask my library for an URL Then I get "-0x0/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 9 - URL method returns proper flop when no width or height specified Given An image URL of "my.server.com/some/path/to/image.jpg" And the flop flag When I ask my library for an URL Then I get "0x-0/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 10 - URL method returns proper flip-flop when no width or height specified Given An image URL of "my.server.com/some/path/to/image.jpg" And the flip flag And the flop flag When I ask my library for an URL Then I get "-0x-0/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 11 - URL method returns proper flip URL specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a width of 200 And the flip flag When I ask my library for an URL Then I get "-200x0/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 12 - URL method returns proper flop URL specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a height of 200 And the flop flag When I ask my library for an URL Then I get "0x-200/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 13 - URL method returns proper horizontal alignment specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a 'left' horizontal alignment option When I ask my library for an URL Then I get "left/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 14 - URL method returns image hash if 'center' horizontal alignment specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a 'center' horizontal alignment option When I ask my library for an URL Then I get "left/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 15 - URL method returns proper vertical alignment specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a 'top' vertical alignment option When I ask my library for an URL Then I get "top/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 16 - URL method returns image hash if 'middle' vertical alignment specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a 'middle' vertical alignment option When I ask my library for an URL Then I get "middle/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 17 - URL method returns proper horizontal and vertical alignments specified Given An image URL of "my.server.com/some/path/to/image.jpg" And a 'left' horizontal alignment option And a 'top' vertical alignment option When I ask my library for an URL Then I get "left/top/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 18 - URL method returns proper meta url Given An image URL of "my.server.com/some/path/to/image.jpg" And a 'meta' flag When I ask my library for an URL Then I get "meta/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 19 - URL method returns proper manual crop url Given An image URL of "my.server.com/some/path/to/image.jpg" And a manual crop left-top point of (10, 20) And a manual crop right-bottom point of (30, 40) When I ask my library for an URL Then I get "10x20:30x40/84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 20 - URL method returns image hash if all crop points are zero Given An image URL of "my.server.com/some/path/to/image.jpg" And a manual crop left-top point of (0, 0) And a manual crop right-bottom point of (0, 0) When I ask my library for an URL Then I get "84996242f65a4d864aceb125e1c4c5ba" as URL
####Scenario 21 - URL method returns 'smart' cropping information after alignment Given An image URL of "my.server.com/some/path/to/image.jpg" And a 'smart' flag And a 'left' horizontal alignment option When I ask my library for an URL Then I get "left/smart/84996242f65a4d864aceb125e1c4c5ba" as URL