mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-05 00:02:41 -04:00
Besides other documentation changes, this commit ensures the generated HTML doc for HexDocs.pm will become the source of truth for this Elixir library and leverage on latest features of ExDoc.
42 lines
1.3 KiB
Elixir
42 lines
1.3 KiB
Elixir
defmodule HTTParrot.BasicAuthHandlerTest do
|
|
use ExUnit.Case
|
|
import :meck
|
|
import HTTParrot.BasicAuthHandler
|
|
|
|
setup do
|
|
new :cowboy_req
|
|
new JSX
|
|
on_exit fn -> unload() end
|
|
:ok
|
|
end
|
|
|
|
test "is_authorized returns true if user and passwd match" do
|
|
expect(:cowboy_req, :binding, [{[:user, :req1], :user},
|
|
{[:passwd, :req1], :passwd}])
|
|
expect(:cowboy_req, :parse_header, [{["authorization", :req1], {:basic, :user, :passwd}}])
|
|
|
|
assert is_authorized(:req1, :state) == {true, :req1, :user}
|
|
|
|
assert validate :cowboy_req
|
|
assert validate JSX
|
|
end
|
|
|
|
test "is_authorized returns false if user and passwd doesn't match" do
|
|
expect(:cowboy_req, :binding, [{[:user, :req1], :user},
|
|
{[:passwd, :req1], :passwd}])
|
|
expect(:cowboy_req, :parse_header, [{["authorization", :req1], {:basic, :not_the_user, :passwd}}])
|
|
|
|
assert is_authorized(:req1, :state) == {{false, "Basic realm=\"Fake Realm\""}, :req1, :state}
|
|
|
|
assert validate :cowboy_req
|
|
assert validate JSX
|
|
end
|
|
|
|
test "returns user and if it's authenticated" do
|
|
expect(JSX, :encode!, [{[[authenticated: true, user: :user]], :json}])
|
|
|
|
assert get_json(:req1, :user) == {:json, :req1, nil}
|
|
|
|
assert validate JSX
|
|
end
|
|
end
|