Skip to content

Commit

Permalink
temporary commit - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
romeodemeteriojr committed Jul 10, 2024
1 parent 83f0b5f commit 6e7bbb7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 80 deletions.
1 change: 1 addition & 0 deletions packages/zcli-core/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const CONFIG_PATH = path.join(HOME_DIR, '.zcli')
export default class Config {
async ensureConfigFile () {
if (!await fs.pathExists(CONFIG_PATH)) {
console.log('Creating config file')
await fs.outputJson(CONFIG_PATH, {})
}
}
Expand Down
159 changes: 81 additions & 78 deletions packages/zcli-core/src/lib/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,96 @@
import { expect, test } from '@oclif/test'
import { createRequestConfig, requestAPI } from './request'
import Auth from './auth'
import { HttpStatusCode } from 'axios'
import { createRequestConfig } from './request'
import * as requestUtils from './requestUtils'
// import { createRequestConfig, requestAPI } from './request'
// import Auth from './auth'
// import { HttpStatusCode } from 'axios'

describe('createRequestConfig', () => {
test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_OAUTH_TOKEN: 'good_token'
})
.stub(requestUtils, 'getSubdomain', () => 'fake')
.stub(requestUtils, 'getDomain', () => 'fake.com')
.it('should create a request with an OAuth token', async () => {
const req = await createRequestConfig('api/v2/me')
console.log('req: ', req)
expect(req.headers.Authorization).to.equal('Bearer good_token')
})

test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_OAUTH_TOKEN: 'good_token'
})
.it('should be able to attach extra headers to request', async () => {
const req = await createRequestConfig('api/v2/me', {
headers: { foo: 'bar' },
method: 'GET'
})
expect(req.headers.Authorization).to.equal('Bearer good_token')
expect(req.headers.foo).to.equal('bar')
})

test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_DOMAIN: 'expected.com',
ZENDESK_EMAIL: 'test@zendesk.com',
ZENDESK_API_TOKEN: '123456'
})
.it('should be able to create a request with a Basic auth token', async () => {
const req = await createRequestConfig('api/v2/me', {})
expect(req.headers.Authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
})

test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_DOMAIN: 'expected.com',
ZENDESK_EMAIL: 'test@zendesk.com',
ZENDESK_API_TOKEN: '123456'
})
.stub(Auth, 'getLoggedInProfile', () => ({
subdomain: 'fake',
domain: 'fake.com'
}))
.it('should create a request with the correct domain', async () => {
const req = await createRequestConfig('api/v2/me')
expect(req.baseURL).to.equal('https://z3ntest.expected.com')
})

