Skip to content

Commit d21f8b4

Browse files
npenderybegedin
authored andcommitted
Add /github-connect route and action
- adds route - adds user controller action - adds api docs for action Action - calls placeholder method in github module - responds with user if ok - responds with 422 if code param is bad - responds with 500 if something else goes wrong
1 parent 46d7410 commit d21f8b4

5 files changed

Lines changed: 73 additions & 2 deletions

File tree

blueprint/api.apib

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,32 @@ This endpoint handles the User resources for Code Corps.
22652265

22662266
+ Attributes (Record Not Found Response)
22672267

2268+
### Connect user to GitHub [POST /github_connect]
2269+
2270+
This endpoint allows a user to connect their CodeCorps account to their respective GitHub account.
2271+
2272+
+ Request
2273+
2274+
+ Attributes
2275+
2276+
+ code (string) - Client generated code for authenication when sending request to GitHub.
2277+
2278+
+ Headers
2279+
2280+
Accept: application/vnd.api+json
2281+
2282+
+ Response 201 (application/vnd.api+json; charset=utf-8)
2283+
2284+
+ Attributes (User Response)
2285+
2286+
+ Response 401 (application/vnd.api+json; charset=utf-8)
2287+
2288+
+ Attributes (JSON Web Token Invalid Response)
2289+
2290+
+ Response 422 (application/vnd.api+json; charset=utf-8)
2291+
2292+
+ Attributes (Unprocessable Entity Response)
2293+
22682294
### Check email availability [GET /users/email_available?email={email}]
22692295

22702296
This endpoint allows you to check whether an email is valid (by checking simply for presence of the `@` symbol) and whether the email is not already registered.

lib/code_corps/github.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ defmodule CodeCorps.Github do
22

33
alias CodeCorps.{User, Repo}
44

5+
@doc """
6+
Temporary function until the actual behavior is implemented.
7+
"""
8+
def connect(user, _code), do: {:ok, user}
9+
510
def associate(user, params) do
611
user
712
|> User.github_associate_changeset(params)
813
|> Repo.update()
914
end
10-
end
15+
end

test/controllers/user_controller_test.exs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,34 @@ defmodule CodeCorps.UserControllerTest do
242242
end
243243
end
244244

245+
describe "github_connect" do
246+
test "return the user when current user connects successfully", %{conn: conn} do
247+
user = insert(:user)
248+
249+
code = %{"code" => "client generated code"}
250+
251+
path = user_path(conn, :github_connect, code)
252+
253+
json = conn |> authenticate(user) |> post(path) |> json_response(200)
254+
255+
assert json["data"]["id"] |> String.to_integer == user.id
256+
end
257+
258+
test "return unauthenticated error code when no current user", %{conn: conn} do
259+
code = %{"code" => "client generated code"}
260+
261+
path = user_path(conn, :github_connect, code)
262+
263+
conn |> post(path) |> json_response(401)
264+
end
265+
end
266+
245267
describe "email_available" do
246268
test "returns valid and available when email is valid and available", %{conn: conn} do
247269
resp = get conn, user_path(conn, :email_available, %{email: "[email protected]"})
248270
json = json_response(resp, 200)
249271
assert json["available"]
250272
assert json["valid"]
251-
252273
end
253274

254275
test "returns valid but inavailable when email is valid but taken", %{conn: conn} do

web/controllers/user_controller.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule CodeCorps.UserController do
66

77
alias CodeCorps.User
88
alias CodeCorps.Services.UserService
9+
alias CodeCorps.Github
910

1011
plug :load_and_authorize_resource, model: User, only: [:update]
1112
plug JaResource
@@ -34,6 +35,23 @@ defmodule CodeCorps.UserController do
3435
end
3536
end
3637

38+
def github_connect(conn, code) do
39+
current_user = Guardian.Plug.current_resource(conn)
40+
with {:ok, user} <- Github.connect(current_user, code)
41+
do
42+
conn |> render(:show, data: user)
43+
else
44+
{:error, %Ecto.Changeset{} = changeset} ->
45+
conn
46+
|> put_status(:unprocessable_entity)
47+
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
48+
{:error, _error} ->
49+
conn
50+
|> put_status(:internal_server_error)
51+
|> render(CodeCorps.ErrorView, "500.json-api")
52+
end
53+
end
54+
3755
def email_available(conn, %{"email" => email}) do
3856
hash = User.check_email_availability(email)
3957
conn |> json(hash)

web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ defmodule CodeCorps.Router do
6363
resources "/categories", CategoryController, only: [:create, :update]
6464
resources "/comments", CommentController, only: [:create, :update]
6565
resources "/donation-goals", DonationGoalController, only: [:create, :update, :delete]
66+
post "/github-connect", UserController, :github_connect
6667
resources "/organizations", OrganizationController, only: [:create, :update]
6768
post "/password/reset", PasswordResetController, :reset_password
6869
resources "/previews", PreviewController, only: [:create]

0 commit comments

Comments
 (0)