diff --git a/packages/zcli-apps/tests/functional/create.test.ts b/packages/zcli-apps/tests/functional/create.test.ts index 820b3e55..c600ec6a 100644 --- a/packages/zcli-apps/tests/functional/create.test.ts +++ b/packages/zcli-apps/tests/functional/create.test.ts @@ -5,6 +5,7 @@ import * as createAppUtils from '../../src/utils/createApp' import * as appConfig from '../../src/utils/appConfig' import * as requestUtils from '../../../zcli-core/src/lib/requestUtils' import * as packageUtil from '../../src/lib/package' +import env from './env' describe('apps', function () { const singleProductApp = path.join(__dirname, 'mocks/single_product_app') @@ -27,11 +28,7 @@ describe('apps', function () { .stub(createAppUtils, 'getManifestAppName', () => 'importantAppName') .stub(requestUtils, 'getSubdomain', () => Promise.resolve('z3ntest')) .stub(appConfig, 'setConfig', () => Promise.resolve()) - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .do(() => { createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') uploadAppPkgStub.onFirstCall().resolves({ id: 817 }) @@ -67,11 +64,7 @@ describe('apps', function () { describe('with single app', () => { test .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .do(() => { createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') uploadAppPkgStub.onFirstCall().resolves({ id: 819 }) @@ -97,11 +90,7 @@ describe('apps', function () { describe('with requirements-only app', () => { test .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .do(() => { createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') uploadAppPkgStub.onFirstCall().resolves({ id: 819 }) @@ -126,11 +115,7 @@ describe('apps', function () { describe('with single app', () => { test .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .do(() => { createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') uploadAppPkgStub.onFirstCall().resolves({ id: 819 }) @@ -153,11 +138,7 @@ describe('apps', function () { describe('with requirements-only app', () => { test .stub(packageUtil, 'createAppPkg', () => createAppPkgStub) - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .do(() => { createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled') uploadAppPkgStub.onFirstCall().resolves({ id: 819 }) diff --git a/packages/zcli-apps/tests/functional/env.ts b/packages/zcli-apps/tests/functional/env.ts new file mode 100644 index 00000000..785b2b54 --- /dev/null +++ b/packages/zcli-apps/tests/functional/env.ts @@ -0,0 +1,5 @@ +export default { + ZENDESK_SUBDOMAIN: 'z3ntest', + ZENDESK_EMAIL: 'admin@z3ntest.com', + ZENDESK_API_TOKEN: '123456' +} diff --git a/packages/zcli-apps/tests/functional/package.test.ts b/packages/zcli-apps/tests/functional/package.test.ts index c6fdaa4a..d97f86ba 100644 --- a/packages/zcli-apps/tests/functional/package.test.ts +++ b/packages/zcli-apps/tests/functional/package.test.ts @@ -3,15 +3,12 @@ import * as path from 'path' import * as fs from 'fs' import * as readline from 'readline' import * as AdmZip from 'adm-zip' +import env from './env' describe('package', function () { const appPath = path.join(__dirname, 'mocks/single_product_app') test - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .nock('https://z3ntest.zendesk.com', api => { api .post('/api/v2/apps/validate') @@ -25,11 +22,7 @@ describe('package', function () { }) test - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .nock('https://z3ntest.zendesk.com', api => { api .post('/api/v2/apps/validate') @@ -69,11 +62,7 @@ describe('zcliignore', function () { }) test - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .nock('https://z3ntest.zendesk.com', api => { api .post('/api/v2/apps/validate') diff --git a/packages/zcli-apps/tests/functional/validate.test.ts b/packages/zcli-apps/tests/functional/validate.test.ts index 3673ae72..f50ae177 100644 --- a/packages/zcli-apps/tests/functional/validate.test.ts +++ b/packages/zcli-apps/tests/functional/validate.test.ts @@ -1,13 +1,10 @@ import { expect, test } from '@oclif/test' import * as path from 'path' +import env from './env' describe('validate', function () { test - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .nock('https://z3ntest.zendesk.com', api => { api .post('/api/v2/apps/validate') @@ -20,11 +17,7 @@ describe('validate', function () { }) test - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .nock('https://z3ntest.zendesk.com', api => { api .post('/api/v2/apps/validate') diff --git a/packages/zcli-core/src/lib/auth.test.ts b/packages/zcli-core/src/lib/auth.test.ts index 9f13420b..b84aab6d 100644 --- a/packages/zcli-core/src/lib/auth.test.ts +++ b/packages/zcli-core/src/lib/auth.test.ts @@ -6,10 +6,6 @@ import Auth from './auth' import SecureStore from './secureStore' import { Profile } from '../types' -const mockCreateBasicAuthToken = (...args: any[]) => { - return `Basic ${args[0]}_${args[1]}_base64` -} - describe('Auth', () => { describe('createBasicAuthToken', () => { test @@ -17,7 +13,7 @@ describe('Auth', () => { const auth = new Auth() expect( await auth.createBasicAuthToken('test@zendesk.com', '123456') - ).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbToxMjM0NTY=') + ).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') }) }) @@ -35,19 +31,8 @@ describe('Auth', () => { ZENDESK_EMAIL: 'test@zendesk.com', ZENDESK_API_TOKEN: 'test_api_token' }) - .stub(auth, 'createBasicAuthToken', mockCreateBasicAuthToken) .it('should return basic token if ZENDESK_EMAIL and ZENDESK_API_TOKEN is set', async () => { - expect(await auth.getAuthorizationToken()).to.equal('Basic test@zendesk.com/token_test_api_token_base64') - }) - - test - .env({ - ZENDESK_EMAIL: 'test@zendesk.com', - ZENDESK_PASSWORD: '123456' - }) - .stub(auth, 'createBasicAuthToken', mockCreateBasicAuthToken) - .it('should return basic token if ZENDESK_EMAIL and ZENDESK_PASSWORD is set', async () => { - expect(await auth.getAuthorizationToken()).to.equal('Basic test@zendesk.com_123456_base64') + expect(await auth.getAuthorizationToken()).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjp0ZXN0X2FwaV90b2tlbg==') }) test @@ -74,10 +59,20 @@ describe('Auth', () => { ZENDESK_API_TOKEN: 'test_api_token', ZENDESK_PASSWORD: '123456' }) - .stub(auth, 'createBasicAuthToken', mockCreateBasicAuthToken) .it('should give precedence to ZENDESK_EMAIL and ZENDESK_API_TOKEN when ZENDESK_OAUTH_TOKEN is not defined', async () => { - expect(await auth.getAuthorizationToken()).to.equal('Basic test@zendesk.com/token_test_api_token_base64') + expect(await auth.getAuthorizationToken()).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjp0ZXN0X2FwaV90b2tlbg==') + }) + + test + .env({ + ZENDESK_EMAIL: 'test@zendesk.com', + ZENDESK_PASSWORD: '123456' + }) + .do(async () => { + await auth.getAuthorizationToken() }) + .catch(chalk.red('Basic authentication of type \'password\' is not supported.')) + .it('should throw an error if only ZENDESK_EMAIL and ZENDESK_PASSWORD are set - basic auth with password not supported') }) describe('loginInteractively', () => { @@ -93,12 +88,11 @@ describe('Auth', () => { .stub(CliUx.ux, 'prompt', () => promptStub) .stub(auth.secureStore, 'setPassword', () => Promise.resolve()) .stub(auth, 'setLoggedInProfile', () => Promise.resolve()) - .stub(auth, 'createBasicAuthToken', mockCreateBasicAuthToken) .nock('https://z3ntest.zendesk.com', api => { api .get('/api/v2/account/settings.json') .reply(function () { - expect(this.req.headers.authorization).to.equal('Basic test@zendesk.com_123456_base64') + expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') return [200] }) }) @@ -116,12 +110,11 @@ describe('Auth', () => { .stub(CliUx.ux, 'prompt', () => promptStub) .stub(auth.secureStore, 'setPassword', () => Promise.resolve()) .stub(auth, 'setLoggedInProfile', () => Promise.resolve()) - .stub(auth, 'createBasicAuthToken', mockCreateBasicAuthToken) .nock('https://z3ntest.example.com', api => { api .get('/api/v2/account/settings.json') .reply(function () { - expect(this.req.headers.authorization).to.equal('Basic test@zendesk.com_123456_base64') + expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') return [200] }) }) @@ -138,12 +131,11 @@ describe('Auth', () => { .stub(CliUx.ux, 'prompt', () => promptStub) .stub(auth.secureStore, 'setPassword', () => Promise.resolve()) .stub(auth, 'setLoggedInProfile', () => Promise.resolve()) - .stub(auth, 'createBasicAuthToken', mockCreateBasicAuthToken) .nock('https://z3ntest.example.com', api => { api .get('/api/v2/account/settings.json') .reply(function () { - expect(this.req.headers.authorization).to.equal('Basic test@zendesk.com_123456_base64') + expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') return [200] }) }) diff --git a/packages/zcli-core/src/lib/auth.ts b/packages/zcli-core/src/lib/auth.ts index 61bf3b8a..9f8d9913 100644 --- a/packages/zcli-core/src/lib/auth.ts +++ b/packages/zcli-core/src/lib/auth.ts @@ -48,7 +48,7 @@ export default class Auth { } createBasicAuthToken (user: string, secret: string, secretType: SecretType = SecretType.Token) { - const basicBase64 = (str: string) => ` Basic ${Buffer.from(str).toString('base64')}` + const basicBase64 = (str: string) => `Basic ${Buffer.from(str).toString('base64')}` if (secretType === SecretType.Token) { return basicBase64(`${user}/${SecretType.Token}:${secret}`) } diff --git a/packages/zcli-themes/tests/functional/env.ts b/packages/zcli-themes/tests/functional/env.ts index 546191c9..785b2b54 100644 --- a/packages/zcli-themes/tests/functional/env.ts +++ b/packages/zcli-themes/tests/functional/env.ts @@ -1,5 +1,5 @@ export default { ZENDESK_SUBDOMAIN: 'z3ntest', ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password + ZENDESK_API_TOKEN: '123456' } diff --git a/packages/zcli-themes/tests/functional/preview.test.ts b/packages/zcli-themes/tests/functional/preview.test.ts index 83f30649..7b357131 100644 --- a/packages/zcli-themes/tests/functional/preview.test.ts +++ b/packages/zcli-themes/tests/functional/preview.test.ts @@ -6,6 +6,7 @@ import * as nock from 'nock' import axios from 'axios' import { cloneDeep } from 'lodash' import PreviewCommand from '../../src/commands/themes/preview' +import env from './env' describe('themes:preview', function () { const baseThemePath = path.join(__dirname, 'mocks/base_theme') @@ -15,11 +16,7 @@ describe('themes:preview', function () { const preview = test .stdout() - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .nock('https://z3ntest.zendesk.com', api => { api .put('/hc/api/internal/theming/local_preview') @@ -79,11 +76,7 @@ describe('themes:preview', function () { test .stdout() - .env({ - ZENDESK_SUBDOMAIN: 'z3ntest', - ZENDESK_EMAIL: 'admin@z3ntest.com', - ZENDESK_PASSWORD: '123456' // the universal password - }) + .env(env) .it('should report template errors', async (ctx) => { nock('https://z3ntest.zendesk.com').put('/hc/api/internal/theming/local_preview').reply(400, { template_errors: {