Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Allow file stashing within a create/action #45

Merged
merged 4 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/tools/create-file-stasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ const createFileStasher = (input) => {
}

const isRunningOnHydrator = _.get(input, '_zapier.event.method', '').indexOf('hydrators.') === 0;
if (!isRunningOnHydrator) {
return ZapierPromise.reject(new Error('Cannot stash files outside an hydration function/method.'));
const isRunningOnCreate = _.get(input, '_zapier.event.method', '').indexOf('creates.') === 0;

if (!isRunningOnHydrator && !isRunningOnCreate) {
return ZapierPromise.reject(new Error('Files can only be stashed within a create or hydration function/method.'));
}

return rpc('get_presigned_upload_post_data')
Expand Down
46 changes: 44 additions & 2 deletions test/tools/file-stasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('file upload', () => {
.catch(done);
});

it('should fail if not being called from an hydrator event', (done) => {
it('should fail if being called from a trigger event', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Please consider adding a test for a search (and maybe passing ones for hydration and create) since you're being specific here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All set!

const pollingStashFile = createFileStasher({
_zapier: {
rpc,
Expand All @@ -124,7 +124,49 @@ describe('file upload', () => {
pollingStashFile(file)
.then(() => done(new Error('this should have exploded')))
.catch(err => {
should(err.message).containEql('Cannot stash files outside an hydration function/method');
should(err.message).containEql('Files can only be stashed within a create or hydration function/method');
done();
})
.catch(done);
});

it('should fail if being called from a search event', (done) => {
const stashFileTest = createFileStasher({
_zapier: {
rpc,
event: {
method: 'search.test.operation.perform',
},
},
});

const file = fs.createReadStream(path.join(__dirname, 'test.txt'));
stashFileTest(file)
.then(() => done(new Error('this should have exploded')))
.catch(err => {
should(err.message).containEql('Files can only be stashed within a create or hydration function/method');
done();
})
.catch(done);
});

it('should work if being called from a create/action event', (done) => {
mocky.mockRpcCall(mocky.fakeSignedPostData);
mocky.mockUpload();

const stashFileTest = createFileStasher({
_zapier: {
rpc,
event: {
method: 'creates.test.operation.perform',
},
},
});

const file = 'hello world this is a plain blob of text';
stashFileTest(file)
.then((url) => {
should(url).eql(`${mocky.fakeSignedPostData.url}${mocky.fakeSignedPostData.fields.key}`);
done();
})
.catch(done);
Expand Down