【问题标题】:Authorization for subscriptions Graphql (Absinthe) with token使用令牌授权订阅 Graphql(苦艾酒)
【发布时间】:2018-12-14 06:08:20
【问题描述】:

我正在尝试为此订阅测试添加身份验证,因为当我运行mix test 时,我不断收到Not Authorized。 我已经看到您需要向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(我已经硬编码)传递给一个函数来验证订阅吗?

【问题讨论】:

    标签: graphql phoenix-framework elixir absinthe


    【解决方案1】:

    我设法进行如下测试,使用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
    

    【讨论】:

      【解决方案2】:

      您可以制作一个有效期为 100 年的令牌,并使用您的密钥签名。只要您使用相同的 SK,就可以对其进行硬编码。

      您是否在令牌前添加“Bearer”?

      【讨论】:

        猜你喜欢
        • 2020-05-04
        • 2021-07-08
        • 2018-12-25
        • 2020-10-20
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        • 2018-06-09
        • 2021-12-15
        相关资源
        最近更新 更多