Add user registration changeset.
This commit is contained in:
parent
4f7adb343c
commit
03c6148bb3
@ -9,6 +9,8 @@ defmodule Pleroma.User do
|
|||||||
field :name, :string
|
field :name, :string
|
||||||
field :nickname, :string
|
field :nickname, :string
|
||||||
field :password_hash, :string
|
field :password_hash, :string
|
||||||
|
field :password, :string, virtual: true
|
||||||
|
field :password_confirmation, :string, virtual: true
|
||||||
field :following, { :array, :string }, default: []
|
field :following, { :array, :string }, default: []
|
||||||
field :ap_id, :string
|
field :ap_id, :string
|
||||||
|
|
||||||
@ -29,6 +31,27 @@ def follow_changeset(struct, params \\ %{}) do
|
|||||||
|> validate_required([:following])
|
|> validate_required([:following])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def register_changeset(struct, params \\ %{}) do
|
||||||
|
changeset = struct
|
||||||
|
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
|
||||||
|
|> validate_required([:bio, :email, :name, :nickname, :password, :password_confirmation])
|
||||||
|
|> validate_confirmation(:password)
|
||||||
|
|> unique_constraint(:email)
|
||||||
|
|> unique_constraint(:nickname)
|
||||||
|
|
||||||
|
if changeset.valid? do
|
||||||
|
hashed = Comeonin.Pbkdf2.hashpwsalt(changeset.changes[:password])
|
||||||
|
ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
|
||||||
|
followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
|
||||||
|
changeset
|
||||||
|
|> put_change(:password_hash, hashed)
|
||||||
|
|> put_change(:ap_id, ap_id)
|
||||||
|
|> put_change(:following, [followers])
|
||||||
|
else
|
||||||
|
changeset
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def follow(%User{} = follower, %User{} = followed) do
|
def follow(%User{} = follower, %User{} = followed) do
|
||||||
ap_followers = User.ap_followers(followed)
|
ap_followers = User.ap_followers(followed)
|
||||||
following = [ap_followers | follower.following]
|
following = [ap_followers | follower.following]
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
defmodule Pleroma.Repo.Migrations.AddUniqueIndexToEmailAndNickname do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
create unique_index(:users, [:email])
|
||||||
|
create unique_index(:users, [:nickname])
|
||||||
|
end
|
||||||
|
end
|
@ -53,4 +53,35 @@ test "test if a user is following another user" do
|
|||||||
assert User.following?(user, followed)
|
assert User.following?(user, followed)
|
||||||
refute User.following?(followed, user)
|
refute User.following?(followed, user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "user registration" do
|
||||||
|
@full_user_data %{
|
||||||
|
bio: "A guy",
|
||||||
|
name: "my name",
|
||||||
|
nickname: "nick",
|
||||||
|
password: "test",
|
||||||
|
password_confirmation: "test",
|
||||||
|
email: "email@example.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
test "it requires a bio, email, name, nickname and password" do
|
||||||
|
@full_user_data
|
||||||
|
|> Map.keys
|
||||||
|
|> Enum.each(fn (key) ->
|
||||||
|
params = Map.delete(@full_user_data, key)
|
||||||
|
changeset = User.register_changeset(%User{}, params)
|
||||||
|
assert changeset.valid? == false
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it sets the password_hash, ap_id and following fields" do
|
||||||
|
changeset = User.register_changeset(%User{}, @full_user_data)
|
||||||
|
|
||||||
|
assert changeset.valid?
|
||||||
|
|
||||||
|
assert is_binary(changeset.changes[:password_hash])
|
||||||
|
assert changeset.changes[:ap_id] == User.ap_id(%User{nickname: @full_user_data.nickname})
|
||||||
|
assert changeset.changes[:following] == [User.ap_followers(%User{nickname: @full_user_data.nickname})]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user