-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
registrations/lib/registrations_web/controllers/api/user_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
registrations/test/registrations_web/controllers/api_user_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |