Changes to the actor - user relation

Now the GraphQL API replies mostly with users which have the default_actor
property filled to show profile information

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-11-29 17:43:22 +01:00
parent 6f15127cb0
commit 51aa511101
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
10 changed files with 232 additions and 123 deletions

View File

@ -59,12 +59,16 @@ defmodule Mobilizon.Actors do
""" """
@spec get_actor_for_user(Mobilizon.Actors.User.t()) :: Mobilizon.Actors.Actor.t() @spec get_actor_for_user(Mobilizon.Actors.User.t()) :: Mobilizon.Actors.Actor.t()
def get_actor_for_user(%Mobilizon.Actors.User{} = user) do def get_actor_for_user(%Mobilizon.Actors.User{} = user) do
with %User{default_actor: actor} = user when not is_nil(user) and not is_nil(actor) <- case Repo.one(from(a in Actor, join: u in User, on: u.default_actor_id == a.id)) do
Repo.preload(user, [:default_actor]) do nil -> get_actors_for_user(user) |> hd
actor actor -> actor
end end
end end
def get_actors_for_user(%User{id: user_id}) do
Repo.all(from(a in Actor, where: a.user_id == ^user_id))
end
def get_actor_with_everything!(id) do def get_actor_with_everything!(id) do
actor = Repo.get!(Actor, id) actor = Repo.get!(Actor, id)
Repo.preload(actor, [:organized_events, :followers, :followings]) Repo.preload(actor, [:organized_events, :followers, :followings])
@ -106,6 +110,13 @@ defmodule Mobilizon.Actors do
|> Repo.update() |> Repo.update()
end end
def update_user_default_actor(user_id, actor_id) do
with from(u in User, where: u.id == ^user_id, update: [set: [default_actor_id: ^actor_id]])
|> Repo.update_all([]) do
Repo.get!(User, user_id) |> Repo.preload([:default_actor])
end
end
@doc """ @doc """
Deletes a Actor. Deletes a Actor.
@ -248,14 +259,25 @@ defmodule Mobilizon.Actors do
@spec get_user_with_actors!(integer()) :: User.t() @spec get_user_with_actors!(integer()) :: User.t()
def get_user_with_actors!(id) do def get_user_with_actors!(id) do
user = Repo.get!(User, id) user = Repo.get!(User, id)
Repo.preload(user, :actors) Repo.preload(user, [:actors, :default_actor])
end end
@doc """
Get user with it's actors by ID
"""
@spec get_user_with_actors(integer()) :: User.t() @spec get_user_with_actors(integer()) :: User.t()
def get_user_with_actors(id) do def get_user_with_actors(id) do
case Repo.get(User, id) do case Repo.get(User, id) do
nil -> {:error, "User with ID #{id} not found"} nil ->
user -> {:ok, Repo.preload(user, :actors)} {:error, "User with ID #{id} not found"}
user ->
user =
user
|> Repo.preload([:actors, :default_actor])
|> Map.put(:actors, get_actors_for_user(user))
{:ok, user}
end end
end end
@ -497,22 +519,21 @@ defmodule Mobilizon.Actors do
pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing() pem = [entry] |> :public_key.pem_encode() |> String.trim_trailing()
with avatar <- gravatar(email), with avatar <- gravatar(email),
actor_changeset <-
Mobilizon.Actors.Actor.registration_changeset(%Mobilizon.Actors.Actor{}, %{
preferred_username: username,
domain: nil,
keys: pem,
avatar_url: avatar
}),
{:ok, %Mobilizon.Actors.Actor{} = actor} <- Mobilizon.Repo.insert(actor_changeset),
user_changeset <- user_changeset <-
Mobilizon.Actors.User.registration_changeset(%Mobilizon.Actors.User{}, %{ User.registration_changeset(%User{}, %{
email: email, email: email,
password: password, password: password,
default_actor: actor default_actor: %{
preferred_username: username,
domain: nil,
keys: pem,
avatar_url: avatar
}
}), }),
{:ok, %Mobilizon.Actors.User{} = user} <- Mobilizon.Repo.insert(user_changeset) do {:ok, %User{default_actor: %Actor{} = actor, id: user_id} = user} <-
{:ok, Map.put(actor, :user, user)} Mobilizon.Repo.insert(user_changeset),
{:ok, %Actor{} = _actor} <- update_actor(actor, %{user_id: user_id}) do
{:ok, Repo.preload(user, [:actors])}
else else
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
handle_actor_user_changeset(changeset) handle_actor_user_changeset(changeset)
@ -600,9 +621,20 @@ defmodule Mobilizon.Actors do
def get_user_by_email(email, activated \\ nil) do def get_user_by_email(email, activated \\ nil) do
query = query =
case activated do case activated do
nil -> from(u in User, where: u.email == ^email) nil ->
true -> from(u in User, where: u.email == ^email and not is_nil(u.confirmed_at)) from(u in User, where: u.email == ^email, preload: :default_actor)
false -> from(u in User, where: u.email == ^email and is_nil(u.confirmed_at))
true ->
from(u in User,
where: u.email == ^email and not is_nil(u.confirmed_at),
preload: :default_actor
)
false ->
from(u in User,
where: u.email == ^email and is_nil(u.confirmed_at),
preload: :default_actor
)
end end
case Repo.one(query) do case Repo.one(query) do
@ -611,6 +643,9 @@ defmodule Mobilizon.Actors do
end end
end end
@doc """
Get an user by it's activation token
"""
@spec get_user_by_activation_token(String.t()) :: Actor.t() @spec get_user_by_activation_token(String.t()) :: Actor.t()
def get_user_by_activation_token(token) do def get_user_by_activation_token(token) do
Repo.one( Repo.one(
@ -621,6 +656,19 @@ defmodule Mobilizon.Actors do
) )
end end
@doc """
Get an user by it's reset password token
"""
@spec get_user_by_reset_password_token(String.t()) :: Actor.t()
def get_user_by_reset_password_token(token) do
Repo.one(
from(u in User,
where: u.reset_password_token == ^token,
preload: [:default_actor]
)
)
end
@doc """ @doc """
Updates a user. Updates a user.
@ -634,9 +682,12 @@ defmodule Mobilizon.Actors do
""" """
def update_user(%User{} = user, attrs) do def update_user(%User{} = user, attrs) do
user with {:ok, %User{} = user} <-
|> User.changeset(attrs) user
|> Repo.update() |> User.changeset(attrs)
|> Repo.update() do
{:ok, Repo.preload(user, [:default_actor])}
end
end end
@doc """ @doc """

