Skip to content

Commit

Permalink
feat: revert to use got and nock
Browse files Browse the repository at this point in the history
  • Loading branch information
ClayChipps committed Feb 11, 2024
1 parent 4da6937 commit b4193d8
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 183 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"csv-parse": "^5.5.3",
"csv-writer": "^1.6.0",
"form-data": "^4.0.0",
"msw": "^2.1.7",
"got": "^14.2.0",
"nock": "^13.5.1",
"p-queue": "^8.0.1"
},
"devDependencies": {
Expand Down
25 changes: 13 additions & 12 deletions src/common/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fs from 'node:fs';
import path from 'node:path';
import FormData from 'form-data';
import got from 'got';
import { Connection } from '@salesforce/core';
import { ContentVersionCreateRequest, ContentVersion, CreateResult } from './typeDefs.js';

Expand All @@ -26,19 +27,19 @@ export async function uploadContentVersion(
Title: title ?? path.basename(pathOnClient),
};

const formData = new FormData();
formData.append('entity_content', JSON.stringify(contentVersionCreateRequest), { contentType: 'application/json' });
formData.append('VersionData', fs.createReadStream(pathOnClient), { filename: path.basename(pathOnClient) });
const form = new FormData();
form.append('entity_content', JSON.stringify(contentVersionCreateRequest), { contentType: 'application/json' });
form.append('VersionData', fs.createReadStream(pathOnClient), { filename: path.basename(pathOnClient) });

const response = await fetch(`${targetOrgConnection.baseUrl()}/sobjects/ContentVersion`, {
method: 'POST',
body: formData,
headers: {
Authorization: `Bearer ${targetOrgConnection.accessToken}`,
'Content-Type': `multipart/form-data; boundary="${formData.getBoundary()}"`,
},
});
const data = (await response.json()) as CreateResult;
const data = await got
.post(`${targetOrgConnection.baseUrl()}/sobjects/ContentVersion`, {
body: form,
headers: {
Authorization: `Bearer ${targetOrgConnection.accessToken}`,
'Content-Type': `multipart/form-data; boundary="${form.getBoundary()}"`,
},
})
.json<CreateResult>();

const queryResult = await targetOrgConnection.singleRecordQuery(
`SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id='${data.id}'`
Expand Down
15 changes: 5 additions & 10 deletions test/commands/chipps/data/file/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import nock from 'nock';
import { expect } from 'chai';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { Connection, SfError } from '@salesforce/core';
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup.js';
import DataFileUpload from '../../../../../src/commands/chipps/data/file/upload.js';
Expand Down Expand Up @@ -36,12 +35,10 @@ describe('chipps data file upload', () => {
});

it('should return content version successfully', async () => {
const server = setupServer(
http.post(`${(await testOrg.getConnection()).baseUrl()}/sobjects/ContentVersion`, () =>
HttpResponse.json([{ id: '123', success: true }])
)
);
server.listen();
nock(testOrg.instanceUrl)
.post('/services/data/v42.0/sobjects/ContentVersion')
.reply(200, { id: '123', success: true })
.persist();

$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
Id: '123',
Expand All @@ -63,7 +60,5 @@ describe('chipps data file upload', () => {
expect(response.Name).to.equal('coolFile');
expect(response.Title).to.equal('coolFile');
expect(response.FileExtension).to.equal('.json');

server.close();
});
});
17 changes: 6 additions & 11 deletions test/commands/chipps/data/files/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import fs from 'node:fs';
import { parse } from 'csv-parse/sync';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import nock from 'nock';
import { expect } from 'chai';
import { Connection, SfError } from '@salesforce/core';
import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup.js';
Expand Down Expand Up @@ -39,12 +38,10 @@ describe('chipps data files upload', () => {
});

it('should write results to csv', async () => {
const server = setupServer(
http.post(`${(await testOrg.getConnection()).baseUrl()}/sobjects/ContentVersion`, () =>
HttpResponse.json([{ id: '123', success: true }])
)
);
server.listen();
nock(testOrg.instanceUrl)
.post('/services/data/v42.0/sobjects/ContentVersion')
.reply(200, { id: '123', success: true })
.persist();

$$.SANDBOX.stub(Connection.prototype, 'singleRecordQuery').resolves({
Id: '123',
Expand All @@ -64,7 +61,7 @@ describe('chipps data files upload', () => {
const errorResults = parse(fs.readFileSync('error.csv'), { bom: true, columns: true }) as FileToUpload[];
const successResults = parse(fs.readFileSync('success.csv'), { bom: true, columns: true }) as FileToUpload[];

expect(errorResults[0].Error).to.contain('Error: ENOENT: no such file or directory');
expect(errorResults[0].Error).to.contain('RequestError: ENOENT: no such file or directory');
expect(successResults).to.deep.equal([
{
ContentDocumentId: '123',
Expand All @@ -79,7 +76,5 @@ describe('chipps data files upload', () => {
Title: 'Watch Doges',
},
]);

server.close();
});
});
Loading

0 comments on commit b4193d8

Please sign in to comment.