From 874da5a7313f8bd75f7623041a385d47a48b3340 Mon Sep 17 00:00:00 2001 From: anushkafka Date: Thu, 22 Aug 2024 16:37:44 +1000 Subject: [PATCH 1/3] Remove axios from zcli-core --- packages/zcli-core/package.json | 1 - packages/zcli-core/src/lib/auth.test.ts | 69 ++++++++++++++-------- packages/zcli-core/src/lib/auth.ts | 12 ++-- packages/zcli-core/src/lib/request.test.ts | 14 +++-- packages/zcli-core/src/lib/request.ts | 18 +++++- 5 files changed, 75 insertions(+), 39 deletions(-) diff --git a/packages/zcli-core/package.json b/packages/zcli-core/package.json index 9bae8bfb..db69e8d3 100644 --- a/packages/zcli-core/package.json +++ b/packages/zcli-core/package.json @@ -24,7 +24,6 @@ }, "dependencies": { "@oclif/plugin-plugins": "=2.1.12", - "axios": "^0.27.2", "chalk": "^4.1.2", "fs-extra": "^10.1.0" }, diff --git a/packages/zcli-core/src/lib/auth.test.ts b/packages/zcli-core/src/lib/auth.test.ts index 3132b948..5ff3183e 100644 --- a/packages/zcli-core/src/lib/auth.test.ts +++ b/packages/zcli-core/src/lib/auth.test.ts @@ -78,6 +78,8 @@ describe('Auth', () => { describe('loginInteractively', () => { const auth = new Auth({ secureStore: new SecureStore() }) const promptStub = sinon.stub() + const sandbox = sinon.createSandbox() + const fetchStub = sandbox.stub(global, 'fetch') test .do(() => { @@ -88,16 +90,21 @@ describe('Auth', () => { .stub(CliUx.ux, 'prompt', () => promptStub) .stub(auth.secureStore, 'setSecret', () => Promise.resolve()) .stub(auth, 'setLoggedInProfile', () => Promise.resolve()) - .nock('https://z3ntest.zendesk.com', api => { - api - .get('/api/v2/account/settings.json') - .reply(function () { - expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') - return [200] - }) + .do(() => { + fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/account/settings.json', + sinon.match(function (params) { + if (params.headers.Authorization === 'Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') { + return true + } + return false + })).resolves({ + status: 200, + json: () => Promise.resolve({}) + } as any) }) .it('should return true on login success', async () => { expect(await auth.loginInteractively()).to.equal(true) + sandbox.reset() }) test @@ -110,16 +117,21 @@ describe('Auth', () => { .stub(CliUx.ux, 'prompt', () => promptStub) .stub(auth.secureStore, 'setSecret', () => Promise.resolve()) .stub(auth, 'setLoggedInProfile', () => Promise.resolve()) - .nock('https://z3ntest.example.com', api => { - api - .get('/api/v2/account/settings.json') - .reply(function () { - expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') - return [200] - }) + .do(() => { + fetchStub.withArgs('https://z3ntest.example.com/api/v2/account/settings.json', + sinon.match(function (params) { + if (params.headers.Authorization === 'Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') { + return true + } + return false + })).resolves({ + status: 200, + json: () => Promise.resolve({}) + } as any) }) .it('should login successfully using the passed domain and the prompted subdomain', async () => { expect(await auth.loginInteractively({ domain: 'example.com' } as Profile)).to.equal(true) + sandbox.reset() }) test @@ -131,16 +143,21 @@ describe('Auth', () => { .stub(CliUx.ux, 'prompt', () => promptStub) .stub(auth.secureStore, 'setSecret', () => Promise.resolve()) .stub(auth, 'setLoggedInProfile', () => Promise.resolve()) - .nock('https://z3ntest.example.com', api => { - api - .get('/api/v2/account/settings.json') - .reply(function () { - expect(this.req.headers.authorization).to.equal('Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') - return [200] - }) + .do(() => { + fetchStub.withArgs('https://z3ntest.example.com/api/v2/account/settings.json', + sinon.match(function (params) { + if (params.headers.Authorization === 'Basic dGVzdEB6ZW5kZXNrLmNvbS90b2tlbjoxMjM0NTY=') { + return true + } + return false + })).resolves({ + status: 200, + json: () => Promise.resolve({}) + } as any) }) .it('should login successfully using the passed subdomain and domain', async () => { expect(await auth.loginInteractively({ subdomain: 'z3ntest', domain: 'example.com' })).to.equal(true) + sandbox.reset() }) test @@ -151,11 +168,15 @@ describe('Auth', () => { promptStub.onThirdCall().resolves('123456') }) .stub(CliUx.ux, 'prompt', () => promptStub) - .nock('https://z3ntest.zendesk.com', api => api - .get('/api/v2/account/settings.json') - .reply(403)) + .do(() => { + fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/account/settings.json', sinon.match.any).resolves({ + status: 403, + json: () => Promise.resolve({}) + } as any) + }) .it('should return false on login failure', async () => { expect(await auth.loginInteractively()).to.equal(false) + sandbox.reset() }) }) diff --git a/packages/zcli-core/src/lib/auth.ts b/packages/zcli-core/src/lib/auth.ts index af1c7916..ca6a382a 100644 --- a/packages/zcli-core/src/lib/auth.ts +++ b/packages/zcli-core/src/lib/auth.ts @@ -2,7 +2,6 @@ import { CLIError } from '@oclif/core/lib/errors' import * as chalk from 'chalk' import { CliUx } from '@oclif/core' import Config from './config' -import axios from 'axios' import SecureStore from './secureStore' import { Profile } from '../types' import { getAccount, parseSubdomain } from './authUtils' @@ -67,12 +66,11 @@ export default class Auth { const email = await CliUx.ux.prompt('Email') const token = await CliUx.ux.prompt('API Token', { type: 'hide' }) const authToken = this.createBasicAuthToken(email, token) - const testAuth = await axios.get( - `${baseUrl}/api/v2/account/settings.json`, - { - headers: { Authorization: authToken }, - validateStatus: function (status) { return status < 500 } - }) + const testAuth = await fetch(`${baseUrl}/api/v2/account/settings.json`, { + headers: { + Authorization: authToken + } + }) if (testAuth.status === 200 && this.secureStore) { await this.secureStore.setSecret(account, authToken) diff --git a/packages/zcli-core/src/lib/request.test.ts b/packages/zcli-core/src/lib/request.test.ts index e1aa5bd1..586ec8c6 100644 --- a/packages/zcli-core/src/lib/request.test.ts +++ b/packages/zcli-core/src/lib/request.test.ts @@ -1,4 +1,5 @@ import { expect, test } from '@oclif/test' +import * as sinon from 'sinon' import { createRequestConfig, requestAPI } from './request' import * as requestUtils from './requestUtils' import Auth from './auth' @@ -90,6 +91,8 @@ describe('createRequestConfig', () => { }) describe('requestAPI', () => { + const sandbox = sinon.createSandbox() + const fetchStub = sandbox.stub(global, 'fetch') test .env({ ZENDESK_SUBDOMAIN: 'z3ntest', @@ -99,13 +102,16 @@ describe('requestAPI', () => { }) .stub(requestUtils, 'getSubdomain', () => 'fake') .stub(requestUtils, 'getDomain', () => 'fake.com') - .nock('https://z3ntest.zendesk.com', api => { - api - .get('/api/v2/me') - .reply(200) + .do(() => { + fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/me', sinon.match.any).resolves({ + status: 200, + ok: true, + json: () => Promise.resolve({}) + } as any) }) .it('should call an http endpoint', async () => { const response = await requestAPI('api/v2/me', { method: 'GET' }) expect(response.status).to.equal(200) + sandbox.reset() }) }) diff --git a/packages/zcli-core/src/lib/request.ts b/packages/zcli-core/src/lib/request.ts index b4329c61..0677b1a1 100644 --- a/packages/zcli-core/src/lib/request.ts +++ b/packages/zcli-core/src/lib/request.ts @@ -1,4 +1,3 @@ -import axios from 'axios' import SecureStore from './secureStore' import Auth from './auth' import { CLIError } from '@oclif/core/lib/errors' @@ -47,8 +46,21 @@ export const createRequestConfig = async (url: string, options: any = {}) => { } } -// eslint-disable-next-line @typescript-eslint/no-explicit-any export const requestAPI = async (url: string, options: any = {}, json = false) => { const requestConfig = await createRequestConfig(url, options) - return axios.request(requestConfig) + const response = await fetch(`${requestConfig.baseURL}/${requestConfig.url}`, { + method: requestConfig.method, + headers: requestConfig.headers, + body: requestConfig.data ? JSON.stringify(requestConfig.data) : undefined + }) + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + + return { + config: requestConfig, + status: response.status, + data: await response.json() + } } From 1eb6876339773728e92c6355295f2549cfa6aa49 Mon Sep 17 00:00:00 2001 From: anushkafka Date: Thu, 22 Aug 2024 17:57:19 +1000 Subject: [PATCH 2/3] Restores all fakes created through sandbox --- packages/zcli-core/src/lib/auth.test.ts | 4 ++++ packages/zcli-core/src/lib/request.test.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/packages/zcli-core/src/lib/auth.test.ts b/packages/zcli-core/src/lib/auth.test.ts index 5ff3183e..2115ed9d 100644 --- a/packages/zcli-core/src/lib/auth.test.ts +++ b/packages/zcli-core/src/lib/auth.test.ts @@ -178,6 +178,10 @@ describe('Auth', () => { expect(await auth.loginInteractively()).to.equal(false) sandbox.reset() }) + + after(function () { + sandbox.restore() + }) }) describe('logout', () => { diff --git a/packages/zcli-core/src/lib/request.test.ts b/packages/zcli-core/src/lib/request.test.ts index 586ec8c6..16e8520b 100644 --- a/packages/zcli-core/src/lib/request.test.ts +++ b/packages/zcli-core/src/lib/request.test.ts @@ -114,4 +114,7 @@ describe('requestAPI', () => { expect(response.status).to.equal(200) sandbox.reset() }) + after(function () { + sandbox.restore() + }) }) From e434431e7faee92f9a6eae739d2ea97b7525002e Mon Sep 17 00:00:00 2001 From: anushkafka Date: Thu, 22 Aug 2024 18:29:02 +1000 Subject: [PATCH 3/3] Use before() afer() with sandbox --- packages/zcli-core/src/lib/auth.test.ts | 13 ++++++++----- packages/zcli-core/src/lib/request.test.ts | 11 +++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/zcli-core/src/lib/auth.test.ts b/packages/zcli-core/src/lib/auth.test.ts index 2115ed9d..378f51f0 100644 --- a/packages/zcli-core/src/lib/auth.test.ts +++ b/packages/zcli-core/src/lib/auth.test.ts @@ -79,7 +79,14 @@ describe('Auth', () => { const auth = new Auth({ secureStore: new SecureStore() }) const promptStub = sinon.stub() const sandbox = sinon.createSandbox() - const fetchStub = sandbox.stub(global, 'fetch') + let fetchStub: any + + before(() => { + fetchStub = sandbox.stub(global, 'fetch') + }) + after(() => { + sandbox.restore() + }) test .do(() => { @@ -178,10 +185,6 @@ describe('Auth', () => { expect(await auth.loginInteractively()).to.equal(false) sandbox.reset() }) - - after(function () { - sandbox.restore() - }) }) describe('logout', () => { diff --git a/packages/zcli-core/src/lib/request.test.ts b/packages/zcli-core/src/lib/request.test.ts index 16e8520b..2de02aa3 100644 --- a/packages/zcli-core/src/lib/request.test.ts +++ b/packages/zcli-core/src/lib/request.test.ts @@ -92,7 +92,13 @@ describe('createRequestConfig', () => { describe('requestAPI', () => { const sandbox = sinon.createSandbox() - const fetchStub = sandbox.stub(global, 'fetch') + let fetchStub: any + before(() => { + fetchStub = sandbox.stub(global, 'fetch') + }) + after(() => { + sandbox.restore() + }) test .env({ ZENDESK_SUBDOMAIN: 'z3ntest', @@ -114,7 +120,4 @@ describe('requestAPI', () => { expect(response.status).to.equal(200) sandbox.reset() }) - after(function () { - sandbox.restore() - }) })