Skip to content

Commit

Permalink
add test for invalid client_id for #55
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 29, 2020
1 parent d379fb6 commit 254e7a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
8 changes: 5 additions & 3 deletions lib/auth_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ defmodule AuthWeb.AuthController do
true -> # redirect
case get_client_secret_from_state(state) do
0 ->
IO.inspect("client_secret is 0 (error)")
# IO.inspect("client_secret is 0 (error)")
unauthorized(conn)
secret ->
IO.inspect(secret, label: "secret")
# IO.inspect(secret, label: "secret")
conn
|> redirect(external: add_jwt_url_param(person, state, secret))
end
Expand All @@ -74,8 +74,10 @@ defmodule AuthWeb.AuthController do
end

defp unauthorized(conn) do
# IO.inspect(conn)
conn
|> put_resp_header("www-authenticate", "Bearer realm=\"Person access\"")
# |> put_resp_header("www-authenticate", "Bearer realm=\"Person access\"")
|> put_resp_content_type("text/html")
|> send_resp(401, "invalid client_id")
|> halt()
end
Expand Down
10 changes: 10 additions & 0 deletions test/auth_web/controllers/apikey_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ defmodule AuthWeb.ApikeyControllerTest do
assert decrypted == person_id
end

test "decode_decrypt/1 with invalid client_id" do
valid_key = AuthWeb.ApikeyController.encrypt_encode(1)
person_id = AuthWeb.ApikeyController.decode_decrypt(valid_key)
assert person_id == 1

invalid_key = String.slice(valid_key, 0..-2)
error = AuthWeb.ApikeyController.decode_decrypt(invalid_key)
assert error == 0
end

property "Check a batch of int values can be decoded decode_decrypt/1" do
check all(int <- integer()) do
assert decode_decrypt(encrypt_encode(int)) == int
Expand Down
15 changes: 7 additions & 8 deletions test/auth_web/controllers/auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ defmodule AuthWeb.AuthControllerTest do
# assert html_response(conn, 302) =~ "redirected"
end

test "decode_decrypt/1 with invalid client_id" do
valid_key = AuthWeb.ApikeyController.encrypt_encode(1)
person_id = AuthWeb.ApikeyController.decode_decrypt(valid_key)
assert person_id == 1

invalid_key = String.slice(valid_key, 0..-2)
error = AuthWeb.ApikeyController.decode_decrypt(invalid_key)
assert error == 0
test "google_handler/2 with invalid client_id", %{conn: conn} do
invalid_key = String.slice(AuthPlug.Token.client_id(), 0..-2)
conn = get(conn, "/auth/google/callback",
%{code: "234", state: "www.example.com" <>
"&client_id=" <> invalid_key })
# assert html_response(conn, 200) =~ "google account"
assert html_response(conn, 401) =~ "invalid client_id"
end


Expand Down

0 comments on commit 254e7a5

Please sign in to comment.