Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Control over underlying data #21

Open
hickscorp opened this issue Aug 30, 2020 · 2 comments
Open

Control over underlying data #21

hickscorp opened this issue Aug 30, 2020 · 2 comments

Comments

@hickscorp
Copy link

We're trying to write tests against an Absinthe mutation that uses uploads. We cannot figure out how to have a %Plug.Upload{} added to the underlying data.

With CURL or Postman, we can easily ensure that the mutation works by, for example, having the following query:

mutation($input: CreateDocumentInput!) {
  createDocument(input: $input) {
    document {
      id
      download_url
    }
  }
}

Variables:

{ "input": { "upload": "upload_file" }}

And of course, in the same form-data body, an upload_file parameter containing the file content.

Is there any way to test mutations that require an upload using Wormwood?

@churcho
Copy link

churcho commented Mar 19, 2021

@hickscorp did you ever get a solution on how to test this? Facing the same issue right now.

@hickscorp
Copy link
Author

Yes @churcho, here is a snippet of how we got this to work:

Test:

  describe "when authenticated" do
    setup [:create_user, :create_session, :create_context, :create_upload]

    test "returns a valid document", %{context: ctx, upload: upload} do
      %{filename: filename, content_type: content_type} = upload

      [
        context: add_plug_upload(ctx, upload, "FILE_UPLOAD"),
        variables: %{"input" => %{"upload" => "FILE_UPLOAD"}}
      ]
      |> query_gql()
      |> assert_match(
        {:ok,
         %{
           data: %{
             "createDocument" => %{
               "document" => %{
                 "id" => document_gid,
                 "hash" => hash,
                 "downloadUrl" => download_url,
                 "uploadedFile" => %{
                   "filename" => ^filename,
                   "contentType" => ^content_type,
                   "size" => 36,
                   "hash" => hash
                 }
               }
             }
           }
         }}
        when is_binary(document_gid) and is_binary(hash) and is_binary(download_url)
      )
    end
  end

Useful functions:

  defp create_user(_),
    do: %{user: create(:user, :nocrypto)}

  defp create_session(%{user: user}),
    do: %{session: create(:session, :standard, user: user)}

  defp create_context(%{user: user, session: session}),
    do: %{context: %{user: user, session: session}}

  defp create_upload(_),
    do: %{
      upload:
        build(:upload, :real,
          on_exit: &on_exit/1,
          original_filename: "whatever.jpg",
          content: uuid()
        )
    }

  defp add_plug_upload(ctx, upload, name),
    do: ctx |> deep_merge(%{__absinthe_plug__: %{uploads: %{name => upload}}})

I'll leave it to you to extract the important stuff here :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants