Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EventHandler.is_authrozied/2 for unauthorized requests #128

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/poxa/event_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ defmodule Poxa.EventHandler do
{qs_vals, req} = :cowboy_req.qs_vals(req)
{method, req} = :cowboy_req.method(req)
{path, req} = :cowboy_req.path(req)
authorized = Authentication.check(method, path, body, qs_vals)
req = if authorized do
req
else
:cowboy_req.set_resp_body(@authentication_error_json, req)
end
{authorized, req, state}
if Authentication.check(method, path, body, qs_vals) do
{true, req, state}
else
req = :cowboy_req.set_resp_body(@authentication_error_json, req)
{{false, "Authentication error"}, req, state}
end
end


Expand Down
2 changes: 1 addition & 1 deletion test/event_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Poxa.EventHandlerTest do
expect(:cowboy_req, :path, 1, {:path, :req3})
expect(:cowboy_req, :set_resp_body, 2, :req4)

assert is_authorized(:req, %{body: :body}) == {false, :req4, %{body: :body}}
assert is_authorized(:req, %{body: :body}) == {{false, "Authentication error"}, :req4, %{body: :body}}

assert validate Authentication
assert validate :cowboy_req
Expand Down
13 changes: 12 additions & 1 deletion test/integration/trigger_event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ defmodule Poxa.Integration.TriggerEvent do

setup_all do
Application.ensure_all_started(:pusher)
Pusher.configure!("localhost", 8080, "app_id", "app_key", "secret")
:ok
end

setup do
Pusher.configure!("localhost", 8080, "app_id", "app_key", "secret")
end

test "trigger event returns 200" do
[channel, socket_id] = ["channel", "123.456"]

Expand All @@ -26,4 +29,12 @@ defmodule Poxa.Integration.TriggerEvent do

assert Pusher.trigger("test_event", %{}, channel, socket_id) == :error
end


test "trigger event returns 401 on invalid authentication" do
[channel, socket_id] = ["channel", "123.456"]

Pusher.configure!("localhost", 8080, "app_id", "app_key", "wrong_secret")
assert Pusher.trigger("test_event", %{}, channel, socket_id) == :error
end
end