Skip to content

Commit

Permalink
fix: fix unit and functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romeodemeteriojr committed Jul 11, 2024
1 parent 415b5e1 commit e4eb869
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 87 deletions.
31 changes: 6 additions & 25 deletions packages/zcli-apps/tests/functional/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 817 })
Expand Down Expand Up @@ -67,11 +64,7 @@ describe('apps', function () {
describe('with single app', () => {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
Expand All @@ -97,11 +90,7 @@ describe('apps', function () {
describe('with requirements-only app', () => {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
Expand All @@ -126,11 +115,7 @@ describe('apps', function () {
describe('with single app', () => {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
Expand All @@ -153,11 +138,7 @@ describe('apps', function () {
describe('with requirements-only app', () => {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
Expand Down
5 changes: 5 additions & 0 deletions packages/zcli-apps/tests/functional/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_API_TOKEN: '123456'
}
19 changes: 4 additions & 15 deletions packages/zcli-apps/tests/functional/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
.post('/api/v2/apps/validate')
Expand All @@ -25,11 +22,7 @@ describe('package', function () {
})

test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
.post('/api/v2/apps/validate')
Expand Down Expand Up @@ -69,11 +62,7 @@ describe('zcliignore', function () {
})

test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
.post('/api/v2/apps/validate')
Expand Down
13 changes: 3 additions & 10 deletions packages/zcli-apps/tests/functional/validate.test.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
.post('/api/v2/apps/validate')
Expand All @@ -20,11 +17,7 @@ describe('validate', function () {
})

test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
.post('/api/v2/apps/validate')
Expand Down
42 changes: 17 additions & 25 deletions packages/zcli-core/src/lib/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ 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
.it('should create basic auth token', async () => {
const auth = new Auth()
expect(
await auth.createBasicAuthToken('[email protected]', '123456')
).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbToxMjM0NTY=')
).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
})
})

Expand All @@ -35,19 +31,8 @@ describe('Auth', () => {
ZENDESK_EMAIL: '[email protected]',
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 [email protected]/token_test_api_token_base64')
})

test
.env({
ZENDESK_EMAIL: '[email protected]',
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 [email protected]_123456_base64')
expect(await auth.getAuthorizationToken()).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjp0ZXN0X2FwaV90b2tlbg==')
})

test
Expand All @@ -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 [email protected]/token_test_api_token_base64')
expect(await auth.getAuthorizationToken()).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjp0ZXN0X2FwaV90b2tlbg==')
})

test
.env({
ZENDESK_EMAIL: '[email protected]',
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', () => {
Expand All @@ -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 [email protected]_123456_base64')
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
return [200]
})
})
Expand All @@ -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 [email protected]_123456_base64')
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
return [200]
})
})
Expand All @@ -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 [email protected]_123456_base64')
expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
return [200]
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/zcli-core/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/zcli-themes/tests/functional/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
ZENDESK_API_TOKEN: '123456'
}
13 changes: 3 additions & 10 deletions packages/zcli-themes/tests/functional/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -15,11 +16,7 @@ describe('themes:preview', function () {

const preview = test
.stdout()
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
ZENDESK_PASSWORD: '123456' // the universal password
})
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
.put('/hc/api/internal/theming/local_preview')
Expand Down Expand Up @@ -79,11 +76,7 @@ describe('themes:preview', function () {

test
.stdout()
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: '[email protected]',
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: {
Expand Down

0 comments on commit e4eb869

Please sign in to comment.