Add more tests for user

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2018-11-28 14:48:55 +01:00
parent c9f58ea899
commit 0cb39de01e
2 changed files with 332 additions and 218 deletions

View file

@ -27,7 +27,7 @@ defmodule MobilizonWeb.Resolvers.User 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}),
%Actor{} = actor <- Actors.get_actor_for_user(user) do %Actor{} = actor <- Actors.get_actor_for_user(user) do
{:ok, %{token: token, user: user, actor: actor}} {: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"}

View file

@ -8,7 +8,7 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
@valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
describe "User Resolver" 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
user = insert(:user) user = insert(:user)
@ -71,7 +71,12 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
end end
end end
@account_creation %{email: "test@demo.tld", password: "long password", username: "test_account"} describe "Resolver: Create an user & actor" do
@account_creation %{
email: "test@demo.tld",
password: "long password",
username: "test_account"
}
@account_creation_bad_email %{ @account_creation_bad_email %{
email: "y@l@", email: "y@l@",
password: "long password", password: "long password",
@ -125,9 +130,12 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "Email doesn't fit required format" assert hd(json_response(res, 200)["errors"])["message"] ==
"Email doesn't fit required format"
end
end end
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, actor} = Actors.register(@valid_actor_params)
@ -184,8 +192,11 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
assert hd(json_response(res, 200)["errors"])["message"] == "Invalid token" assert hd(json_response(res, 200)["errors"])["message"] == "Invalid token"
end end
end
test "test resend_confirmation_email/3 with valid email resends an validation email", context do describe "Resolver: Resend confirmation emails" do
test "test resend_confirmation_email/3 with valid email resends an validation email",
context do
{:ok, actor} = Actors.register(@valid_actor_params) {:ok, actor} = Actors.register(@valid_actor_params)
mutation = """ mutation = """
@ -235,7 +246,9 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
assert hd(json_response(res, 200)["errors"])["message"] == assert hd(json_response(res, 200)["errors"])["message"] ==
"No user to validate with this email was found" "No user to validate with this email was found"
end end
end
describe "Resolver: Send reset password" do
test "test send_reset_password/3 with valid email", context do test "test send_reset_password/3 with valid email", context do
user = insert(:user) user = insert(:user)
@ -267,9 +280,12 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "No user with this email was found" assert hd(json_response(res, 200)["errors"])["message"] ==
"No user with this email was found"
end
end end
describe "Resolver: Reset user's password" do
test "test reset_password/3 with valid email", context do test "test reset_password/3 with valid email", context do
%User{} = user = insert(:user) %User{} = user = insert(:user)
%Actor{} = insert(:actor, user: user) %Actor{} = insert(:actor, user: user)
@ -346,3 +362,101 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do
assert hd(json_response(res, 200)["errors"])["message"] == "invalid_token" assert hd(json_response(res, 200)["errors"])["message"] == "invalid_token"
end end
end end
describe "Resolver: Login an user" do
test "test login_user/3 with valid credentials", context do
{:ok, %Actor{user: user}} = Actors.register(@valid_actor_params)
{:ok, %User{} = _user} =
Actors.update_user(user, %{
"confirmed_at" => DateTime.utc_now(),
"confirmation_sent_at" => nil,
"confirmation_token" => nil
})
mutation = """
mutation {
login(
email: "#{@valid_actor_params.email}",
password: "#{@valid_actor_params.password}",
) {
token,
person {
preferred_username,
}
}
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert login = json_response(res, 200)["data"]["login"]
assert Map.has_key?(login, "token") && not is_nil(login["token"])
assert login["person"]["preferred_username"] == @valid_actor_params.username
end
test "test login_user/3 with invalid password", context do
{:ok, %Actor{user: user}} = Actors.register(@valid_actor_params)
{:ok, %User{} = _user} =
Actors.update_user(user, %{
"confirmed_at" => DateTime.utc_now(),
"confirmation_sent_at" => nil,
"confirmation_token" => nil
})
mutation = """
mutation {
login(
email: "#{@valid_actor_params.email}",
password: "bad password",
) {
token,
person {
preferred_username,
}
}
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "Impossible to authenticate"
end
test "test login_user/3 with invalid email", context do
{:ok, %Actor{user: user}} = Actors.register(@valid_actor_params)
{:ok, %User{} = _user} =
Actors.update_user(user, %{
"confirmed_at" => DateTime.utc_now(),
"confirmation_sent_at" => nil,
"confirmation_token" => nil
})
mutation = """
mutation {
login(
email: "bad email",
password: "bad password",
) {
token,
person {
preferred_username,
}
}
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "User with email not found"
end
end
end