Skip to content

Commit

Permalink
Merge pull request #36 from dwyl/export-testdouble-issue#35
Browse files Browse the repository at this point in the history
Export TestDouble issue #35
  • Loading branch information
SimonLab authored Apr 3, 2020
2 parents 6475166 + 2c653ee commit 1ea331e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 43 deletions.
2 changes: 0 additions & 2 deletions .env_sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export GOOGLE_CLIENT_ID=YourAppsClientId.apps.googleusercontent.com
export GOOGLE_CLIENT_SECRET=SuperSecret
export SECRET_KEY_BASE=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx
export ENCRYPTION_KEYS='nMdayQpR0aoasLaq1g94FLba+A+wB44JLko47sVQXMg=,L+ZVX8iheoqgqb22mUpATmMDsvVGtafoAeb0KN5uWf0='
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ There were already _several_ available options
for adding Google Auth to apps on
[hex.pm/packages?search=google](https://hex.pm/packages?search=google) <br />
that all added _far_ too many implementation steps (complexity)
and had incomplete documentation (**`@doc false`**) and testing. <br />
and had incomplete docs (**`@doc false`**) and tests. <br />
e.g:
[github.com/googleapis/elixir-google-api](https://github.com/googleapis/elixir-google-api)
which is a
Expand Down
5 changes: 0 additions & 5 deletions config/config.exs

This file was deleted.

14 changes: 10 additions & 4 deletions lib/elixir_auth_google.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ defmodule ElixirAuthGoogle do
Minimalist Google OAuth Authentication for Elixir Apps.
Extensively tested, documented, maintained and in active use in production.
"""
@httpoison Application.get_env(:elixir_auth_google, :httpoison) || HTTPoison
@google_auth_url "https://accounts.google.com/o/oauth2/v2/auth?response_type=code"
@google_token_url "https://oauth2.googleapis.com/token"
@google_user_profile "https://www.googleapis.com/oauth2/v3/userinfo"

@doc """
`inject_poison/0` injects a TestDouble of HTTPoison in Test
so that we don't have duplicate mock in consuming apps.
see: https://github.com/dwyl/elixir-auth-google/issues/35
"""
def inject_poison() do
Mix.env() == :test && ElixirAuthGoogle.HTTPoisonMock || HTTPoison
end

@doc """
`get_baseurl_from_conn/1` derives the base URL from the conn struct
Expand Down Expand Up @@ -67,8 +74,7 @@ defmodule ElixirAuthGoogle do
grant_type: "authorization_code",
code: code
})

@httpoison.post(@google_token_url, body)
inject_poison().post(@google_token_url, body)
|> parse_body_response()
end

Expand All @@ -83,7 +89,7 @@ defmodule ElixirAuthGoogle do
@spec get_user_profile(String.t) :: String.t
def get_user_profile(token) do
"#{@google_user_profile}?access_token=#{token}"
|> @httpoison.get()
|> inject_poison().get()
|> parse_body_response()
end

Expand Down
27 changes: 0 additions & 27 deletions lib/httpoison/in_memory.ex

This file was deleted.

39 changes: 39 additions & 0 deletions lib/httpoison_mock.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule ElixirAuthGoogle.HTTPoisonMock do
@moduledoc """
This is a TestDouble for HTTPoison which returns a predictable response.
Please see: https://github.com/dwyl/elixir-auth-google/issues/35
"""

@doc """
get/1 passing in the wrong_token is used to test failure in the auth process.
Obviously, don't invoke it from your App unless you want people to see fails.
"""
def get("https://www.googleapis.com/oauth2/v3/userinfo?access_token=wrong_token") do
{:error, :bad_request}
end

@doc """
get/1 using a dummy _url to test body decoding.
"""
def get(_url) do
{:ok, %{body: Poison.encode!(
%{
email: "nelson@gmail.com",
email_verified: true,
family_name: "Correia",
given_name: "Nelson",
locale: "en",
name: "Nelson Correia",
picture: "https://lh3.googleusercontent.com/a-/AAuE7mApnYb260YC1JY7a",
sub: "940732358705212133793"
}
)}}
end

@doc """
post/2 passing in dummy _url & _body to test return of access_token.
"""
def post(_url, _body) do
{:ok, %{body: Poison.encode!(%{access_token: "token1"})}}
end
end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ElixirAuthGoogle.MixProject do
use Mix.Project

@description "Minimalist Google OAuth Authentication for Elixir Apps"
@version "1.1.0"
@version "1.2.0"

def project do
[
Expand Down Expand Up @@ -50,7 +50,7 @@ defmodule ElixirAuthGoogle.MixProject do
maintainers: ["dwyl"],
licenses: ["GNU GPL v2.0"],
links: %{github: "https://github.com/dwyl/elixir-auth-google"},
files: ~w(lib LICENSE mix.exs README.md .formatter.exs config/config.exs)
files: ~w(lib LICENSE mix.exs README.md .formatter.exs)
]
end
end
18 changes: 16 additions & 2 deletions test/elixir_auth_google_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ defmodule ElixirAuthGoogleTest do
host: "localhost",
port: 4000
}
assert ElixirAuthGoogle.generate_oauth_url(conn, "state1") == "https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=&scope=profile email&redirect_uri=http://localhost:4000/auth/google/callback&state=state1"
url = ElixirAuthGoogle.generate_oauth_url(conn, "state1")
id = System.get_env("GOOGLE_CLIENT_ID")
expected = "https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=" <> id
<> "&scope=profile email&redirect_uri=http://localhost:4000/auth/google/callback&state=state1"
assert url == expected
end

test "get Google token" do
Expand All @@ -47,7 +51,17 @@ defmodule ElixirAuthGoogleTest do
end

test "get_user_profile/1" do
assert ElixirAuthGoogle.get_user_profile("123") == {:ok, %{name: "dwyl"}}
res = %{
email: "nelson@gmail.com",
email_verified: true,
family_name: "Correia",
given_name: "Nelson",
locale: "en",
name: "Nelson Correia",
picture: "https://lh3.googleusercontent.com/a-/AAuE7mApnYb260YC1JY7a",
sub: "940732358705212133793"
}
assert ElixirAuthGoogle.get_user_profile("123") == {:ok, res}
end

test "return error with incorrect token" do
Expand Down

0 comments on commit 1ea331e

Please sign in to comment.