From 9075b254e47fcf80345179917c91cea9f318204c Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sun, 12 Feb 2023 21:59:20 +1100 Subject: [PATCH] Clean up upload specs --- app/commands/media/upload.rb | 1 - .../adamantium/unit/commands/media/upload_spec.rb | 15 +++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/commands/media/upload.rb b/app/commands/media/upload.rb index b2a3ba6..5c542ab 100644 --- a/app/commands/media/upload.rb +++ b/app/commands/media/upload.rb @@ -15,7 +15,6 @@ module Adamantium def call(file:) mime = FileMagic.new - type = mime.file(file[:tempfile].path, true) return Failure(:invalid_file_type) unless VALID_UPLOAD_TYPES.include? type.to_sym diff --git a/spec/adamantium/unit/commands/media/upload_spec.rb b/spec/adamantium/unit/commands/media/upload_spec.rb index 571092e..4f7ef84 100644 --- a/spec/adamantium/unit/commands/media/upload_spec.rb +++ b/spec/adamantium/unit/commands/media/upload_spec.rb @@ -1,26 +1,33 @@ # frozen_string_literal: true require "dry/monads" +require "base64" RSpec.describe Adamantium::Commands::Media::Upload do subject { described_class.new } it "saves a file and returns its URL" do + image = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" + tempfile = Tempfile.new(["test", ".jpg"]) + tempfile.write Base64.decode64(image) + tempfile.rewind file = { - filename: "foo.txt", - tempfile: Tempfile.new + filename: "foo.jpg", + tempfile: tempfile } 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.value!).to match expected_path File.read("public/#{result.value!.gsub("http://localhost/", "")}") File.delete("public/#{result.value!.gsub("http://localhost/", "")}") + tempfile.close + tempfile.unlink end 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) expect(result).to be_failure