Skip to content

Commit 10a24cc

Browse files
committed
Use alias and refactor test methods
1 parent cf73098 commit 10a24cc

File tree

9 files changed

+69
-61
lines changed

9 files changed

+69
-61
lines changed

lib/mix/tasks/phoenix_oauth2_provider.install.ex

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Install do
22
use Mix.Task
33

44
alias PhoenixOauth2Provider.Mix.Utils
5+
alias Mix.Tasks.ExOauth2Provider.Install, as: ExOAuth2ProviderInstall
6+
alias Mix.{Phoenix, Project}
57
require Logger
68

79
@shortdoc "Configure the PhoenixOauth2Provider Package"
@@ -160,7 +162,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Install do
160162
defp install_ex_oauth2_provider(%{provider: true, repo: _repo} = config) do
161163
config
162164
|> install_ex_oauth2_provider_task_args()
163-
|> Mix.Tasks.ExOauth2Provider.Install.run()
165+
|> ExOAuth2ProviderInstall.run()
164166

165167
config
166168
end
@@ -203,7 +205,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Install do
203205
source = "priv/boilerplate"
204206
mapping = [{:eex, "phoenix_oauth2_provider_web.ex", Utils.web_path("phoenix_oauth2_provider_web.ex")}]
205207

206-
Mix.Phoenix.copy_from(@apps, source, binding, mapping)
208+
Phoenix.copy_from(@apps, source, binding, mapping)
207209

208210
config
209211
end
@@ -228,7 +230,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Install do
228230
|> Enum.map(&(elem(&1, 1)))
229231
|> Enum.map(&({:eex, &1, Utils.web_path("views/phoenix_oauth2_provider/#{&1}")}))
230232

231-
Mix.Phoenix.copy_from(@apps, source, binding, mapping)
233+
Phoenix.copy_from(@apps, source, binding, mapping)
232234

233235
config
234236
end
@@ -260,7 +262,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Install do
260262
source = "priv/boilerplate/templates/#{name}"
261263
mapping = copy_templates_files(name, file_list)
262264

263-
Mix.Phoenix.copy_from(@apps, source, binding, mapping)
265+
Phoenix.copy_from(@apps, source, binding, mapping)
264266
end
265267

266268
defp copy_templates_files(name, file_list) do
@@ -287,7 +289,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Install do
287289
|> Enum.map(&(elem(&1, 1)))
288290
|> Enum.map(&({:text, &1, Utils.web_path("controllers/phoenix_oauth2_provider/#{&1}")}))
289291

290-
Mix.Phoenix.copy_from(@apps, source, binding, mapping)
292+
Phoenix.copy_from(@apps, source, binding, mapping)
291293
Enum.each(mapping, &update_controller_file_with_base_module!(&1, base))
292294

293295
config
@@ -379,10 +381,10 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.Install do
379381
do_config({bin_opts, opts})
380382
end
381383
defp do_config({bin_opts, opts}) do
382-
binding = Mix.Project.config()
384+
binding = Project.config()
383385
|> Keyword.fetch!(:app)
384386
|> Atom.to_string()
385-
|> Mix.Phoenix.inflect()
387+
|> Phoenix.inflect()
386388

387389
base = opts[:module] || binding[:base]
388390
opts = Keyword.put(opts, :base, base)

lib/mix/utils.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule PhoenixOauth2Provider.Mix.Utils do
22
@moduledoc false
33

4+
alias Mix.Phoenix
5+
46
@spec raise_option_errors([atom()]) :: no_return
57
def raise_option_errors(list) do
68
list
@@ -46,7 +48,7 @@ defmodule PhoenixOauth2Provider.Mix.Utils do
4648
@spec web_path(Path.t()) :: binary()
4749
def web_path(path), do: Path.join(get_web_prefix(), path)
4850

49-
defp get_web_prefix, do: Mix.Phoenix.web_path(Mix.Phoenix.otp_app())
51+
defp get_web_prefix, do: Phoenix.web_path(Phoenix.otp_app())
5052

5153
@spec list_to_existing_atoms([binary()]) :: [atom()]
5254
def list_to_existing_atoms(list) do

