Skip to content

Commit

Permalink
Updated tests to use put_req_header
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanjos committed Apr 10, 2015
1 parent d5b18ce commit ed599bd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule PlugJwt.Mixfile do

defp deps do
[
{:joken, "~> 0.11"},
{:joken, "~> 0.12"},
{:plug, ">= 0.7.0"},
{:cowboy, "~> 1.0.0"},
{:jsx, "~> 2.1.1", only: :test}
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
%{"cowboy": {:hex, :cowboy, "1.0.0"},
"cowlib": {:hex, :cowlib, "1.0.1"},
"jazz": {:package, "0.2.1"},
"joken": {:hex, :joken, "0.11.0"},
"joken": {:hex, :joken, "0.12.0"},
"jsx": {:hex, :jsx, "2.1.1"},
"plug": {:hex, :plug, "0.11.1"},
"plug": {:hex, :plug, "0.7.0"},
"ranch": {:hex, :ranch, "1.0.0"},
"timex": {:hex, :timex, "0.13.1"}}
20 changes: 16 additions & 4 deletions test/plug_jwt_router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ defmodule PlugJwtRouterTest do
{:ok, token} = Joken.Token.encode("secret", TestJsx, payload)

auth_header = "Bearer " <> token
conn = conn(:get, "/", [], headers: [{"authorization", auth_header}]) |> TestRouterPlug.call([])
conn = conn(:get, "/", [])
|> put_req_header("authorization", auth_header)
|> TestRouterPlug.call([])

assert conn.status == 200
assert conn.resp_body == "Hello Tester"
assert conn.assigns.claims == %{admin: true, name: "John Doe", sub: 1234567890}
Expand All @@ -74,22 +77,31 @@ defmodule PlugJwtRouterTest do
{:ok, token} = Joken.Token.encode("test", TestJsx, payload)

auth_header = "Bearer " <> token
conn = conn(:get, "/", [], headers: [{"authorization", auth_header}]) |> TestRouterPlugFromConfig.call([])
conn = conn(:get, "/", [])
|> put_req_header("authorization", auth_header)
|> TestRouterPlugFromConfig.call([])

assert conn.status == 200
assert conn.resp_body == "Hello Tester"
assert conn.assigns.claims == %{admin: true, name: "John Doe", sub: 1234567890}
end

test "Send 401 when invalid token sent" do
incorrect_credentials = "Bearer " <> "Not a token"
conn = conn(:get, "/", [], headers: [{"authorization", incorrect_credentials}]) |> TestRouterPlug.call([])
conn = conn(:get, "/", [])
|> put_req_header("authorization", incorrect_credentials)
|> TestRouterPlug.call([])

assert conn.status == 401
assert conn.resp_body == "{\"description\":\"Invalid JSON Web Token\",\"error\":\"Unauthorized\",\"status_code\":401}"
end

test "Send 401 when invalid token sent (settings from config)" do
incorrect_credentials = "Bearer " <> "Not a token"
conn = conn(:get, "/", [], headers: [{"authorization", incorrect_credentials}]) |> TestRouterPlugFromConfig.call([])
conn = conn(:get, "/", [])
|> put_req_header("authorization", incorrect_credentials)
|> TestRouterPlugFromConfig.call([])

assert conn.status == 401
assert conn.resp_body == "{\"description\":\"Invalid JSON Web Token\",\"error\":\"Unauthorized\",\"status_code\":401}"
end
Expand Down

0 comments on commit ed599bd

Please sign in to comment.