Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodecleyre committed Jul 18, 2023
1 parent 05cab2f commit eddb54e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 39 deletions.
23 changes: 4 additions & 19 deletions src/m365/spo/commands/file/file-sharinglink-clear.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
import { sinonUtil } from '../../../../utils/sinonUtil';
import commands from '../../commands';
import * as spoFileSharingLinkListCommand from './file-sharinglink-list';
import { GraphFileDetails } from '../../../../utils/spo';
import { GraphFileDetails, spo } from '../../../../utils/spo';
const command: Command = require('./file-sharinglink-clear');

describe(commands.FILE_SHARINGLINK_CLEAR, () => {
Expand All @@ -27,10 +26,6 @@ describe(commands.FILE_SHARINGLINK_CLEAR, () => {
VroomItemID: '01A5WCPNXHFAS23ZNOF5D3XU2WU7S3I2AU'
};
const sharingLink = { "id": "8c2c9168-7d3d-4119-bcab-3c5340ce603b", "roles": ["read"], "hasPassword": false, "grantedToIdentitiesV2": [{ "group": { "displayName": "h Members", "email": "[email protected]", "id": "94da1e01-bbab-41e9-b9a4-4595c5805a6b" }, "siteUser": { "displayName": "h Members", "email": "[email protected]", "id": "428", "loginName": "c:0o.c|federateddirectoryclaimprovider|94da1e01-bbab-41e9-b9a4-4595c5805a6b" } }], "grantedToIdentities": [{ "user": { "displayName": "h Members", "email": "[email protected]", "id": "94da1e01-bbab-41e9-b9a4-4595c5805a6b" } }], "link": { "scope": "anonymous", "type": "view", "webUrl": "https://contoso.sharepoint.com/:b:/s/pnpcoresdktestgroup/EY50lub3559MtRKfj2hrZqoBea_L-lv1lND19RSCJGtWNg", "preventsDownload": false } };
const sharingLinksListCommandResponse = {
"stdout": JSON.stringify([sharingLink]),
"stderr": ""
};

let log: any[];
let logger: Logger;
Expand Down Expand Up @@ -71,7 +66,7 @@ describe(commands.FILE_SHARINGLINK_CLEAR, () => {
request.delete,
request.get,
Cli.prompt,
Cli.executeCommandWithOutput
spo.getFileSharingLinks
]);
});

Expand Down Expand Up @@ -147,12 +142,7 @@ describe(commands.FILE_SHARINGLINK_CLEAR, () => {
throw 'Invalid request';
});

sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise<any> => {
if (command === spoFileSharingLinkListCommand) {
return sharingLinksListCommandResponse;
}
throw 'Error occured while executing the command.';
});
sinon.stub(spo, 'getFileSharingLinks').resolves([sharingLink]);

const deleteStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/sites/${fileInformationResponse.SiteId}/drives/${fileInformationResponse.VroomDriveID}/items/${fileInformationResponse.VroomItemID}/permissions/${sharingLink.id}`) {
Expand All @@ -178,12 +168,7 @@ describe(commands.FILE_SHARINGLINK_CLEAR, () => {
throw 'Invalid request';
});

sinon.stub(Cli, 'executeCommandWithOutput').callsFake(async (command): Promise<any> => {
if (command === spoFileSharingLinkListCommand) {
return sharingLinksListCommandResponse;
}
throw 'Error occured while executing the command.';
});
sinon.stub(spo, 'getFileSharingLinks').resolves([sharingLink]);

const deleteStub = sinon.stub(request, 'delete').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/sites/${fileInformationResponse.SiteId}/drives/${fileInformationResponse.VroomDriveID}/items/${fileInformationResponse.VroomItemID}/permissions/${sharingLink.id}`) {
Expand Down
20 changes: 1 addition & 19 deletions src/m365/spo/commands/file/file-sharinglink-clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import GlobalOptions from '../../../../GlobalOptions';
import { validation } from '../../../../utils/validation';
import SpoCommand from '../../../base/SpoCommand';
import commands from '../../commands';
import { Options as SpoFileSharingLinkListOptions } from './file-sharinglink-list';
import { Cli } from '../../../../cli/Cli';
import * as spoFileSharingLinkListCommand from './file-sharinglink-list';
import Command from '../../../../Command';
import request, { CliRequestOptions } from '../../../../request';
import { spo } from '../../../../utils/spo';

Expand Down Expand Up @@ -107,7 +104,7 @@ class SpoFileSharingLinkClearCommand extends SpoCommand {
}

const fileDetails = await spo.getVroomFileDetails(args.options.webUrl, args.options.fileId, args.options.fileUrl);
const sharingLinks = await this.getFileSharingLinks(args.options.webUrl, args.options.fileId, args.options.fileUrl, args.options.scope);
const sharingLinks = await spo.getFileSharingLinks(args.options.webUrl, args.options.fileId, args.options.fileUrl, args.options.scope);

const requestOptions: CliRequestOptions = {
headers: {
Expand Down Expand Up @@ -142,21 +139,6 @@ class SpoFileSharingLinkClearCommand extends SpoCommand {
}
}
}

private async getFileSharingLinks(webUrl: string, fileId?: string, fileUrl?: string, scope?: string): Promise<any[]> {
const sharingLinkListOptions: SpoFileSharingLinkListOptions = {
webUrl: webUrl,
fileId: fileId,
fileUrl: fileUrl,
scope: scope,
debug: this.debug,
verbose: this.verbose
};

const commandOutput = await Cli.executeCommandWithOutput(spoFileSharingLinkListCommand as Command, { options: { ...sharingLinkListOptions, _: [] } });
const outputParsed = JSON.parse(commandOutput.stdout);
return outputParsed;
}
}

