Skip to content

Commit

Permalink
fix: inconsistent behaviour when ZENDESK_DOMAIN env is set
Browse files Browse the repository at this point in the history
When ZENDESK_DOMAIN and ZENDESK_SUBDOMAIN are not set, domain is taken from an active profile. This works just fine. However when domain is taken from the env, the request sends a corrupted payload and server responds with 400. This is a workaround to make the function behave consistently whether env or profile is being used.
  • Loading branch information
romeodemeteriojr committed Jul 11, 2024
1 parent 97f6ce9 commit 7d1f234
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
5 changes: 4 additions & 1 deletion packages/zcli-apps/tests/functional/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('apps', function () {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
.stub(createAppUtils, 'getManifestAppName', () => 'importantAppName')
.stub(requestUtils, 'getSubdomain', () => Promise.resolve('z3ntest'))
.stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined))
.stub(requestUtils, 'getDomain', () => Promise.resolve(undefined))
.stub(appConfig, 'setConfig', () => Promise.resolve())
.env(env)
.do(() => {
Expand Down Expand Up @@ -64,6 +65,8 @@ describe('apps', function () {
describe('with single app', () => {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
.stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined))
.stub(requestUtils, 'getDomain', () => Promise.resolve(undefined))
.env(env)
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
Expand Down
7 changes: 7 additions & 0 deletions packages/zcli-apps/tests/functional/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import * as fs from 'fs'
import * as readline from 'readline'
import * as AdmZip from 'adm-zip'
import env from './env'
import * as requestUtils from '../../../zcli-core/src/lib/requestUtils'

describe('package', function () {
const appPath = path.join(__dirname, 'mocks/single_product_app')
test
.stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined))
.stub(requestUtils, 'getDomain', () => Promise.resolve(undefined))
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
Expand All @@ -22,6 +25,8 @@ describe('package', function () {
})

test
.stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined))
.stub(requestUtils, 'getDomain', () => Promise.resolve(undefined))
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
Expand Down Expand Up @@ -62,6 +67,8 @@ describe('zcliignore', function () {
})

test
.stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined))
.stub(requestUtils, 'getDomain', () => Promise.resolve(undefined))
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
Expand Down
5 changes: 5 additions & 0 deletions packages/zcli-apps/tests/functional/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { expect, test } from '@oclif/test'
import * as path from 'path'
import env from './env'
import * as requestUtils from '../../../zcli-core/src/lib/requestUtils'

describe('validate', function () {
test
.stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined))
.stub(requestUtils, 'getDomain', () => Promise.resolve(undefined))
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
Expand All @@ -17,6 +20,8 @@ describe('validate', function () {
})

test
.stub(requestUtils, 'getSubdomain', () => Promise.resolve(undefined))
.stub(requestUtils, 'getDomain', () => Promise.resolve(undefined))
.env(env)
.nock('https://z3ntest.zendesk.com', api => {
api
Expand Down
29 changes: 20 additions & 9 deletions packages/zcli-core/src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import Auth from './auth'
import { CLIError } from '@oclif/core/lib/errors'
import * as chalk from 'chalk'
import { EnvVars, varExists } from './env'
import { getBaseUrl, getDomain, getSubdomain } from './requestUtils'
import { getBaseUrl, getDomain as getProfileDomain, getSubdomain as getProfileSubdomain } from './requestUtils'

const MSG_ENV_OR_LOGIN = 'Set the following environment variables: ZENDESK_SUBDOMAIN, ZENDESK_EMAIL, ZENDESK_API_TOKEN. Or try logging in via `zcli login -i`'
const ERR_NO_AUTH_TOKEN = `No authorization token found. ${MSG_ENV_OR_LOGIN}`
const ERR_AUTH_FAILED = `Authorization failed. ${MSG_ENV_OR_LOGIN}`
const ERR_ENV_SUBDOMAIN_NOT_FOUND = `No subdomain found. ${MSG_ENV_OR_LOGIN}`

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const requestAPI = async (url: string, options: any = {}, json = false) => {
let auth
if (
varExists(EnvVars.SUBDOMAIN, EnvVars.EMAIL, EnvVars.PASSWORD) ||
varExists(EnvVars.SUBDOMAIN, EnvVars.EMAIL, EnvVars.API_TOKEN) ||
varExists(EnvVars.SUBDOMAIN, EnvVars.OAUTH_TOKEN)
varExists(EnvVars.SUBDOMAIN, EnvVars.OAUTH_TOKEN) ||
varExists(EnvVars.SUBDOMAIN, EnvVars.EMAIL, EnvVars.API_TOKEN)
) {
auth = new Auth()
} else {
Expand All @@ -22,23 +26,30 @@ export const requestAPI = async (url: string, options: any = {}, json = false) =
}

const authToken = await auth.getAuthorizationToken()
const subdomain = process.env[EnvVars.SUBDOMAIN] || (await getSubdomain(auth))
const domain = process.env[EnvVars.SUBDOMAIN] ? process.env[EnvVars.DOMAIN] : await getDomain(auth)
if (!authToken) throw new CLIError(chalk.red(ERR_NO_AUTH_TOKEN))

const profileSubdomain = await getProfileSubdomain(auth)
const subdomain = process.env[EnvVars.SUBDOMAIN] || profileSubdomain
if (!subdomain) throw new CLIError(chalk.red(ERR_ENV_SUBDOMAIN_NOT_FOUND))

const profileDomain = await getProfileDomain(auth)
// If subdomain is set, use domain env even if not set. Otherwise, use profile domain.
const domain = process.env[EnvVars.SUBDOMAIN] ? process.env[EnvVars.DOMAIN] : profileDomain

if (options.headers) {
options.headers = { Authorization: authToken, ...options.headers }
} else {
options.headers = { Authorization: authToken }
}

if (authToken && subdomain) {
const baseURL = getBaseUrl(subdomain, domain)
return axios.request({
baseURL: getBaseUrl(subdomain, domain),
baseURL,
url,
validateStatus: function (status) { return status < 500 },
...options
})
}

throw new CLIError(chalk.red('Authorization Failed, try logging in via `zcli login -i`!'))
throw new CLIError(chalk.red(ERR_AUTH_FAILED))
}

0 comments on commit 7d1f234

Please sign in to comment.