/
resources_controller_spec.rb
258 lines (213 loc) · 7.48 KB
/
resources_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
# frozen_string_literal: true
require "rails_helper"
describe Admin::ResourcesController, type: :controller do
render_views
before do
create(:blog)
admin = create :user, :as_admin
sign_in admin
end
describe "#index" do
before do
get :index
end
it "renders index template" do
assert_response :success
assert_template "index"
expect(assigns(:resources)).not_to be_nil
end
end
describe "#destroy" do
let(:uploaded_file) { file_upload("testfile.txt", "text/plain") }
it "redirects to the index" do
res_id = create(:resource, upload: uploaded_file).id
delete :destroy, params: { id: res_id }
expect(response).to redirect_to(action: "index")
end
end
# TODO: Should be create, mkay?
describe "#upload" do
before do
ResourceUploader.enable_processing = true
end
after do
ResourceUploader.enable_processing = false
end
context "when uploading a text file" do
let(:upload) { file_upload("testfile.txt", "text/plain") }
it "creates a new Resource" do
expect { post :upload, params: { upload: upload } }.
to change(Resource, :count).by(1)
end
it "sets the content type to text/plain" do
post :upload, params: { upload: upload }
expect(Resource.last.mime).to eq "text/plain"
end
it "sets the flash to success" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).not_to be_nil
expect(flash[:warning]).to be_nil
end
end
end
context "when uploading an image file" do
let(:upload) { file_upload("testfile.png", "image/png") }
it "creates a new Resource" do
expect { post :upload, params: { upload: upload } }.
to change(Resource, :count).by(1)
end
it "sets the content type correctly" do
post :upload, params: { upload: upload }
expect(Resource.last.mime).to eq "image/png"
end
it "sets the flash to success" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).not_to be_nil
expect(flash[:warning]).to be_nil
end
end
end
context "when uploading an image file with exif data" do
let(:upload) { file_upload("testfile.jpg", "image/jpeg") }
it "creates a new Resource" do
expect { post :upload, params: { upload: upload } }.
to change(Resource, :count).by(1)
end
it "strips EXIF data" do
post :upload, params: { upload: upload }
resource = Resource.last
img = MiniMagick::Image.open resource.upload.file.file
expect(img.exif).to be_empty
end
it "sets the content type correctly" do
post :upload, params: { upload: upload }
expect(Resource.last.mime).to eq "image/jpeg"
end
it "sets the flash to success" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).not_to be_nil
expect(flash[:warning]).to be_nil
end
end
end
context "when attempting to upload a dangerous svg" do
let(:upload) { file_upload("exploit.svg", "image/svg") }
it "does not create a new image Resource" do
expect { post :upload, params: { upload: upload } }.
not_to change(Resource, :count)
end
it "does not attempt to process the image" do
post :upload, params: { upload: upload }
result = assigns(:up)
expect(result.errors[:upload]).
to match_array ["has MIME type mismatch", "can't be blank"]
end
it "sets the flash to failure" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).to be_nil
expect(flash[:warning]).not_to be_nil
end
end
end
context "when attempting to upload a fake png with a txt extension" do
let(:upload) { file_upload("testfile.txt", "image/png") }
it "does not create a new fake image Resource" do
expect { post :upload, params: { upload: upload } }.
not_to change(Resource, :count)
end
it "does not attempt to process a new fake image Resource" do
post :upload, params: { upload: upload }
result = assigns(:up)
expect(result.errors[:upload]).
to match_array ["has MIME type mismatch", "can't be blank"]
end
it "sets the flash to failure" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).to be_nil
expect(flash[:warning]).not_to be_nil
end
end
end
context "when attempting to upload a fake png with a png extension" do
let(:upload) { file_upload("fakepng.png", "image/png") }
it "does not create a new fake image Resource" do
expect { post :upload, params: { upload: upload } }.
not_to change(Resource, :count)
end
it "does not attempt to process a new fake image Resource" do
post :upload, params: { upload: upload }
result = assigns(:up)
expect(result.errors[:upload]).
to match_array ["has MIME type mismatch", "can't be blank"]
end
it "sets the flash to failure" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).to be_nil
expect(flash[:warning]).not_to be_nil
end
end
end
context "when attempting to upload an html file" do
let(:upload) { file_upload("just_some.html", "text/html") }
it "does not create a new Resource" do
expect { post :upload, params: { upload: upload } }.
not_to change(Resource, :count)
end
it "warns the user they can't upload this type of file" do
post :upload, params: { upload: upload }
result = assigns(:up)
expect(result.errors[:upload]).
to match_array [
%r{You are not allowed to upload text/html files},
"can't be blank",
]
end
it "sets the flash to failure" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).to be_nil
expect(flash[:warning]).not_to be_nil
end
end
end
context "when attempting to upload an html file as text/plain" do
let(:upload) { file_upload("just_some.html", "text/plain") }
it "does not create a new Resource" do
expect { post :upload, params: { upload: upload } }.
not_to change(Resource, :count)
end
it "warns the user they can't upload this type of file" do
post :upload, params: { upload: upload }
result = assigns(:up)
expect(result.errors[:upload]).
to match_array ["has MIME type mismatch", "can't be blank"]
end
it "sets the flash to failure" do
post :upload, params: { upload: upload }
aggregate_failures do
expect(flash[:success]).to be_nil
expect(flash[:warning]).not_to be_nil
end
end
end
context "when uploading nothing" do
it "does not create a new Resource" do
expect { post :upload }.
not_to change(Resource, :count)
end
it "sets the flash to failure" do
post :upload
aggregate_failures do
expect(flash[:success]).to be_nil
expect(flash[:warning]).not_to be_nil
end
end
end
end
end