forked from thumbor/libthumbor
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generic_views.py
164 lines (122 loc) · 5.2 KB
/
test_generic_views.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/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 [email protected]
"""libthumbor generic views tests"""
import os
import pytest
from preggy import expect
from libthumbor.crypto import CryptoURL
os.environ["DJANGO_SETTINGS_MODULE"] = "tests.testproj.testproj.settings"
try:
from django.conf import settings
from django.test import TestCase
from django.http import QueryDict
DJANGO_PRESENT = True
except ImportError:
DJANGO_PRESENT = False
HTTP_NOT_FOUND = 404
HTTP_METHOD_NOT_ALLOWED = 405
HTTP_OK = 200
HTTP_BAD_REQUEST = 400
@pytest.mark.skip_if(not DJANGO_PRESENT, "django must be present to run this test")
class GenericViewsTestCase(TestCase):
def setUp(self):
self.url_query = QueryDict("", mutable=True)
def test_without_url_param(self):
response = self.client.get("/gen_url/")
expect(response.status_code).to_equal(HTTP_BAD_REQUEST)
def test_generate_url_with_params_via_post(self):
image_args = {"image_url": "globo.com/media/img/my_image.jpg"}
response = self.client.post("/gen_url/", image_args)
expect(response.status_code).to_equal(HTTP_METHOD_NOT_ALLOWED)
def test_generate_url_with_params_via_get(self):
crypto = CryptoURL(settings.THUMBOR_SECURITY_KEY)
image_args = {"image_url": "globo.com/media/img/my_image.jpg"}
self.url_query.update(image_args)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_OK)
expect(response.content).to_equal(
settings.THUMBOR_SERVER + crypto.generate(**image_args).strip("/")
)
def test_passing_invalid_value_for_width(self):
self.url_query.update(
{"image_url": "globo.com/media/img/my_image.jpg", "width": 1.2}
)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_BAD_REQUEST)
expect(str(response.content)).to_include(
"The width value '1.2' is not an integer."
)
def test_passing_invalid_value_for_height(self):
self.url_query.update(
{"image_url": "globo.com/media/img/my_image.jpg", "height": "s"}
)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_BAD_REQUEST)
expect(str(response.content)).to_include(
"The height value 's' is not an integer."
)
def test_passing_invalid_aligns(self):
self.url_query.update(
{"image_url": "globo.com/media/img/my_image.jpg", "halign": "sss"}
)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_BAD_REQUEST)
def test_passing_only_one_crop_value(self):
self.url_query.update(
{"image_url": "globo.com/media/img/my_image.jpg", "crop_left": 100}
)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_BAD_REQUEST)
expect(str(response.content)).to_include("Missing values for cropping")
def test_passing_only_one_crop_with_invalid_value(self):
self.url_query.update(
{
"image_url": "globo.com/media/img/my_image.jpg",
"crop_top": "bla",
"crop_left": 200,
"crop_right": "1",
"crop_bottom": "blas",
}
)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_BAD_REQUEST)
expect(str(response.content)).to_include("Invalid values for cropping")
def test_passing_various_erroneous_values(self):
self.url_query.update(
{
"image_url": "globo.com/media/img/my_image.jpg",
"crop_left": 100,
"width": "aaa",
"height": 123,
}
)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_BAD_REQUEST)
def test_passing_all_params(self):
image_args = {
"image_url": "globo.com/media/img/my_image.jpg",
"halign": "left",
"valign": "middle",
"meta": True,
"smart": True,
"width": 400,
"height": 400,
"flip": True,
"flop": True,
}
self.url_query.update(image_args)
self.url_query.update(
{"crop_top": 100, "crop_left": 100, "crop_bottom": 200, "crop_right": 200}
)
image_args.update({"crop": ((100, 100), (200, 200))})
crypto = CryptoURL(settings.THUMBOR_SECURITY_KEY)
response = self.client.get("/gen_url/?" + self.url_query.urlencode())
expect(response.status_code).to_equal(HTTP_OK)
expect(response.content).to_equal(
settings.THUMBOR_SERVER + crypto.generate(**image_args).strip("/")
)