Commit a64b0034 authored by James Laver's avatar James Laver

Add usernameAvailable graphql query

parent 6acc780e
Pipeline #64841673 passed with stages
in 7 minutes and 16 seconds
...@@ -5,8 +5,9 @@ ...@@ -5,8 +5,9 @@
defmodule MoodleNetWeb.GraphQL.UserResolver do defmodule MoodleNetWeb.GraphQL.UserResolver do
import MoodleNetWeb.GraphQL.MoodleNetSchema import MoodleNetWeb.GraphQL.MoodleNetSchema
alias MoodleNetWeb.GraphQL.Errors
require ActivityPub.Guards, as: APG require ActivityPub.Guards, as: APG
alias MoodleNetWeb.GraphQL.Errors
alias MoodleNet.{Accounts, OAuth, Repo}
def me(_, info) do def me(_, info) do
with {:ok, actor} <- current_actor(info) do with {:ok, actor} <- current_actor(info) do
...@@ -27,8 +28,8 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do ...@@ -27,8 +28,8 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do
def create_user(%{user: attrs}, info) do def create_user(%{user: attrs}, info) do
attrs = attrs |> set_icon() |> set_location() |> set_website() attrs = attrs |> set_icon() |> set_location() |> set_website()
with {:ok, %{actor: actor, user: user}} <- MoodleNet.Accounts.register_user(attrs), with {:ok, %{actor: actor, user: user}} <- Accounts.register_user(attrs),
{:ok, token} <- MoodleNet.OAuth.create_token(user.id) do {:ok, token} <- OAuth.create_token(user.id) do
auth_payload = prepare(:auth_payload, token, actor, info) auth_payload = prepare(:auth_payload, token, actor, info)
{:ok, auth_payload} {:ok, auth_payload}
end end
...@@ -37,7 +38,7 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do ...@@ -37,7 +38,7 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do
def update_profile(%{profile: attrs}, info) do def update_profile(%{profile: attrs}, info) do
with {:ok, current_actor} <- current_actor(info), with {:ok, current_actor} <- current_actor(info),
{:ok, current_actor} <- MoodleNet.Accounts.update_user(current_actor, attrs) do {:ok, current_actor} <- Accounts.update_user(current_actor, attrs) do
user_fields = requested_fields(info, :user) user_fields = requested_fields(info, :user)
current_actor = prepare(:me, current_actor, user_fields) current_actor = prepare(:me, current_actor, user_fields)
{:ok, current_actor} {:ok, current_actor}
...@@ -47,14 +48,14 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do ...@@ -47,14 +48,14 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do
def delete_user(_, info) do def delete_user(_, info) do
with {:ok, current_actor} <- current_actor(info) do with {:ok, current_actor} <- current_actor(info) do
MoodleNet.Accounts.delete_user(current_actor) Accounts.delete_user(current_actor)
{:ok, true} {:ok, true}
end end
end end
def create_session(%{email: email, password: password}, info) do def create_session(%{email: email, password: password}, info) do
with {:ok, user} <- MoodleNet.Accounts.authenticate_by_email_and_pass(email, password), with {:ok, user} <- Accounts.authenticate_by_email_and_pass(email, password),
{:ok, token} <- MoodleNet.OAuth.create_token(user.id) do {:ok, token} <- OAuth.create_token(user.id) do
actor = load_actor(user) actor = load_actor(user)
auth_payload = prepare(:auth_payload, token, actor, info) auth_payload = prepare(:auth_payload, token, actor, info)
{:ok, auth_payload} {:ok, auth_payload}
...@@ -66,26 +67,29 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do ...@@ -66,26 +67,29 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do
def delete_session(_, info) do def delete_session(_, info) do
with {:ok, _} <- current_user(info) do with {:ok, _} <- current_user(info) do
MoodleNet.OAuth.revoke_token(info.context.auth_token) OAuth.revoke_token(info.context.auth_token)
{:ok, true} {:ok, true}
end end
end end
def check_username_available(%{username: username}, _info),
do: {:ok, Accounts.is_username_available?(username)}
def reset_password_request(%{email: email}, _info) do def reset_password_request(%{email: email}, _info) do
# Note: This can be done async, but then, the async tests will fail # Note: This can be done async, but then, the async tests will fail
MoodleNet.Accounts.reset_password_request(email) Accounts.reset_password_request(email)
{:ok, true} {:ok, true}
end end
def reset_password(%{token: token, password: password}, _info) do def reset_password(%{token: token, password: password}, _info) do
with {:ok, _} <- MoodleNet.Accounts.reset_password(token, password) do with {:ok, _} <- Accounts.reset_password(token, password) do
{:ok, true} {:ok, true}
end end
|> Errors.handle_error() |> Errors.handle_error()
end end
def confirm_email(%{token: token}, _info) do def confirm_email(%{token: token}, _info) do
with {:ok, _} <- MoodleNet.Accounts.confirm_email(token) do with {:ok, _} <- Accounts.confirm_email(token) do
{:ok, true} {:ok, true}
end end
|> Errors.handle_error() |> Errors.handle_error()
...@@ -104,4 +108,5 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do ...@@ -104,4 +108,5 @@ defmodule MoodleNetWeb.GraphQL.UserResolver do
|> preload_aspect_cond([:actor_aspect], fields) |> preload_aspect_cond([:actor_aspect], fields)
|> prepare_common_fields() |> prepare_common_fields()
end end
end end
...@@ -21,6 +21,12 @@ defmodule MoodleNetWeb.GraphQL.UserSchema do ...@@ -21,6 +21,12 @@ defmodule MoodleNetWeb.GraphQL.UserSchema do
arg(:local_id, non_null(:integer)) arg(:local_id, non_null(:integer))
resolve(&UserResolver.user/2) resolve(&UserResolver.user/2)
end end
@desc "Check if a user exists with a username"
field :username_available, type: :boolean do
arg(:username, non_null(:string))
resolve(&UserResolver.check_username_available/2)
end
end end
object :user_mutations do object :user_mutations do
......
...@@ -281,6 +281,35 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do ...@@ -281,6 +281,35 @@ defmodule MoodleNetWeb.GraphQL.UserSchemaTest do
assert user["primaryLanguage"] == actor["primary_language"] assert user["primaryLanguage"] == actor["primary_language"]
end end
@tag :user
test "check if a preferred username is taken", %{conn: conn, actor: actor} do
query = """
{
usernameAvailable(username: "jameslaver")
}
"""
assert true ==
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("data")
|> Map.fetch!("usernameAvailable")
query = """
{
usernameAvailable(username: "#{actor.preferred_username}")
}
"""
assert false ==
conn
|> post("/api/graphql", %{query: query})
|> json_response(200)
|> Map.fetch!("data")
|> Map.fetch!("usernameAvailable")
end
@tag :user @tag :user
test "joined_communities connection", %{conn: conn, actor: actor} do test "joined_communities connection", %{conn: conn, actor: actor} do
local_id = local_id(actor) local_id = local_id(actor)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment