Exemplo n.º 1
0
        public async Task TeamifyTeamSiteUsingDelegatedPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteOptions teamSiteToCreate = null;

            // Create the site collection
            Uri createdSiteCollection = null;

            try
            {
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
                {
                    // Persist the used site url as we need to have the same url when we run an offline test
                    string alias;
                    if (!TestCommon.Instance.Mocking)
                    {
                        alias = $"pnpcoresdktestteamsite{Guid.NewGuid().ToString().Replace("-", "")}";
                        Dictionary <string, string> properties = new Dictionary <string, string>
                        {
                            { "Alias", alias }
                        };
                        TestManager.SaveProperties(context, properties);
                    }
                    else
                    {
                        alias = TestManager.GetProperties(context)["Alias"];
                    }

                    teamSiteToCreate = new TeamSiteOptions(alias, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                        IsPublic    = true,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions))
                    {
                        createdSiteCollection = newSiteContext.Uri;

                        Assert.IsTrue(newSiteContext.Site.GroupId != Guid.Empty);

                        // Check prompt
                        var isAddTeamsPromptHidden = newSiteContext.GetSiteCollectionManager().IsAddTeamsPromptHidden(newSiteContext.Uri);

                        Assert.IsFalse(isAddTeamsPromptHidden);

                        // Hide prompt
                        var hidden = newSiteContext.GetSiteCollectionManager().HideAddTeamsPrompt(newSiteContext.Uri);

                        isAddTeamsPromptHidden = newSiteContext.GetSiteCollectionManager().IsAddTeamsPromptHidden(newSiteContext.Uri);
                        Assert.IsTrue(isAddTeamsPromptHidden);

                        // Add teams team
                        using (var contextWithTeam = newSiteContext.GetTeamManager().CreateTeam(new TeamForGroupOptions(newSiteContext.Site.GroupId)))
                        {
                            Assert.IsTrue(contextWithTeam.Team.Requested);
                            Assert.IsTrue(contextWithTeam.Team.IsPropertyAvailable(p => p.Id));
                        }
                    }

                    if (context.Mode == TestMode.Record)
                    {
                        // Add a little delay between creation and deletion
                        await Task.Delay(TimeSpan.FromSeconds(15));
                    }
                }
            }
            finally
            {
                TestCommon.Instance.UseApplicationPermissions = false;
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(createdSiteCollection);
                }
            }
        }
Exemplo n.º 2
0
        public async Task CreateTeamSiteUsingDelegatedPermissions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteOptions teamSiteToCreate = null;

            // Create the site collection
            Uri createdSiteCollection = null;

            try
            {
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
                {
                    // Persist the used site url as we need to have the same url when we run an offline test
                    string alias;
                    if (!TestCommon.Instance.Mocking)
                    {
                        alias = $"pnpcoresdktestteamsite{Guid.NewGuid().ToString().Replace("-", "")}";
                        Dictionary <string, string> properties = new Dictionary <string, string>
                        {
                            { "Alias", alias }
                        };
                        TestManager.SaveProperties(context, properties);
                    }
                    else
                    {
                        alias = TestManager.GetProperties(context)["Alias"];
                    }

                    teamSiteToCreate = new TeamSiteOptions(alias, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                        IsPublic    = true,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    int rewrites = 0;
                    context.BatchClient.MockingFileRewriteHandler = (string input) =>
                    {
                        if (!siteCreationOptions.UsingApplicationPermissions.Value && rewrites < 2 && input.Contains(",\"SiteStatus\":2,"))
                        {
                            input = input.Replace(",\"SiteStatus\":2,", ",\"SiteStatus\":1,");
                            rewrites++;
                        }
                        return(input);
                    };

                    using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions))
                    {
                        createdSiteCollection = newSiteContext.Uri;

                        Assert.IsTrue(newSiteContext.Site.GroupId != Guid.Empty);

                        var web = await newSiteContext.Web.GetAsync(p => p.Title, p => p.Description, p => p.Language);

                        Assert.IsTrue(web.Description == teamSiteToCreate.Description);
                        Assert.IsTrue(web.Language == (int)teamSiteToCreate.Language);
                    }

                    if (context.Mode == TestMode.Record)
                    {
                        // Add a little delay between creation and deletion
                        await Task.Delay(TimeSpan.FromSeconds(15));
                    }
                }
            }
            finally
            {
                TestCommon.Instance.UseApplicationPermissions = false;
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(createdSiteCollection);
                }
            }
        }