lib/phoenix_oauth2_provider.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule PhoenixOauth2Provider do
1212
[ex_oauth2_provider](https://github.com/danschultzer/ex_oauth2_provider)
1313
library.
1414
"""
15+
alias Mix.Phoenix
1516

1617
@doc """
1718
Will get current resource owner from endpoint
@@ -43,7 +44,7 @@ defmodule PhoenixOauth2Provider do
4344

4445
defp web_module do
4546
config()
46-
|> Keyword.get(:module, Mix.Phoenix.base())
47+
|> Keyword.get(:module, Phoenix.base())
4748
|> Kernel.to_string()
4849
|> Kernel.<>("Web")
4950
end
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
defmodule PhoenixOauth2Provider.AuthorizedApplicationController do
22
@moduledoc false
33
use PhoenixOauth2Provider.Web, :controller
4-
54
alias ExOauth2Provider.OauthApplications
6-
import PhoenixOauth2Provider
75

86
def index(conn, _params) do
9-
applications = OauthApplications.get_authorized_applications_for(current_resource_owner(conn))
10-
render(conn, "index.html", applications: applications)
7+
applications = conn
8+
|> PhoenixOauth2Provider.current_resource_owner()
9+
|> OauthApplications.get_authorized_applications_for()
10+
11+
render(conn, "index.html", applications: applications)
1112
end
1213

1314
def delete(conn, %{"uid" => uid}) do
1415
application = OauthApplications.get_application!(uid)
15-
{:ok, _application} = OauthApplications.revoke_all_access_tokens_for(application, current_resource_owner(conn))
16+
{:ok, _application} = OauthApplications.revoke_all_access_tokens_for(application, PhoenixOauth2Provider.current_resource_owner(conn))
1617

1718
conn
1819
|> put_flash(:info, "Application revoked.")
19-
|> redirect(to: router_helpers().oauth_authorized_application_path(conn, :index))
20+
|> redirect(to: PhoenixOauth2Provider.router_helpers().oauth_authorized_application_path(conn, :index))
2021
end
2122
end

test/controllers/authorization_controller_test.exs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
defmodule PhoenixOauth2Provider.AuthorizationControllerTest do
22
use PhoenixOauth2Provider.Test.ConnCase
3-
alias ExOauth2Provider.OauthApplications
3+
alias ExOauth2Provider.{OauthAccessGrants.OauthAccessGrant,
4+
OauthApplications,
5+
Scopes}
46
alias PhoenixOauth2Provider.Test.Fixtures
57

6-
def valid_request(%OauthApplications.OauthApplication{} = application) do
7-
%{client_id: application.uid, response_type: "code"}
8-
end
9-
10-
def last_grant do
11-
ExOauth2Provider.OauthAccessGrants.OauthAccessGrant
12-
|> ExOauth2Provider.repo.all()
13-
|> List.last()
14-
end
15-
16-
def last_grant_token do
17-
last_grant().token
18-
end
19-
208
setup %{conn: conn} do
219
user = Fixtures.user()
2210
conn = assign conn, :current_test_user, user
@@ -32,7 +20,7 @@ defmodule PhoenixOauth2Provider.AuthorizationControllerTest do
3220
assert body =~ "Authorize <strong>#{application.name}</strong> to use your account?"
3321
assert body =~ application.name
3422
application.scopes
35-
|> ExOauth2Provider.Scopes.to_list
23+
|> Scopes.to_list()
3624
|> Enum.each(fn(scope) ->
3725
assert body =~ "<li>#{scope}</li>"
3826
end)
@@ -100,4 +88,14 @@ defmodule PhoenixOauth2Provider.AuthorizationControllerTest do
10088
assert "The resource owner or authorization server denied the request." == body["error_description"]
10189
end
10290
end
91+
92+
defp valid_request(%{uid: uid}), do: %{client_id: uid, response_type: "code"}
93+
94+
defp last_grant do
95+
OauthAccessGrant
96+
|> ExOauth2Provider.repo().all()
97+
|> List.last()
98+
end
99+
100+
defp last_grant_token, do: last_grant().token
103101
end

test/controllers/token_controller_test.exs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
defmodule PhoenixOauth2Provider.TokenControllerTest do
22
use PhoenixOauth2Provider.Test.ConnCase
33
alias PhoenixOauth2Provider.Test.Fixtures
4-
5-
def last_access_token do
6-
ExOauth2Provider.OauthAccessTokens.OauthAccessToken
7-
|> ExOauth2Provider.repo.all()
8-
|> List.last()
9-
|> Map.get(:token)
10-
end
4+
alias ExOauth2Provider.{OauthAccessTokens,
5+
OauthAccessTokens.OauthAccessToken}
116

127
setup %{conn: conn} do
138
application = Fixtures.application(%{user: Fixtures.user()})
@@ -107,7 +102,7 @@ defmodule PhoenixOauth2Provider.TokenControllerTest do
107102
conn = post conn, Routes.oauth_token_path(conn, :revoke, request)
108103
body = json_response(conn, 200)
109104
assert body == %{}
110-
assert ExOauth2Provider.OauthAccessTokens.is_revoked?(last_access_token())
105+
assert OauthAccessTokens.is_revoked?(last_access_token())
111106
end
112107

113108
test "revoke/2 with invalid token", %{conn: conn, request: request} do
@@ -116,4 +111,11 @@ defmodule PhoenixOauth2Provider.TokenControllerTest do
116111
assert body == %{}
117112
end
118113
end
114+
115+
defp last_access_token do
116+
OauthAccessToken
117+
|> ExOauth2Provider.repo.all()
118+
|> List.last()
119+
|> Map.get(:token)
120+
end
119121
end

test/mix/tasks/phoenix_oauth2_provider.install_test.exs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Mix.shell(Mix.Shell.Process)
55
defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
66
use ExUnit.Case
77
alias PhoenixOauth2Provider.Test.MixHelpers
8+
alias Mix.Tasks.PhoenixOauth2Provider.Install
89

910
defmodule MigrationsRepo do
1011
def __adapter__ do
@@ -24,8 +25,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
2425

2526
test "generates files for application" do
2627
MixHelpers.in_tmp "generates_files_for_application", fn ->
27-
~w(--repo PhoenixOauth2Provider.Test.Repo --log-only --controllers --module PhoenixOauth2Provider.Test --no-provider)
28-
|> Mix.Tasks.PhoenixOauth2Provider.Install.run()
28+
Install.run(~w(--repo PhoenixOauth2Provider.Test.Repo --log-only --controllers --module PhoenixOauth2Provider.Test --no-provider))
2929

3030
~w(application_view.ex authorization_view.ex authorized_application_view.ex phoenix_oauth2_provider_view.ex layout_view.ex phoenix_oauth2_provider_view_helpers.ex)
3131
|> MixHelpers.assert_file_list(@all_views, web_path("views/phoenix_oauth2_provider/"))
@@ -44,8 +44,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
4444

4545
test "does not generate files for full" do
4646
MixHelpers.in_tmp "does_not_generate_files_for_full", fn ->
47-
~w(--repo PhoenixOauth2Provider.Test.Repo --full --log-only --no-boilerplate --no-provider)
48-
|> Mix.Tasks.PhoenixOauth2Provider.Install.run()
47+
Install.run(~w(--repo PhoenixOauth2Provider.Test.Repo --full --log-only --no-boilerplate --no-provider))
4948

5049
MixHelpers.assert_file_list([], @all_views, web_path("views/phoenix_oauth2_provider/"))
5150

@@ -59,8 +58,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
5958
MixHelpers.in_tmp "installs_phoenix_oauth2_provider_config", fn ->
6059
file_path = "config.exs"
6160
File.touch!(file_path)
62-
~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --no-migrations --config-file #{File.cwd!}/#{file_path})
63-
|> Mix.Tasks.PhoenixOauth2Provider.Install.run()
61+
Install.run(~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --no-migrations --config-file #{File.cwd!}/#{file_path}))
6462

6563
MixHelpers.assert_file file_path, fn file ->
6664
assert file =~ "config :phoenix_oauth2_provider, PhoenixOauth2Provider"
@@ -77,8 +75,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
7775

7876
test "instructions" do
7977
MixHelpers.in_tmp "prints_instructions", fn ->
80-
~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --no-migrations --no-config)
81-
|> Mix.Tasks.PhoenixOauth2Provider.Install.run()
78+
Install.run(~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --no-migrations --no-config))
8279

