Skip to content

Commit

Permalink
Added untested API call for github connect.
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed May 15, 2017
1 parent b790c7b commit a7f6ca2
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 156 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export CLOUDEX_API_KEY=
export CLOUDEX_CLOUD_NAME=
export CLOUDEX_SECRET=
export CLOUDFRONT_DOMAIN=
export GITHUB_CLIENT_ID=
export GITHUB_CLIENT_SECRET=
export POSTMARK_API_KEY=
export S3_BUCKET=
export SEGMENT_WRITE_KEY=
Expand Down
6 changes: 6 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ config :guardian, Guardian,

config :code_corps, :analytics, CodeCorps.Analytics.InMemoryAPI

config :code_corps, :github_api, CodeCorps.Github.API

# Configures stripe for dev mode
config :code_corps, :stripe, Stripe
config :code_corps, :stripe_env, :dev
Expand All @@ -70,3 +72,7 @@ if System.get_env("CLOUDEX_API_KEY") == nil do
config :code_corps, :cloudex, CloudexTest
config :cloudex, api_key: "test_key", secret: "test_secret", cloud_name: "test_cloud_name"
end

config :code_corps,
github_client_id: System.get_env("GITHUB_CLIENT_ID"),
github_client_secret: System.get_env("GITHUB_CLIENT_SECRET")
4 changes: 4 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ config :code_corps,
postmark_project_acceptance_template: "1447041",
postmark_receipt_template: "1255222"

config :code_corps,
github_client_id: System.get_env("GITHUB_CLIENT_ID"),
github_client_secret: System.get_env("GITHUB_CLIENT_SECRET")

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down
4 changes: 4 additions & 0 deletions config/remote-development.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ config :code_corps,
postmark_project_acceptance_template: "123",
postmark_receipt_template: "123"

config :code_corps,
github_client_id: System.get_env("GITHUB_CLIENT_ID"),
github_client_secret: System.get_env("GITHUB_CLIENT_SECRET")

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down
4 changes: 4 additions & 0 deletions config/staging.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ config :code_corps,
postmark_project_acceptance_template: "1447022",
postmark_receipt_template: "1252361"

config :code_corps,
github_client_id: System.get_env("GITHUB_CLIENT_ID"),
github_client_secret: System.get_env("GITHUB_CLIENT_SECRET")

# ## SSL Support
#
# To get SSL working, you will need to add the `https` key
Expand Down
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ config :guardian, Guardian,

config :code_corps, :analytics, CodeCorps.Analytics.TestAPI

config :code_corps, :github_api, CodeCorps.Github.TestAPI

# Configures stripe for test mode
config :code_corps, :stripe, CodeCorps.StripeTesting
config :code_corps, :stripe_env, :test
Expand Down
43 changes: 19 additions & 24 deletions lib/code_corps/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,33 @@ defmodule CodeCorps.Github do

alias CodeCorps.{User, Repo}

@token_url ""
@client_id ""
@client_secret ""
@api Application.get_env(:code_corps, :github_api)

@doc """
Temporary function until the actual behavior is implemented.
Posts code to github to receive an auth token, associates user with that
auth token.
Returns one of the following:
- {:ok, %CodeCorps.User{}}
- {:error, %Ecto.Changeset{}}
- {:error, "some_github_error"}
"""
def connect(user, code) do
github_post(code)
|> Tentacat.Client.new
|> Tentacat.Users.me
|> update_user(user)
case code |> @api.connect do
{:ok, github_auth_token} -> user |> associate(%{github_auth_token: github_auth_token})
{:error, error} -> {:error, error}
end
end

defp update_user(%{github_id: _github_id} = github_info, user) do
associate(user, github_info)
end
@doc """
Associates user with an auth token
defp github_post(code) do
with {:ok, response_struct} <- Tentacat.post(@token_url, %{
client_id: @client_id,
client_secret: @client_secret,
code: code,
accept: :json
})
do
%{access_token: _access_token} = Tentacat.process_response(response_struct)
else
{:error, error_struct} -> error_struct
end
end
Returns one of the following:
- {:ok, %CodeCorps.User{}}
- {:error, %Ecto.Changeset{}}
"""
def associate(user, params) do
user
|> User.github_associate_changeset(params)
Expand Down
28 changes: 28 additions & 0 deletions lib/code_corps/github/api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule CodeCorps.Github.API do
@client_secret Application.get_env(:code_corps, :github_client_secret)
@client_id Application.get_env(:code_corps, :github_client_id)

@base_connect_params %{
client_id: @client_id,
client_secret: @client_secret
}

def connect(code) do
with {:ok, %HTTPoison.Response{body: response}} <- code |> build_connect_params() |> do_connect(),
{:ok, %{"access_token" => access_token}} <- response |> Poison.decode
do
{:ok, access_token}
else
{:ok, %{"error" => error}} -> {:error, error}
end
end

@connect_url "https://github.com/login/oauth/access_token"

defp do_connect(params) do
HTTPoison.post(@connect_url, "", [{"Accept", "application/json"}], [params: params])
end

defp build_connect_params(code),
do: @base_connect_params |> Map.put(:code, code)
end
4 changes: 4 additions & 0 deletions lib/code_corps/github/test_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule CodeCorps.Github.TestAPI do
def connect("valid_code"), do: {:ok, "test_auth_token"}
def connect("bad_code"), do: {:error, "bad_verification_code"}
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule CodeCorps.Repo.Migrations.RenameUserGithubIdToGithubAuthToken do
use Ecto.Migration

def change do
rename table(:users), :github_id, to: :github_auth_token
end
end
Loading

0 comments on commit a7f6ca2

Please sign in to comment.