View File

@ -3,7 +3,7 @@ defmodule Mobilizon.Actors.Service.ResetPassword do
require Logger require Logger
alias Mobilizon.{Mailer, Repo, Actors.User} alias Mobilizon.{Mailer, Repo, Actors.User, Actors}
alias Mobilizon.Email.User, as: UserEmail alias Mobilizon.Email.User, as: UserEmail
alias Mobilizon.Actors.Service.Tools alias Mobilizon.Actors.Service.Tools
@ -12,7 +12,7 @@ defmodule Mobilizon.Actors.Service.ResetPassword do
""" """
@spec check_reset_password_token(String.t(), String.t()) :: tuple @spec check_reset_password_token(String.t(), String.t()) :: tuple
def check_reset_password_token(password, token) do def check_reset_password_token(password, token) do
with %User{} = user <- Repo.get_by(User, reset_password_token: token), with %User{} = user <- Actors.get_user_by_reset_password_token(token),
{:ok, %User{} = user} <- {:ok, %User{} = user} <-
Repo.update( Repo.update(
User.password_reset_changeset(user, %{ User.password_reset_changeset(user, %{

View File

@ -13,7 +13,7 @@ defmodule Mobilizon.Actors.User do
field(:password, :string, virtual: true) field(:password, :string, virtual: true)
field(:role, :integer, default: 0) field(:role, :integer, default: 0)
has_many(:actors, Actor) has_many(:actors, Actor)
has_one(:default_actor, Actor) belongs_to(:default_actor, Actor)
field(:confirmed_at, :utc_datetime) field(:confirmed_at, :utc_datetime)
field(:confirmation_sent_at, :utc_datetime) field(:confirmation_sent_at, :utc_datetime)
field(:confirmation_token, :string) field(:confirmation_token, :string)
@ -25,34 +25,40 @@ defmodule Mobilizon.Actors.User do
@doc false @doc false
def changeset(%User{} = user, attrs) do def changeset(%User{} = user, attrs) do
user changeset =
|> cast(attrs, [ user
:email, |> cast(attrs, [
:role, :email,
# :default_actor, :role,
:password_hash, :password_hash,
:confirmed_at, :confirmed_at,
:confirmation_sent_at, :confirmation_sent_at,
:confirmation_token, :confirmation_token,
:reset_password_sent_at, :reset_password_sent_at,
:reset_password_token :reset_password_token
]) ])
|> validate_required([:email]) |> validate_required([:email])
|> unique_constraint(:email, message: "registration.error.email_already_used") |> unique_constraint(:email, message: "registration.error.email_already_used")
|> validate_format(:email, ~r/@/) |> validate_format(:email, ~r/@/)
|> validate_length( |> validate_length(
:password, :password,
min: 6, min: 6,
max: 100, max: 100,
message: "registration.error.password_too_short" message: "registration.error.password_too_short"
) )
if Map.has_key?(attrs, :default_actor) do
put_assoc(changeset, :default_actor, attrs.default_actor)
else
changeset
end
end end
def registration_changeset(struct, params) do def registration_changeset(struct, params) do
struct struct
|> changeset(params) |> changeset(params)
|> cast(params, ~w(password)a, []) |> cast(params, ~w(password)a, [])
|> put_assoc(:default_actor, params.default_actor) |> cast_assoc(:default_actor)
|> validate_required([:email, :password]) |> validate_required([:email, :password])
|> validate_email() |> validate_email()
|> validate_length( |> validate_length(

View File

@ -26,9 +26,8 @@ defmodule MobilizonWeb.Resolvers.User do
""" """
def login_user(_parent, %{email: email, password: password}, _resolution) do def login_user(_parent, %{email: email, password: password}, _resolution) do
with {:ok, %User{} = user} <- Actors.get_user_by_email(email, true), with {:ok, %User{} = user} <- Actors.get_user_by_email(email, true),
{:ok, token, _} <- Actors.authenticate(%{user: user, password: password}), {:ok, token, _} <- Actors.authenticate(%{user: user, password: password}) do
%Actor{} = actor <- Actors.get_actor_for_user(user) do {:ok, %{token: token, user: user}}
{:ok, %{token: token, user: user, person: actor}}
else else
{:error, :user_not_found} -> {:error, :user_not_found} ->
{:error, "User with email not found"} {:error, "User with email not found"}
@ -47,9 +46,9 @@ defmodule MobilizonWeb.Resolvers.User do
""" """
@spec create_user_actor(any(), map(), any()) :: tuple() @spec create_user_actor(any(), map(), any()) :: tuple()
def create_user_actor(_parent, args, _resolution) do def create_user_actor(_parent, args, _resolution) do
with {:ok, %Actor{user: user} = actor} <- Actors.register(args) do with {:ok, %User{} = user} <- Actors.register(args) do
Mobilizon.Actors.Service.Activation.send_confirmation_email(user) Mobilizon.Actors.Service.Activation.send_confirmation_email(user)
{:ok, actor} {:ok, user}
end end
end end
@ -114,9 +113,8 @@ defmodule MobilizonWeb.Resolvers.User do
def reset_password(_parent, %{password: password, token: token}, _resolution) do def reset_password(_parent, %{password: password, token: token}, _resolution) do
with {:ok, %User{} = user} <- with {:ok, %User{} = user} <-
Mobilizon.Actors.Service.ResetPassword.check_reset_password_token(password, token), Mobilizon.Actors.Service.ResetPassword.check_reset_password_token(password, token),
%Actor{} = actor <- Actors.get_actor_for_user(user),
{:ok, token, _} <- MobilizonWeb.Guardian.encode_and_sign(user) do {:ok, token, _} <- MobilizonWeb.Guardian.encode_and_sign(user) do
{:ok, %{token: token, user: user, actor: actor}} {:ok, %{token: token, user: user}}
end end
end end
@ -124,8 +122,17 @@ defmodule MobilizonWeb.Resolvers.User do
def change_default_actor(_parent, %{preferred_username: username}, %{ def change_default_actor(_parent, %{preferred_username: username}, %{
context: %{current_user: user} context: %{current_user: user}
}) do }) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(username) do with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(username),
Actors.update_user(user, %{default_actor: actor}) {:user_actor, true} <-
{:user_actor, actor_id in Enum.map(Actors.get_actors_for_user(user), & &1.id)},
%User{} = user <- Actors.update_user_default_actor(user.id, actor_id) do
{:ok, user}
else
{:user_actor, _} ->
{:error, :actor_not_from_user}
_err ->
{:error, :unable_to_change_default_actor}
end end
end end
end end

View File

@ -202,7 +202,6 @@ defmodule MobilizonWeb.Schema do
object :login do object :login do
field(:token, non_null(:string), description: "A JWT Token for this session") field(:token, non_null(:string), description: "A JWT Token for this session")
field(:user, non_null(:user), description: "The user associated to this session") field(:user, non_null(:user), description: "The user associated to this session")
field(:person, non_null(:person), description: "The person associated to this session")
end end
@desc "An event" @desc "An event"
@ -480,8 +479,8 @@ defmodule MobilizonWeb.Schema do
resolve(&Resolvers.Category.create_category/3) resolve(&Resolvers.Category.create_category/3)
end end
@desc "Create an user (returns an actor)" @desc "Create an user"
field :create_user, type: :person do field :create_user, type: :user do
arg(:email, non_null(:string)) arg(:email, non_null(:string))
arg(:password, non_null(:string)) arg(:password, non_null(:string))
arg(:username, non_null(:string)) arg(:username, non_null(:string))

View File

@ -306,15 +306,17 @@ defmodule Mobilizon.ActorsTest do
# There's no create_user/1, just register/1 # There's no create_user/1, just register/1
test "register/1 with valid data creates a user" do test "register/1 with valid data creates a user" do
assert {:ok, %Actor{preferred_username: username, user: %User{email: email}}} = assert {:ok,
Actors.register(@valid_attrs) %User{email: email, default_actor: %Actor{preferred_username: username} = actor} =
user} = Actors.register(@valid_attrs)
assert email == @valid_attrs.email assert email == @valid_attrs.email
assert username == @valid_attrs.username assert username == @valid_attrs.username
assert [actor.id] == Actors.get_actors_for_user(user) |> Enum.map(& &1.id)
end end
test "create_user/1 with invalid data returns error changeset" do test "create_user/1 with invalid data returns error changeset" do
assert {:error, :empty_email} = Actors.register(@invalid_attrs) assert {:error, "can't be blank"} = Actors.register(@invalid_attrs)
end end
test "update_user/2 with valid data updates the user" do test "update_user/2 with valid data updates the user" do
@ -343,7 +345,7 @@ defmodule Mobilizon.ActorsTest do
@email "email@domain.tld" @email "email@domain.tld"
@password "password" @password "password"
test "authenticate/1 checks the user's password" do test "authenticate/1 checks the user's password" do
{:ok, %Actor{user: user} = _actor} = {:ok, %User{} = user} =
Actors.register(%{email: @email, password: @password, username: "yolo"}) Actors.register(%{email: @email, password: @password, username: "yolo"})
assert {:ok, _, _} = Actors.authenticate(%{user: user, password: @password}) assert {:ok, _, _} = Actors.authenticate(%{user: user, password: @password})
@ -353,7 +355,7 @@ defmodule Mobilizon.ActorsTest do
end end
test "get_user_by_email/1 finds an user by it's email" do test "get_user_by_email/1 finds an user by it's email" do
{:ok, %Actor{user: %User{email: email} = user} = _actor} = {:ok, %User{email: email} = user} =
Actors.register(%{email: @email, password: @password, username: "yolo"}) Actors.register(%{email: @email, password: @password, username: "yolo"})
assert email == @email assert email == @email
@ -363,7 +365,7 @@ defmodule Mobilizon.ActorsTest do
end end
test "get_user_by_email/1 finds an activated user by it's email" do test "get_user_by_email/1 finds an activated user by it's email" do
{:ok, %Actor{user: user}} = {:ok, %User{} = user} =
Actors.register(%{email: @email, password: @password, username: "yolo"}) Actors.register(%{email: @email, password: @password, username: "yolo"})
{:ok, %User{id: id}} = Actors.get_user_by_email(@email, false) {:ok, %User{id: id}} = Actors.get_user_by_email(@email, false)

View File

@ -1,15 +1,15 @@
defmodule MobilizonWeb.Resolvers.CategoryResolverTest do defmodule MobilizonWeb.Resolvers.CategoryResolverTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.{Actor, User}
alias MobilizonWeb.AbsintheHelpers alias MobilizonWeb.AbsintheHelpers
import Mobilizon.Factory import Mobilizon.Factory
setup %{conn: conn} do setup %{conn: conn} do
{:ok, %Actor{} = actor} = {:ok, %User{default_actor: %Actor{} = actor} = user} =
Actors.register(%{email: "test@test.tld", password: "testest", username: "test"}) Actors.register(%{email: "test@test.tld", password: "testest", username: "test"})
{:ok, conn: conn, actor: actor} {:ok, conn: conn, actor: actor, user: user}
end end
describe "Category Resolver" do describe "Category Resolver" do
@ -38,7 +38,7 @@ defmodule MobilizonWeb.Resolvers.CategoryResolverTest do
end end
# We can't test an upload…yet? # We can't test an upload…yet?
# test "create_category/3 creates a category", %{conn: conn, actor: actor} do # test "create_category/3 creates a category", %{conn: conn, actor: actor, user: user} do
# mutation = """ # mutation = """
# mutation { # mutation {
# createCategory(title: "my category", description: "my desc") { # createCategory(title: "my category", description: "my desc") {
@ -51,7 +51,7 @@ defmodule MobilizonWeb.Resolvers.CategoryResolverTest do
# res = # res =
# conn # conn
# |> auth_conn(actor.user) # |> auth_conn(user)
# |> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) # |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
# assert json_response(res, 200)["data"]["createCategory"]["title"] == "my category" # assert json_response(res, 200)["data"]["createCategory"]["title"] == "my category"

View File

@ -1,17 +1,17 @@
defmodule MobilizonWeb.Resolvers.EventResolverTest do defmodule MobilizonWeb.Resolvers.EventResolverTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
alias Mobilizon.{Events, Actors} alias Mobilizon.{Events, Actors}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.{Actor, User}
alias MobilizonWeb.AbsintheHelpers alias MobilizonWeb.AbsintheHelpers
import Mobilizon.Factory import Mobilizon.Factory
@event %{description: "some body", title: "some title", begins_on: Ecto.DateTime.utc()} @event %{description: "some body", title: "some title", begins_on: Ecto.DateTime.utc()}
setup %{conn: conn} do setup %{conn: conn} do
{:ok, %Actor{} = actor} = {:ok, %User{default_actor: %Actor{} = actor} = user} =
Actors.register(%{email: "test@test.tld", password: "testest", username: "test"}) Actors.register(%{email: "test@test.tld", password: "testest", username: "test"})
{:ok, conn: conn, actor: actor} {:ok, conn: conn, actor: actor, user: user}
end end
describe "Event Resolver" do describe "Event Resolver" do
@ -108,7 +108,7 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
] ]
end end
test "create_event/3 creates an event", %{conn: conn, actor: actor} do test "create_event/3 creates an event", %{conn: conn, actor: actor, user: user} do
category = insert(:category) category = insert(:category)
mutation = """ mutation = """
@ -129,7 +129,7 @@ defmodule MobilizonWeb.Resolvers.EventResolverTest do
res = res =
conn conn
|> auth_conn(actor.user) |> auth_conn(user)
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["createEvent"]["title"] == "come to my event" assert json_response(res, 200)["data"]["createEvent"]["title"] == "come to my event"

View File

@ -1,6 +1,7 @@
defmodule MobilizonWeb.Resolvers.PersonResolverTest do defmodule MobilizonWeb.Resolvers.PersonResolverTest do
use MobilizonWeb.ConnCase use MobilizonWeb.ConnCase
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.{User, Actor}
alias MobilizonWeb.AbsintheHelpers alias MobilizonWeb.AbsintheHelpers
@valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
@ -8,7 +9,7 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do
describe "Person Resolver" do describe "Person Resolver" do
test "find_actor/3 returns a person by it's username", context do test "find_actor/3 returns a person by it's username", context do
{:ok, actor} = Actors.register(@valid_actor_params) {:ok, %User{default_actor: %Actor{} = actor} = _user} = Actors.register(@valid_actor_params)
query = """ query = """
{ {
@ -44,7 +45,7 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do
end end
test "get_current_person/3 returns the current logged-in actor", context do test "get_current_person/3 returns the current logged-in actor", context do
{:ok, actor} = Actors.register(@valid_actor_params) {:ok, %User{default_actor: %Actor{} = actor} = user} = Actors.register(@valid_actor_params)
query = """ query = """
{ {
@ -66,7 +67,7 @@ defmodule MobilizonWeb.Resolvers.PersonResolverTest do
res = res =
context.conn context.conn
|> auth_conn(actor.user) |> auth_conn(user)
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person")) |> get("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
assert json_response(res, 200)["data"]["loggedPerson"]["preferredUsername"] == assert json_response(res, 200)["data"]["loggedPerson"]["preferredUsername"] ==

View File

@ -7,6 +7,7 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
use Bamboo.Test use Bamboo.Test
@valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
@valid_single_actor_params %{preferred_username: "test2", keys: "yolo"}
describe "Resolver: Get an user" do describe "Resolver: Get an user" do
test "find_user/3 returns an user by it's id", context do test "find_user/3 returns an user by it's id", context do
@ -91,10 +92,10 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
password: "#{@account_creation.password}", password: "#{@account_creation.password}",
username: "#{@account_creation.username}" username: "#{@account_creation.username}"
) { ) {
preferred_username, default_actor {
user { preferred_username,
email },
} email
} }
} }
""" """
@ -103,11 +104,10 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["createUser"]["preferred_username"] == assert json_response(res, 200)["data"]["createUser"]["default_actor"]["preferred_username"] ==
@account_creation.username @account_creation.username
assert json_response(res, 200)["data"]["createUser"]["user"]["email"] == assert json_response(res, 200)["data"]["createUser"]["email"] == @account_creation.email
@account_creation.email
end end
test "test create_user_actor/3 doesn't create an user with bad email", context do test "test create_user_actor/3 doesn't create an user with bad email", context do
@ -118,10 +118,10 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
password: "#{@account_creation.password}", password: "#{@account_creation.password}",
username: "#{@account_creation.username}" username: "#{@account_creation.username}"
) { ) {
preferred_username, default_actor {
user { preferred_username,
email },
} email,
} }
} }
""" """
@ -138,20 +138,20 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
describe "Resolver: Validate an user" do describe "Resolver: Validate an user" do
@valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
test "test validate_user/3 validates an user", context do test "test validate_user/3 validates an user", context do
{:ok, actor} = Actors.register(@valid_actor_params) {:ok, %User{default_actor: %Actor{} = _actor} = user} = Actors.register(@valid_actor_params)
mutation = """ mutation = """
mutation { mutation {
validateUser( validateUser(
token: "#{actor.user.confirmation_token}" token: "#{user.confirmation_token}"
) { ) {
token, token,
user { user {
id id,
default_actor {
preferredUsername
}
}, },
person {
preferredUsername
}
} }
} }
""" """
@ -160,15 +160,16 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["validateUser"]["person"]["preferredUsername"] == assert json_response(res, 200)["data"]["validateUser"]["user"]["default_actor"][
@valid_actor_params.username "preferredUsername"
] == @valid_actor_params.username
assert json_response(res, 200)["data"]["validateUser"]["user"]["id"] == assert json_response(res, 200)["data"]["validateUser"]["user"]["id"] == to_string(user.id)
to_string(actor.user.id)
end end
test "test validate_user/3 with invalid token doesn't validate an user", context do test "test validate_user/3 with invalid token doesn't validate an user", context do
{:ok, _actor} = Actors.register(@valid_actor_params) {:ok, %User{default_actor: %Actor{} = _actor} = _user} =
Actors.register(@valid_actor_params)
mutation = """ mutation = """
mutation { mutation {
@ -177,11 +178,11 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
) { ) {
token, token,
user { user {
id id,
default_actor {
preferredUsername
}
}, },
person {
preferredUsername
}
} }
} }
""" """
@ -197,12 +198,12 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
describe "Resolver: Resend confirmation emails" do describe "Resolver: Resend confirmation emails" do
test "test resend_confirmation_email/3 with valid email resends an validation email", test "test resend_confirmation_email/3 with valid email resends an validation email",
context do context do
{:ok, actor} = Actors.register(@valid_actor_params) {:ok, %User{default_actor: %Actor{} = _actor} = user} = Actors.register(@valid_actor_params)
mutation = """ mutation = """
mutation { mutation {
resendConfirmationEmail( resendConfirmationEmail(
email: "#{actor.user.email}" email: "#{user.email}"
) )
} }
""" """
@ -215,21 +216,22 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
"You requested again a confirmation email too soon" "You requested again a confirmation email too soon"
# Hammer time ! # Hammer time !
Mobilizon.Actors.update_user(actor.user, %{ Mobilizon.Actors.update_user(user, %{
confirmation_sent_at: Timex.shift(actor.user.confirmation_sent_at, hours: -3) confirmation_sent_at: Timex.shift(user.confirmation_sent_at, hours: -3)
}) })
res = res =
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["resendConfirmationEmail"] == actor.user.email assert json_response(res, 200)["data"]["resendConfirmationEmail"] == user.email
assert_delivered_email(Mobilizon.Email.User.confirmation_email(actor.user)) assert_delivered_email(Mobilizon.Email.User.confirmation_email(user))
end end
test "test resend_confirmation_email/3 with invalid email resends an validation email", test "test resend_confirmation_email/3 with invalid email resends an validation email",
context do context do
{:ok, _actor} = Actors.register(@valid_actor_params) {:ok, %User{default_actor: %Actor{} = _actor} = _user} =
Actors.register(@valid_actor_params)
mutation = """ mutation = """
mutation { mutation {
@ -365,7 +367,7 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
describe "Resolver: Login an user" do describe "Resolver: Login an user" do
test "test login_user/3 with valid credentials", context do test "test login_user/3 with valid credentials", context do
{:ok, %Actor{user: user}} = Actors.register(@valid_actor_params) {:ok, %User{} = user} = Actors.register(@valid_actor_params)
{:ok, %User{} = _user} = {:ok, %User{} = _user} =
Actors.update_user(user, %{ Actors.update_user(user, %{
@ -381,8 +383,10 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
password: "#{@valid_actor_params.password}", password: "#{@valid_actor_params.password}",
) { ) {
token, token,
person { user {
preferred_username, default_actor {
preferred_username,
}
} }
} }
} }
@ -394,11 +398,11 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
assert login = json_response(res, 200)["data"]["login"] assert login = json_response(res, 200)["data"]["login"]
assert Map.has_key?(login, "token") && not is_nil(login["token"]) assert Map.has_key?(login, "token") && not is_nil(login["token"])
assert login["person"]["preferred_username"] == @valid_actor_params.username assert login["user"]["default_actor"]["preferred_username"] == @valid_actor_params.username
end end
test "test login_user/3 with invalid password", context do test "test login_user/3 with invalid password", context do
{:ok, %Actor{user: user}} = Actors.register(@valid_actor_params) {:ok, %User{} = user} = Actors.register(@valid_actor_params)
{:ok, %User{} = _user} = {:ok, %User{} = _user} =
Actors.update_user(user, %{ Actors.update_user(user, %{
@ -414,8 +418,10 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
password: "bad password", password: "bad password",
) { ) {
token, token,
person { user {
preferred_username, default_actor {
preferred_username,
}
} }
} }
} }
@ -429,7 +435,7 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
end end
test "test login_user/3 with invalid email", context do test "test login_user/3 with invalid email", context do
{:ok, %Actor{user: user}} = Actors.register(@valid_actor_params) {:ok, %User{} = user} = Actors.register(@valid_actor_params)
{:ok, %User{} = _user} = {:ok, %User{} = _user} =
Actors.update_user(user, %{ Actors.update_user(user, %{
@ -445,8 +451,10 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
password: "bad password", password: "bad password",
) { ) {
token, token,
person { user {
preferred_username, default_actor {
preferred_username,
}
} }
} }
} }
@ -459,4 +467,39 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
assert hd(json_response(res, 200)["errors"])["message"] == "User with email not found" assert hd(json_response(res, 200)["errors"])["message"] == "User with email not found"
end end
end end
describe "Resolver: change default actor for user" do
test "test change_default_actor/3 with valid actor", context do
# Prepare user with two actors
assert {:ok, %User{id: user_id, default_actor: %Actor{} = actor} = user} =
Actors.register(@valid_actor_params)
assert {:ok, %User{actors: actors}} = Actors.get_user_with_actors(user_id)
actor_params = @valid_single_actor_params |> Map.put(:user_id, user_id)
assert {:ok, %Actor{} = actor2} = Actors.create_actor(actor_params)
assert {:ok, %User{actors: actors}} = Actors.get_user_with_actors(user_id)
assert length(actors) == 2
mutation = """
mutation {
changeDefaultActor(preferred_username: "#{actor2.preferred_username}") {
default_actor {
preferred_username
}
}
}
"""
res =
context.conn
|> auth_conn(user)
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["changeDefaultActor"]["default_actor"][
"preferred_username"
] == actor2.preferred_username
end
end
end end