File: test_cryptourl.py

package info (click to toggle)
libthumbor 2.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: python: 900; makefile: 18
file content (103 lines) | stat: -rw-r--r-- 3,354 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
# -*- coding: utf-8 -*-

# libthumbor - python extension to thumbor
# http://github.com/heynemann/libthumbor

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Bernardo Heynemann heynemann@gmail.com

"""libthumbor cryptography tests"""

from unittest import TestCase

from preggy import expect
from six import ensure_text

from libthumbor.crypto import CryptoURL

IMAGE_URL = "my.server.com/some/path/to/image.jpg"
KEY = b"my-security-key"


class NewFormatUrlTestsMixin:
    def test_generated_url_1(self):
        url = self.crypto.generate(image_url=IMAGE_URL, width=300, height=200)
        expect(url).to_equal(
            "/8ammJH8D-7tXy6kU3lTvoXlhu4o=/300x200/my.server.com/some/path/to/image.jpg"
        )

    def test_generated_url_2(self):
        url = self.crypto.generate(
            image_url=IMAGE_URL, width=300, height=200, crop=((10, 10), (200, 200))
        )
        expect(url).to_equal(
            "/B35oBEIwztbc3jm7vsdqLez2C78=/10x10:200x200/300x200/"
            "my.server.com/some/path/to/image.jpg"
        )

    def test_generated_url_3(self):
        url = self.crypto.generate(
            image_url=IMAGE_URL,
            width=300,
            height=200,
            crop=((10, 10), (200, 200)),
            filters=("brightness(20)", "contrast(10)"),
        )
        expect(url).to_equal(
            "/as8U2DbUUtTMgvPF26LkjS3MocY=/10x10:200x200/300x200/"
            "filters:brightness(20):contrast(10)/my.server.com/some/path/to/image.jpg"
        )

    def test_generated_url_4(self):
        url = self.crypto.generate(
            image_url=IMAGE_URL,
            width=300,
            height=200,
            crop=((10, 10), (200, 200)),
            filters=("brightness(20)", "contrast(10)"),
        )
        expect(url).to_equal(
            "/as8U2DbUUtTMgvPF26LkjS3MocY=/10x10:200x200/300x200/"
            "filters:brightness(20):contrast(10)/my.server.com/some/path/to/image.jpg"
        )
        # making sure no internal state affects subsequent calls.
        url = self.crypto.generate(
            image_url=IMAGE_URL,
            width=300,
            height=200,
            crop=((10, 10), (200, 200)),
            filters=("brightness(20)", "contrast(10)"),
        )
        expect(url).to_equal(
            "/as8U2DbUUtTMgvPF26LkjS3MocY=/10x10:200x200/300x200/"
            "filters:brightness(20):contrast(10)/my.server.com/some/path/to/image.jpg"
        )


class NewFormatUrl(TestCase, NewFormatUrlTestsMixin):
    def setUp(self):
        self.crypto = CryptoURL(KEY)


class NewFormatUrlWithUnicodeKey(TestCase, NewFormatUrlTestsMixin):
    def setUp(self):
        self.crypto = CryptoURL(ensure_text(KEY))


class GenerateWithUnsafeTestCase(TestCase):
    def setUp(self):
        self.crypto = CryptoURL(KEY)

    def test_should_pass_unsafe_to_generate_and_get_an_unsafe_url(self):
        url = self.crypto.generate(
            image_url=IMAGE_URL, crop=((10, 20), (30, 40)), unsafe=True
        )
        expect(url.startswith("unsafe")).to_be_true()

    def test_should_not_get_an_unsafe_url_when_unsafe_is_false(self):
        url = self.crypto.generate(
            image_url=IMAGE_URL, crop=((10, 20), (30, 40)), unsafe=False
        )
        expect(url.startswith("unsafe")).to_be_false()