From 373753e595de96a0492edfc6cc180c3e47696a55 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 9 May 2017 18:11:51 +0200 Subject: [PATCH] Add some basic changesets. --- lib/pleroma/object.ex | 9 ++++++++- lib/pleroma/user.ex | 14 ++++++++++++++ test/object_test.exs | 13 ++++++++++++- test/user_test.exs | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index 949ccb0f6..715a35591 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Object do use Ecto.Schema alias Pleroma.{Repo, Object} - import Ecto.Query + import Ecto.{Query, Changeset} schema "objects" do field :data, :map @@ -9,6 +9,13 @@ defmodule Pleroma.Object do timestamps() end + def change(struct, params \\ %{}) do + changeset = struct + |> cast(params, [:data]) + |> validate_required([:data]) + |> unique_constraint(:ap_id, name: :objects_unique_apid_index) + end + def get_by_ap_id(ap_id) do Repo.one(from object in Object, where: fragment("? @> ?", object.data, ^%{id: ap_id})) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 4510be770..9b2433674 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -61,6 +61,17 @@ def user_info(%User{} = user) do } end + @email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ + def remote_user_creation(params) do + changeset = %User{} + |> cast(params, [:bio, :name, :ap_id, :nickname, :info]) + |> validate_required([:bio, :name, :ap_id, :nickname]) + |> unique_constraint(:nickname) + |> validate_format(:nickname, @email_regex) + |> validate_length(:bio, max: 1000) + |> validate_length(:name, max: 100) + end + def register_changeset(struct, params \\ %{}) do changeset = struct |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) @@ -69,6 +80,9 @@ def register_changeset(struct, params \\ %{}) do |> unique_constraint(:email) |> unique_constraint(:nickname) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) + |> validate_format(:email, @email_regex) + |> validate_length(:bio, max: 1000) + |> validate_length(:name, max: 100) if changeset.valid? do hashed = Pbkdf2.hashpwsalt(changeset.changes[:password]) diff --git a/test/object_test.exs b/test/object_test.exs index 3ffddbbc5..ae9515c26 100644 --- a/test/object_test.exs +++ b/test/object_test.exs @@ -1,11 +1,22 @@ defmodule Pleroma.ObjectTest do use Pleroma.DataCase import Pleroma.Factory + alias Pleroma.{Repo, Object} test "returns an object by it's AP id" do object = insert(:note) - found_object = Pleroma.Object.get_by_ap_id(object.data["id"]) + found_object = Object.get_by_ap_id(object.data["id"]) assert object == found_object end + + describe "generic changeset" do + test "it ensures uniqueness of the id" do + object = insert(:note) + cs = Object.change(%Object{}, %{data: %{id: object.data["id"]}}) + assert cs.valid? + + {:error, result} = Repo.insert(cs) + end + end end diff --git a/test/user_test.exs b/test/user_test.exs index e6de4a5cd..bfa79db28 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -139,5 +139,46 @@ test "returns an ap_followers link for a user" do user = insert(:user) assert User.ap_followers(user) == Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :feed_redirect, user.nickname) <> "/followers" end + + describe "remote user creation changeset" do + @valid_remote %{ + bio: "hello", + name: "Someone", + nickname: "a@b.de", + ap_id: "http...", + info: %{ some: "info" } + } + + test "it confirms validity" do + cs = User.remote_user_creation(@valid_remote) + assert cs.valid? + end + + test "it enforces the fqn format for nicknames" do + cs = User.remote_user_creation(%{@valid_remote | nickname: "bla"}) + refute cs.valid? + end + + test "it has required fields" do + [:bio, :name, :nickname, :ap_id] + |> Enum.each(fn (field) -> + cs = User.remote_user_creation(Map.delete(@valid_remote, field)) + refute cs.valid? + end) + end + + test "it restricts some sizes" do + [bio: 1000, name: 100] + |> Enum.each(fn ({field, size}) -> + string = String.pad_leading(".", size) + cs = User.remote_user_creation(Map.put(@valid_remote, field, string)) + assert cs.valid? + + string = String.pad_leading(".", size + 1) + cs = User.remote_user_creation(Map.put(@valid_remote, field, string)) + refute cs.valid? + end) + end + end end