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 f7ba773
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
15 changes: 8 additions & 7 deletions src/m365/spo/commands/file/file-move.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ describe(commands.FILE_MOVE, () => {

afterEach(() => {
sinonUtil.restore([
Cli.executeCommand,
request.post,
request.get
request.get,
spo.removeFile
]);
});

Expand Down Expand Up @@ -175,7 +175,7 @@ describe(commands.FILE_MOVE, () => {
message: 'does not exist'
};

sinon.stub(Cli, 'executeCommand').returns(Promise.reject(fileDeleteError));
sinon.stub(spo, 'removeFile').rejects(fileDeleteError);

await command.action(logger, {
options: {
Expand All @@ -187,6 +187,7 @@ describe(commands.FILE_MOVE, () => {
}
});
});

it('should show error when recycleFile rejects with error', async () => {
stubAllPostRequests();
stubAllGetRequests();
Expand All @@ -198,7 +199,7 @@ describe(commands.FILE_MOVE, () => {
stderr: ''
};

sinon.stub(Cli, 'executeCommand').returns(Promise.reject(fileDeleteError));
sinon.stub(spo, 'removeFile').rejects(fileDeleteError);

await assert.rejects(command.action(logger, {
options: {
Expand All @@ -213,7 +214,7 @@ describe(commands.FILE_MOVE, () => {
it('should recycleFile format target url', async () => {
stubAllPostRequests();
stubAllGetRequests();
sinon.stub(Cli, 'executeCommand').returns(Promise.reject('abc'));
sinon.stub(spo, 'removeFile').rejects(new Error('abc'));
await assert.rejects(command.action(logger, {
options: {
debug: true,
Expand Down Expand Up @@ -261,7 +262,7 @@ describe(commands.FILE_MOVE, () => {
});
stubAllPostRequests(null, waitForJobResult);
stubAllGetRequests();
sinon.stub(Cli, 'executeCommand');
sinon.stub(spo, 'removeFile').resolves();
await assert.rejects(command.action(logger, {
options: {
verbose: true,
Expand All @@ -280,7 +281,7 @@ describe(commands.FILE_MOVE, () => {
});
stubAllPostRequests(null, waitForJobResult);
stubAllGetRequests();
sinon.stub(Cli, 'executeCommand');
sinon.stub(spo, 'removeFile').resolves();
await assert.rejects(command.action(logger, {
options: {
debug: true,
Expand Down
15 changes: 1 addition & 14 deletions src/m365/spo/commands/file/file-move.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as url from 'url';
import Command from '../../../../Command';
import { Cli } from '../../../../cli/Cli';
import { Logger } from '../../../../cli/Logger';
import GlobalOptions from '../../../../GlobalOptions';
import request, { CliRequestOptions } from '../../../../request';
Expand All @@ -9,9 +7,7 @@ import { urlUtil } from '../../../../utils/urlUtil';
import { validation } from '../../../../utils/validation';
import SpoCommand from '../../../base/SpoCommand';
import commands from '../../commands';
import { Options as SpoFileRemoveOptions } from './file-remove';
import { formatting } from '../../../../utils/formatting';
const removeCommand: Command = require('./file-remove');

interface CommandArgs {
options: Options;
Expand Down Expand Up @@ -182,17 +178,8 @@ class SpoFileMoveCommand extends SpoCommand {

const targetFileServerRelativeUrl: string = `${urlUtil.getServerRelativePath(contextResponse.WebFullUrl, targetUrl)}/${filename}`;

const removeOptions: SpoFileRemoveOptions = {
webUrl: contextResponse.WebFullUrl,
url: targetFileServerRelativeUrl,
recycle: true,
confirm: true,
debug: this.debug,
verbose: this.verbose
};

try {
await Cli.executeCommand(removeCommand as Command, { options: { ...removeOptions, _: [] } });
await spo.removeFile(contextResponse.WebFullUrl, targetFileServerRelativeUrl, true);
}
catch (err: any) {
if (err !== undefined && err.message !== undefined && err.message.includes('does not exist')) {
Expand Down
28 changes: 28 additions & 0 deletions src/utils/spo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,4 +1107,32 @@ describe('utils/spo', () => {

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

it('removes a file', async () => {
const postStub = sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/_api/web/GetFileByServerRelativeUrl('%2FSharedDocuments%2FDocument.docx')`) {
return;
}

throw 'Invalid request';
});


await spo.removeFile('https://contoso.sharepoint.com', 'SharedDocuments/Document.docx');
assert(postStub.called);
});

it('removes a file and recycles it', async () => {
const postStub = sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/_api/web/GetFileByServerRelativeUrl('%2FSharedDocuments%2FDocument.docx')/recycle()`) {
return;
}

throw 'Invalid request';
});


await spo.removeFile('https://contoso.sharepoint.com', 'SharedDocuments/Document.docx', true);
assert(postStub.called);
});
});
24 changes: 23 additions & 1 deletion src/utils/spo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,5 +835,27 @@ export const spo = {
roledefinition.RoleTypeKindValue = RoleType[roledefinition.RoleTypeKind];

return roledefinition;
},

async removeFile(webUrl: string, url: string, recycle?: boolean): Promise<void> {
const serverRelativePath = urlUtil.getServerRelativePath(webUrl, url);
let requestUrl = `${webUrl}/_api/web/GetFileByServerRelativeUrl('${formatting.encodeQueryParameter(serverRelativePath)}')`;

if (recycle) {
requestUrl += `/recycle()`;
}

const requestOptions: CliRequestOptions = {
url: requestUrl,
method: 'POST',
headers: {
'X-HTTP-Method': 'DELETE',
'If-Match': '*',
'accept': 'application/json;odata=nometadata'
},
responseType: 'json'
};

await request.post(requestOptions);
}
};
};

0 comments on commit f7ba773

Please sign in to comment.