-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
500c323
commit 71ddc59
Showing
4 changed files
with
207 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
defmodule Mix.Tasks.AshAuthentication.InstallTest do | ||
use ExUnit.Case | ||
|
||
import Igniter.Test | ||
|
||
test "installation creates a secrets module" do | ||
test_project() | ||
|> Igniter.Project.Deps.add_dep({:simple_sat, ">= 0.0.0"}) | ||
|> Igniter.compose_task("ash_authentication.install") | ||
|> assert_creates("lib/test/secrets.ex", """ | ||
defmodule Test.Secrets do | ||
use AshAuthentication.Secret | ||
def secret_for([:authentication, :tokens, :signing_secret], Test.Accounts.User, _opts) do | ||
Application.fetch_env(:test, :token_signing_secret) | ||
end | ||
end | ||
""") | ||
end | ||
|
||
test "installation adds the supervisor to the app" do | ||
test_project() | ||
|> Igniter.Project.Deps.add_dep({:simple_sat, ">= 0.0.0"}) | ||
|> Igniter.compose_task("ash_authentication.install") | ||
|> assert_has_patch("lib/test/application.ex", """ | ||
8 | children = [{AshAuthentication.Supervisor, [otp_app: :test]}] | ||
""") | ||
end | ||
|
||
test "installation adds config files" do | ||
test_project() | ||
|> Igniter.Project.Deps.add_dep({:simple_sat, ">= 0.0.0"}) | ||
|> Igniter.compose_task("ash_authentication.install") | ||
|> assert_creates("config/runtime.exs", """ | ||
import Config | ||
if config_env() == :prod do | ||
config :test, | ||
token_signing_secret: | ||
System.get_env("TOKEN_SIGNING_SECRET") || | ||
raise("Missing environment variable `TOKEN_SIGNING_SECRET`!") | ||
end | ||
""") | ||
|
||
# can't easily test this with the helpers we have. | ||
# we can make `assert_creates` take a function potentially | ||
# for now, this is simple enough that its almost testing `igniter`. | ||
# |> assert_creates("config/dev.exs", """ | ||
# import Config | ||
# config :test, token_signing_secret: "kDL+MmXw8E0xbN//xYTowcR1tt5yCLSu" | ||
# """) | ||
# |> assert_creates("config/test.exs", """ | ||
# import Config | ||
# config :test, token_signing_secret: "7vg4IwKCttu/eMU3PYPLCjrl277OlNvr" | ||
# """) | ||
end | ||
|
||
test "installation adds a user resource" do | ||
test_project() | ||
|> Igniter.Project.Deps.add_dep({:simple_sat, ">= 0.0.0"}) | ||
|> Igniter.compose_task("ash_authentication.install") | ||
|> assert_creates("lib/test/accounts/user.ex", """ | ||
defmodule Test.Accounts.User do | ||
use Ash.Resource, | ||
otp_app: :ash_authentication, | ||
domain: Test.Accounts, | ||
authorizers: [Ash.Policy.Authorizer], | ||
data_layer: AshPostgres.DataLayer | ||
postgres do | ||
table("users") | ||
repo(AshAuthentication.Repo) | ||
end | ||
authentication do | ||
tokens do | ||
enabled?(true) | ||
token_resource(Test.Accounts.Token) | ||
signing_secret(:token_signing_secret) | ||
end | ||
end | ||
end | ||
""") | ||
end | ||
|
||
test "instalation adds a user token resource" do | ||
test_project() | ||
|> Igniter.Project.Deps.add_dep({:simple_sat, ">= 0.0.0"}) | ||
|> Igniter.compose_task("ash_authentication.install") | ||
|> assert_creates("lib/test/accounts/token.ex", """ | ||
defmodule Test.Accounts.Token do | ||
use Ash.Resource, | ||
otp_app: :ash_authentication, | ||
domain: Test.Accounts, | ||
authorizers: [Ash.Policy.Authorizer], | ||
data_layer: AshPostgres.DataLayer | ||
attributes do | ||
attribute :jti, :string do | ||
primary_key?(true) | ||
public?(true) | ||
allow_nil?(false) | ||
sensitive?(true) | ||
end | ||
attribute :subject, :string do | ||
allow_nil?(false) | ||
end | ||
attribute :expires_at, :utc_datetime do | ||
allow_nil?(false) | ||
end | ||
attribute :purpose, :string do | ||
allow_nil?(false) | ||
public?(true) | ||
end | ||
attribute :extra_data, :map do | ||
public?(true) | ||
end | ||
timestamps() | ||
end | ||
postgres do | ||
table("tokens") | ||
repo(AshAuthentication.Repo) | ||
end | ||
actions do | ||
read :expired do | ||
filter(expr(expires_at < now())) | ||
end | ||
read :get_token do | ||
get?(true) | ||
argument(:token, :string, sensitive?: true) | ||
argument(:jti, :string, sensitive?: true) | ||
argument(:purpose, :string, sensitive?: false) | ||
prepare(AshAuthentication.TokenResource.GetTokenPreparation) | ||
end | ||
read :revoked? do | ||
argument(:token, :string, sensitive?: true) | ||
argument(:jti, :string, sensitive?: true) | ||
get?(true) | ||
prepare(AshAuthentication.TokenResource.IsRevokedPreparation) | ||
end | ||
read :get_confirmation_changes do | ||
argument(:jti, :string, allow_nil?: false, sensitive?: true) | ||
get?(true) | ||
prepare(AshAuthentication.TokenResource.GetConfirmationChangesPreparation) | ||
end | ||
create :revoke_token do | ||
accept([:extra_data]) | ||
argument(:token, :string, allow_nil?: false, sensitive?: true) | ||
change(AshAuthentication.TokenResource.RevokeTokenChange) | ||
end | ||
create :store_confirmation_changes do | ||
accept([:extra_data, :purpose]) | ||
argument(:token, :string, allow_nil?: false, sensitive?: true) | ||
change(AshAuthentication.TokenResource.StoreConfirmationChangesChange) | ||
end | ||
create :store_token do | ||
accept([:extra_data, :purpose]) | ||
argument(:token, :string, allow_nil?: false, sensitive?: true) | ||
change(AshAuthentication.TokenResource.StoreTokenChange) | ||
end | ||
destroy :expunge_expired do | ||
change(filter(expr(expires_at < now()))) | ||
end | ||
end | ||
policies do | ||
bypass AshAuthentication.Checks.AshAuthenticationInteraction do | ||
authorize_if(always()) | ||
end | ||
policies do | ||
policy always() do | ||
forbid_if(always()) | ||
end | ||
end | ||
end | ||
end | ||
""") | ||
end | ||
end |