|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import fs from 'node:fs'; |
| 8 | +import path from 'node:path'; |
| 9 | +import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit'; |
| 10 | +import { expect } from 'chai'; |
| 11 | +import { SaveResult } from '@jsforce/jsforce-node'; |
| 12 | +import { SoqlQueryResult } from '../../../../src/dataSoqlQueryTypes.js'; |
| 13 | +import { ContentVersion } from '../../../../src/api/file/fileToContentVersion.js'; |
| 14 | + |
| 15 | +describe('data create file NUTs', () => { |
| 16 | + const filename = 'hi.txt'; |
| 17 | + let session: TestSession; |
| 18 | + let acctId: string | undefined; |
| 19 | + before(async () => { |
| 20 | + session = await TestSession.create({ |
| 21 | + project: { name: 'dataCreateFile' }, |
| 22 | + scratchOrgs: [{ setDefault: true, edition: 'developer' }], |
| 23 | + devhubAuthStrategy: 'AUTO', |
| 24 | + }); |
| 25 | + // create one record in the org that we'll use to attach stuff to |
| 26 | + acctId = execCmd<SaveResult>('data:create:record -s Account -v "Name=TestAccount" --json', { |
| 27 | + ensureExitCode: 0, |
| 28 | + }).jsonOutput?.result.id; |
| 29 | + expect(acctId).to.be.a('string'); |
| 30 | + // make a file we can upload |
| 31 | + await fs.promises.writeFile(path.join(session.project.dir, filename), 'hi'); |
| 32 | + }); |
| 33 | + |
| 34 | + after(async () => { |
| 35 | + await session?.clean(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('basic file upload', () => { |
| 39 | + const command = `data:create:file --file ${filename} --json`; |
| 40 | + const output = execCmd<ContentVersion>(command, { ensureExitCode: 0 }).jsonOutput?.result; |
| 41 | + expect(output?.ContentDocumentId) |
| 42 | + .to.be.a('string') |
| 43 | + .match(/069\w{15}/); |
| 44 | + expect(output?.Id) |
| 45 | + .to.be.a('string') |
| 46 | + .match(/068\w{15}/); |
| 47 | + expect(output?.FileExtension).to.equal('txt'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('file upload + attach with filename', () => { |
| 51 | + const newName = 'newName.txt'; |
| 52 | + const command = `data:create:file --file ${filename} --parent-id ${acctId} --name ${newName} --json`; |
| 53 | + const output = execCmd<ContentVersion>(command, { ensureExitCode: 0 }).jsonOutput?.result; |
| 54 | + expect(output?.ContentDocumentId) |
| 55 | + .to.be.a('string') |
| 56 | + .match(/069\w{15}/); |
| 57 | + expect(output?.Id) |
| 58 | + .to.be.a('string') |
| 59 | + .match(/068\w{15}/); |
| 60 | + expect(output?.Title).to.equal(newName); |
| 61 | + |
| 62 | + // make sure the file is attached to the record |
| 63 | + const query = `SELECT Id FROM ContentDocumentLink WHERE LinkedEntityId='${acctId}' AND ContentDocumentId='${output?.ContentDocumentId}'`; |
| 64 | + const result = execCmd<SoqlQueryResult['result']>(`data:query -q "${query}" --json`, { ensureExitCode: 0 }) |
| 65 | + .jsonOutput?.result; |
| 66 | + expect(result?.totalSize).to.equal(1); |
| 67 | + }); |
| 68 | +}); |
0 commit comments