From d9b8f73a5a42a8bae4c259e72e820ca959dc294c Mon Sep 17 00:00:00 2001 From: Milan Holemans <11723921+milanholemans@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:04:51 +0200 Subject: [PATCH] Fixes failing tests for 'site admin remove' command --- .../commands/site/site-admin-remove.spec.ts | 56 +++++++++---------- .../spo/commands/site/site-admin-remove.ts | 11 ++-- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/m365/spo/commands/site/site-admin-remove.spec.ts b/src/m365/spo/commands/site/site-admin-remove.spec.ts index 02586171779..372b42b9dc8 100644 --- a/src/m365/spo/commands/site/site-admin-remove.spec.ts +++ b/src/m365/spo/commands/site/site-admin-remove.spec.ts @@ -144,7 +144,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { @@ -156,9 +156,9 @@ describe(commands.SITE_ADMIN_REMOVE, () => { const postStub = sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: listOfAdminsFromAdminSource - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators`) { @@ -195,7 +195,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } @@ -208,7 +208,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { const postStub = sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: [ ...listOfAdminsFromAdminSource, { @@ -218,7 +218,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { userPrincipalName: '' } ] - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators`) { @@ -255,7 +255,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { @@ -267,9 +267,9 @@ describe(commands.SITE_ADMIN_REMOVE, () => { const postStub = sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: listOfAdminsFromAdminSource - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators`) { @@ -297,7 +297,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { @@ -309,9 +309,9 @@ describe(commands.SITE_ADMIN_REMOVE, () => { sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: listOfAdminsFromAdminSource - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators` && opts.data.secondaryAdministratorsFieldsData) { @@ -331,7 +331,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/users('user1loginName%40email.com')`) { @@ -347,9 +347,9 @@ describe(commands.SITE_ADMIN_REMOVE, () => { const postStub = sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: listOfAdminsFromAdminSource - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators`) { @@ -377,7 +377,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { @@ -397,7 +397,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/users('userPrimaryAdminEmail%40email.com')`) { @@ -422,7 +422,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { @@ -434,7 +434,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { const postStub = sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: [ ...listOfAdminsFromAdminSource, { @@ -444,7 +444,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { userPrincipalName: '' } ] - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators`) { @@ -473,7 +473,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { @@ -485,7 +485,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { const postStub = sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: [ ...listOfAdminsFromAdminSource, { @@ -495,7 +495,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { userPrincipalName: '' } ] - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators`) { @@ -524,7 +524,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/sites('${siteId}')?$select=OwnerLoginName`) { - return JSON.stringify({ OwnerLoginName: primaryAdminLoginName }); + return { OwnerLoginName: primaryAdminLoginName }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { @@ -536,7 +536,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { const postStub = sinon.stub(request, 'post').callsFake(async opts => { if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${siteId}'`) { - return JSON.stringify({ + return { value: [ ...listOfAdminsFromAdminSource, { @@ -546,7 +546,7 @@ describe(commands.SITE_ADMIN_REMOVE, () => { userPrincipalName: '' } ] - }); + }; } if (opts.url === `${adminUrl}/_api/SPOInternalUseOnly.Tenant/SetSiteSecondaryAdministrators`) { @@ -712,9 +712,9 @@ describe(commands.SITE_ADMIN_REMOVE, () => { } if (opts.url === `${adminUrl}/_api/SPO.Tenant/GetSiteAdministrators?siteId='${incorrectSiteId}'`) { - return JSON.stringify({ + return { value: listOfAdminsFromAdminSource - }); + }; } if (opts.url === `https://graph.microsoft.com/v1.0/sites/contoso.sharepoint.com:/sites/site?$select=id`) { diff --git a/src/m365/spo/commands/site/site-admin-remove.ts b/src/m365/spo/commands/site/site-admin-remove.ts index cf2a9fc4006..5136679d77f 100644 --- a/src/m365/spo/commands/site/site-admin-remove.ts +++ b/src/m365/spo/commands/site/site-admin-remove.ts @@ -171,12 +171,12 @@ class SpoSiteAdminRemoveCommand extends SpoCommand { headers: { accept: 'application/json;odata=nometadata', 'content-type': 'application/json;charset=utf-8' - } + }, + responseType: 'json' }; - const response: string = await request.post(requestOptions); - const responseContent: AdminResult = JSON.parse(response); - return responseContent.value; + const response = await request.post(requestOptions); + return response.value; } private async getCorrectLoginName(options: Options): Promise { @@ -216,7 +216,8 @@ class SpoSiteAdminRemoveCommand extends SpoCommand { secondaryAdministratorLoginNames: admins.map(u => u.loginName) } - } + }, + responseType: 'json' }; await request.post(requestOptions);