Exemplo n.º 3
0
        public async Task HandleExceptions()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
            {
                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    using (var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(null, siteCreationOptions))
                    {
                    }
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    CommunicationSiteOptions communicationSiteToCreate = new CommunicationSiteOptions(null, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                    };
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    CommunicationSiteOptions communicationSiteToCreate = new CommunicationSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), null)
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                    };
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    TeamSiteOptions communicationSiteToCreate = new TeamSiteOptions(null, "display name");
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    TeamSiteOptions communicationSiteToCreate = new TeamSiteOptions("alias", null);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(null, "title", "webtemplate", "owner", Language.Default, Model.SharePoint.TimeZone.None);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "", "webtemplate", "owner", Language.Default, Model.SharePoint.TimeZone.None);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "title", "", "owner", Language.Default, Model.SharePoint.TimeZone.None);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "title", "webtemplate", "", Language.Default, Model.SharePoint.TimeZone.None);
                });

                Assert.ThrowsException <ArgumentException>(() =>
                {
                    ClassicSiteOptions communicationSiteToCreate = new ClassicSiteOptions(new Uri("https://contoso.sharepoint.com/sites/dummy"), "title", "webtemplate", "owner", Language.Default, Model.SharePoint.TimeZone.None);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    CreationOptions creationOptions = new CreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    context.GetSiteCollectionManager().ConnectSiteCollectionToGroup(null, creationOptions);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    ConnectSiteToGroupOptions communicationSiteToCreate = new ConnectSiteToGroupOptions(null, "alias", "displayname");
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    context.GetSiteCollectionManager().RecycleSiteCollection(null);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    context.GetSiteCollectionManager().RestoreSiteCollection(null);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(null);
                });

                Assert.ThrowsException <ArgumentNullException>(() =>
                {
                    context.GetSiteCollectionManager().GetSiteCollectionProperties(null);
                });
            }
        }
Exemplo n.º 4
0
        public async Task SetSiteCollectionAdminsGroupSite()
        {
            //TestCommon.Instance.Mocking = false;
            TestCommon.Instance.UseApplicationPermissions = false;

            TeamSiteOptions teamSiteToCreate = null;

            // Create the site collection
            Uri createdSiteCollection = null;

            try
            {
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
                {
                    // Persist the used site url as we need to have the same url when we run an offline test
                    string alias;
                    if (!TestCommon.Instance.Mocking)
                    {
                        alias = $"pnpcoresdktestteamsite{Guid.NewGuid().ToString().Replace("-", "")}";
                        Dictionary <string, string> properties = new Dictionary <string, string>
                        {
                            { "Alias", alias }
                        };
                        TestManager.SaveProperties(context, properties);
                    }
                    else
                    {
                        alias = TestManager.GetProperties(context)["Alias"];
                    }

                    teamSiteToCreate = new TeamSiteOptions(alias, "PnP Core SDK Test")
                    {
                        Description = "This is a test site collection",
                        Language    = Language.English,
                        IsPublic    = true,
                    };


                    SiteCreationOptions siteCreationOptions = new SiteCreationOptions()
                    {
                        UsingApplicationPermissions = false
                    };

                    var newSiteContext = context.GetSiteCollectionManager().CreateSiteCollection(teamSiteToCreate, siteCreationOptions);
                    createdSiteCollection = newSiteContext.Uri;

                    // get current admins
                    var admins = context.GetSiteCollectionManager().GetSiteCollectionAdmins(createdSiteCollection);

                    // update admins
                    List <string> newAdmins = new List <string>();
                    foreach (var admin in admins)
                    {
                        newAdmins.Add(admin.LoginName);
                    }

                    // everyone claim
                    newAdmins.Add("c:0(.s|true");

                    //Also set admin via group owner membership
                    List <Guid> newGroupOwners = new List <Guid>();
                    foreach (var groupOwner in admins.Where(p => p.IsMicrosoft365GroupOwner == true))
                    {
                        newGroupOwners.Add(groupOwner.Id);
                    }

                    // set admins
                    context.GetSiteCollectionManager().SetSiteCollectionAdmins(createdSiteCollection, newAdmins, newGroupOwners);

                    // Get admins again and verify if the added admin is present
                    admins = context.GetSiteCollectionManager().GetSiteCollectionAdmins(createdSiteCollection);

                    Assert.IsNotNull(admins.FirstOrDefault(p => p.LoginName == "c:0(.s|true"));


                    if (context.Mode == TestMode.Record)
                    {
                        // Add a little delay between creation and deletion
                        await Task.Delay(TimeSpan.FromSeconds(15));
                    }
                }
            }
            finally
            {
                TestCommon.Instance.UseApplicationPermissions = false;
                using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite, 1))
                {
                    context.GetSiteCollectionManager().DeleteSiteCollection(createdSiteCollection);
                }
            }
        }