mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 13:24:14 +12:00
115 lines
2.5 KiB
Elixir
115 lines
2.5 KiB
Elixir
|
defmodule AshGraphql.PlugTest do
|
||
|
@moduledoc false
|
||
|
use ExUnit.Case, async: false
|
||
|
require Ash.Query
|
||
|
import Plug.Conn
|
||
|
|
||
|
@moduletag capture_log: true
|
||
|
|
||
|
setup do
|
||
|
on_exit(fn ->
|
||
|
AshGraphql.TestHelpers.stop_ets()
|
||
|
end)
|
||
|
end
|
||
|
|
||
|
defmodule Pipeline do
|
||
|
@moduledoc false
|
||
|
use Plug.Router
|
||
|
plug(Plug.Parsers, parsers: [:urlencoded, :multipart, :json], json_decoder: Jason)
|
||
|
plug(AshGraphql.Plug)
|
||
|
plug(:match)
|
||
|
plug(:dispatch)
|
||
|
forward("/", to: Absinthe.Plug, init_opts: [schema: AshGraphql.Test.Schema])
|
||
|
end
|
||
|
|
||
|
test "when the actor is set, the current user returns the correct value" do
|
||
|
user =
|
||
|
AshGraphql.Test.User
|
||
|
|> Ash.Changeset.for_create(:create, %{name: "Marty McFly"})
|
||
|
|> AshGraphql.Test.Api.create!()
|
||
|
|
||
|
resp =
|
||
|
"""
|
||
|
query {
|
||
|
currentUser {
|
||
|
id
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
"""
|
||
|
|> conn()
|
||
|
|> Ash.PlugHelpers.set_actor(user)
|
||
|
|> run()
|
||
|
|
||
|
assert resp["data"]["currentUser"]["id"] == user.id
|
||
|
assert resp["data"]["currentUser"]["name"] == user.name
|
||
|
end
|
||
|
|
||
|
test "when the actor is not set, the current user return nil" do
|
||
|
resp =
|
||
|
"""
|
||
|
query {
|
||
|
currentUser {
|
||
|
id
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
"""
|
||
|
|> conn()
|
||
|
|> run()
|
||
|
|
||
|
refute resp["data"]["currentUser"]
|
||
|
end
|
||
|
|
||
|
test "when the tenant is set, the the multi tenant tag is correct" do
|
||
|
tenant = "Marty McFly"
|
||
|
|
||
|
tag =
|
||
|
AshGraphql.Test.MultitenantTag
|
||
|
|> Ash.Changeset.for_create(:create, [name: "1985"], tenant: tenant)
|
||
|
|> AshGraphql.Test.Api.create!()
|
||
|
|
||
|
resp =
|
||
|
"""
|
||
|
query MultitenantTag($id: ID!) {
|
||
|
getMultitenantTag(id: $id) {
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
"""
|
||
|
|> conn(%{id: tag.id})
|
||
|
|> Ash.PlugHelpers.set_tenant(tenant)
|
||
|
|> run()
|
||
|
|
||
|
assert resp["data"]["getMultitenantTag"]["name"] == "1985"
|
||
|
end
|
||
|
|
||
|
test "when the tenant is not set, the multi tennant tag is nil" do
|
||
|
resp =
|
||
|
"""
|
||
|
query MultitenantTag($id: ID!) {
|
||
|
getMultitenantTag(id: $id) {
|
||
|
name
|
||
|
}
|
||
|
}
|
||
|
"""
|
||
|
|> conn(%{id: Ecto.UUID.generate()})
|
||
|
|> run()
|
||
|
|
||
|
refute resp["data"]["getMultitenantTag"]
|
||
|
end
|
||
|
|
||
|
def conn(query, variables \\ %{}) do
|
||
|
:post
|
||
|
|> Plug.Test.conn("/", %{query: query, variables: variables})
|
||
|
|> put_req_header("content-type", "application/json")
|
||
|
end
|
||
|
|
||
|
def run(conn) do
|
||
|
conn
|
||
|
|> Pipeline.call([])
|
||
|
|> Map.fetch!(:resp_body)
|
||
|
|> Jason.decode!()
|
||
|
end
|
||
|
end
|