zapier / zapier-platform

The SDK for you to build an integration on Zapier

Home Page:https://platform.zapier.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't test stashFile

Janpot opened this issue · comments

Bug Description

Trying to write a test for a create that stashes a file, this throws an error Only absolute URLs are supported

Reproduction Steps

  1. have a perform that stashes a file
async function perform (z, bundle) {
  const content = 'Hello world!';
  const url = await z.stashFile(content, content.length, 'hello.txt', 'text/plain');
  return { url };
}

module.exports = {
  key: 'create_file',
  noun: 'test',
  operation: {
    perform
  },
  display: {
    description: 'test',
    label: 'test'
  }
};
  1. try to test it with the appTester:
  it('should stash file', async () => {
    const result = await appTester(
      app.creates.create_file.operation.perform,
      {}
    );
  });
  1. get error
1) Create - files
should stash file:
     Only absolute URLs are supported
What happened:
  Executing creates.create_file.operation.perform with bundle
  Only absolute URLs are supported
  TypeError: Only absolute URLs are supported
      at getNodeRequestOptions (~/zapier/node_modules/node-fetch/lib/index.js:1305:9)
      at ~/zapier/node_modules/node-fetch/lib/index.js:1410:19
      at new Promise (<anonymous>)
      at fetch (~/zapier/node_modules/node-fetch/lib/index.js:1407:9)
      at newFetch (~/zapier/node_modules/zapier-platform-core/src/tools/fetch.js:55:10)
      at request (~/zapier/node_modules/zapier-platform-core/src/tools/request-client-internal.js:32:22)
      at ~/zapier/node_modules/zapier-platform-core/src/tools/request-sugar.js:29:12
      at ~/zapier/node_modules/zapier-platform-core/src/tools/create-rpc-client.js:47:12
      at Object.stashFile (~/zapier/node_modules/zapier-platform-core/src/tools/create-file-stasher.js:106:12)
      at perform (~/zapier/creates/createFile.js:3:23)
      at execute (~/zapier/node_modules/zapier-platform-core/src/execute.js:72:12)
      at ~/zapier/node_modules/zapier-platform-core/src/create-command-handler.js:29:20
      at Object.<anonymous> (~/zapier/node_modules/zapier-platform-core/src/middleware.js:90:22)
      at bound (domain.js:413:15)
      at Object.runBound (domain.js:424:12)
      at Object.tryCatcher (~/zapier/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (~/zapier/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (~/zapier/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (~/zapier/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (~/zapier/node_modules/bluebird/js/release/promise.js:729:18)
      at _drainQueueStep (~/zapier/node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (~/zapier/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (~/zapier/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues (~/zapier/node_modules/bluebird/js/release/async.js:15:14)
      at processImmediate (internal/timers.js:461:21)
      at process.topLevelDomainCallback (domain.js:144:15)
      at process.callbackTrampoline (internal/async_hooks.js:129:14)

Seems to originate in create-rpc-client

Version Info

  • CLI version: 11.1.0
  • Node.js version: v14.16.0
  • OS info: darwin-x64
  • zapier-platform-core dependency: 11.1.0

Hey @Janpot. I'm having trouble reproducing this- when I run your test locally, I get a url:

{
    url: 'https://zapier-dev-files.s3.amazonaws.com/cli-platform/18885/ylhuyUPOtYikt-_-...'
}

The only thing that comes to mind is that stashFile tests depend on having an active deploy key. The test code checks for this (if I manually delete my key API key, I get:

No deploy key found. Make sure you set the `ZAPIER_DEPLOY_KEY` environment variable to write tests that rely on the RPC API (i.e. z.stashFile)

Given that you're not seeing that, it makes me thing your key is defined.

if you start a fresh app (zapier init my-test --template files) and install dependencies, does zapier test work?

If it does, the next step would be to package up your whole sample project and send it over (if possible). That's an odd one!

@xavdid Ok, didn't know tests had to be run using the zapier CLI, I have it working now, I'm sorry for wasting your time. The reason why I didn't see the ZAPIER_DEPLOY_KEY warning is that, not knowing to run it through the CLI, I had written my own logic that populates the environment with the deploy key from the home dir.

Perhaps it could be valuable to mention in this error that the tests need to be run through the zapier cli?

No deploy key found. Make sure you set the `ZAPIER_DEPLOY_KEY` environment variable to write tests that rely on the RPC API (i.e. z.stashFile). Make sure you are running your tests using the `zapier test` command.

As a small additional feature request, it would be interesting for us if the npm test script is configurable as to not hijack the npm test script (which we standardize on across our projects). perhaps something like:

"unit": "mocha .",
"test": "zapier test -s unit"

No waste at all! Happy to help.

Most tests can be run using regular npm test or mocha ..., but certain ones (such as those that use stashFile) need a deploy key set. You can do that yourself, but it's simpler to just use zapier test.

Just so I understand, you:

  1. Ran npm test
  2. saw that error message
  3. wrote logic to surface the deploy key
  4. got the error you initially filed this issue about

If so, we could improve that error message to hopefully point people in the right direction.


re: changing the test command. zapier test is intentionally very simplistic. It:

  1. sets up the envirionment
  2. runs npm test

It was done this way to play nicely with the existing ecosystem of tools. I'm not sure we could fit that to your use case well. Hopefully it's not too much of a burden to change the test command occasionally. :)