From 39c7cd6c2c75d188212651d68cba1c260774bbf6 Mon Sep 17 00:00:00 2001 From: Nico De Cleyre Date: Sat, 15 Jul 2023 22:36:12 +0200 Subject: [PATCH] Added logging --- .../commands/file/file-sharinglink-clear.ts | 2 +- src/utils/spo.spec.ts | 78 ++++++++++++++++++- src/utils/spo.ts | 15 +++- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/src/m365/spo/commands/file/file-sharinglink-clear.ts b/src/m365/spo/commands/file/file-sharinglink-clear.ts index df081e5ff23..9588882a45f 100644 --- a/src/m365/spo/commands/file/file-sharinglink-clear.ts +++ b/src/m365/spo/commands/file/file-sharinglink-clear.ts @@ -104,7 +104,7 @@ class SpoFileSharingLinkClearCommand extends SpoCommand { } const fileDetails = await spo.getVroomFileDetails(args.options.webUrl, args.options.fileId, args.options.fileUrl); - const sharingLinks = await spo.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, logger, this.verbose); const requestOptions: CliRequestOptions = { headers: { diff --git a/src/utils/spo.spec.ts b/src/utils/spo.spec.ts index 6bed83b1cd3..2c56d8e953d 100644 --- a/src/utils/spo.spec.ts +++ b/src/utils/spo.spec.ts @@ -72,7 +72,8 @@ describe('utils/spo', () => { request.post, auth.storeConnectionInfo, spo.getSpoAdminUrl, - spo.getRequestDigest + spo.getRequestDigest, + spo.getVroomFileDetails ]); auth.service.spoUrl = undefined; auth.service.tenantId = undefined; @@ -1108,7 +1109,7 @@ 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 () => { + it(`retrieves the file sharing link by file id`, async () => { const fileDetailsResponse: GraphFileDetails = { SiteId: "0f9b8f4f-0e8e-4630-bb0a-501442db9b64", VroomItemID: "013TMHP6UOOSLON57HT5GLKEU7R5UGWZVK", @@ -1175,7 +1176,78 @@ describe('utils/spo', () => { throw 'Invalid request'; }); - const actual = await spo.getFileSharingLinks(webUrl, 'b2307a39-e878-458b-bc90-03bc578531d6', '/sites/sales/documents/Test1.docx', 'users'); + const actual = await spo.getFileSharingLinks(webUrl, 'b2307a39-e878-458b-bc90-03bc578531d6', undefined, 'users', logger, true); + assert.deepStrictEqual(actual, graphResponse.value); + }); + + it(`retrieves the file sharing link by file url`, 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, undefined, '/sites/sales/documents/Test1.docx', 'users', logger, true); assert.deepStrictEqual(actual, graphResponse.value); }); }); \ No newline at end of file diff --git a/src/utils/spo.ts b/src/utils/spo.ts index 2d758ebe858..d9fea917947 100644 --- a/src/utils/spo.ts +++ b/src/utils/spo.ts @@ -837,7 +837,20 @@ export const spo = { return roledefinition; }, - async getFileSharingLinks(webUrl: string, fileId?: string | undefined, fileUrl?: string | undefined, scope?: string | undefined): Promise { + /** + * Retrieves the sharing links for a given file + * Returns a list of sharing links + * @param webUrl The web url + * @param fileId The id of the file. Specify either fileId or fileUrl but not both + * @param fileUrl The url of the file. Specify either fileId or fileUrl but not both + * @param scope The scope of the file + * @param logger The logger object + * @param verbose Set for verbose logging + */ + async getFileSharingLinks(webUrl: string, fileId?: string | undefined, fileUrl?: string | undefined, scope?: string | undefined, logger?: Logger, verbose?: boolean): Promise { + if (verbose && logger) { + logger.logToStderr(`Getting the sharing links for the file ${fileId || fileUrl}`); + } 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) {