Skip to content

Commit

Permalink
Adds tests to credo
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neighman committed Feb 17, 2016
1 parent 9fef337 commit 5b5f99f
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 76 deletions.
91 changes: 91 additions & 0 deletions .credo.exs
@@ -0,0 +1,91 @@
# This file contains the configuration for Credo.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [
%{
#
# Run any config using `mix credo -C <name>`. If no config name is given
# "default" is used.
name: "default",
#
# these are the files included in the analysis
files: %{
#
# you can give explicit globs or simply directories
# in the latter case `**/*.{ex,exs}` will be used
included: ["lib/", "test/"],
excluded: []
},
#
# The `checks:` field contains all the checks that are run. You can
# customize the parameters of any given check by adding a second element
# to the tuple.
#
# There are two ways of deactivating a check:
# 1. deleting the check from this list
# 2. putting `false` as second element (to quickly "comment it out"):
#
# {Credo.Check.Consistency.ExceptionNames, false}
#
checks: [
{Credo.Check.Consistency.ExceptionNames},
{Credo.Check.Consistency.LineEndings},
{Credo.Check.Consistency.SpaceAroundOperators},
{Credo.Check.Consistency.SpaceInParentheses},
{Credo.Check.Consistency.TabsOrSpaces},

# For some checks, like AliasUsage, you can only customize the priority
# Priority values are: `low, normal, high, higher`
{Credo.Check.Design.AliasUsage, priority: :low},
# For others you can set parameters
{Credo.Check.Design.DuplicatedCode, mass_threshold: 16, nodes_threshold: 2},

# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 80},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleDoc},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.VariableNames},

{Credo.Check.Refactor.ABCSize},
{Credo.Check.Refactor.CaseTrivialMatches},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
{Credo.Check.Warning.NameRedeclarationByAssignment},
{Credo.Check.Warning.NameRedeclarationByCase},
{Credo.Check.Warning.NameRedeclarationByDef},
{Credo.Check.Warning.NameRedeclarationByFn},
{Credo.Check.Warning.OperationOnSameValues},
{Credo.Check.Warning.UnusedEnumOperation},
{Credo.Check.Warning.UnusedKeywordOperation},
{Credo.Check.Warning.UnusedListOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},
{Credo.Check.Warning.OperationWithConstantResult},
]
}
]
}
1 change: 1 addition & 0 deletions test/guardian/claims_test.exs
@@ -1,4 +1,5 @@
defmodule Guardian.ClaimsTest do
@moduledoc false
use ExUnit.Case, async: true
@tag timeout: 1000

Expand Down
1 change: 1 addition & 0 deletions test/guardian/keys_test.exs
@@ -1,4 +1,5 @@
defmodule Guardian.KeysTest do
@moduledoc false
use ExUnit.Case, async: true

test "base_key with atom" do
Expand Down
5 changes: 3 additions & 2 deletions test/guardian/permissions_test.exs
@@ -1,4 +1,5 @@
defmodule Guardian.PermissionsTest do
@moduledoc false
use ExUnit.Case, async: true
alias Guardian.Permissions

Expand Down Expand Up @@ -176,13 +177,13 @@ defmodule Guardian.PermissionsTest do
end

