Skip to content

Commit

Permalink
Allow using custom models
Browse files Browse the repository at this point in the history
  • Loading branch information
fatum authored and Sgiath committed Dec 19, 2023
1 parent cc28bd9 commit 597c254
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/ex_oauth2_provider/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ defmodule ExOauth2Provider.Config do
do: get_oauth_struct(config, :application)

defp get_oauth_struct(config, name, namespace \\ "oauth") do
context = Macro.camelize("#{namespace}_#{name}s")
module = Macro.camelize("#{namespace}_#{name}")
case get(config, name) do
nil ->
context = Macro.camelize("#{namespace}_#{name}s")
module = Macro.camelize("#{namespace}_#{name}")

config
|> get(name)
|> Kernel.||(app_module(config, context, module))
config
|> get(name)
|> Kernel.||(app_module(config, context, module))

module ->
module
end
end

@doc """
Expand Down
27 changes: 27 additions & 0 deletions test/ex_oauth2_provider/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ defmodule ExOauth2Provider.ConfigTest do
end)
end

test "application/1" do
assert Config.application(otp_app: :my_app) == MyApp.OauthApplications.OauthApplication

Application.delete_env(:ex_oauth2_provider, ExOauth2Provider)
Application.put_env(:my_app, ExOauth2Provider, application: :custom_value)

assert Config.application(otp_app: :my_app) == :custom_value
end

test "acess_token/1" do
assert Config.access_token(otp_app: :my_app) == MyApp.OauthAccessTokens.OauthAccessToken

Application.delete_env(:ex_oauth2_provider, ExOauth2Provider)
Application.put_env(:my_app, ExOauth2Provider, access_token: :custom_value)

assert Config.access_token(otp_app: :my_app) == :custom_value
end

test "acess_grant/1" do
assert Config.access_grant(otp_app: :my_app) == MyApp.OauthAccessGrants.OauthAccessGrant

Application.delete_env(:ex_oauth2_provider, ExOauth2Provider)
Application.put_env(:my_app, ExOauth2Provider, access_grant: :custom_value)

assert Config.access_grant(otp_app: :my_app) == :custom_value
end

test "repo/1" do
assert Config.repo(otp_app: :my_app) == Dummy.Repo

Expand Down

0 comments on commit 597c254

Please sign in to comment.