docbrown/spec/requests/image_uploads_spec.rb
Ernesto Tagwerker 9ea3a8c67d
Upgrade to Rails 6.0 (#7658)
* Add dual booting logic to Gemfile

This might be helpful for the Rails 6.0 upgrade project.

* Add more than one gemfile to Travis' configuration

We want to see how the application behaves with more than one Rails versions:

- Gemfile -> Rails 5.2
- Gemfile.next -> Rails 6.0

This will help us figure out what needs to be addressed before migrating to Rails 6.0.

If you want to read more about this technique (dual booting) you can check out this page: https://www.fastruby.io/blog/upgrade-rails/dual-boot/dual-boot-with-rails-6-0-beta.html

* Fix joins

* Upgrade Gemfile.next.lock

* Make sure we're installing the correct versions of gems

* Add Rails 6 notes

* Update rubocop in Gemfile.next.lock

* Fix organization spec

* Fix page_views_spec

* Add Rails 6 and run rails app:update

* Remove some tricks

* Remove Gemfile.next for now

* Fix .content_type deprecation

* Fix deprecation of .where.not NAND/NOR behavior

* Fix deprecation of parameterized emails

* Fix specs

* Remove next flag for now

* Fix spec (hopefully)

* Add wait_for_javascript

* Fix spec, thanks @maestromac!

* Try without wait for javascript hack

* Remove unnecessary bin/update

* Remove file that snuck in the rebase

* Update the vendored gems

* Replace migrate+db:setup with db:prepare

* Update vendored gems

* Fix Gemfile.lock and update vendored stuff

* Fix Gemfile.lock to be the same as master's minus the changes

Co-authored-by: rhymes <rhymesete@gmail.com>
2020-06-04 11:54:25 +02:00

115 lines
4 KiB
Ruby

require "rails_helper"
RSpec.describe "ImageUploads", type: :request do
describe "POST/image_uploads" do
let(:user) { create(:user) }
let(:headers) { { "Content-Type": "application/json", Accept: "application/json" } }
let(:image) do
Rack::Test::UploadedFile.new(
Rails.root.join("spec/support/fixtures/images/image1.jpeg"),
"image/jpeg",
)
end
let(:bad_image) do
Rack::Test::UploadedFile.new(
Rails.root.join("spec/support/fixtures/images/bad-image.jpg"),
"image/jpeg",
)
end
context "when not logged-in" do
it "responds with 401" do
post "/image_uploads", headers: headers
expect(response).to have_http_status(:unauthorized)
end
end
context "when logged-in" do
before do
sign_in user
end
it "returns json" do
post "/image_uploads", headers: headers
expect(response.media_type).to eq("application/json")
end
it "provides a link" do
# this test is a little flimsy
post "/image_uploads", headers: headers, params: { image: [image] }
expect(response.parsed_body["links"].length).to eq(1)
expect(response.body).to match("\/i\/.+\.")
end
it "supports for uploading a single image not in an array" do
post "/image_uploads", headers: headers, params: { image: image }
expect(response.parsed_body["links"].length).to eq(1)
expect(response.body).to match("\/i\/.+\.")
end
it "supports upload of more than one image at a time" do
image2 = Rack::Test::UploadedFile.new(
Rails.root.join("spec/support/fixtures/images/image2.jpeg"), "image/jpeg"
)
post "/image_uploads", headers: headers, params: { image: [image, image2] }
expect(response.body).to match("\/i\/.+\.")
expect(response.parsed_body["links"].length).to eq(2)
end
it "prevents image with resolutions larger than 4096x4096" do
post "/image_uploads", headers: headers, params: { image: [bad_image] }
expect(response).to have_http_status(:unprocessable_entity)
end
it "returns a JSON error if something goes wrong" do
post "/image_uploads", headers: headers, params: { image: [bad_image] }
expect(response.parsed_body["error"]).not_to be_nil
end
it "returns error if image file name is too long" do
allow(image).to receive(:original_filename).and_return("#{'a_very_long_filename' * 15}.png")
post "/image_uploads", headers: headers, params: { image: image }
expect(response).to have_http_status(:unprocessable_entity)
end
it "returns error if image file is not a file" do
allow(bad_image).to receive(:respond_to?).with(:original_filename).and_return(false)
post "/image_uploads", headers: headers, params: { image: bad_image }
expect(response).to have_http_status(:unprocessable_entity)
end
end
context "when uploading rate limiting works" do
let(:cache_store) { ActiveSupport::Cache.lookup_store(:redis_cache_store) }
let(:cache) { Rails.cache }
let(:cache_key) { "#{user.id}_image_upload" }
before do
sign_in user
allow(Rails).to receive(:cache).and_return(cache_store)
end
it "counts number of uploads in cache" do
post "/image_uploads", headers: headers, params: { image: [image] }
expect(cache.read(cache_key, raw: true).to_i).to eq(1)
end
it "responds with HTTP 429 with too many uploads" do
upload = proc do
Rack::Test::UploadedFile.new(
Rails.root.join("spec/support/fixtures/images/image1.jpeg"),
"image/jpeg",
)
end
11.times { post "/image_uploads", headers: headers, params: { image: [upload.call] } }
expect(response).to have_http_status(:too_many_requests)
expected_retry_after = RateLimitChecker::ACTION_LIMITERS.dig(:image_upload, :retry_after)
expect(response.headers["Retry-After"]).to eq(expected_retry_after)
end
end
end
end