Skip to content

Commit

Permalink
URL generation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Oct 22, 2015
1 parent 506d7bb commit 8872048
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 2 deletions.
151 changes: 151 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# thumbor imaging service
# https://github.com/thumbor/thumbor/wiki

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 globo.com timehome@corp.globo.com

from unittest import TestCase

from preggy import expect

from thumbor.url import Url


class UrlTestCase(TestCase):
def setUp(self):
Url.compiled_regex = None

def test_can_get_regex(self):
regex = Url.regex()

expect(regex).to_equal(
'/?(?:(?:(?P<unsafe>unsafe)|(?P<hash>.+?))/)?(?:(?P<debug>debug)/)?(?:(?P<meta>meta)/)?'
'(?:(?P<trim>trim(?::(?:top-left|bottom-right))?(?::\\d+)?)/)?'
'(?:(?P<crop_left>\\d+)x(?P<crop_top>\\d+):(?P<crop_right>\\d+)x(?P<crop_bottom>\\d+)/)?'
'(?:(?P<adaptive>adaptive-)?(?P<full>full-)?(?P<fit_in>fit-in)/)?(?:(?P<horizontal_flip>-)?'
'(?P<width>(?:\\d+|orig))?x(?P<vertical_flip>-)?(?P<height>(?:\\d+|orig))?/)?'
'(?:(?P<halign>left|right|center)/)?(?:(?P<valign>top|bottom|middle)/)?'
'(?:(?P<smart>smart)/)?(?:filters:(?P<filters>.+?\\))/)?(?P<image>.+)'
)

def test_can_get_regex_without_unsafe(self):
regex = Url.regex(False)

expect(regex).to_equal(
'/?(?:(?P<debug>debug)/)?(?:(?P<meta>meta)/)?'
'(?:(?P<trim>trim(?::(?:top-left|bottom-right))?(?::\\d+)?)/)?'
'(?:(?P<crop_left>\\d+)x(?P<crop_top>\\d+):(?P<crop_right>\\d+)x(?P<crop_bottom>\\d+)/)?'
'(?:(?P<adaptive>adaptive-)?(?P<full>full-)?(?P<fit_in>fit-in)/)?(?:(?P<horizontal_flip>-)?'
'(?P<width>(?:\\d+|orig))?x(?P<vertical_flip>-)?(?P<height>(?:\\d+|orig))?/)?'
'(?:(?P<halign>left|right|center)/)?(?:(?P<valign>top|bottom|middle)/)?'
'(?:(?P<smart>smart)/)?(?:filters:(?P<filters>.+?\\))/)?(?P<image>.+)'
)

def test_parsing_invalid_url(self):
expect(Url.compiled_regex).to_be_null()

url = ""
expect(Url.parse_decrypted(url)).to_be_null()

def test_parsing_complete_url(self):
url = '/debug/meta/trim/300x200:400x500/adaptive-full-fit-in/-300x-400/' \
'left/top/smart/filters:brightness(100)/some/image.jpg'

expected = {
'trim': 'trim',
'full': True,
'halign': 'left',
'fit_in': True,
'vertical_flip': True,
'image': 'some/image.jpg',
'crop': {'top': 200, 'right': 400, 'bottom': 500, 'left': 300},
'height': 400,
'width': 300,
'meta': True,
'horizontal_flip': True,
'filters': 'brightness(100)',
'valign': 'top',
'debug': True,
'adaptive': True,
'smart': True,
}

result = Url.parse_decrypted(url)
expect(result).not_to_be_null()
expect(result).to_be_like(expected)

# do it again to use compiled regex
result = Url.parse_decrypted(url)
expect(result).not_to_be_null()
expect(result).to_be_like(expected)

def test_can_generate_url(self):
url = Url.generate_options(
debug=True,
width=300,
height=200,
smart=True,
meta=True,
trim=True,
adaptive=True,
full=True,
fit_in=True,
horizontal_flip=True,
vertical_flip=True,
halign='left',
valign='top',
crop_left=100,
crop_top=100,
crop_right=400,
crop_bottom=400,
filters='brightness(100)'
)

expect(url).to_equal(
'debug/meta/trim/100x100:400x400/adaptive-full-fit-in/-300x-200/left/top/smart/filters:brightness(100)'
)

def test_can_generate_url_with_defaults(self):
url = Url.generate_options()

expect(url).to_be_empty()

def test_can_generate_url_with_fitin(self):
url = Url.generate_options(fit_in=True, adaptive=False, full=False)

expect(url).to_equal('fit-in')

def test_can_generate_url_with_custom_trim(self):
url = Url.generate_options(
debug=True,
width=300,
height=200,
smart=True,
meta=True,
trim='300x200',
adaptive=True,
full=True,
fit_in=True,
horizontal_flip=True,
vertical_flip=True,
halign='left',
valign='top',
crop_left=100,
crop_top=100,
crop_right=400,
crop_bottom=400,
filters='brightness(100)'
)

expect(url).to_equal(
'debug/meta/trim:300x200/100x100:400x400/adaptive-full-fit-in/-300x-200/left/top/smart/filters:brightness(100)'
)

def test_can_encode_url(self):
url = '/tes+ t:?%=&()~",\'$'

expect(Url.encode_url(url)).to_equal('/tes%2B%20t:?%=&()~",\'$')
8 changes: 6 additions & 2 deletions thumbor/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def parse_decrypted(cls, url):
result = result.groupdict()

int_or_0 = lambda value: 0 if value is None else int(value)

adaptive = (result.get('adaptive', '') or '').startswith('adaptive')
full = (result.get('full', '') or '').startswith('full')

values = {
'debug': result['debug'] == 'debug',
'meta': result['meta'] == 'meta',
Expand All @@ -74,8 +78,8 @@ def parse_decrypted(cls, url):
'right': int_or_0(result['crop_right']),
'bottom': int_or_0(result['crop_bottom'])
},
'adaptive': result['adaptive'] == 'adaptive',
'full': result['full'] == 'full',
'adaptive': adaptive,
'full': full,
'fit_in': result['fit_in'] == 'fit-in',
'width': result['width'] == 'orig' and 'orig' or int_or_0(result['width']),
'height': result['height'] == 'orig' and 'orig' or int_or_0(result['height']),
Expand Down

0 comments on commit 8872048

Please sign in to comment.