Clean up upload specs

This commit is contained in:
2023-02-12 21:59:20 +11:00
parent 2b81839127
commit 9075b254e4
2 changed files with 11 additions and 5 deletions

View File

@@ -15,7 +15,6 @@ module Adamantium
def call(file:) def call(file:)
mime = FileMagic.new mime = FileMagic.new
type = mime.file(file[:tempfile].path, true) type = mime.file(file[:tempfile].path, true)
return Failure(:invalid_file_type) unless VALID_UPLOAD_TYPES.include? type.to_sym return Failure(:invalid_file_type) unless VALID_UPLOAD_TYPES.include? type.to_sym

View File

@@ -1,26 +1,33 @@
# frozen_string_literal: true # frozen_string_literal: true
require "dry/monads" require "dry/monads"
require "base64"
RSpec.describe Adamantium::Commands::Media::Upload do RSpec.describe Adamantium::Commands::Media::Upload do
subject { described_class.new } subject { described_class.new }
it "saves a file and returns its URL" do it "saves a file and returns its URL" do
image = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
tempfile = Tempfile.new(["test", ".jpg"])
tempfile.write Base64.decode64(image)
tempfile.rewind
file = { file = {
filename: "foo.txt", filename: "foo.jpg",
tempfile: Tempfile.new tempfile: tempfile
} }
result = subject.call(file: file) result = subject.call(file: file)
expected_path = /media\/#{Time.now.strftime("%m-%Y")}\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}.txt/ expected_path = /media\/#{Time.now.strftime("%m-%Y")}\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}.jpg/
expect(result).to be_success expect(result).to be_success
expect(result.value!).to match expected_path expect(result.value!).to match expected_path
File.read("public/#{result.value!.gsub("http://localhost/", "")}") File.read("public/#{result.value!.gsub("http://localhost/", "")}")
File.delete("public/#{result.value!.gsub("http://localhost/", "")}") File.delete("public/#{result.value!.gsub("http://localhost/", "")}")
tempfile.close
tempfile.unlink
end end
it "returns a Failure if the file couldn't be saved" do it "returns a Failure if the file couldn't be saved" do
file = {filename: "file.txt", tempfile: ""} file = {filename: "file.txt", tempfile: Tempfile.new}
result = subject.call(file: file) result = subject.call(file: file)
expect(result).to be_failure expect(result).to be_failure