Skip to content

Commit

Permalink
Merge branch 'branch-6.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus committed Nov 18, 2016
2 parents 0996853 + 255d7f0 commit 024ded0
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 9 deletions.
2 changes: 1 addition & 1 deletion tests/test_app.py
Expand Up @@ -16,7 +16,7 @@
from thumbor.app import (
ThumborServiceApp
)
from libthumbor.url import Url
from thumbor.url import Url


class AppTestCase(TestCase):
Expand Down
146 changes: 146 additions & 0 deletions tests/test_url.py
@@ -0,0 +1,146 @@
#!/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)'
)
Empty file added tests/url_signers/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions tests/url_signers/test_base64_hmac_sha1_signer.py
@@ -0,0 +1,38 @@
#!/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

import base64
import hmac
import hashlib
from unittest import TestCase

from preggy import expect

from thumbor.url_signers.base64_hmac_sha1 import (
UrlSigner
)


class Base64HmacSha1UrlSignerTestCase(TestCase):
def test_can_create_signer(self):
signer = UrlSigner(security_key="something")
expect(signer).to_be_instance_of(UrlSigner)
expect(signer.security_key).to_equal('something')

def test_can_sign_url(self):
signer = UrlSigner(security_key="something")
url = '10x11:12x13/-300x-300/center/middle/smart/some/image.jpg'
expected = base64.urlsafe_b64encode(
hmac.new(
'something', unicode(url).encode('utf-8'), hashlib.sha1
).digest()
)
actual = signer.signature(url)
expect(actual).to_equal(expected)
43 changes: 43 additions & 0 deletions tests/url_signers/test_base_url_signer.py
@@ -0,0 +1,43 @@
#!/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_signers import (
BaseUrlSigner
)


class BaseSignerTestCase(TestCase):
def test_can_create_signer(self):
signer = BaseUrlSigner(security_key="something")
expect(signer).to_be_instance_of(BaseUrlSigner)
expect(signer.security_key).to_equal('something')

def test_can_create_unicode_signer(self):
signer = BaseUrlSigner(security_key=u"téste")
expect(signer).to_be_instance_of(BaseUrlSigner)
expect(signer.security_key).to_equal('t\xc3\xa9ste')

def test_can_validate_url(self):
class TestSigner(BaseUrlSigner):
def signature(self, url):
return "%s+1" % url

signer = TestSigner(security_key=u"téste")
expect(signer.validate("http://www.test.com+1", "http://www.test.com")).to_be_true()

def test_has_abstract_method(self):
signer = BaseUrlSigner(security_key=u"téste")

with expect.error_to_happen(NotImplementedError):
signer.signature("test-url")
4 changes: 2 additions & 2 deletions thumbor/__init__.py
Expand Up @@ -10,5 +10,5 @@

'''This is the main module in thumbor'''

__version__ = "6.0.1"
__release_date__ = "22-Mar-2016"
__version__ = "6.1.5"
__release_date__ = "18-Aug-2016"
3 changes: 1 addition & 2 deletions thumbor/app.py
Expand Up @@ -10,12 +10,11 @@
import tornado.web
import tornado.ioloop

from libthumbor.url import Url

from thumbor.handlers.blacklist import BlacklistHandler
from thumbor.handlers.healthcheck import HealthcheckHandler
from thumbor.handlers.upload import ImageUploadHandler
from thumbor.handlers.image_resource import ImageResourceHandler
from thumbor.url import Url
from thumbor.handlers.imaging import ImagingHandler


Expand Down
4 changes: 2 additions & 2 deletions thumbor/crypto.py
Expand Up @@ -15,8 +15,8 @@

# Import the Signer class to support backward-compatibility
# and existing documentation
from libthumbor.url_signers.base64_hmac_sha1 import UrlSigner as Signer # NOQA
from libthumbor.url import Url
from thumbor.url_signers.base64_hmac_sha1 import UrlSigner as Signer # NOQA
from thumbor.url import Url


class Cryptor(object):
Expand Down
3 changes: 1 addition & 2 deletions thumbor/filters/extract_focal.py
Expand Up @@ -9,9 +9,8 @@

import re

from libthumbor.url import Url

from thumbor.filters import BaseFilter, filter_method, PHASE_PRE_LOAD
from thumbor.url import Url
from thumbor.point import FocalPoint


Expand Down

0 comments on commit 024ded0

Please sign in to comment.