UNPKG

@zendesk/zcli-core

Version:

ZCLI core libraries and services

174 lines (173 loc) 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const test_1 = require("@oclif/test"); const sinon = require("sinon"); const axios_1 = require("axios"); const request_1 = require("./request"); const requestUtils = require("./requestUtils"); const auth_1 = require("./auth"); describe('createRequestConfig', () => { test_1.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 (0, request_1.createRequestConfig)('api/v2/me'); (0, test_1.expect)(req.headers.Authorization).to.equal('Bearer good_token'); }); test_1.test .env({ ZENDESK_SUBDOMAIN: 'z3ntest', ZENDESK_OAUTH_TOKEN: 'good_token' }) .stub(requestUtils, 'getSubdomain', () => 'fake') .stub(requestUtils, 'getDomain', () => 'fake.com') .it('should be able to attach extra headers to request', async () => { const req = await (0, request_1.createRequestConfig)('api/v2/me', { headers: { foo: 'bar' }, method: 'GET' }); (0, test_1.expect)(req.headers.Authorization).to.equal('Bearer good_token'); (0, test_1.expect)(req.headers.foo).to.equal('bar'); }); test_1.test .env({ ZENDESK_SUBDOMAIN: 'z3ntest', ZENDESK_DOMAIN: 'expected.com', ZENDESK_EMAIL: 'test@zendesk.com', ZENDESK_API_TOKEN: '123456' }) .stub(requestUtils, 'getSubdomain', () => 'fake') .stub(requestUtils, 'getDomain', () => 'fake.com') .it('should be able to create a request with a Basic auth token', async () => { const req = await (0, request_1.createRequestConfig)('api/v2/me', {}); (0, test_1.expect)(req.headers.Authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY='); }); test_1.test .env({ ZENDESK_SUBDOMAIN: 'z3ntest', ZENDESK_DOMAIN: 'expected.com', ZENDESK_EMAIL: 'test@zendesk.com', ZENDESK_API_TOKEN: '123456' }) .stub(requestUtils, 'getSubdomain', () => 'fake') .stub(requestUtils, 'getDomain', () => 'fake.com') .it('should create a request with the correct domain', async () => { const req = await (0, request_1.createRequestConfig)('api/v2/me'); (0, test_1.expect)(req.baseURL).to.equal('https://z3ntest.expected.com'); }); test_1.test .env({ ZENDESK_SUBDOMAIN: 'z3ntest', ZENDESK_EMAIL: 'test@zendesk.com', ZENDESK_API_TOKEN: '123456' }) .stub(requestUtils, 'getSubdomain', () => 'fake') .stub(requestUtils, 'getDomain', () => '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 (0, request_1.createRequestConfig)('api/v2/me'); (0, test_1.expect)(req.baseURL).to.equal('https://z3ntest.zendesk.com'); }); test_1.test .env({ ZENDESK_EMAIL: 'test@zendesk.com', ZENDESK_API_TOKEN: '123456' }) .stub(requestUtils, 'getSubdomain', () => 'ping') .stub(requestUtils, 'getDomain', () => 'me.com') .stub(auth_1.default, 'getLoggedInProfile', () => ({ subdomain: 'ping', domain: 'me.com' })) .it('should be able to create auth using profile subdomain and domain', async () => { const req = await (0, request_1.createRequestConfig)('api/v2/me', {}); (0, test_1.expect)(req.baseURL).to.equal('https://ping.me.com'); (0, test_1.expect)(req.headers.Authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY='); }); }); describe('requestAPI', () => { let fetchStub; beforeEach(() => { fetchStub = sinon.stub(global, 'fetch'); }); afterEach(() => { fetchStub.restore(); }); test_1.test .env({ ZENDESK_SUBDOMAIN: 'z3ntest', ZENDESK_EMAIL: 'test@zendesk.com', ZENDESK_API_TOKEN: '123456', ZENDESK_OAUTH_TOKEN: 'good_token' }) .stub(requestUtils, 'getSubdomain', () => 'fake') .stub(requestUtils, 'getDomain', () => 'fake.com') .do(() => { fetchStub.withArgs(sinon.match({ url: 'https://z3ntest.zendesk.com/api/v2/me', method: 'GET' })).resolves({ status: 200, ok: true, text: () => Promise.resolve('') }); }) .it('should call an http endpoint', async () => { const response = await (0, request_1.requestAPI)('api/v2/me', { method: 'GET' }); (0, test_1.expect)(response.status).to.equal(200); }); }); describe('requestRaw', () => { let axiosStub; beforeEach(() => { axiosStub = sinon.stub(axios_1.default, 'request'); }); afterEach(() => { axiosStub.restore(); }); it('should make a raw request without adding any extra headers', async () => { axiosStub.resolves({ status: 200, ok: true, data: { result: 'ok' } }); const customHeaders = { 'Content-Type': 'application/zip' }; const response = await (0, request_1.requestRaw)('https://example.com/upload', { method: 'PUT', headers: customHeaders, data: Buffer.from('test') }); (0, test_1.expect)(response.status).to.equal(200); (0, test_1.expect)(axiosStub.called).to.equal(true); const callArgs = axiosStub.firstCall.args[0]; (0, test_1.expect)(callArgs.headers).to.deep.equal(customHeaders); }); it('should return 403 response without throwing error', async () => { axiosStub.resolves({ status: 403, ok: false, data: { error: 'Access denied' } }); const response = await (0, request_1.requestRaw)('https://example.com/upload', { method: 'PUT', headers: { 'Content-Type': 'application/zip' } }); (0, test_1.expect)(response.status).to.equal(403); (0, test_1.expect)(response.data.error).to.equal('Access denied'); }); it('should throw error on 500 server error', async () => { const axiosError = new Error('Server error'); axiosError.response = { status: 500 }; axiosStub.rejects(axiosError); try { await (0, request_1.requestRaw)('https://example.com/api', { method: 'POST', data: { test: 'data' } }); test_1.expect.fail('Should have thrown an error'); } catch (error) { (0, test_1.expect)(error.response.status).to.equal(500); } }); });