0

I'm trying to add authentication to this subscription test, since I keep getting Not Authorized when I run mix test. I've seen that you need to add a login mutation to the push_doc function but I was wondering. Is there any way to use only the token to authenticate, just like regular mutations that use :

conn =
      build_conn()
      |> put_req_header("authorization", @token)
      |> get("/api", query: @query)

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

This is my current subscription test:

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

My general question is. Can I only pass the token (which I have hardcoded) into a function to authenticate for subscriptions?

4

2 回答 2

0

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

您是否在令牌前添加“承载者”?

于 2019-07-07T22:51:34.577 回答
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
于 2018-07-09T20:08:26.467 回答