Test UploadsController
This commit is contained in:
@@ -1,16 +1,28 @@
|
||||
class UploadsController < ApplicationController
|
||||
include ActiveStorage::SetCurrent
|
||||
|
||||
before_action :set_file, only: :create
|
||||
before_action :set_attachment, only: :show
|
||||
|
||||
def create
|
||||
file = params[:file]
|
||||
blob = ActiveStorage::Blob.create_and_upload! io: file, filename: file.original_filename, content_type: file.content_type
|
||||
@upload = Current.account.uploads_attachments.create! blob: blob
|
||||
render :create, status: :created, formats: :json
|
||||
@upload = Current.account.uploads_attachments.create! blob: create_blob!
|
||||
end
|
||||
|
||||
def show
|
||||
@attachment = ActiveStorage::Attachment.find_by! slug: "#{params[:slug]}.#{params[:format]}"
|
||||
expires_in 1.year, public: true
|
||||
redirect_to @attachment.url
|
||||
end
|
||||
|
||||
private
|
||||
def set_file
|
||||
@file = params[:file]
|
||||
end
|
||||
|
||||
def set_attachment
|
||||
@attachment = ActiveStorage::Attachment.find_by! slug: "#{params[:slug]}.#{params[:format]}"
|
||||
end
|
||||
|
||||
def create_blob!
|
||||
ActiveStorage::Blob.create_and_upload! io: @file, filename: @file.original_filename, content_type: @file.content_type
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
require "test_helper"
|
||||
|
||||
class UploadsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in_as :kevin
|
||||
end
|
||||
|
||||
test "create" do
|
||||
assert_changes -> { ActiveStorage::Attachment.count }, 1 do
|
||||
post uploads_url(format: "json"), params: { file: fixture_file_upload("moon.jpg", "image/jpeg") }, as: :xhr
|
||||
end
|
||||
|
||||
assert_response :success
|
||||
assert_equal ActiveStorage::Attachment.last.slug_url(host: "www.example.com", port: nil), response.parsed_body["fileUrl"]
|
||||
assert_equal "image/jpeg", response.parsed_body["mimetype"]
|
||||
assert_equal "moon.jpg", response.parsed_body["fileName"]
|
||||
end
|
||||
|
||||
test "show" do
|
||||
accounts("37s").uploads.attach fixture_file_upload("moon.jpg", "image/jpeg")
|
||||
get upload_url(slug: accounts("37s").uploads.last.slug)
|
||||
assert_response :redirect
|
||||
assert_match /\/rails\/active_storage\/.*\/moon\.jpg/, @response.redirect_url
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user