8380
assert_received {:mix_shell, :info, [
8481
"""
@@ -124,8 +121,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
124121
"""
125122
]}
126123

127-
~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --controllers --no-migrations --no-config)
128-
|> Mix.Tasks.PhoenixOauth2Provider.Install.run()
124+
Install.run(~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --controllers --no-migrations --no-config))
129125

130126
assert_received {:mix_shell, :info, [
131127
"""
@@ -162,8 +158,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
162158

163159
describe "installs ex_oauth2_provider" do
164160
test "adds migrations" do
165-
~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --no-config --repo #{to_string MigrationsRepo})
166-
|> Mix.Tasks.PhoenixOauth2Provider.Install.run()
161+
Install.run(~w(--repo PhoenixOauth2Provider.Test.Repo --no-boilerplate --no-config --repo #{to_string MigrationsRepo}))
167162

168163
assert [_] = MixHelpers.tmp_path() |> Path.join("migrations/*_create_oauth_tables.exs") |> Path.wildcard()
169164
assert_received {:mix_shell, :info, [
@@ -178,8 +173,7 @@ defmodule Mix.Tasks.PhoenixOauth2Provider.InstallTest do
178173
describe "installed options" do
179174
test "install options default" do
180175
Application.put_env :phoenix_oauth2_provider, :opts, [:application]
181-
~w(--repo PhoenixOauth2Provider.Test.Repo --installed-options --no-provider)
182-
|> Mix.Tasks.PhoenixOauth2Provider.Install.run()
176+
Install.run(~w(--repo PhoenixOauth2Provider.Test.Repo --installed-options --no-provider))
183177

184178
assert_received {:mix_shell, :info, ["mix phoenix_oauth2_provider.install --application"]}
185179
end

test/support/conn_case.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,28 @@ defmodule PhoenixOauth2Provider.Test.ConnCase do
1414
"""
1515
use ExUnit.CaseTemplate
1616

17+
alias Ecto.Adapters.SQL.Sandbox
18+
alias PhoenixOauth2Provider.Test.{Endpoint,
19+
ErrorView,
20+
Repo,
21+
Router.Helpers}
22+
alias Phoenix.ConnTest
23+
1724
using do
1825
quote do
1926
# Import conveniences for testing with connections
20-
use Phoenix.ConnTest
21-
import PhoenixOauth2Provider.Test.ErrorView
22-
alias PhoenixOauth2Provider.Test.Router.Helpers, as: Routes
27+
use ConnTest
28+
import ErrorView
29+
alias Helpers, as: Routes
2330

24-
@endpoint PhoenixOauth2Provider.Test.Endpoint
31+
@endpoint Endpoint
2532
end
2633
end
2734

2835
setup tags do
2936
unless tags[:async] do
30-
:ok = Ecto.Adapters.SQL.Sandbox.checkout(PhoenixOauth2Provider.Test.Repo)
37+
:ok = Sandbox.checkout(Repo)
3138
end
32-
{:ok, conn: Phoenix.ConnTest.build_conn()}
39+
{:ok, conn: ConnTest.build_conn()}
3340
end
3441
end

test/support/fixtures.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ defmodule PhoenixOauth2Provider.Test.Fixtures do
22
@moduledoc false
33

44
alias PhoenixOauth2Provider.Test.{Repo, User}
5-
alias ExOauth2Provider.{OauthAccessTokens,
5+
alias ExOauth2Provider.{Config,
6+
OauthAccessTokens,
67
OauthAccessTokens.OauthAccessToken,
78
OauthAccessGrants,
89
OauthAccessGrants.OauthAccessGrant,
@@ -38,7 +39,7 @@ defmodule PhoenixOauth2Provider.Test.Fixtures do
3839
@spec access_grant(map()) :: OauthAccessGrant.t()
3940
def access_grant(%{application: application, user: user} = attrs) do
4041
attrs = %{redirect_uri: application.redirect_uri,
41-
expires_in: ExOauth2Provider.Config.authorization_code_expires_in()}
42+
expires_in: Config.authorization_code_expires_in()}
4243
|> Map.merge(attrs)
4344

4445
{:ok, access_token} = OauthAccessGrants.create_grant(user, application, attrs)

0 commit comments

Comments
 (0)