Skip to content

Commit

Permalink
Add missing test to FieldAuthorization and remove unnecessary code fr…
Browse files Browse the repository at this point in the history
…om ObjectScope
  • Loading branch information
gabrielpra1 committed Oct 30, 2019
1 parent 7c3fa55 commit 58bd693
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 0 additions & 6 deletions lib/middlewares/object_scope_authorization.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ defmodule Rajska.ObjectScopeAuthorization do
end
end

# Invalid object
defp result(%{emitter: %{schema_node: schema_node}, root_value: root_value}, _context) do
type = Introspection.get_object_type(schema_node.type)
raise "Expected a Struct for object #{inspect(type.identifier)}, got #{inspect(root_value)}"
end

# List
defp result(%{values: values} = result, context) do
%{result | values: walk_result(values, context)}
Expand Down
21 changes: 21 additions & 0 deletions test/middlewares/field_authorization_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ defmodule Rajska.FieldAuthorizationTest do
field :get_both_scopes, :both_scopes do
resolve fn _args, _ -> {:ok, %{phone: "123456"}} end
end

field :not_struct, :user do
resolve fn _args, _ -> {:ok, %{id: 1}} end
end
end

object :user do
Expand Down Expand Up @@ -183,6 +187,12 @@ defmodule Rajska.FieldAuthorizationTest do
end
end

test "Raises when source object is not a struct" do
assert_raise RuntimeError, ~r/Expected a Struct for source object in field \"phone\", got %{id: 1}/, fn ->
Absinthe.run(not_struct_query(), __MODULE__.Schema, context(:user, 2))
end
end

defp get_user_query(id, is_email_public) do
"""
{
Expand Down Expand Up @@ -217,5 +227,16 @@ defmodule Rajska.FieldAuthorizationTest do
"""
end

defp not_struct_query do
"""
{
notStruct {
name
phone
}
}
"""
end

defp context(role, id), do: [context: %{current_user: %{role: role, id: id}}]
end

0 comments on commit 58bd693

Please sign in to comment.