Skip to content

Commit

Permalink
Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodecleyre committed Jul 18, 2023
1 parent eddb54e commit 39c7cd6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/m365/spo/commands/file/file-sharinglink-clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
78 changes: 75 additions & 3 deletions src/utils/spo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
});
});
15 changes: 14 additions & 1 deletion src/utils/spo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,20 @@ export const spo = {
return roledefinition;
},

async getFileSharingLinks(webUrl: string, fileId?: string | undefined, fileUrl?: string | undefined, scope?: string | undefined): Promise<any> {
/**
* 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<any> {
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) {
Expand Down

0 comments on commit 39c7cd6

Please sign in to comment.