Skip to content

Commit

Permalink
Add endpoint to update user’s name
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace committed Dec 17, 2024
1 parent da7ffd8 commit 573cfc1
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
7 changes: 7 additions & 0 deletions registrations/lib/registrations/waydowntown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ defmodule Registrations.Waydowntown do
alias Registrations.Waydowntown.Run
alias Registrations.Waydowntown.Specification
alias Registrations.Waydowntown.Submission
alias RegistrationsWeb.User

def update_user(user, attrs) do
user
|> User.name_changeset(attrs)
|> Repo.update()
end

defp concepts_yaml do
ConCache.get_or_store(:registrations_cache, :concepts_yaml, fn ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule RegistrationsWeb.ApiUserController do
use RegistrationsWeb, :controller

alias Registrations.Waydowntown
alias RegistrationsWeb.JSONAPI.UserView

action_fallback(RegistrationsWeb.FallbackController)

def update(conn, %{"id" => id} = params) do
user = Pow.Plug.current_user(conn)

case Waydowntown.update_user(user, params) do
{:ok, updated_user} ->
conn
|> put_view(UserView)
|> render("show.json", data: updated_user, conn: conn, params: params)

{:error, changeset} ->
errors = Ecto.Changeset.traverse_errors(changeset, &RegistrationsWeb.ErrorHelpers.translate_error/1)

conn
|> put_status(:unprocessable_entity)
|> json(%{
errors:
Enum.map(errors, fn {field, message} ->
%{
detail: "#{message}",
source: %{pointer: "/data/attributes/#{field}"}
}
end)
})
end
end
end
8 changes: 8 additions & 0 deletions registrations/lib/registrations_web/models/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ defmodule RegistrationsWeb.User do
|> validate_required(required_fields)
end

def name_changeset(model, params \\ %{}) do
required_fields = ~w(name)a

model
|> cast(params, required_fields)
|> validate_required(required_fields)
end

def voicepass_changeset(model, params \\ %{}) do
required_fields = ~w(voicepass)a

Expand Down
1 change: 1 addition & 0 deletions registrations/lib/registrations_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,6 @@ defmodule RegistrationsWeb.Router do
pipe_through([:pow_json_api_protected])

get("/session", SessionController, :show)
post("/me", ApiUserController, :update)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ defmodule RegistrationsWeb.JSONAPI.UserView do
use JSONAPI.View, type: "users"

def fields do
[:email, :name]
[:admin, :email, :name]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
defmodule RegistrationsWeb.ApiUserControllerTest do
use RegistrationsWeb.ConnCase

alias Registrations.Waydowntown.Region
alias Registrations.Waydowntown.Specification

setup %{conn: conn} do
user = insert(:octavia, admin: true)

auth_conn = build_conn()

auth_conn =
post(auth_conn, Routes.api_session_path(auth_conn, :create), %{
"user" => %{"email" => user.email, "password" => "Xenogenesis"}
})

json = json_response(auth_conn, 200)
authorization_token = json["data"]["access_token"]

%{
user: user,
authorization_token: authorization_token,
conn:
conn
|> put_req_header("accept", "application/vnd.api+json")
|> put_req_header("content-type", "application/vnd.api+json")
}
end

test "updates the user name", %{conn: conn, authorization_token: authorization_token, user: user} do
conn =
conn
|> put_req_header("authorization", "#{authorization_token}")
|> post(Routes.api_user_path(conn, :update), %{
"data" => %{
"type" => "users",
"id" => user.id,
"attributes" => %{
"name" => "Octavia"
}
}
})

assert json_response(conn, 200)["data"]["id"] == user.id
assert json_response(conn, 200)["data"]["attributes"]["admin"]
assert json_response(conn, 200)["data"]["attributes"]["email"] == user.email
assert json_response(conn, 200)["data"]["attributes"]["name"] == "Octavia"
end

test "returns error with invalid attributes", %{
conn: conn,
authorization_token: authorization_token,
user: user
} do
conn =
conn
|> put_req_header("authorization", "#{authorization_token}")
|> post(Routes.api_user_path(conn, :update), %{
"data" => %{
"type" => "users",
"id" => user.id,
"attributes" => %{}
}
})

assert json_response(conn, 422)["errors"] == [
%{"source" => %{"pointer" => "/data/attributes/name"}, "detail" => "can't be blank"}
]
end
end

0 comments on commit 573cfc1

Please sign in to comment.