带有令牌的订阅Graphql(苦艾酒)的授权

时间:2018-07-05 17:07:41

标签: elixir graphql phoenix-framework elixir-framework absinthe

我正在尝试向此订阅测试添加身份验证,因为我在运行Not Authorized时一直得到mix test。  我已经看到您需要向push_doc函数添加登录突变,但是我想知道。有什么方法可以只使用令牌进行身份验证,就像使用:

的常规突变一样
conn =
      build_conn()
      |> put_req_header("authorization", @token)
      |> get("/api", query: @query)

    assert json_response(conn, 200) == %{
       "data" => %{
          "authors" => [%{"name" => "Jennifer"}]
       }
    }

这是我当前的订阅测试:

test "1. Subscribe to createAuthor", %{socket: socket} do

    # setup a subscription
    ref = push_doc(socket, @subscription)
    assert_reply(ref, :ok, %{subscriptionId: subscription_id})

    # run a mutation to trigger the subscription
    ref = push_doc(socket, @mutation)
    assert_reply(ref, :ok, reply)

    assert %{
         data: %{
           "createAuthor" => %{
             "name" => "Jennifer"
           }
         }
       } = reply

    # check to see if we got subscription data
    expected = %{
      result: %{
        data: %{
          "createAuthor" => %{
            "name" => "Jennifer"
          }
        }
      },
      subscriptionId: subscription_id
    }

    assert_push("subscription:data", push)
    assert expected == push
end

我的一般问题是。我能否仅将token(我已经硬编码)传递到用于认证订阅的函数中?

2 个答案:

答案 0 :(得分:0)

我设法进行了如下测试,使用put_req_header()

进行了突变
test "1. Subscribe to createAuthor", %{socket: socket} do
    # setup a subscription
    ref = push_doc(socket, @subscription)
    assert_reply(ref, :ok, %{subscriptionId: subscription_id})

    # run a mutation to trigger the subscription
    conn =
      post(
        build_conn()
        |> put_req_header("authorization", @token),
        "/api",
        query: @mutation
      )

    assert json_response(conn, 200) == %{
             "data" => %{
               "createAuthor" => %{
                 "name" => "Jennifer"
               }
             }
           }

    # check to see if we got subscription data
    expected = %{
      result: %{
        data: %{
          "createAuthor" => %{
            "name" => "Jennifer"
          }
        }
      },
      subscriptionId: subscription_id
    }

    assert_push("subscription:data", push)
    assert expected == push
  end

答案 1 :(得分:0)

您可以制作一个在100年后到期的令牌,并用密钥签名。硬编码,只要您使用相同的SK,这永远是件好事。

您要在令牌前添加“承载者”吗?