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 79d0367f70
commit 2cb641be79
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,278 +71,392 @@ 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_bad_email %{ @account_creation %{
email: "y@l@", email: "test@demo.tld",
password: "long password", password: "long password",
username: "test_account" username: "test_account"
} }
@account_creation_bad_email %{
email: "y@l@",
password: "long password",
username: "test_account"
}
test "test create_user_actor/3 creates an user", context do test "test create_user_actor/3 creates an user", context do
mutation = """ mutation = """
mutation { mutation {
createUser( createUser(
email: "#{@account_creation.email}", email: "#{@account_creation.email}",
password: "#{@account_creation.password}", password: "#{@account_creation.password}",
username: "#{@account_creation.username}" username: "#{@account_creation.username}"
) { ) {
preferred_username, preferred_username,
user { user {
email email
}
} }
} }
} """
"""
res = res =
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"]["preferred_username"] ==
@account_creation.username @account_creation.username
assert json_response(res, 200)["data"]["createUser"]["user"]["email"] == assert json_response(res, 200)["data"]["createUser"]["user"]["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
mutation = """ mutation = """
mutation { mutation {
createUser( createUser(
email: "#{@account_creation_bad_email.email}", email: "#{@account_creation_bad_email.email}",
password: "#{@account_creation.password}", password: "#{@account_creation.password}",
username: "#{@account_creation.username}" username: "#{@account_creation.username}"
) { ) {
preferred_username, preferred_username,
user { user {
email email
}
} }
} }
} """
"""
res = res =
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
@valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"} describe "Resolver: Validate an user" do
test "test validate_user/3 validates an user", context do @valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
{:ok, actor} = Actors.register(@valid_actor_params) test "test validate_user/3 validates an user", context do
{:ok, actor} = Actors.register(@valid_actor_params)
mutation = """ mutation = """
mutation { mutation {
validateUser( validateUser(
token: "#{actor.user.confirmation_token}" token: "#{actor.user.confirmation_token}"
) { ) {
token, token,
user { user {
id id
}, },
person { person {
preferredUsername preferredUsername
}
} }
} }
} """
"""
res = res =
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"]["person"]["preferredUsername"] ==
@valid_actor_params.username @valid_actor_params.username
assert json_response(res, 200)["data"]["validateUser"]["user"]["id"] == assert json_response(res, 200)["data"]["validateUser"]["user"]["id"] ==
to_string(actor.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, _actor} = Actors.register(@valid_actor_params)
mutation = """ mutation = """
mutation { mutation {
validateUser( validateUser(
token: "no pass" token: "no pass"
) { ) {
token, token,
user { user {
id id
}, },
person { person {
preferredUsername preferredUsername
}
} }
} }
} """
"""
res = res =
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
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
{:ok, actor} = Actors.register(@valid_actor_params) test "test resend_confirmation_email/3 with valid email resends an validation email",
context do
{:ok, actor} = Actors.register(@valid_actor_params)
mutation = """ mutation = """
mutation { mutation {
resendConfirmationEmail( resendConfirmationEmail(
email: "#{actor.user.email}" email: "#{actor.user.email}"
) )
} }
""" """
res = res =
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == assert hd(json_response(res, 200)["errors"])["message"] ==
"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(actor.user, %{
confirmation_sent_at: Timex.shift(actor.user.confirmation_sent_at, hours: -3) confirmation_sent_at: Timex.shift(actor.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"] == actor.user.email
assert_delivered_email(Mobilizon.Email.User.confirmation_email(actor.user)) assert_delivered_email(Mobilizon.Email.User.confirmation_email(actor.user))
end
test "test resend_confirmation_email/3 with invalid email resends an validation email",
context do
{:ok, _actor} = Actors.register(@valid_actor_params)
mutation = """
mutation {
resendConfirmationEmail(
email: "oh no"
)
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] ==
"No user to validate with this email was found"
end
end end
test "test resend_confirmation_email/3 with invalid email resends an validation email", describe "Resolver: Send reset password" do
context do test "test send_reset_password/3 with valid email", context do
{:ok, _actor} = Actors.register(@valid_actor_params) user = insert(:user)
mutation = """ mutation = """
mutation { mutation {
resendConfirmationEmail( sendResetPassword(
email: "oh no" email: "#{user.email}"
) )
} }
""" """
res = res =
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == assert json_response(res, 200)["data"]["sendResetPassword"] == user.email
"No user to validate with this email was found" end
test "test send_reset_password/3 with invalid email", context do
mutation = """
mutation {
sendResetPassword(
email: "oh no"
)
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] ==
"No user with this email was found"
end
end end
test "test send_reset_password/3 with valid email", context do describe "Resolver: Reset user's password" do
user = insert(:user) test "test reset_password/3 with valid email", context do
%User{} = user = insert(:user)
%Actor{} = insert(:actor, user: user)
{:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user)
%User{reset_password_token: reset_password_token} = Mobilizon.Actors.get_user!(user.id)
mutation = """ mutation = """
mutation { mutation {
sendResetPassword( resetPassword(
email: "#{user.email}" token: "#{reset_password_token}",
) password: "new password"
} ) {
""" user {
id
res = }
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["sendResetPassword"] == user.email
end
test "test send_reset_password/3 with invalid email", context do
mutation = """
mutation {
sendResetPassword(
email: "oh no"
)
}
"""
res =
context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "No user with this email was found"
end
test "test reset_password/3 with valid email", context do
%User{} = user = insert(:user)
%Actor{} = insert(:actor, user: user)
{:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user)
%User{reset_password_token: reset_password_token} = Mobilizon.Actors.get_user!(user.id)
mutation = """
mutation {
resetPassword(
token: "#{reset_password_token}",
password: "new password"
) {
user {
id
} }
} }
} """
"""
res = res =
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert json_response(res, 200)["data"]["resetPassword"]["user"]["id"] == to_string(user.id) assert json_response(res, 200)["data"]["resetPassword"]["user"]["id"] == to_string(user.id)
end end
test "test reset_password/3 with a password too short", context do test "test reset_password/3 with a password too short", context do
%User{} = user = insert(:user) %User{} = user = insert(:user)
{:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user) {:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user)
%User{reset_password_token: reset_password_token} = Mobilizon.Actors.get_user!(user.id) %User{reset_password_token: reset_password_token} = Mobilizon.Actors.get_user!(user.id)
mutation = """ mutation = """
mutation { mutation {
resetPassword( resetPassword(
token: "#{reset_password_token}", token: "#{reset_password_token}",
password: "new" password: "new"
) { ) {
user { user {
id id
}
} }
} }
} """
"""
res = res =
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "password_too_short" assert hd(json_response(res, 200)["errors"])["message"] == "password_too_short"
end end
test "test reset_password/3 with an invalid token", context do test "test reset_password/3 with an invalid token", context do
%User{} = user = insert(:user) %User{} = user = insert(:user)
{:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user) {:ok, _email_sent} = Mobilizon.Actors.Service.ResetPassword.send_password_reset_email(user)
%User{} = Mobilizon.Actors.get_user!(user.id) %User{} = Mobilizon.Actors.get_user!(user.id)
mutation = """ mutation = """
mutation { mutation {
resetPassword( resetPassword(
token: "not good", token: "not good",
password: "new" password: "new"
) { ) {
user { user {
id id
}
} }
} }
} """
"""
res = res =
context.conn context.conn
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation)) |> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
assert hd(json_response(res, 200)["errors"])["message"] == "invalid_token" assert hd(json_response(res, 200)["errors"])["message"] == "invalid_token"
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
end end