test "integration with generating and decoding permissions" do
{ :ok, jwt, _ } = Guardian.encode_and_sign(
{:ok, jwt, _} = Guardian.encode_and_sign(
"User:1",
"token",
%{perms: %{default: [:read, :write], other: [:other_read, :other_write]}}
)

{ :ok, claims } = Guardian.decode_and_verify(jwt, %{})
{:ok, claims} = Guardian.decode_and_verify(jwt, %{})

# Check all the permutations of string vs atoms
default_val = Permissions.to_value([:read, :write])
Expand Down
3 changes: 3 additions & 0 deletions test/guardian/phoenix/socket_test.exs
@@ -1,9 +1,12 @@
defmodule Guardian.Phoenix.SocketTest do
@moduledoc false
use ExUnit.Case, async: true

alias Guardian.Phoenix.Socket, as: GuardianSocket

defmodule TestSocket do
@moduledoc false

defstruct assigns: %{}
end

Expand Down
27 changes: 19 additions & 8 deletions test/guardian/plug/ensure_authenticated_test.exs
@@ -1,11 +1,14 @@
defmodule Guardian.Plug.EnsureAuthenticatedTest do
@moduledoc false
use ExUnit.Case, async: true
use Plug.Test
import Guardian.TestHelper

alias Guardian.Plug.EnsureAuthenticated

defmodule TestHandler do
@moduledoc false

def unauthenticated(conn, _) do
conn
|> Plug.Conn.assign(:guardian_spec, :unauthenticated)
Expand Down Expand Up @@ -61,7 +64,7 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
end

test "validates claims and calls through if claims are ok", %{conn: conn} do
claims = %{ "aud" => "token", "sub" => "user1" }
claims = %{"aud" => "token", "sub" => "user1"}

ensured_conn =
conn
Expand All @@ -72,7 +75,7 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
end

test "it validates claims and fails if claims don't match", %{conn: conn} do
claims = %{ "aud" => "oauth", "sub" => "user1" }
claims = %{"aud" => "oauth", "sub" => "user1"}

ensured_conn =
conn
Expand All @@ -83,7 +86,7 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
end

test "doesn't call unauth when session for default key", %{conn: conn} do
claims = %{ "aud" => "token", "sub" => "user1" }
claims = %{"aud" => "token", "sub" => "user1"}

ensured_conn =
conn
Expand All @@ -94,7 +97,7 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
end

test "doesn't call unauthenticated when session for key", %{conn: conn} do
claims = %{ "aud" => "token", "sub" => "user1" }
claims = %{"aud" => "token", "sub" => "user1"}

ensured_conn =
conn
Expand All @@ -111,15 +114,23 @@ defmodule Guardian.Plug.EnsureAuthenticatedTest do
end

test "calls unauthenticated when no session for key", %{conn: conn} do
ensured_conn = run_plug(conn, EnsureAuthenticated, handler: TestHandler,
key: :secret)
ensured_conn = run_plug(
conn,
EnsureAuthenticated,
handler: TestHandler,
key: :secret
)

assert must_authenticate?(ensured_conn)
end

test "it halts the connection", %{conn: conn} do
ensured_conn = run_plug(conn, EnsureAuthenticated, handler: TestHandler,
key: :secret)
ensured_conn = run_plug(
conn,
EnsureAuthenticated,
handler: TestHandler,
key: :secret
)

assert ensured_conn.halted
end
Expand Down
15 changes: 9 additions & 6 deletions test/guardian/plug/ensure_not_authenticated_test.exs
@@ -1,10 +1,13 @@
defmodule Guardian.Plug.EnsureNotAuthenticatedTest do
@moduledoc false
use ExUnit.Case, async: true
use Plug.Test

alias Guardian.Plug.EnsureNotAuthenticated

defmodule TestHandler do
@moduledoc false

def already_authenticated(conn, _) do
conn
|> Plug.Conn.assign(:guardian_spec, :authenticated)
Expand Down Expand Up @@ -35,31 +38,31 @@ defmodule Guardian.Plug.EnsureNotAuthenticatedTest do
end

test "it validates claims and fails if the claims do match" do
claims = %{ "aud" => "token", "sub" => "user1" }
conn = :get |> conn("/foo") |> Guardian.Plug.set_claims({ :ok, claims })
claims = %{"aud" => "token", "sub" => "user1"}
conn = :get |> conn("/foo") |> Guardian.Plug.set_claims({:ok, claims})
opts = EnsureNotAuthenticated.init(handler: TestHandler, aud: "token")
ensured_conn = EnsureNotAuthenticated.call(conn, opts)
assert already_authenticated?(ensured_conn)
end

test "it validates claims and calls through if the claims are not ok" do
claims = %{ "aud" => "oauth", "sub" => "user1" }
claims = %{"aud" => "oauth", "sub" => "user1"}
conn = :get |> conn("/foo") |> Guardian.Plug.set_claims({:ok, claims})
opts = EnsureNotAuthenticated.init(handler: TestHandler, aud: "token")
ensured_conn = EnsureNotAuthenticated.call(conn, opts)
refute already_authenticated?(ensured_conn)
end

test "call authenticated when there's a session with default key" do
claims = %{ "aud" => "token", "sub" => "user1" }
conn = :get |> conn("/foo") |> Guardian.Plug.set_claims({ :ok, claims })
claims = %{"aud" => "token", "sub" => "user1"}
conn = :get |> conn("/foo") |> Guardian.Plug.set_claims({:ok, claims})
opts = EnsureNotAuthenticated.init(handler: TestHandler)
ensured_conn = EnsureNotAuthenticated.call(conn, opts)
assert already_authenticated?(ensured_conn)
end

test "call authenticated when theres a session with specific key" do
claims = %{ "aud" => "token", "sub" => "user1" }
claims = %{"aud" => "token", "sub" => "user1"}
conn = :get
|> conn("/foo")
|> Guardian.Plug.set_claims({:ok, claims}, :secret)
Expand Down
13 changes: 8 additions & 5 deletions test/guardian/plug/ensure_permissions_test.exs
@@ -1,11 +1,14 @@
defmodule Guardian.Plug.EnsurePermissionTest do
@moduledoc false
use ExUnit.Case, async: true
use Plug.Test
import Guardian.TestHelper

alias Guardian.Plug.EnsurePermissions

defmodule TestHandler do
@moduledoc false

def unauthorized(conn, _) do
conn
|> Plug.Conn.assign(:guardian_spec, :forbidden)
Expand All @@ -20,7 +23,7 @@ defmodule Guardian.Plug.EnsurePermissionTest do

test "doesnt call unauthorized when permissions are present", %{conn: conn} do
pems = Guardian.Permissions.to_value([:read, :write])
claims = %{ "pem" => %{ "default" => pems } }
claims = %{"pem" => %{"default" => pems}}

expected_conn =
conn
Expand All @@ -34,7 +37,7 @@ defmodule Guardian.Plug.EnsurePermissionTest do

test "is invalid when missing a requested permission", %{conn: conn} do
pems = Guardian.Permissions.to_value([:read])
claims = %{ "pem" => %{ "default" => pems } }
claims = %{"pem" => %{"default" => pems}}

expected_conn =
conn
Expand All @@ -48,7 +51,7 @@ defmodule Guardian.Plug.EnsurePermissionTest do

test "is invalid when claims don't include the pem key", %{conn: conn} do
pems = Guardian.Permissions.to_value([:other_read], :other)
claims = %{ "pem" => %{ "default" => pems } }
claims = %{"pem" => %{"default" => pems}}

expected_conn =
conn
Expand All @@ -66,7 +69,7 @@ defmodule Guardian.Plug.EnsurePermissionTest do
:default
)
other_pems = Guardian.Permissions.to_value([:other_write], :other)
claims = %{ "pem" => %{ "default" => pems, "other" => other_pems } }
claims = %{"pem" => %{"default" => pems, "other" => other_pems}}

expected_conn =
conn
Expand All @@ -89,7 +92,7 @@ defmodule Guardian.Plug.EnsurePermissionTest do
:other
)

claims = %{ "pem" => %{ "default" => pems, "other" => other_pems } }
claims = %{"pem" => %{"default" => pems, "other" => other_pems}}

expected_conn =
conn
Expand Down
1 change: 1 addition & 0 deletions test/guardian/plug/error_handler_test.exs
@@ -1,4 +1,5 @@
defmodule Guardian.Plug.ErrorHandlerTest do
@moduledoc false
use ExUnit.Case, async: true
use Plug.Test

Expand Down

0 comments on commit 5b5f99f

Please sign in to comment.