test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: 'test@zendesk.com',
ZENDESK_API_TOKEN: '123456'
})
.stub(Auth, 'getLoggedInProfile', () => ({
subdomain: 'fake',
domain: 'fake.com'
}))
.it('should use the default domain if ZENDESK_SUBDOMAIN is provided and ZENDESK_DOMAIN is not provided, not the profile domain', async () => {
const req = await createRequestConfig('api/v2/me')
expect(req.baseURL).to.equal('https://z3ntest.zendesk.com')
})
// test
// .env({
// ZENDESK_SUBDOMAIN: 'z3ntest',
// ZENDESK_OAUTH_TOKEN: 'good_token'
// })
// .it('should be able to attach extra headers to request', async () => {
// const req = await createRequestConfig('api/v2/me', {
// headers: { foo: 'bar' },
// method: 'GET'
// })
// expect(req.headers.Authorization).to.equal('Bearer good_token')
// expect(req.headers.foo).to.equal('bar')
// })
//
// test
// .env({
// ZENDESK_SUBDOMAIN: 'z3ntest',
// ZENDESK_DOMAIN: 'expected.com',
// ZENDESK_EMAIL: 'test@zendesk.com',
// ZENDESK_API_TOKEN: '123456'
// })
// .it('should be able to create a request with a Basic auth token', async () => {
// const req = await createRequestConfig('api/v2/me', {})
// expect(req.headers.Authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=')
// })
//
// test
// .env({
// ZENDESK_SUBDOMAIN: 'z3ntest',
// ZENDESK_DOMAIN: 'expected.com',
// ZENDESK_EMAIL: 'test@zendesk.com',
// ZENDESK_API_TOKEN: '123456'
// })
// .stub(Auth, 'getLoggedInProfile', () => ({
// subdomain: 'fake',
// domain: 'fake.com'
// }))
// .it('should create a request with the correct domain', async () => {
// const req = await createRequestConfig('api/v2/me')
// expect(req.baseURL).to.equal('https://z3ntest.expected.com')
// })
//
// test
// .env({
// ZENDESK_SUBDOMAIN: 'z3ntest',
// ZENDESK_EMAIL: 'test@zendesk.com',
// ZENDESK_API_TOKEN: '123456'
// })
// .stub(Auth, 'getLoggedInProfile', () => ({
// subdomain: 'fake',
// domain: 'fake.com'
// }))
// .it('should use the default domain if ZENDESK_SUBDOMAIN is provided and ZENDESK_DOMAIN is not provided, not the profile domain', async () => {
// const req = await createRequestConfig('api/v2/me')
// expect(req.baseURL).to.equal('https://z3ntest.zendesk.com')
// })
})

describe('requestAPI', () => {
test
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: 'test@zendesk.com',
ZENDESK_API_TOKEN: '123456',
ZENDESK_OAUTH_TOKEN: 'good_token'
})
.nock('https://z3ntest.zendesk.com', api => {
api
.get('/api/v2/me')
.reply(HttpStatusCode.Ok)
})
.it('should call an http endpoint', async () => {
const response = await requestAPI('api/v2/me', { method: 'GET' })
expect(response.status).to.equal(HttpStatusCode.Ok)
})
})
// describe('requestAPI', () => {
// test
// .env({
// ZENDESK_SUBDOMAIN: 'z3ntest',
// ZENDESK_EMAIL: 'test@zendesk.com',
// ZENDESK_API_TOKEN: '123456',
// ZENDESK_OAUTH_TOKEN: 'good_token'
// })
// .nock('https://z3ntest.zendesk.com', api => {
// api
// .get('/api/v2/me')
// .reply(HttpStatusCode.Ok)
// })
// .it('should call an http endpoint', async () => {
// const response = await requestAPI('api/v2/me', { method: 'GET' })
// expect(response.status).to.equal(HttpStatusCode.Ok)
// })
// })
4 changes: 2 additions & 2 deletions packages/zcli-core/src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Auth from './auth'
import { CLIError } from '@oclif/core/lib/errors'
import * as chalk from 'chalk'
import { EnvVars, varExists } from './env'
import { getBaseUrl, getDomain as getProfileDomain, getSubdomain as getProfileSubdomain } from './requestUtils'
import { getBaseUrl, getDomain, getSubdomain } 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_AUTH_FAILED = `Authorization failed. ${MSG_ENV_OR_LOGIN}`
Expand All @@ -24,7 +24,7 @@ export const createRequestConfig = async (url: string, options: any = {}) => {
auth = new Auth({ secureStore })
}
const [authToken, profileSubdomain, profileDomain] =
await Promise.all([auth.getAuthorizationToken(), getProfileSubdomain(auth), getProfileDomain(auth)])
await Promise.all([auth.getAuthorizationToken(), getSubdomain(auth), getDomain(auth)])
if (!authToken) throw new CLIError(chalk.red(ERR_AUTH_FAILED))
const subdomain = process.env[EnvVars.SUBDOMAIN] || profileSubdomain
if (!subdomain) throw new CLIError(chalk.red(ERR_ENV_SUBDOMAIN_NOT_FOUND))
Expand Down

0 comments on commit 6e7bbb7

Please sign in to comment.