2018-11-06 10:30:27 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.UserResolverTest do
|
|
|
|
use MobilizonWeb.ConnCase
|
2019-09-17 02:45:32 +02:00
|
|
|
|
|
|
|
import Mobilizon.Factory
|
|
|
|
import Mock
|
|
|
|
|
|
|
|
use Bamboo.Test
|
|
|
|
|
2019-09-08 00:05:54 +02:00
|
|
|
alias Mobilizon.{Actors, Config, Users}
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-17 02:45:32 +02:00
|
|
|
alias Mobilizon.Service.Users.ResetPassword
|
2019-08-12 16:04:16 +02:00
|
|
|
alias Mobilizon.Users
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Users.User
|
2019-09-17 02:45:32 +02:00
|
|
|
|
|
|
|
alias MobilizonWeb.{AbsintheHelpers, Email}
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
@valid_actor_params %{email: "test@test.tld", password: "testest", username: "test"}
|
2018-11-29 17:43:22 +01:00
|
|
|
@valid_single_actor_params %{preferred_username: "test2", keys: "yolo"}
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
describe "Resolver: Get an user" do
|
2019-10-15 21:18:03 +02:00
|
|
|
test "find_user/3 returns an user by its id", context do
|
2018-11-06 10:30:27 +01:00
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
user(id: "#{user.id}") {
|
|
|
|
email,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["user"]["email"] == user.email
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
user(id: "#{0}") {
|
|
|
|
email,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["user"] == nil
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] == "User with ID #{0} not found"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get_current_user/3 returns the current logged-in user", context do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
loggedUser {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["loggedUser"] == nil
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"You need to be logged-in to view current user"
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["loggedUser"]["id"] == to_string(user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 11:41:28 +01:00
|
|
|
describe "Resolver: List users" do
|
2019-03-06 18:45:26 +01:00
|
|
|
test "list_users/3 doesn't return anything with a non moderator user", context do
|
|
|
|
insert(:user, email: "riri@example.com", role: :moderator)
|
|
|
|
user = insert(:user, email: "fifi@example.com")
|
|
|
|
insert(:user, email: "loulou@example.com", role: :administrator)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
users {
|
|
|
|
total,
|
|
|
|
elements {
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "user"))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"You need to have admin access to list users"
|
|
|
|
end
|
|
|
|
|
2019-03-01 11:41:28 +01:00
|
|
|
test "list_users/3 returns a list of users", context do
|
2019-03-06 18:45:26 +01:00
|
|
|
user = insert(:user, email: "riri@example.com", role: :moderator)
|
2019-03-01 11:41:28 +01:00
|
|
|
insert(:user, email: "fifi@example.com")
|
|
|
|
insert(:user, email: "loulou@example.com")
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
users {
|
|
|
|
total,
|
|
|
|
elements {
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2019-03-06 18:45:26 +01:00
|
|
|
|> auth_conn(user)
|
2019-03-01 11:41:28 +01:00
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["users"]["total"] == 3
|
|
|
|
assert json_response(res, 200)["data"]["users"]["elements"] |> length == 3
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["users"]["elements"]
|
|
|
|
|> Enum.map(& &1["email"]) == [
|
2019-03-05 12:14:31 +01:00
|
|
|
"loulou@example.com",
|
2019-03-01 11:41:28 +01:00
|
|
|
"fifi@example.com",
|
2019-03-05 12:14:31 +01:00
|
|
|
"riri@example.com"
|
2019-03-01 11:41:28 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
users(page: 2, limit: 1) {
|
|
|
|
total,
|
|
|
|
elements {
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2019-03-06 18:45:26 +01:00
|
|
|
|> auth_conn(user)
|
2019-03-01 11:41:28 +01:00
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["users"]["total"] == 3
|
|
|
|
assert json_response(res, 200)["data"]["users"]["elements"] |> length == 1
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["users"]["elements"] |> Enum.map(& &1["email"]) == [
|
|
|
|
"fifi@example.com"
|
|
|
|
]
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
users(page: 3, limit: 1, sort: ID, direction: DESC) {
|
|
|
|
total,
|
|
|
|
elements {
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
2019-03-06 18:45:26 +01:00
|
|
|
|> auth_conn(user)
|
2019-03-01 11:41:28 +01:00
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["users"]["total"] == 3
|
|
|
|
assert json_response(res, 200)["data"]["users"]["elements"] |> length == 1
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["users"]["elements"] |> Enum.map(& &1["email"]) == [
|
|
|
|
"riri@example.com"
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "get_current_user/3 returns the current logged-in user", context do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
loggedUser {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["loggedUser"] == nil
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"You need to be logged-in to view current user"
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> get("/api", AbsintheHelpers.query_skeleton(query, "logged_user"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["loggedUser"]["id"] == to_string(user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
describe "Resolver: Create an user & actor" do
|
2019-01-29 11:02:32 +01:00
|
|
|
@user_creation %{
|
2018-11-28 14:48:55 +01:00
|
|
|
email: "test@demo.tld",
|
2019-01-29 11:02:32 +01:00
|
|
|
password: "long password",
|
2019-10-01 13:08:09 +02:00
|
|
|
locale: "fr_FR",
|
2019-01-29 11:02:32 +01:00
|
|
|
username: "toto",
|
|
|
|
name: "Sir Toto",
|
|
|
|
summary: "Sir Toto, prince of the functional tests"
|
2018-11-28 14:48:55 +01:00
|
|
|
}
|
2019-01-29 11:02:32 +01:00
|
|
|
@user_creation_bad_email %{
|
2018-11-28 14:48:55 +01:00
|
|
|
email: "y@l@",
|
2019-01-25 13:59:58 +01:00
|
|
|
password: "long password"
|
2018-11-28 14:48:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 11:02:32 +01:00
|
|
|
test "test create_user/3 creates an user and register_person/3 registers a profile",
|
|
|
|
context do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createUser(
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
password: "#{@user_creation.password}",
|
2019-10-01 13:08:09 +02:00
|
|
|
locale: "#{@user_creation.locale}"
|
2019-01-29 11:02:32 +01:00
|
|
|
) {
|
|
|
|
id,
|
2019-10-01 13:08:09 +02:00
|
|
|
email,
|
|
|
|
locale
|
2019-01-29 11:02:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["createUser"]["email"] == @user_creation.email
|
2019-10-01 13:08:09 +02:00
|
|
|
assert json_response(res, 200)["data"]["createUser"]["locale"] == @user_creation.locale
|
|
|
|
|
|
|
|
{:ok, user} = Users.get_user_by_email(@user_creation.email)
|
|
|
|
|
|
|
|
assert_delivered_email(Email.User.confirmation_email(user, @user_creation.locale))
|
2019-01-29 11:02:32 +01:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
registerPerson(
|
|
|
|
preferredUsername: "#{@user_creation.username}",
|
|
|
|
name: "#{@user_creation.name}",
|
|
|
|
summary: "#{@user_creation.summary}",
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
) {
|
|
|
|
preferredUsername,
|
|
|
|
name,
|
|
|
|
summary,
|
2019-05-22 14:12:11 +02:00
|
|
|
avatar {
|
|
|
|
url
|
|
|
|
},
|
2019-01-29 11:02:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["registerPerson"]["preferredUsername"] ==
|
|
|
|
@user_creation.username
|
|
|
|
end
|
|
|
|
|
2019-10-01 09:26:07 +02:00
|
|
|
test "create_user/3 doesn't allow two users with the same email", %{conn: conn} do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createUser(
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
password: "#{@user_creation.password}",
|
|
|
|
) {
|
|
|
|
id,
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["createUser"]["email"] == @user_creation.email
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createUser(
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
password: "#{@user_creation.password}",
|
|
|
|
) {
|
|
|
|
id,
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] == "This email is already used."
|
|
|
|
end
|
|
|
|
|
2019-01-29 11:02:32 +01:00
|
|
|
test "register_person/3 doesn't register a profile from an unknown email", context do
|
2018-11-28 14:48:55 +01:00
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createUser(
|
2019-01-29 11:02:32 +01:00
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
password: "#{@user_creation.password}",
|
2018-11-28 14:48:55 +01:00
|
|
|
) {
|
2019-01-25 13:59:58 +01:00
|
|
|
id,
|
2018-11-29 17:43:22 +01:00
|
|
|
email
|
2018-11-06 10:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-01-29 11:02:32 +01:00
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
registerPerson(
|
|
|
|
preferredUsername: "#{@user_creation.username}",
|
|
|
|
name: "#{@user_creation.name}",
|
|
|
|
summary: "#{@user_creation.summary}",
|
|
|
|
email: "random",
|
|
|
|
) {
|
|
|
|
preferredUsername,
|
|
|
|
name,
|
|
|
|
summary,
|
2019-05-22 14:12:11 +02:00
|
|
|
avatar {
|
|
|
|
url
|
|
|
|
},
|
2019-01-29 11:02:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-10-15 18:13:05 +02:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"No user with this email was found"
|
2019-01-29 11:02:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "register_person/3 can't be called with an existing profile", context do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createUser(
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
password: "#{@user_creation.password}",
|
|
|
|
) {
|
|
|
|
id,
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
registerPerson(
|
|
|
|
preferredUsername: "#{@user_creation.username}",
|
|
|
|
name: "#{@user_creation.name}",
|
|
|
|
summary: "#{@user_creation.summary}",
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
) {
|
|
|
|
preferredUsername,
|
|
|
|
name,
|
|
|
|
summary,
|
2019-05-22 14:12:11 +02:00
|
|
|
avatar {
|
|
|
|
url
|
|
|
|
},
|
2019-01-29 11:02:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["data"]["registerPerson"]["preferredUsername"] ==
|
|
|
|
@user_creation.username
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
registerPerson(
|
|
|
|
preferredUsername: "#{@user_creation.username}",
|
|
|
|
name: "#{@user_creation.name}",
|
|
|
|
summary: "#{@user_creation.summary}",
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
) {
|
|
|
|
preferredUsername,
|
|
|
|
name,
|
|
|
|
summary,
|
2019-05-22 14:12:11 +02:00
|
|
|
avatar {
|
|
|
|
url
|
|
|
|
},
|
2019-01-29 11:02:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"You already have a profile for this user"
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-01-25 13:59:58 +01:00
|
|
|
test "test create_user/3 doesn't create an user with bad email", context do
|
2018-11-28 14:48:55 +01:00
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createUser(
|
2019-01-29 11:02:32 +01:00
|
|
|
email: "#{@user_creation_bad_email.email}",
|
|
|
|
password: "#{@user_creation_bad_email.password}",
|
2018-11-28 14:48:55 +01:00
|
|
|
) {
|
2019-01-25 13:59:58 +01:00
|
|
|
id,
|
|
|
|
email
|
2018-11-06 10:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Email doesn't fit required format"
|
|
|
|
end
|
2019-03-22 10:53:38 +01:00
|
|
|
|
|
|
|
test "test create_user/3 doesn't create a user when registration is disabled", context do
|
2019-09-08 00:05:54 +02:00
|
|
|
with_mock Config, instance_registrations_open?: fn -> false end do
|
2019-03-22 10:53:38 +01:00
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
createUser(
|
|
|
|
email: "#{@user_creation.email}",
|
|
|
|
password: "#{@user_creation.password}",
|
|
|
|
) {
|
|
|
|
id,
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Registrations are not enabled"
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
describe "Resolver: Validate an user" do
|
|
|
|
test "test validate_user/3 validates an user", context do
|
2019-03-08 18:52:27 +01:00
|
|
|
{:ok, %User{} = user} = Users.register(@valid_actor_params)
|
2018-11-28 14:48:55 +01:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
validateUser(
|
2018-11-29 17:43:22 +01:00
|
|
|
token: "#{user.confirmation_token}"
|
2018-11-28 14:48:55 +01:00
|
|
|
) {
|
2019-08-12 16:04:16 +02:00
|
|
|
accessToken,
|
2018-11-28 14:48:55 +01:00
|
|
|
user {
|
2018-11-29 17:43:22 +01:00
|
|
|
id,
|
2018-11-28 14:48:55 +01:00
|
|
|
},
|
2018-11-06 10:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
assert json_response(res, 200)["data"]["validateUser"]["user"]["id"] == to_string(user.id)
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
test "test validate_user/3 with invalid token doesn't validate an user", context do
|
2019-01-25 13:59:58 +01:00
|
|
|
insert(:user, confirmation_token: "t0t0")
|
2018-11-28 14:48:55 +01:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
validateUser(
|
|
|
|
token: "no pass"
|
|
|
|
) {
|
2019-08-12 16:04:16 +02:00
|
|
|
accessToken,
|
2018-11-28 14:48:55 +01:00
|
|
|
user {
|
2019-01-25 13:59:58 +01:00
|
|
|
id
|
2018-11-28 14:48:55 +01:00
|
|
|
},
|
2018-11-06 10:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2019-01-25 13:59:58 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] == "Unable to validate user"
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
describe "Resolver: Resend confirmation emails" do
|
|
|
|
test "test resend_confirmation_email/3 with valid email resends an validation email",
|
|
|
|
context do
|
2019-03-08 18:52:27 +01:00
|
|
|
{:ok, %User{} = user} = Users.register(%{email: "toto@tata.tld", password: "p4ssw0rd"})
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
resendConfirmationEmail(
|
2018-11-29 17:43:22 +01:00
|
|
|
email: "#{user.email}"
|
2018-11-28 14:48:55 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"You requested again a confirmation email too soon"
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
# Hammer time !
|
2019-03-05 17:23:05 +01:00
|
|
|
Mobilizon.Users.update_user(user, %{
|
2018-11-29 17:43:22 +01:00
|
|
|
confirmation_sent_at: Timex.shift(user.confirmation_sent_at, hours: -3)
|
2018-11-28 14:48:55 +01:00
|
|
|
})
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
assert json_response(res, 200)["data"]["resendConfirmationEmail"] == user.email
|
2019-09-17 02:45:32 +02:00
|
|
|
assert_delivered_email(Email.User.confirmation_email(user))
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
test "test resend_confirmation_email/3 with invalid email resends an validation email",
|
|
|
|
context do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
resendConfirmationEmail(
|
|
|
|
email: "oh no"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"No user to validate with this email was found"
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
describe "Resolver: Send reset password" do
|
|
|
|
test "test send_reset_password/3 with valid email", context do
|
|
|
|
user = insert(:user)
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
sendResetPassword(
|
|
|
|
email: "#{user.email}"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
"""
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
assert json_response(res, 200)["data"]["sendResetPassword"] == user.email
|
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
test "test send_reset_password/3 with invalid email", context do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
sendResetPassword(
|
|
|
|
email: "oh no"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
"""
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"No user with this email was found"
|
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
end
|
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
describe "Resolver: Reset user's password" do
|
|
|
|
test "test reset_password/3 with valid email", context do
|
2019-03-08 18:52:27 +01:00
|
|
|
{:ok, %User{} = user} = Users.register(%{email: "toto@tata.tld", password: "p4ssw0rd"})
|
2018-11-28 14:48:55 +01:00
|
|
|
%Actor{} = insert(:actor, user: user)
|
2019-03-05 17:23:05 +01:00
|
|
|
{:ok, _email_sent} = ResetPassword.send_password_reset_email(user)
|
|
|
|
%User{reset_password_token: reset_password_token} = Mobilizon.Users.get_user!(user.id)
|
2018-11-28 14:48:55 +01:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
resetPassword(
|
|
|
|
token: "#{reset_password_token}",
|
|
|
|
password: "new password"
|
|
|
|
) {
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
2018-11-27 17:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
"""
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
assert json_response(res, 200)["data"]["resetPassword"]["user"]["id"] == to_string(user.id)
|
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
test "test reset_password/3 with a password too short", context do
|
|
|
|
%User{} = user = insert(:user)
|
2019-03-05 17:23:05 +01:00
|
|
|
{:ok, _email_sent} = ResetPassword.send_password_reset_email(user)
|
|
|
|
%User{reset_password_token: reset_password_token} = Mobilizon.Users.get_user!(user.id)
|
2018-11-28 14:48:55 +01:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
resetPassword(
|
|
|
|
token: "#{reset_password_token}",
|
|
|
|
password: "new"
|
|
|
|
) {
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
2018-11-27 17:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2019-01-24 15:23:27 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"The password you have choosen is too short. Please make sure your password contains at least 6 charaters."
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
test "test reset_password/3 with an invalid token", context do
|
|
|
|
%User{} = user = insert(:user)
|
2019-03-05 17:23:05 +01:00
|
|
|
{:ok, _email_sent} = ResetPassword.send_password_reset_email(user)
|
|
|
|
%User{} = Mobilizon.Users.get_user!(user.id)
|
2018-11-28 14:48:55 +01:00
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
resetPassword(
|
|
|
|
token: "not good",
|
|
|
|
password: "new"
|
|
|
|
) {
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2019-01-24 15:23:27 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"The token you provided is invalid. Make sure that the URL is exactly the one provided inside the email you got."
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
end
|
|
|
|
|
2019-08-12 16:04:16 +02:00
|
|
|
describe "Resolver: Login a user" do
|
2018-11-28 14:48:55 +01:00
|
|
|
test "test login_user/3 with valid credentials", context do
|
2019-03-08 18:52:27 +01:00
|
|
|
{:ok, %User{} = user} = Users.register(%{email: "toto@tata.tld", password: "p4ssw0rd"})
|
2018-11-28 14:48:55 +01:00
|
|
|
|
|
|
|
{:ok, %User{} = _user} =
|
2019-03-05 17:23:05 +01:00
|
|
|
Users.update_user(user, %{
|
2019-02-14 14:19:55 +01:00
|
|
|
"confirmed_at" => DateTime.utc_now() |> DateTime.truncate(:second),
|
2018-11-28 14:48:55 +01:00
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
login(
|
2019-01-25 13:59:58 +01:00
|
|
|
email: "#{user.email}",
|
|
|
|
password: "#{user.password}",
|
2018-11-28 14:48:55 +01:00
|
|
|
) {
|
2019-08-12 16:04:16 +02:00
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
2018-11-29 17:43:22 +01:00
|
|
|
user {
|
2019-03-01 11:41:28 +01:00
|
|
|
id
|
2018-11-28 14:48:55 +01:00
|
|
|
}
|
2018-11-27 17:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert login = json_response(res, 200)["data"]["login"]
|
2019-08-12 16:04:16 +02:00
|
|
|
assert Map.has_key?(login, "accessToken") && not is_nil(login["accessToken"])
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "test login_user/3 with invalid password", context do
|
2019-03-08 18:52:27 +01:00
|
|
|
{:ok, %User{} = user} = Users.register(%{email: "toto@tata.tld", password: "p4ssw0rd"})
|
2018-11-28 14:48:55 +01:00
|
|
|
|
|
|
|
{:ok, %User{} = _user} =
|
2019-03-05 17:23:05 +01:00
|
|
|
Users.update_user(user, %{
|
2019-02-14 14:19:55 +01:00
|
|
|
"confirmed_at" => DateTime.utc_now() |> DateTime.truncate(:second),
|
2018-11-28 14:48:55 +01:00
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
login(
|
2019-01-25 13:59:58 +01:00
|
|
|
email: "#{user.email}",
|
2018-11-28 14:48:55 +01:00
|
|
|
password: "bad password",
|
|
|
|
) {
|
2019-08-12 16:04:16 +02:00
|
|
|
accessToken,
|
2018-11-29 17:43:22 +01:00
|
|
|
user {
|
|
|
|
default_actor {
|
|
|
|
preferred_username,
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2018-11-28 14:48:55 +01:00
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
2019-01-24 15:23:27 +01:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Impossible to authenticate, either your email or password are invalid."
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
test "test login_user/3 with invalid email", context do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
login(
|
|
|
|
email: "bad email",
|
|
|
|
password: "bad password",
|
|
|
|
) {
|
2019-08-12 16:04:16 +02:00
|
|
|
accessToken,
|
2018-11-29 17:43:22 +01:00
|
|
|
user {
|
|
|
|
default_actor {
|
|
|
|
preferred_username,
|
|
|
|
}
|
2018-11-28 14:48:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
2018-11-27 17:54:54 +01:00
|
|
|
|
2019-10-15 18:13:05 +02:00
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"No user with this email was found"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "test login_user/3 with unconfirmed user", context do
|
|
|
|
{:ok, %User{} = user} = Users.register(%{email: "toto@tata.tld", password: "p4ssw0rd"})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
login(
|
|
|
|
email: "#{user.email}",
|
|
|
|
password: "#{user.password}",
|
|
|
|
) {
|
|
|
|
accessToken,
|
|
|
|
user {
|
|
|
|
default_actor {
|
|
|
|
preferred_username,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] == "User account not confirmed"
|
2018-11-28 14:48:55 +01:00
|
|
|
end
|
2018-11-27 17:54:54 +01:00
|
|
|
end
|
2018-11-29 17:43:22 +01:00
|
|
|
|
2019-08-12 16:04:16 +02:00
|
|
|
describe "Resolver: Refresh a token" do
|
|
|
|
test "test refresh_token/3 with a bad token", context do
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
refreshToken(
|
|
|
|
refreshToken: "bad_token"
|
|
|
|
) {
|
|
|
|
accessToken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"Cannot refresh the token"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "test refresh_token/3 with an appropriate token", context do
|
|
|
|
user = insert(:user)
|
|
|
|
{:ok, refresh_token} = Users.generate_refresh_token(user)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
refreshToken(
|
|
|
|
refreshToken: "#{refresh_token}"
|
|
|
|
) {
|
|
|
|
accessToken
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
|
|
|
|
access_token = json_response(res, 200)["data"]["refreshToken"]["accessToken"]
|
|
|
|
assert String.length(access_token) > 10
|
|
|
|
|
|
|
|
query = """
|
|
|
|
{
|
|
|
|
loggedPerson {
|
|
|
|
preferredUsername,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
context.conn
|
|
|
|
|> Plug.Conn.put_req_header("authorization", "Bearer #{access_token}")
|
|
|
|
|> post("/api", AbsintheHelpers.query_skeleton(query, "logged_person"))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-29 17:43:22 +01:00
|
|
|
describe "Resolver: change default actor for user" do
|
|
|
|
test "test change_default_actor/3 with valid actor", context do
|
|
|
|
# Prepare user with two actors
|
2019-01-25 13:59:58 +01:00
|
|
|
user = insert(:user)
|
|
|
|
insert(:actor, user: user)
|
2018-11-29 17:43:22 +01:00
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
assert {:ok, %User{actors: actors}} = Users.get_user_with_actors(user.id)
|
2018-11-29 17:43:22 +01:00
|
|
|
|
2019-01-25 13:59:58 +01:00
|
|
|
actor_params = @valid_single_actor_params |> Map.put(:user_id, user.id)
|
2018-11-29 17:43:22 +01:00
|
|
|
assert {:ok, %Actor{} = actor2} = Actors.create_actor(actor_params)
|
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
assert {:ok, %User{actors: actors}} = Users.get_user_with_actors(user.id)
|
2018-11-29 17:43:22 +01:00
|
|
|
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
|
2019-09-24 18:08:33 +02:00
|
|
|
|
|
|
|
describe "Resolver: Change password for an user" do
|
|
|
|
@email "toto@tata.tld"
|
|
|
|
@old_password "p4ssw0rd"
|
|
|
|
@new_password "upd4t3d"
|
|
|
|
|
|
|
|
test "change_password/3 with valid password", %{conn: conn} do
|
|
|
|
{:ok, %User{} = user} = Users.register(%{email: @email, password: @old_password})
|
|
|
|
|
|
|
|
# Hammer time !
|
|
|
|
{:ok, %User{} = _user} =
|
|
|
|
Users.update_user(user, %{
|
|
|
|
"confirmed_at" => Timex.shift(user.confirmation_sent_at, hours: -3),
|
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
login(
|
|
|
|
email: "#{@email}",
|
|
|
|
password: "#{@old_password}",
|
|
|
|
) {
|
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert login = json_response(res, 200)["data"]["login"]
|
|
|
|
assert Map.has_key?(login, "accessToken") && not is_nil(login["accessToken"])
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
changePassword(old_password: "#{@old_password}", new_password: "#{@new_password}") {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert json_response(res, 200)["errors"] == nil
|
|
|
|
assert json_response(res, 200)["data"]["changePassword"]["id"] == to_string(user.id)
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
login(
|
|
|
|
email: "#{@email}",
|
|
|
|
password: "#{@new_password}",
|
|
|
|
) {
|
|
|
|
accessToken,
|
|
|
|
refreshToken,
|
|
|
|
user {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert login = json_response(res, 200)["data"]["login"]
|
|
|
|
assert Map.has_key?(login, "accessToken") && not is_nil(login["accessToken"])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "change_password/3 with invalid password", %{conn: conn} do
|
|
|
|
{:ok, %User{} = user} = Users.register(%{email: @email, password: @old_password})
|
|
|
|
|
|
|
|
# Hammer time !
|
|
|
|
|
|
|
|
{:ok, %User{} = _user} =
|
|
|
|
Users.update_user(user, %{
|
|
|
|
"confirmed_at" => Timex.shift(user.confirmation_sent_at, hours: -3),
|
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
changePassword(old_password: "invalid password", new_password: "#{@new_password}") {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] == "The current password is invalid"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "change_password/3 with same password", %{conn: conn} do
|
|
|
|
{:ok, %User{} = user} = Users.register(%{email: @email, password: @old_password})
|
|
|
|
|
|
|
|
# Hammer time !
|
|
|
|
{:ok, %User{} = _user} =
|
|
|
|
Users.update_user(user, %{
|
|
|
|
"confirmed_at" => Timex.shift(user.confirmation_sent_at, hours: -3),
|
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
changePassword(old_password: "#{@old_password}", new_password: "#{@old_password}") {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"The new password must be different"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "change_password/3 with new password too short", %{conn: conn} do
|
|
|
|
{:ok, %User{} = user} = Users.register(%{email: @email, password: @old_password})
|
|
|
|
|
|
|
|
# Hammer time !
|
|
|
|
{:ok, %User{} = _user} =
|
|
|
|
Users.update_user(user, %{
|
|
|
|
"confirmed_at" => Timex.shift(user.confirmation_sent_at, hours: -3),
|
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
changePassword(old_password: "#{@old_password}", new_password: "new") {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> auth_conn(user)
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"The password you have chosen is too short. Please make sure your password contains at least 6 characters."
|
|
|
|
end
|
|
|
|
|
|
|
|
test "change_password/3 without being authenticated", %{conn: conn} do
|
|
|
|
{:ok, %User{} = user} = Users.register(%{email: @email, password: @old_password})
|
|
|
|
|
|
|
|
# Hammer time !
|
|
|
|
{:ok, %User{} = _user} =
|
|
|
|
Users.update_user(user, %{
|
|
|
|
"confirmed_at" => Timex.shift(user.confirmation_sent_at, hours: -3),
|
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
})
|
|
|
|
|
|
|
|
mutation = """
|
|
|
|
mutation {
|
|
|
|
changePassword(old_password: "#{@old_password}", new_password: "#{@new_password}") {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
res =
|
|
|
|
conn
|
|
|
|
|> post("/api", AbsintheHelpers.mutation_skeleton(mutation))
|
|
|
|
|
|
|
|
assert hd(json_response(res, 200)["errors"])["message"] ==
|
|
|
|
"You need to be logged-in to change your password"
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|