1
0
Fork 0
forked from mirrors/akkoma

Add some basic changesets.

This commit is contained in:
Roger Braun 2017-05-09 18:11:51 +02:00
parent e12a6d5666
commit 373753e595
4 changed files with 75 additions and 2 deletions

View file

@ -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}))

View file

@ -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])

View file

@ -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

View file

@ -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