Add validation in Pleroma.List.create/2
authorEgor Kislitsyn <egor@kislitsyn.com>
Mon, 26 Aug 2019 11:59:57 +0000 (18:59 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Mon, 26 Aug 2019 11:59:57 +0000 (18:59 +0700)
lib/pleroma/list.ex
test/list_test.exs

index 1d320206e3091b68ec61535247666f42f2232700..c572380c2325b90e0dd58e26a7e71789401c67bc 100644 (file)
@@ -109,15 +109,19 @@ defmodule Pleroma.List do
   end
 
   def create(title, %User{} = creator) do
-    list = %Pleroma.List{user_id: creator.id, title: title}
-
-    Repo.transaction(fn ->
-      list = Repo.insert!(list)
-
-      list
-      |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
-      |> Repo.update!()
-    end)
+    changeset = title_changeset(%Pleroma.List{user_id: creator.id}, %{title: title})
+
+    if changeset.valid? do
+      Repo.transaction(fn ->
+        list = Repo.insert!(changeset)
+
+        list
+        |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
+        |> Repo.update!()
+      end)
+    else
+      {:error, changeset}
+    end
   end
 
   def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
index f39033d022d7c58f32ba379ddae54bbc52cdf859..8efba75ea31811e13f2f48b01a04cc7edbd95316 100644 (file)
@@ -15,6 +15,13 @@ defmodule Pleroma.ListTest do
     assert title == "title"
   end
 
+  test "validates title" do
+    user = insert(:user)
+
+    assert {:error, changeset} = Pleroma.List.create("", user)
+    assert changeset.errors == [title: {"can't be blank", [validation: :required]}]
+  end
+
   test "getting a list not belonging to the user" do
     user = insert(:user)
     other_user = insert(:user)