Commit 0a4da9c1 authored by Alex Castaño's avatar Alex Castaño

Merge branch 'release/0.0.6'

parents fe49e13d a1e8122a
......@@ -247,7 +247,7 @@ defmodule MoodleNet do
if Query.has?(comment, :attributed_to, actor) do
ActivityPub.delete(comment)
else
{:error, "operation not allowed"}
{:error, :forbidden}
end
end
......@@ -292,7 +292,7 @@ defmodule MoodleNet do
defp find_current_relation(subject, relation, object) do
if Query.has?(subject, relation, object),
do: :ok,
else: {:error, "Not found previous activity"}
else: {:error, {:not_found, nil, "Activity"}}
end
defp find_activity(type, actor, object) do
......@@ -303,7 +303,7 @@ defmodule MoodleNet do
|> Query.last()
|> case do
nil ->
{:error, "Not found previous activity"}
{:error, {:not_found, nil, "Activity"}}
activity ->
activity = Query.preload_assoc(activity, actor: {[:actor], []}, object: {[:actor], []})
......
......@@ -121,7 +121,7 @@ defmodule MoodleNet.Accounts do
{:ok, reset_password_token}
else
nil -> {:error, :not_found}
nil -> {:error, {:not_found, email, "User"}}
end
end
......@@ -163,7 +163,7 @@ defmodule MoodleNet.Accounts do
^full_token <- rp_token do
{:ok, ret}
else
_ -> {:error, :not_found}
_ -> {:error, {:not_found, full_token, "Token"}}
end
end
......@@ -190,7 +190,7 @@ defmodule MoodleNet.Accounts do
^full_token <- ec_token do
{:ok, ret}
else
_ -> {:error, :not_found}
_ -> {:error, {:not_found, full_token, "Token"}}
end
end
end
defmodule MoodleNetWeb.GraphQL.Errors do
import MoodleNetWeb.Gettext
def handle_error({:error, _, error, _}),
do: handle_error({:error, error})
def handle_error({:error, %Ecto.Changeset{} = ch}),
do: changeset_error(ch)
def handle_error({:error, :not_found}),
do: not_found_error()
def handle_error({:error, {:not_found, value, type}}),
do: not_found_error(value, type)
def handle_error({:error, :unauthorized}),
do: unauthorized_error()
def handle_error({:error, :forbidden}),
do: forbidden_error()
def handle_error({:error, code}) when is_binary(code) or is_atom(code),
do: unknown_error(code)
def handle_error(ret), do: ret
def changeset_error(%Ecto.Changeset{} = changeset) do
errors =
Enum.map(changeset.errors, fn {field, {msg, opts}} ->
message = changeset_error_msg(msg, opts)
extra =
Map.new(opts)
|> Map.put(:field, field)
%{
code: :validation,
message: message,
extra: extra
}
end)
{:error, errors}
end
defp changeset_error_msg(msg, opts) do
if count = opts[:count] do
Gettext.dngettext(MoodleNetWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(MoodleNetWeb.Gettext, "errors", msg, opts)
end
end
def not_found_error() do
{:error,
%{
message: gettext("not found"),
code: :not_found
}}
end
def not_found_error(value, type) do
human_type = human_type(type)
msg = "#{human_type} #{gettext("not found")}"
{:error,
%{
extra: %{value: value, type: type},
message: msg,
code: :not_found,
}}
end
def invalid_credential_error() do
{:error, %{
code: :unathorized,
message: gettext("Invalid credentials"),
}}
end
def unauthorized_error() do
{:error,
%{
message: gettext("You have to log in to proceed"),
code: :unauthorized
}}
end
def forbidden_error() do
{:error,
%{
message: gettext("You are not authorized to perform this action"),
code: :forbidden
}}
end
def unknown_error(code) do
{:error,
%{
message: gettext("An unknown error happened"),
code: code
}}
end
defp human_type(nil),
do: gettext("Object")
defp human_type("Actor"),
do: gettext("User")
defp human_type("MoodleNet:Community"),
do: gettext("Community")
defp human_type("MoodleNet:Collection"),
do: gettext("Collection")
defp human_type("MoodleNet:EducationalResource"),
do: gettext("Resource")
defp human_type("Note"),
do: gettext("Comment")
defp human_type("Context"),
do: gettext("Context")
defp human_type("Activity"),
do: gettext("Activity")
defp human_type("Token"),
do: gettext("Token")
defp human_type(ret), do: ret
end
......@@ -11,7 +11,7 @@ defmodule MoodleNetWeb.Plugs.SetLocale do
end
defp set_locale(nil), do: nil
defp set_locale(locale), do: Gettext.put_locale(MoodleNetWeb.Gettext, locale)
defp set_locale(locale), do: Gettext.put_locale(locale)
defp locale_from_params(conn) do
conn.params["locale"] |> validate_locale()
......
......@@ -4,7 +4,7 @@ defmodule MoodleNet.Mixfile do
def project do
[
app: :moodle_net,
version: "0.0.5",
version: "0.0.6",
elixir: "~> 1.7.4",
elixirc_paths: elixirc_paths(Mix.env()),
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
......
......@@ -67,3 +67,74 @@ msgstr ""
#: lib/moodle_net_web/email/templates/welcome.html.eex:15
msgid "to tell us anything we can do to improve it!"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:106
msgid "Object"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:115
msgid "Collection"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:121
msgid "Comment"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:112
msgid "Community"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:124
msgid "Context"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:118
msgid "Resource"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:109
msgid "User"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:127
msgid "Activity"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:100
msgid "An unknown error happened"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:77
msgid "Invalid credentials"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:130
msgid "Token"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:92
msgid "You are not authorized to perform this action"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:84
msgid "You have to log in to proceed"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:57
#: lib/moodle_net_web/graph_ql/errors.ex:64
msgid "not found"
msgstr ""
......@@ -15,3 +15,127 @@ msgstr ""
#: lib/moodle_net_web/page/templates/index.html.eex:2
msgid "pages.index.welcome"
msgstr "Welcome to %{name}"
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:9
msgid "Alternatively, copy and paste this link into your browser:"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:14
msgid "Click here"
msgstr ""
#, elixir-format
#: lib/moodle_net/email.ex:16
msgid "Did you forget your MoodleNet password?"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:1
#: lib/moodle_net_web/email/templates/welcome.text.eex:1
msgid "Hi %{name}"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:3
#: lib/moodle_net_web/email/templates/welcome.text.eex:3
msgid "To complete your sign up, please verify your email:"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:6
msgid "Verify email"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:12
#: lib/moodle_net_web/email/templates/welcome.text.eex:9
msgid "We'd love your feedback on the sign-up process."
msgstr ""
#, elixir-format
#: lib/moodle_net/email.ex:8
msgid "Welcome to MoodleNet"
msgstr ""
#, elixir-format
#: lib/moodle_net/email.ex:23
msgid "Your MoodleNet password has been reset"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:15
msgid "to tell us anything we can do to improve it!"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:106
msgid "Object"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:115
msgid "Collection"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:121
msgid "Comment"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:112
msgid "Community"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:124
msgid "Context"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:118
msgid "Resource"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:109
msgid "User"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:127
msgid "Activity"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:100
msgid "An unknown error happened"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:77
msgid "Invalid credentials"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:130
msgid "Token"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:92
msgid "You are not authorized to perform this action"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:84
msgid "You have to log in to proceed"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:57
#: lib/moodle_net_web/graph_ql/errors.ex:64
msgid "not found"
msgstr ""
......@@ -15,3 +15,127 @@ msgstr ""
#: lib/moodle_net_web/page/templates/index.html.eex:2
msgid "pages.index.welcome"
msgstr "Bienvenido a %{name}"
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:9
msgid "Alternatively, copy and paste this link into your browser:"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:14
msgid "Click here"
msgstr ""
#, elixir-format
#: lib/moodle_net/email.ex:16
msgid "Did you forget your MoodleNet password?"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:1
#: lib/moodle_net_web/email/templates/welcome.text.eex:1
msgid "Hi %{name}"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:3
#: lib/moodle_net_web/email/templates/welcome.text.eex:3
msgid "To complete your sign up, please verify your email:"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:6
msgid "Verify email"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:12
#: lib/moodle_net_web/email/templates/welcome.text.eex:9
msgid "We'd love your feedback on the sign-up process."
msgstr ""
#, elixir-format
#: lib/moodle_net/email.ex:8
msgid "Welcome to MoodleNet"
msgstr ""
#, elixir-format
#: lib/moodle_net/email.ex:23
msgid "Your MoodleNet password has been reset"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/email/templates/welcome.html.eex:15
msgid "to tell us anything we can do to improve it!"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:106
msgid "Object"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:115
msgid "Collection"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:121
msgid "Comment"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:112
msgid "Community"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:124
msgid "Context"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:118
msgid "Resource"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:109
msgid "User"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:127
msgid "Activity"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:100
msgid "An unknown error happened"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:77
msgid "Invalid credentials"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:130
msgid "Token"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:92
msgid "You are not authorized to perform this action"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:84
msgid "You have to log in to proceed"
msgstr ""
#, elixir-format
#: lib/moodle_net_web/graph_ql/errors.ex:57
#: lib/moodle_net_web/graph_ql/errors.ex:64
msgid "not found"
msgstr ""
......@@ -83,7 +83,7 @@ defmodule MoodleNet.AccountsTest do
end
test "returns error if email not found" do
assert {:error, :not_found} = Accounts.reset_password_request("not_found")
assert {:error, {:not_found, "not_found", "User"}} = Accounts.reset_password_request("not_found")
end
end
......@@ -117,18 +117,18 @@ defmodule MoodleNet.AccountsTest do
|> NaiveDateTime.add(-@three_days)
token = Repo.insert!(%ResetPasswordToken{token: token, user_id: user.id, inserted_at: date})
assert {:error, :not_found} = Accounts.reset_password(token.token, "new_password")
assert {:error, {:not_found, _, "Token"}} = Accounts.reset_password(token.token, "new_password")
end
test "returns error if token not found" do
assert {:error, :not_found} = Accounts.reset_password("1234", "new_password")
assert {:error, {:not_found, _, "Token"}} = Accounts.reset_password("1234", "new_password")
user = Factory.user()
token = MoodleNet.Token.random_key_with_id(user.id)
assert {:error, :not_found} = Accounts.reset_password(token, "new_password")
assert {:error, {:not_found, _, "Token"}} = Accounts.reset_password(token, "new_password")
assert {:ok, %{token: token}} = Accounts.reset_password_request(user.email)
assert {:error, :not_found} = Accounts.reset_password(token <> "1", "new_password")
assert {:error, {:not_found, _, "Token"}} = Accounts.reset_password(token <> "1", "new_password")
end
end
......@@ -142,11 +142,11 @@ defmodule MoodleNet.AccountsTest do
end
test "returns error if token not found" do
assert {:error, :not_found} = Accounts.confirm_email("1234")
assert {:error, {:not_found, "1234", "Token"}} = Accounts.confirm_email("1234")
%{user: user, email_confirmation_token: %{token: token}} = Factory.full_user()
assert {:error, :not_found} = Accounts.confirm_email(MoodleNet.Token.random_key_with_id(user.id))
assert {:error, :not_found} = Accounts.confirm_email(token <> "1")
assert {:error, {:not_found, _, "Token"}} = Accounts.confirm_email(MoodleNet.Token.random_key_with_id(user.id))
assert {:error, {:not_found, _, "Token"}} = Accounts.confirm_email(token <> "1")
end
end
end
......@@ -7,6 +7,50 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchemaTest do
import ActivityPub.Entity, only: [local_id: 1]
@moduletag format: :json
test "createUser errors", %{conn: conn} do
query = """
mutation {
createUser(
user: {
preferredUsername: "alexcastano"
name: "Alejandro Castaño"
summary: "Summary"
location: "MoodleNet"
icon: "https://imag.es/alexcastano"
email: "alexcastano@newworld.com"
password: "short"
primaryLanguage: "Elixir"
}
) {
token
me {
id
}
}
}
"""
assert [error] =
conn
|> Plug.Conn.put_req_header("accept-language", "es")
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("errors")
assert %{
"extra" => %{
"count" => 6,
"kind" => "min",
"validation" => "length",
"field" => "password"
},
"code" => "validation",
"locations" => [%{"column" => 0, "line" => 2}],
"message" => "should be at least 6 character(s)",
"path" => ["createUser"]
} = error
end
test "confirm email", %{conn: conn} do
query = """
mutation {
......@@ -14,13 +58,17 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchemaTest do
}
"""
assert "not_found" =
assert [
%{
"code" => "not_found",
"extra" => %{"type" => "Token", "value" => "not_real_token"},
"message" => "Token not found"
}
] =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("errors")
|> hd()
|> Map.fetch!("message")
%{email_confirmation_token: %{token: token}} = Factory.full_user()
......@@ -88,19 +136,23 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchemaTest do
query = """
mutation {
resetPassword(
token: "invalid_token"
token: "not_real_token"
password: "new_password"
)
}
"""
assert "not_found" =
assert [
%{
"code" => "not_found",
"extra" => %{"type" => "Token", "value" => "not_real_token"},
"message" => "Token not found"
}
] =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("errors")
|> hd()
|> Map.fetch!("message")
end
@tag :user
......@@ -244,13 +296,16 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchemaTest do
}
"""
assert "You are not logged in" ==
assert [
%{
"code" => "unauthorized",
"message" => "You have to log in to proceed"
}
] =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("errors")
|> hd()
|> Map.fetch!("message")
query = """
{
......@@ -507,13 +562,16 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchemaTest do
}
"""
assert "operation not allowed" ==
assert [
%{
"code" => "forbidden",
"message" => "You are not authorized to perform this action"
}
] =
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("errors")
|> hd()
|> Map.fetch!("message")
query = """
{
......@@ -567,13 +625,16 @@ defmodule MoodleNetWeb.GraphQL.MoodleNetSchemaTest do
|> Map.fetch!("data")
|> Map.fetch!("deleteSession")
assert "You are not logged in" ==
assert [
%{
"code" => "unauthorized",
"message" => "You have to log in to proceed"
}