Skip to content

[VEG-2395] Remove Axios and replace it with Fetch #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/zcli-apps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"dependencies": {
"adm-zip": "0.5.10",
"archiver": "^5.3.1",
"axios": "^0.27.2",
"chalk": "^4.1.2",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand Down
180 changes: 125 additions & 55 deletions packages/zcli-apps/tests/functional/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
const successUpdateMessage = 'Successfully updated app'
const uploadAppPkgStub = sinon.stub(createAppUtils, 'uploadAppPkg')
const createAppPkgStub = sinon.stub()
const sandbox = sinon.createSandbox()
let fetchStub: any = null;

Check failure on line 19 in packages/zcli-apps/tests/functional/create.test.ts

View workflow job for this annotation

GitHub Actions / build-and-check (ubuntu-latest, 18.x)

Extra semicolon

afterEach(() => {
uploadAppPkgStub.reset()
createAppPkgStub.reset()
})

describe('create', () => {
beforeEach(() => {
fetchStub = sandbox.stub(global, 'fetch')
})

afterEach(() => {
sandbox.restore()
})

describe('with multiple apps', () => {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
Expand All @@ -34,26 +44,39 @@
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 817 })
uploadAppPkgStub.onSecondCall().resolves({ id: 818 })
})
.nock('https://z3ntest.zendesk.com/', api => {
api
.post('/api/apps.json', { upload_id: 817, name: 'Test App 1' })
.reply(200, { job_id: 127 })
api
.post('/api/apps.json', { upload_id: 818, name: 'Test App 2' })
.reply(200, { job_id: 128 })
api
.get('/api/v2/apps/job_statuses/127')
.reply(200, { status: 'completed', message: 'awesome', app_id: 123456 })
api
.get('/api/v2/apps/job_statuses/128')
.reply(200, { status: 'completed', message: 'awesome', app_id: 123458 })
api
.post('/api/support/apps/installations.json', { app_id: '123456', settings: { name: 'Test App 1' } })
.reply(200)
api
.post('/api/support/apps/installations.json', { app_id: '123458', settings: { name: 'Test App 2', salesForceId: 123 } })
.reply(200)

// Stub fetch calls
fetchStub.withArgs('https://z3ntest.zendesk.com/api/apps.json', sinon.match.any)
.onFirstCall().resolves({
ok: true,
json: () => Promise.resolve({ job_id: 127 })
} as any)
.onSecondCall().resolves({
ok: true,
json: () => Promise.resolve({ job_id: 128 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/job_statuses/127', sinon.match.any)
.resolves({
ok: true,
json: () => Promise.resolve({ status: 'completed', message: 'awesome', app_id: 123456 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/job_statuses/128', sinon.match.any)
.resolves({
ok: true,
json: () => Promise.resolve({ status: 'completed', message: 'awesome', app_id: 123458 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/support/apps/installations.json', sinon.match.any)
.onFirstCall().resolves({
ok: true,
json: () => Promise.resolve({})
} as any)
.onSecondCall().resolves({
ok: true,
json: () => Promise.resolve({})
} as any)
})
.stdout()
.command(['apps:create', singleProductApp, multiProductApp])
Expand All @@ -71,17 +94,30 @@
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
})
.nock('https://z3ntest.zendesk.com/', api => {
api
.post('/api/apps.json', { upload_id: 819, name: 'Test App 1' })
.reply(200, { job_id: 129 })
api
.get('/api/v2/apps/job_statuses/129')
.reply(200, { status: 'completed', message: 'awesome', app_id: 123456 })
api
.post('/api/support/apps/installations.json', { app_id: '123456', settings: { name: 'Test App 1' } })
.reply(200)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/apps.json', {
method: 'POST',
headers: sinon.match.any,
body: sinon.match(function (body) {
return body.includes('Test App 1')
})
})
.resolves({
ok: true,
json: () => Promise.resolve({ job_id: 129 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/job_statuses/129', sinon.match.any)
.resolves({
ok: true,
json: () => Promise.resolve({ status: 'completed', message: 'awesome', app_id: 123456 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/support/apps/installations.json', sinon.match.any)
.resolves({
ok: true,
json: () => Promise.resolve({})
} as any)
})
.stdout()
.command(['apps:create', singleProductApp])
Expand All @@ -97,14 +133,24 @@
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
})
.nock('https://z3ntest.zendesk.com/', api => {
api
.post('/api/apps.json', { upload_id: 819, name: 'Test App 1' })
.reply(200, { job_id: 129 })
api
.get('/api/v2/apps/job_statuses/129')
.reply(200, { status: 'completed', message: 'awesome', app_id: 123456 })

fetchStub.withArgs('https://z3ntest.zendesk.com/api/apps.json', {
method: 'POST',
headers: sinon.match.any,
body: sinon.match(function (body) {
return body.includes('Test App 1') && body.includes('819')
})
})
.resolves({
ok: true,
json: () => Promise.resolve({ job_id: 129 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/job_statuses/129', sinon.match.any)
.resolves({
ok: true,
json: () => Promise.resolve({ status: 'completed', message: 'awesome', app_id: 123456 })
} as any)
})
.stdout()
.command(['apps:create', requirementsOnlyApp])
Expand All @@ -115,21 +161,37 @@
})

describe('update', () => {
beforeEach(() => {
fetchStub = sandbox.stub(global, 'fetch')
})

afterEach(() => {
sandbox.restore()
})

describe('with single app', () => {
test
.stub(packageUtil, 'createAppPkg', () => createAppPkgStub)
.env(env)
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
})
.nock('https://z3ntest.zendesk.com/', api => {
api
.put('/api/v2/apps/123456', { upload_id: 819 })
.reply(200, { job_id: 129 })
api
.get('/api/v2/apps/job_statuses/129')
.reply(200, { status: 'completed', message: 'awesome', app_id: 123456 })

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/123456', {
method: 'PUT',
headers: sinon.match.any,
body: JSON.stringify({ upload_id: 819 })
})
.resolves({
ok: true,
json: () => Promise.resolve({ job_id: 129 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/job_statuses/129', sinon.match.any)
.resolves({
ok: true,
json: () => Promise.resolve({ status: 'completed', message: 'awesome', app_id: 123456 })
} as any)
})
.stdout()
.command(['apps:update', singleProductApp])
Expand All @@ -145,14 +207,22 @@
.do(() => {
createAppPkgStub.onFirstCall().resolves('thePathLessFrequentlyTravelled')
uploadAppPkgStub.onFirstCall().resolves({ id: 819 })
})
.nock('https://z3ntest.zendesk.com/', api => {
api
.put('/api/v2/apps/123456', { upload_id: 819 })
.reply(200, { job_id: 129 })
api
.get('/api/v2/apps/job_statuses/129')
.reply(200, { status: 'completed', message: 'awesome', app_id: 123456 })

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/123456', {
method: 'PUT',
headers: sinon.match.any,
body: JSON.stringify({ upload_id: 819 })
})
.resolves({
ok: true,
json: () => Promise.resolve({ job_id: 129 })
} as any)

fetchStub.withArgs('https://z3ntest.zendesk.com/api/v2/apps/job_statuses/129', sinon.match.any)
.resolves({
ok: true,
json: () => Promise.resolve({ status: 'completed', message: 'awesome', app_id: 123456 })
} as any)
})
.stdout()
.command(['apps:update', requirementsOnlyApp])
Expand Down
38 changes: 21 additions & 17 deletions packages/zcli-apps/tests/functional/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { expect, test } from '@oclif/test'
import * as path from 'path'
import * as http from 'http'
import axios from 'axios'
import * as fs from 'fs'
import { omit } from 'lodash'
import ServerCommand from '../../src/commands/apps/server'
Expand All @@ -24,11 +23,16 @@ describe('apps server', function () {
test
.it('should serve assets on custom port', async () => {
const appHost = 'http://localhost:1234'
const appJSON = await axios.get(`${appHost}/app.json`)
const { installations } = await appJSON.data
expect(appJSON.status).to.eq(200)
expect((await axios.get(`${appHost}/${installations[0].app_id}/assets/logo-small.png`)).status).to.eq(200)
expect((await axios.get(`${appHost}/${installations[0].app_id}/assets/iframe.html`)).status).to.eq(200)
const appJSONResponse = await fetch(`${appHost}/app.json`)
expect(appJSONResponse.status).to.eq(200)
const appJSON = await appJSONResponse.json()
const { installations } = appJSON

const logoResponse = await fetch(`${appHost}/${installations[0].app_id}/assets/logo-small.png`)
expect(logoResponse.status).to.eq(200)

const iframeResponse = await fetch(`${appHost}/${installations[0].app_id}/assets/iframe.html`)
expect(iframeResponse.status).to.eq(200)
})
})

Expand All @@ -37,8 +41,8 @@ describe('apps server', function () {
before(async () => {
server = await ServerCommand.run([singleProductApp, multiProductApp])
appHost = 'http://localhost:4567'
const response = await axios.get(`${appHost}/app.json`)
appJSON = await response.data
const response = await fetch(`${appHost}/app.json`)
appJSON = await response.json()
})

after(() => server.close())
Expand All @@ -47,19 +51,19 @@ describe('apps server', function () {
.it('should serve assets for single_product_app', async () => {
const singleProductApp = appJSON.apps[0]

expect((await axios.get(`${appHost}/${singleProductApp.id}/assets/logo-small.png`)).status).to.eq(200)
expect((await axios.get(`${appHost}/${singleProductApp.id}/assets/iframe.html`)).status).to.eq(200)
expect((await axios.get(`${appHost}/${singleProductApp.id}/assets/icon_nav_bar.svg`)).status).to.eq(200)
expect((await fetch(`${appHost}/${singleProductApp.id}/assets/logo-small.png`)).status).to.eq(200)
expect((await fetch(`${appHost}/${singleProductApp.id}/assets/iframe.html`)).status).to.eq(200)
expect((await fetch(`${appHost}/${singleProductApp.id}/assets/icon_nav_bar.svg`)).status).to.eq(200)
})

test
.it('should serve assets for multi_product_app', async () => {
const multiProductApp = appJSON.apps[1]

expect((await axios.get(`${appHost}/${multiProductApp.id}/assets/support/logo-small.png`)).status).to.eq(200)
expect((await axios.get(`${appHost}/${multiProductApp.id}/assets/support/icon_nav_bar.svg`)).status).to.eq(200)
expect((await axios.get(`${appHost}/${multiProductApp.id}/assets/sell/icon_top_bar.svg`)).status).to.eq(200)
expect((await axios.get(`${appHost}/${multiProductApp.id}/assets/iframe.html`)).status).to.eq(200)
expect((await fetch(`${appHost}/${multiProductApp.id}/assets/support/logo-small.png`)).status).to.eq(200)
expect((await fetch(`${appHost}/${multiProductApp.id}/assets/support/icon_nav_bar.svg`)).status).to.eq(200)
expect((await fetch(`${appHost}/${multiProductApp.id}/assets/sell/icon_top_bar.svg`)).status).to.eq(200)
expect((await fetch(`${appHost}/${multiProductApp.id}/assets/iframe.html`)).status).to.eq(200)
})

test
Expand Down Expand Up @@ -108,8 +112,8 @@ describe('apps server', function () {
// Modifed manifest.json
fs.writeFileSync(manifestPath, JSON.stringify(manifest))
await wait()
const response = await axios.get(`${appHost}/app.json`)
appsJSON = await response.data
const response = await fetch(`${appHost}/app.json`)
appsJSON = await response.json()
const appJSON = appsJSON.apps[index]
expect(appJSON.name).to.eq(`${appName} modified`)
// Restored manifest.json
Expand Down
1 change: 0 additions & 1 deletion packages/zcli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"dependencies": {
"@oclif/plugin-plugins": "=2.1.12",
"axios": "^0.27.2",
"chalk": "^4.1.2",
"fs-extra": "^10.1.0"
},
Expand Down
Loading
Loading