Skip to content

Commit

Permalink
Fingers crossed
Browse files Browse the repository at this point in the history
  • Loading branch information
MathijsVerbeeck committed Jul 25, 2023
1 parent 15b568e commit b05d4c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/m365/spo/commands/listitem/listitem-attachment-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import auth from '../../../../Auth';
import { Cli } from '../../../../cli/Cli';
import { CommandInfo } from '../../../../cli/CommandInfo';
import { Logger } from '../../../../cli/Logger';
import Command from '../../../../Command';
//, { CommandError }
import Command, { CommandError } from '../../../../Command';
import { formatting } from '../../../../utils/formatting';
import { pid } from '../../../../utils/pid';
import { session } from '../../../../utils/session';
Expand Down Expand Up @@ -65,7 +64,7 @@ describe(commands.LISTITEM_ATTACHMENT_ADD, () => {
afterEach(() => {
sinonUtil.restore([,
fs.existsSync,
fs.readSync,
fs.readFileSync,
request.post,
cli.getSettingWithDefaultValue
]);
Expand Down Expand Up @@ -116,6 +115,7 @@ describe(commands.LISTITEM_ATTACHMENT_ADD, () => {

it('adds attachment to listitem in list retrieved by id while specifying fileName', async () => {
sinon.stub(fs, 'existsSync').returns(true);
sinon.stub(fs, 'readFileSync').returns('content read');
sinon.stub(request, 'post').callsFake(async (args) => {
if (args.url === `${webUrl}/_api/web/lists(guid'${listId}')/items(${listItemId})/AttachmentFiles/add(FileName='${fileName}')`) {
return response;
Expand All @@ -130,6 +130,7 @@ describe(commands.LISTITEM_ATTACHMENT_ADD, () => {

it('adds attachment to listitem in list retrieved by url while not specifying fileName', async () => {
sinon.stub(fs, 'existsSync').returns(true);
sinon.stub(fs, 'readFileSync').returns('content read');
sinon.stub(request, 'post').callsFake(async (args) => {
if (args.url === `${webUrl}/_api/web/GetList('${formatting.encodeQueryParameter(listServerRelativeUrl)}')/items(${listItemId})/AttachmentFiles/add(FileName='${filePath.replace(/^.*[\\\/]/, '')}')`) {
return response;
Expand All @@ -144,6 +145,7 @@ describe(commands.LISTITEM_ATTACHMENT_ADD, () => {

it('adds attachment to listitem in list retrieved by url while specifying fileName without extension', async () => {
sinon.stub(fs, 'existsSync').returns(true);
sinon.stub(fs, 'readFileSync').returns('content read');
const fileNameWithoutExtension = fileName.split('.')[0];
const fileNameWithExtension = `${fileNameWithoutExtension}.${filePath.split('.').pop()}`;
sinon.stub(request, 'post').callsFake(async (args) => {
Expand All @@ -160,6 +162,7 @@ describe(commands.LISTITEM_ATTACHMENT_ADD, () => {

it('handles error when file with specific name already exists', async () => {
sinon.stub(fs, 'existsSync').returns(true);
sinon.stub(fs, 'readFileSync').returns('content read');
const error = {
error: {
'odata.error': {
Expand All @@ -179,7 +182,7 @@ describe(commands.LISTITEM_ATTACHMENT_ADD, () => {
throw 'Invalid request';
});

await command.action(logger, { options: { verbose: true, webUrl: webUrl, listId: listId, listItemId: listItemId, filePath: filePath, fileName: fileName } });
assert(loggerLogSpy.calledWith(error.error['odata.error'].message.value));
await assert.rejects(command.action(logger, { options: { verbose: true, webUrl: webUrl, listId: listId, listItemId: listItemId, filePath: filePath, fileName: fileName } }),
new CommandError(error.error['odata.error'].message.value));
});
});
6 changes: 3 additions & 3 deletions src/m365/spo/commands/listitem/listitem-attachment-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ class SpoListItemAttachmentAddCommand extends SpoCommand {

private getListUrl(webUrl: string, listId?: string, listTitle?: string, listUrl?: string): string {
if (listId) {
return `/lists(guid'${formatting.encodeQueryParameter(listId)}')`;
return `lists(guid'${formatting.encodeQueryParameter(listId)}')`;
}
else if (listTitle) {
return `/lists/getByTitle('${formatting.encodeQueryParameter(listTitle)}')`;
return `lists/getByTitle('${formatting.encodeQueryParameter(listTitle)}')`;
}
else {
const listServerRelativeUrl: string = urlUtil.getServerRelativePath(webUrl, listUrl!);
return `/GetList('${formatting.encodeQueryParameter(listServerRelativeUrl)}')`;
return `GetList('${formatting.encodeQueryParameter(listServerRelativeUrl)}')`;
}
}
}
Expand Down

0 comments on commit b05d4c0

Please sign in to comment.