Skip to content

Commit

Permalink
cleanup unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mloitzl committed Jun 23, 2024
1 parent 6939f62 commit 522a671
Showing 1 changed file with 1 addition and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,83 +117,7 @@ public void IApp_ParsePermissionRequests_Succeeds()
#endregion

[TestMethod]
public async Task GetGrants_Async()
{
//TestCommon.Instance.Mocking = false;
using PnPContext context = await TestCommon.Instance.GetContextAsync(TestCommonBase.TestSite);

ITenantApp app = null;
try
{
// App1 has permission request: Microsoft Graph (Sites.Selected)
var appManager = context.GetTenantAppManager();
app = await appManager.AddAsync(packagePath1, true);
var deployResult = await app.DeployAsync(false);

Assert.IsTrue(deployResult);

var permissionGrants1 =
await appManager.ServicePrincipal.ListGrantsAsync2();

Assert.IsNotNull(permissionGrants1);

var approveResult = await app.ApprovePermissionRequestsAsync();

Assert.IsNotNull(approveResult);
Assert.IsTrue(approveResult.Length > 0);

var permissionGrants2 =
await appManager.ServicePrincipal.ListGrantsAsync2();

Assert.IsNotNull(permissionGrants2);
Assert.IsTrue(permissionGrants2.Length > 0);
Assert.IsTrue(permissionGrants2.First(grant => grant.ResourceName.Equals("Microsoft Graph")).Scope.Contains("Sites.Selected"));
}
finally
{
if (app != null)
{
await app.RetractAsync();
await app.RemoveAsync();
}
}
}

[TestMethod]
public async Task ApprovePermissionsRequestTest_Async()
{
//TestCommon.Instance.Mocking = false;
using (PnPContext context = await TestCommon.Instance.GetContextAsync(TestCommonBase.TestSite))
{
ITenantApp app = null;
try
{
var appManager = context.GetTenantAppManager();
app = appManager.Add(packagePath, true);
var deployResult = app.Deploy(false);

Assert.IsTrue(deployResult);

List<IPermissionRequest> permissionRequests =
await appManager.ServicePrincipal.GetPermissionRequestsAsync();

var result =
await appManager.ServicePrincipal.ApprovePermissionRequestAsync(permissionRequests.First().Id
.ToString());

Assert.IsNotNull(result);
Assert.IsTrue(!string.IsNullOrWhiteSpace(result.ObjectId));
}
finally
{
var retractResult = app.Retract();
app.Remove();
}
}
}

[TestMethod]
public async Task GetPermissionsRequestsTest_Async()
public async Task ApprovePermissionRequestsTest_Async()
{
TestCommon.Instance.Mocking = false;
using PnPContext context = await TestCommon.Instance.GetContextAsync(TestCommonBase.TestSite);
Expand All @@ -209,36 +133,17 @@ public async Task GetPermissionsRequestsTest_Async()

Assert.IsTrue(deployResult1);

List<IPermissionRequest> permissionRequests1 =
await appManager.ServicePrincipal.GetPermissionRequestsAsync();

Assert.IsNotNull(permissionRequests1);
Assert.IsTrue(permissionRequests1.Count > 0);

IOAuth2PermissionGrant[] approvedPermissionGrants1
= await app1.ApprovePermissionRequestsAsync();

Assert.AreEqual(1, approvedPermissionGrants1.Length);

// App2 contains permission request: Office 365 SharePoint Online (Sites.Selected)
// Same scope name but different resource!
app2 = await appManager.AddAsync(packagePath2, true);
var deployResult2 = await app2.DeployAsync(false);

Assert.IsTrue(deployResult2);

List<IPermissionRequest> permissionRequests2 =
await appManager.ServicePrincipal.GetPermissionRequestsAsync();

// Now permissionRequests2 should contain Office 365 SharePoint Online, Sites.Selected, but not Microsoft Graph, Sites.Selected

Assert.IsNotNull(permissionRequests2);
Assert.IsTrue(permissionRequests2.Count > 0);
Assert.IsNotNull(permissionRequests2.FirstOrDefault(request => request.Resource.Equals("Office 365 SharePoint Online")));
Assert.IsTrue(permissionRequests2
.First(request => request.Resource.Equals("Office 365 SharePoint Online")).Scope
.Equals("Sites.Selected"));

IOAuth2PermissionGrant[] approvedPermissionGrants2 = await app2.ApprovePermissionRequestsAsync();
Assert.AreEqual(2, approvedPermissionGrants2.Length);
}
Expand Down

0 comments on commit 522a671

Please sign in to comment.