/
pictures_controller_spec.rb
416 lines (363 loc) · 15.7 KB
/
pictures_controller_spec.rb
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
require 'spec_helper'
# Fixes missing method tempfile error
class Rack::Test::UploadedFile
attr_reader :tempfile
end
module Alchemy
describe PicturesController do
let(:public_page) { create(:public_page, restricted: false) }
let(:restricted_page) { create(:public_page, restricted: true) }
let(:element) { create(:element, page: public_page, name: 'bild', create_contents_after_create: true) }
let(:restricted_element) { create(:element, page: restricted_page, name: 'bild', create_contents_after_create: true) }
let(:picture) { create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/image.png', __FILE__), 'image/png')) }
describe '#zoom' do
let(:picture) do
create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/80x60.png', __FILE__), 'image/png'))
end
it "renders the original image without any resizing" do
alchemy_get :zoom, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.body[0x10..0x18].unpack('NN')).to eq([80, 60])
end
context "Requesting a picture that is assigned with restricted pages only" do
before do
essence = restricted_element.contents.where(name: 'image').first.essence
essence.picture_id = picture.id
essence.save
end
context "as guest user" do
it "should not render the picture, but redirect to login path" do
alchemy_get :zoom, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(302)
expect(response).to redirect_to(Alchemy.login_path)
end
end
context "as member user" do
before do
authorize_user(build(:alchemy_dummy_user))
end
it "should render the picture" do
alchemy_get :zoom, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(200)
end
end
end
end
describe '#show' do
it "skips the session cookie" do
expect {
alchemy_get :show, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
}.to change {
request.session_options.fetch(:skip) { false }
}.to(true)
end
context "Requesting a picture with tempared security token" do
it "should render status 400" do
alchemy_get :show, id: picture.id, name: picture.urlname, format: :png, sh: '14m4b4dh4ck3r'
expect(response.status).to eq(400)
end
end
context "Requesting a picture with another format then the original image" do
it "should convert the picture format" do
alchemy_get :show, id: picture.id, name: picture.urlname, format: :jpeg, sh: picture.security_token
expect(response.content_type).to eq('image/jpeg')
end
end
context "Requesting a picture with not allowed format" do
it "should raise error" do
expect {
alchemy_get :show, id: picture.id, name: picture.urlname, format: :wim, sh: picture.security_token
}.to raise_error(ActionController::UnknownFormat)
end
end
context "Requesting a picture that has no image file attached" do
before do
expect(picture).to receive(:image_file).and_return(nil)
expect(Picture).to receive(:find).and_return(picture)
end
it "raises missing file error" do
expect {
alchemy_get :show, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
}.to raise_error(Alchemy::MissingImageFileError)
end
end
context "Requesting a picture with crop_from and crop_size parameters" do
let(:picture) do
create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/500x500.png', __FILE__), 'image/png'))
end
it "renders the cropped picture" do
alchemy_get :show, id: picture.id, name: picture.urlname, crop: 'crop', size: '123x44', crop_size: '123x44',
crop_from: '0x0', format: :png,
sh: picture.security_token(crop_size: '123x44', crop_from: '0x0', crop: true, size: '123x44')
expect(response.body[0x10..0x18].unpack('NN')).to eq([123, 44])
end
end
context "Requesting a picture with crop_from and crop_size parameters with different size param" do
let(:picture) do
create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/500x500.png', __FILE__), 'image/png'))
end
it "renders the cropped picture" do
alchemy_get :show,
id: picture.id,
name: picture.urlname,
crop: 'crop',
size: '100x100',
crop_size: '200x200',
crop_from: '0x0',
format: :png,
sh: picture.security_token(
crop_size: '200x200',
crop_from: '0x0',
crop: true,
size: '100x100'
)
expect(response.body[0x10..0x18].unpack('NN')).to eq [100, 100]
end
end
context "Requesting a picture with crop_from and crop_size parameters with larger size param" do
let(:picture) do
create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/500x500.png', __FILE__), 'image/png'))
end
it "renders the cropped picture without upsampling" do
alchemy_get :show,
id: picture.id,
name: picture.urlname,
crop: 'crop',
size: '400x400',
crop_size: '200x200',
crop_from: '0x0',
format: :png,
sh: picture.security_token(
crop_size: '200x200',
crop_from: '0x0',
crop: true,
size: '400x400'
)
expect(response.body[0x10..0x18].unpack('NN')).to eq [200, 200]
end
end
context "Requesting a picture with crop_from and crop_size parameters with larger size param and upsample set" do
let(:picture) do
create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/500x500.png', __FILE__), 'image/png'))
end
it "renders the cropped picture with upsampling" do
alchemy_get :show,
id: picture.id,
name: picture.urlname,
crop: 'crop',
size: '400x400',
crop_size: '200x200',
crop_from: '0x0',
format: :png,
upsample: 'true',
sh: picture.security_token(
crop_size: '200x200',
crop_from: '0x0',
crop: true,
size: '400x400',
upsample: 'true'
)
expect(response.body[0x10..0x18].unpack('NN')).to eq [400, 400]
end
end
context "Requesting a picture that is not assigned with any page" do
it "should render the picture" do
alchemy_get :show, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(200)
end
end
context "Requesting a picture that is assigned on restricted and non-restricted pages" do
before do
essence = element.contents.where(name: 'image').first.essence
essence.picture_id = picture.id
essence.save
essence = restricted_element.contents.where(name: 'image').first.essence
essence.picture_id = picture.id
essence.save
end
context "as guest user" do
it "should render the picture" do
alchemy_get :show, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(200)
end
end
end
context "Requesting a picture that is assigned with restricted pages only" do
before do
essence = restricted_element.contents.where(name: 'image').first.essence
essence.picture_id = picture.id
essence.save
end
context "as guest user" do
it "should not render the picture, but redirect to login path" do
alchemy_get :show, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(302)
expect(response).to redirect_to(Alchemy.login_path)
end
end
context "as member user" do
before do
authorize_user(build(:alchemy_dummy_user))
end
it "should render the picture" do
alchemy_get :show, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(200)
end
end
end
context 'requesting an animated gif with different format' do
let(:image) do
fixture_file_upload(
File.expand_path('../../fixtures/animated.gif', __FILE__),
'image/gif'
)
end
let(:picture) { build_stubbed(:picture, image_file: image) }
before do
expect(image).to receive(:ext) { 'gif' }
expect(picture).to receive(:image_file) { image }
expect(Alchemy::Picture).to receive(:find) { picture }
end
it 'flattens the gif before converting the format.' do
expect(image).to receive(:encode).with('png', '-flatten') { double(data: '') }
alchemy_get :show, id: picture.id, name: picture.urlname,
format: 'png', sh: picture.security_token
end
end
end
describe '#thumbnail' do
let(:picture) do
create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/500x500.png', __FILE__), 'image/png'))
end
context 'with size param set to small' do
it "resizes the image to 80x60 while maintaining aspect ratio" do
alchemy_get :thumbnail, id: picture.id, name: picture.urlname, size: 'small', format: :png, sh: picture.security_token(size: 'small')
expect(response.body[0x10..0x18].unpack('NN')).to eq([60, 60])
end
end
context 'with size param set to medium' do
it "resizes the image to 160x120 while maintaining aspect ratio" do
alchemy_get :thumbnail, id: picture.id, name: picture.urlname, size: 'medium', format: :png, sh: picture.security_token(size: 'medium')
expect(response.body[0x10..0x18].unpack('NN')).to eq([120, 120])
end
end
context 'with size param set to large' do
it "resizes the image to 240x180 while maintaining aspect ratio" do
alchemy_get :thumbnail, id: picture.id, name: picture.urlname, size: 'large', format: :png, sh: picture.security_token(size: 'large')
expect(response.body[0x10..0x18].unpack('NN')).to eq([180, 180])
end
end
context 'with size param set to nil' do
it "resizes the image to 111x93 while maintaining aspect ratio" do
alchemy_get :thumbnail, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.body[0x10..0x18].unpack('NN')).to eq([93, 93])
end
end
context 'with size param set to another value' do
it "resizes the image to the given size while maintaining aspect ratio" do
alchemy_get :thumbnail, id: picture.id, name: picture.urlname, size: '33x33', format: :png, sh: picture.security_token(size: '33x33')
expect(response.body[0x10..0x18].unpack('NN')).to eq([33, 33])
end
end
context "Requesting a picture that is assigned with restricted pages only" do
before do
essence = restricted_element.contents.where(name: 'image').first.essence
essence.picture_id = picture.id
essence.save
end
context "as guest user" do
it "should not render the picture, but redirect to login path" do
alchemy_get :thumbnail, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(302)
expect(response).to redirect_to(Alchemy.login_path)
end
end
context "as member user" do
before do
authorize_user(build(:alchemy_dummy_user))
end
it "should render the picture" do
alchemy_get :thumbnail, id: picture.id, name: picture.urlname, format: :png, sh: picture.security_token
expect(response.status).to eq(200)
end
end
end
end
describe 'Picture processing' do
let(:big_picture) do
create(:picture, image_file: fixture_file_upload(File.expand_path('../../fixtures/80x60.png', __FILE__), 'image/png'))
end
context "with crop and size parameters" do
it "should return a cropped image." do
options = {
crop: 'crop',
size: '10x10',
format: 'png'
}
alchemy_get :show, options.merge(id: big_picture.id, name: big_picture.urlname, sh: big_picture.security_token(options))
expect(response.body[0x10..0x18].unpack('NN')).to eq([10,10])
end
context "without a full size specification" do
it "should raise an error" do
options = {
crop: 'crop',
size: '10',
format: 'png'
}
expect {
alchemy_get :show, options.merge(id: big_picture.id, name: big_picture.urlname, sh: big_picture.security_token(options))
}.to raise_error ArgumentError
end
end
context "without upsample parameter" do
it "should not upsample the image." do
options = {
crop: 'crop',
size: '10x10',
format: 'png'
}
alchemy_get :show, options.merge(id: picture.id, name: big_picture.urlname, sh: picture.security_token(options))
expect(response.body[0x10..0x18].unpack('NN')).to eq([1,1])
end
end
context "and with upsample true" do
it "should return an upsampled image." do
options = {
crop: 'crop',
size: '10x10',
upsample: 'true',
format: 'png'
}
alchemy_get :show, options.merge(id: picture.id, name: big_picture.urlname, sh: picture.security_token(options))
expect(response.body[0x10..0x18].unpack('NN')).to eq([10,10])
end
end
end
context "without crop but with size parameter" do
it "should resize the image preserving aspect ratio" do
options = {
size: '40x40',
format: 'png'
}
alchemy_get :show, options.merge(id: big_picture.id, name: big_picture.urlname, sh: big_picture.security_token(options))
expect(response.body[0x10..0x18].unpack('NN')).to eq([40,30])
end
it "should resize the image inferring the height if not given" do
options = {
size: '40x',
format: 'png'
}
alchemy_get :show, options.merge(id: big_picture.id, name: big_picture.urlname, sh: big_picture.security_token(options))
expect(response.body[0x10..0x18].unpack('NN')).to eq([40,30])
end
it "should resize the image inferring the width if not given" do
options = {
size: 'x30',
format: 'png'
}
alchemy_get :show, options.merge(id: big_picture.id, name: big_picture.urlname, sh: big_picture.security_token(options))
expect(response.body[0x10..0x18].unpack('NN')).to eq([40,30])
end
end
end
end
end