Skip to content

Commit

Permalink
Tests for register command upgraded.
Browse files Browse the repository at this point in the history
  • Loading branch information
szchenghuang committed Oct 14, 2024
1 parent 08fbf8b commit 2b9ee8e
Showing 1 changed file with 174 additions and 153 deletions.
327 changes: 174 additions & 153 deletions packages/cli/src/tests/register.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require('fs');
const oclif = require('@oclif/test');
const nock = require('nock');
const { expect } = require('chai');
const { captureOutput, runCommand } = require('@oclif/test');
const {
BASE_ENDPOINT,
MIN_TITLE_LENGTH,
Expand Down Expand Up @@ -36,182 +38,201 @@ describe('RegisterCommand', () => {
restoreDeployKey();
});

function getTestObj() {
return oclif.test.nock(BASE_ENDPOINT, (mockApi) =>
mockApi
.get('/api/platform/cli/apps/fields-choices')
.reply(200, registerFieldChoices)
);
function setup() {
nock(BASE_ENDPOINT)
.get('/api/platform/cli/apps/fields-choices')
.reply(200, registerFieldChoices);
}

describe('zapier register should enforce character minimum on title', function () {
getTestObj()
.command(['register', 't'])
.catch((ctx) => {
oclif
.expect(ctx.message)
.to.contain(
`Please provide a title that is ${MIN_TITLE_LENGTH} characters or more.`
);
})
.it('zapier register should enforce character minimum on title flag');
it('zapier register should enforce character minimum on title flag', async function () {
setup();

await captureOutput(async function () {
const { error } = await runCommand(['register', 't']);
expect(error.message).to.contain(
`Please provide a title that is ${MIN_TITLE_LENGTH} characters or more.`
);
});
});
});

describe('zapier register should enforce character limits on flags', function () {
getTestObj()
.command([
'register',
'--desc',
'Cupidatat non elit non enim enim cupidatat ea in consequat exercitation do nisi occaecat amet id deserunt nostrud quis aliqua id fugiat sit elit.',
])
.catch((ctx) => {
oclif
.expect(ctx.message)
.to.contain(
`Please provide a description that is ${MAX_DESCRIPTION_LENGTH} characters or less.`
);
})
.it('zapier register should enforce character limit on desc flag');
it('zapier register should enforce character limit on desc flag', async function () {
setup();

await captureOutput(async function () {
const { error } = await runCommand([
'register',
'--desc',
'Cupidatat non elit non enim enim cupidatat ea in consequat exercitation do nisi occaecat amet id deserunt nostrud quis aliqua id fugiat sit elit.',
]);
expect(error.message).to.contain(
`Please provide a description that is ${MAX_DESCRIPTION_LENGTH} characters or less.`
);
});
});
});

describe('zapier register should validate enum fields that are passed in as flags', function () {
getTestObj()
.command(['register', '--role', 'invalidRole'])
.catch((ctx) => {
oclif
.expect(ctx.message)
.to.contain('invalidRole is not a valid value for role');
})
.it('zapier register should throw error for invalid role');

getTestObj()
.command(['register', '--category', 'invalidCategory'])
.catch((ctx) => {
oclif
.expect(ctx.message)
.to.contain('invalidCategory is not a valid value for category');
})
.it('zapier register should throw error for invalid category');

getTestObj()
.command(['register', '--audience', 'invalidAudience'])
.catch((ctx) => {
oclif
.expect(ctx.message)
.to.contain('invalidAudience is not a valid value for audience');
})
.it('zapier register should throw error for invalid audience');
it('zapier register should throw error for invalid role', async function () {
setup();

await captureOutput(async function () {
const { error } = await runCommand([
'register',
'--role',
'invalidRole',
]);
expect(error.message).to.contain(
'invalidRole is not a valid value for role'
);
});
});

it('zapier register should throw error for invalid category', async function () {
setup();

await captureOutput(async function () {
const { error } = await runCommand([
'register',
'--category',
'invalidCategory',
]);
expect(error.message).to.contain(
'invalidCategory is not a valid value for category'
);
});
});

it('zapier register should throw error for invalid audience', async function () {
setup();

await captureOutput(async function () {
const { error } = await runCommand([
'register',
'--audience',
'invalidAudience',
]);
expect(error.message).to.contain(
'invalidAudience is not a valid value for audience'
);
});
});
});

describe('zapier register should accept all data via flags', function () {
function getTestObj() {
return oclif.test.nock(BASE_ENDPOINT, (mockApi) =>
mockApi
.get('/api/platform/cli/apps/fields-choices')
.reply(200, registerFieldChoices)
.post('/api/platform/cli/apps')
.query({ formId: 'create' })
.reply(201, privateApp)
);
function setup() {
return nock(BASE_ENDPOINT)
.get('/api/platform/cli/apps/fields-choices')
.reply(200, registerFieldChoices)
.post('/api/platform/cli/apps')
.query({ formId: 'create' })
.reply(201, privateApp);
}

getTestObj()
.stdout()
.stderr()
.command([
'register',
'My Cool Integration',
'--desc',
'My Cool Integration helps you integrate your apps with the apps that you need.',
'--url',
'https://www.zapier.com',
'--audience',
'private',
'--role',
'employee',
'--category',
'marketing-automation',
'--subscribe',
])
.it(
'zapier register should successfully register an app with all data provided'
);
it('zapier register should successfully register an app with all data provided', async function () {
setup();

await captureOutput(async function () {
const { error } = await runCommand([
'register',
'My Cool Integration',
'--desc',
'My Cool Integration helps you integrate your apps with the apps that you need.',
'--url',
'https://www.zapier.com',
'--audience',
'private',
'--role',
'employee',
'--category',
'marketing-automation',
'--subscribe',
]);

expect(error.message).to.contain(
'invalidAudience is not a valid value for audience'
);
});
});
});

describe('zapier register should update existing app', function () {
function getTestObj(isPublic) {
function setup(isPublic) {
const exportedApp = isPublic ? publicApp : privateApp;
return oclif.test.nock(BASE_ENDPOINT, (mockApi) =>
mockApi
.get('/api/platform/cli/apps/fields-choices')
.reply(200, registerFieldChoices)
.get(`/api/platform/cli/apps/${exportedApp.id}`)
.reply(200, exportedApp)
.put(`/api/platform/cli/apps/${exportedApp.id}`, {
title: 'Hello',
description: 'Helps you in some way.',
homepage_url: 'https://example.com',
intention: 'global',
role: 'contractor',
app_category: 'productivity',
})
.optionally()
.reply(201, exportedApp)
);
return nock(BASE_ENDPOINT)
.get('/api/platform/cli/apps/fields-choices')
.reply(200, registerFieldChoices)
.get(`/api/platform/cli/apps/${exportedApp.id}`)
.reply(200, exportedApp)
.put(`/api/platform/cli/apps/${exportedApp.id}`, {
title: 'Hello',
description: 'Helps you in some way.',
homepage_url: 'https://example.com',
intention: 'global',
role: 'contractor',
app_category: 'productivity',
})
.optionally()
.reply(201, exportedApp);
}

fs.writeFileSync(
APP_RC_FILE,
`{"id":${privateApp.id},"key":"App${privateApp.id}"}`
);

getTestObj()
.stdout()
.stderr()
.command([
'register',
'Hello',
'-D',
'Helps you in some way.',
'-u',
'https://example.com',
'-a',
'global',
'-r',
'contractor',
'-c',
'productivity',
'--yes',
])
.it('zapier register --yes should update an app without prompts');

getTestObj(true)
.stdout()
.stderr()
.command([
'register',
'Hello',
'-D',
'Helps you in some way.',
'-u',
'https://example.com',
'-a',
'global',
'-r',
'contractor',
'-c',
'productivity',
'--yes',
])
.catch((ctx) => {
oclif
.expect(ctx.message)
.to.contain(
"You can't edit settings for this integration. To edit your integration details on Zapier's public app directory, email [email protected]."
);
})
.it(
'zapier register should not allow a user to update a pre-existing public app'
);
it('zapier register should successfully register an app with all data provided', async function () {
setup();

await captureOutput(async function () {
const { error } = await runCommand([
'register',
'Hello',
'-D',
'Helps you in some way.',
'-u',
'https://example.com',
'-a',
'global',
'-r',
'contractor',
'-c',
'productivity',
'--yes',
]);

expect(error.message).to.contain(
'zapier register --yes should update an app without prompts'
);
});
});

it('zapier register should not allow a user to update a pre-existing public app', async function () {
setup(true);

await captureOutput(async function () {
const { error } = await runCommand([
'register',
'Hello',
'-D',
'Helps you in some way.',
'-u',
'https://example.com',
'-a',
'global',
'-r',
'contractor',
'-c',
'productivity',
'--yes',
]);

expect(error.message).to.contain(
"You can't edit settings for this integration. To edit your integration details on Zapier's public app directory, email [email protected]."
);
});
});
});
});

0 comments on commit 2b9ee8e

Please sign in to comment.