-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eaed63
commit e361f03
Showing
8 changed files
with
134 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,92 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { XMLParser } from 'fast-xml-parser'; | ||
import { TestContext } from '@salesforce/core/lib/testSetup.js'; | ||
import { expect } from 'chai'; | ||
import { stubSfCommandUx } from '@salesforce/sf-plugins-core'; | ||
import PermsFieldNew from '../../../../src/commands/perms/field/new.js'; | ||
import PermsFieldNew, { PermsFieldNewResult } from '../../../../src/commands/perms/field/new.js'; | ||
|
||
const pathToPermissionSet = path.resolve('', 'test', 'force-app', 'main', 'default', 'permissionsets'); | ||
|
||
describe('perms field new', () => { | ||
const $$ = new TestContext(); | ||
let sfCommandStubs: ReturnType<typeof stubSfCommandUx>; | ||
|
||
beforeEach(() => { | ||
sfCommandStubs = stubSfCommandUx($$.SANDBOX); | ||
fs.copyFileSync( | ||
path.resolve(pathToPermissionSet, 'FirstPermissionSet.permissionset-meta.xml'), | ||
path.resolve(pathToPermissionSet, 'FirstPermissionSetBackup.permissionset-meta.xml') | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
$$.restore(); | ||
fs.copyFileSync( | ||
path.resolve(pathToPermissionSet, 'FirstPermissionSetBackup.permissionset-meta.xml'), | ||
path.resolve(pathToPermissionSet, 'FirstPermissionSet.permissionset-meta.xml') | ||
); | ||
fs.unlinkSync(path.resolve(pathToPermissionSet, 'FirstPermissionSetBackup.permissionset-meta.xml')); | ||
}); | ||
|
||
it('runs hello', async () => { | ||
await PermsFieldNew.run([]); | ||
const output = sfCommandStubs.log | ||
.getCalls() | ||
.flatMap((c) => c.args) | ||
.join('\n'); | ||
expect(output).to.include('hello world'); | ||
}); | ||
|
||
it('runs hello with --json and no provided name', async () => { | ||
const result = await PermsFieldNew.run([]); | ||
expect(result.path).to.equal('/Users/raffaele.preziosi/VsCode/sf-perms/src/commands/perms/field/new.ts'); | ||
}); | ||
|
||
it('runs hello world --name Astro', async () => { | ||
await PermsFieldNew.run(['--name', 'Astro']); | ||
const output = sfCommandStubs.log | ||
.getCalls() | ||
.flatMap((c) => c.args) | ||
.join('\n'); | ||
expect(output).to.include('hello Astro'); | ||
it('add new field permission', async () => { | ||
let callCount = 0; | ||
$$.SANDBOX.stub(PermsFieldNew, 'prompts').callsFake(() => { | ||
callCount++; | ||
switch (callCount) { | ||
case 1: | ||
return Promise.resolve({ | ||
permissionSetsSelected: ['FirstPermissionSet.permissionset-meta.xml'], | ||
objectSelected: 'Account', | ||
}); | ||
case 2: | ||
return Promise.resolve({ | ||
Fields: ['Address__c', 'AddressString__c'], | ||
}); | ||
case 3: | ||
return Promise.resolve({ | ||
// eslint-disable-next-line camelcase | ||
Address__c: 'read', | ||
// eslint-disable-next-line camelcase | ||
AddressString__c: 'read_edit', | ||
}); | ||
default: | ||
return Promise.resolve({}); | ||
} | ||
}); | ||
const result: PermsFieldNewResult = await PermsFieldNew.run([]); | ||
expect(result).to.have.property('isSuccess', true); | ||
// check field permissions are added in the file | ||
const permissionSetXml = fs.readFileSync( | ||
path.resolve(pathToPermissionSet, 'FirstPermissionSet.permissionset-meta.xml'), | ||
'utf8' | ||
); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const permissionSetParsedJSON = new XMLParser({ ignoreAttributes: false }).parse(permissionSetXml); | ||
expect(permissionSetParsedJSON).to.have.property('PermissionSet'); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
expect(permissionSetParsedJSON.PermissionSet).to.have.property('fieldPermissions'); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
expect(permissionSetParsedJSON.PermissionSet.fieldPermissions).to.be.an('array'); | ||
expect(permissionSetParsedJSON.PermissionSet.fieldPermissions).to.have.lengthOf(2); | ||
expect( | ||
permissionSetParsedJSON.PermissionSet.fieldPermissions.find( | ||
(fp: { field: string }) => fp.field === 'Account.Address__c' | ||
) | ||
).to.have.property('editable', false); | ||
expect( | ||
permissionSetParsedJSON.PermissionSet.fieldPermissions.find( | ||
(fp: { field: string }) => fp.field === 'Account.Address__c' | ||
) | ||
).to.have.property('readable', true); | ||
expect( | ||
permissionSetParsedJSON.PermissionSet.fieldPermissions.find( | ||
(fp: { field: string }) => fp.field === 'Account.AddressString__c' | ||
) | ||
).to.have.property('editable', true); | ||
expect( | ||
permissionSetParsedJSON.PermissionSet.fieldPermissions.find( | ||
(fp: { field: string }) => fp.field === 'Account.AddressString__c' | ||
) | ||
).to.have.property('readable', true); | ||
}); | ||
}); |
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions
6
test/force-app/main/default/permissionsets/FirstPermissionSet.permissionset-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<description></description> | ||
<hasActivationRequired>false</hasActivationRequired> | ||
<label>First Permission Set</label> | ||
</PermissionSet> |