From 397aaf3bebfeaf3836902ca9cc78b767af6c59b4 Mon Sep 17 00:00:00 2001 From: JannikStreek Date: Wed, 27 Nov 2024 14:19:59 +0100 Subject: [PATCH] make simplified_attached_file_type more robust to handle nil case (#500) --- lib/mindwendel/attachments.ex | 2 +- test/mindwendel/attachments_test.exs | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/mindwendel/attachments.ex b/lib/mindwendel/attachments.ex index 5fe3d512..9cceb802 100644 --- a/lib/mindwendel/attachments.ex +++ b/lib/mindwendel/attachments.ex @@ -29,7 +29,7 @@ defmodule Mindwendel.Attachments do """ def simplified_attached_file_type(file_type) do - case String.split(file_type, "/") do + case String.split(file_type || "", "/") do ["image", _] -> "image" [_, "pdf"] -> "pdf" [_, _] -> "misc" diff --git a/test/mindwendel/attachments_test.exs b/test/mindwendel/attachments_test.exs index ed691b8d..0d66c34c 100644 --- a/test/mindwendel/attachments_test.exs +++ b/test/mindwendel/attachments_test.exs @@ -20,9 +20,17 @@ defmodule Mindwendel.AttachmentsTest do assert Attachments.simplified_attached_file_type("application/pdf") == "pdf" end - test "simplifies the file type for an unknown file" do + test "simplifies the file type for an unknown type" do assert Attachments.simplified_attached_file_type("application_unknown") == "misc" end + + test "simplifies the file type for an empty type" do + assert Attachments.simplified_attached_file_type("") == "misc" + end + + test "simplifies the file type for a missing type" do + assert Attachments.simplified_attached_file_type(nil) == "misc" + end end describe "delete_attached_file" do