module.exports = new SpoFileSharingLinkClearCommand();
73 changes: 72 additions & 1 deletion src/utils/spo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import auth from '../Auth';
import { Logger } from '../cli/Logger';
import request from '../request';
import { sinonUtil } from '../utils/sinonUtil';
import { FormDigestInfo, spo } from '../utils/spo';
import { FormDigestInfo, GraphFileDetails, spo } from '../utils/spo';
import { formatting } from './formatting';
import { RoleDefinition } from '../m365/spo/commands/roledefinition/RoleDefinition';

Expand Down Expand Up @@ -1107,4 +1107,75 @@ describe('utils/spo', () => {

await assert.rejects(spo.getRoleDefinitionByName('https://contoso.sharepoint.com/sites/sales', 'Read', logger, true), 'An error occured');
});

it(`retrieves the file sharing link`, async () => {
const fileDetailsResponse: GraphFileDetails = {
SiteId: "0f9b8f4f-0e8e-4630-bb0a-501442db9b64",
VroomItemID: "013TMHP6UOOSLON57HT5GLKEU7R5UGWZVK",
VroomDriveID: "b!T4-bD44OMEa7ClAUQtubZID9tc40pGJKpguycvELod_Gx-lo4ZQiRJ7vylonTufG"
};

const graphResponse = {
value: [
{
id: '2a021f54-90a2-4016-b3b3-5f34d2e7d932',
roles: [
'read'
],
hasPassword: false,
grantedToIdentitiesV2: [],
grantedToIdentities: [],
link: {
scope: 'anonymous',
type: 'view',
webUrl: 'https://contoso.sharepoint.com/:b:/s/pnpcoresdktestgroup/EY50lub3559MtRKfj2hrZqoBWnHOpGIcgi4gzw9XiWYJ-A',
preventsDownload: false
}
},
{
id: 'a47e5387-8868-497c-bb00-115c66c60390',
roles: [
'read'
],
hasPassword: true,
grantedToIdentitiesV2: [],
grantedToIdentities: [],
link: {
scope: 'users',
type: 'view',
webUrl: 'https://contoso.sharepoint.com/:b:/s/pnpcoresdktestgroup/EY50lub3559MtRKfj2hrZqoBsS_o5pIcCyNIL3D_vEyG5Q',
preventsDownload: true
}
},
{
id: '8bf1ca81-a63f-4796-9af5-d86ded8ce5a7',
roles: [
'write'
],
hasPassword: true,
grantedToIdentitiesV2: [],
grantedToIdentities: [],
link: {
scope: 'organization',
type: 'edit',
webUrl: 'https://contoso.sharepoint.com/:b:/s/pnpcoresdktestgroup/EY50lub3559MtRKfj2hrZqoBDyAMq6f9C2eqWwFsbei6nA',
preventsDownload: false
}
}
]
};

sinon.stub(spo, 'getVroomFileDetails').resolves(fileDetailsResponse);

sinon.stub(request, 'get').callsFake(async (opts) => {
if (opts.url === `https://graph.microsoft.com/v1.0/sites/${fileDetailsResponse.SiteId}/drives/${fileDetailsResponse.VroomDriveID}/items/${fileDetailsResponse.VroomItemID}/permissions?$filter=Link ne null and Link/Scope eq 'users'`) {
return graphResponse;
}

throw 'Invalid request';
});

const actual = await spo.getFileSharingLinks(webUrl, 'b2307a39-e878-458b-bc90-03bc578531d6', '/sites/sales/documents/Test1.docx', 'users');
assert.deepStrictEqual(actual, graphResponse.value);
});
});
11 changes: 11 additions & 0 deletions src/utils/spo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,5 +835,16 @@ export const spo = {
roledefinition.RoleTypeKindValue = RoleType[roledefinition.RoleTypeKind];

return roledefinition;
},

async getFileSharingLinks(webUrl: string, fileId?: string | undefined, fileUrl?: string | undefined, scope?: string | undefined): Promise<any> {
const fileDetails = await spo.getVroomFileDetails(webUrl, fileId, fileUrl);
let url = `https://graph.microsoft.com/v1.0/sites/${fileDetails.SiteId}/drives/${fileDetails.VroomDriveID}/items/${fileDetails.VroomItemID}/permissions?$filter=Link ne null`;
if (scope) {
url += ` and Link/Scope eq '${scope}'`;
}

const sharingLinks = await odata.getAllItems<any>(url);
return sharingLinks;
}
};

0 comments on commit eddb54e

Please sign in to comment.