public async Task ReconcileIntegrationTest()
        {
            var client    = new VstsRestClient(_config.Organization, _config.Token);
            var projectId = (await client.GetAsync(Project.Properties(_config.Project))).Id;

            await ManagePermissions
            .ForRepository(client, projectId, RepositoryId)
            .Permissions(Permissions.DeleteRepository)
            .SetToAsync(PermissionId.Allow);

            var rule = new NobodyCanDeleteTheRepository(client);

            (await rule.EvaluateAsync(projectId, RepositoryId))
            .ShouldBe(false);

            await rule.ReconcileAsync(projectId, RepositoryId);

            await Policy
            .Handle <Exception>()
            .WaitAndRetryAsync(Constants.NumRetries, t => TimeSpan.FromSeconds(t))
            .ExecuteAsync(async() =>
            {
                (await rule.EvaluateAsync(projectId, RepositoryId)).ShouldBe(true);
            });
        }
        public async Task EvaluateIntegrationTest()
        {
            var client    = new VstsRestClient(_config.Organization, _config.Token);
            var projectId = (await client.GetAsync(Project.Properties(_config.Project))).Id;

            var rule = new NobodyCanDeleteTheRepository(client);

            (await rule.EvaluateAsync(projectId, RepositoryId)).ShouldBe(true);
        }
        public async Task GivenNoApplicationGroupHasPermissionToDeleteRepo_WhenEvaluating_ThenTrue()
        {
            var client = Substitute.For <IVstsRestClient>();

            InitializeLookupData(client, PermissionId.Deny);

            var rule = new NobodyCanDeleteTheRepository(client);

            (await rule.EvaluateAsync("", RepositoryId)).ShouldBe(true);
        }
        public async Task GivenAnApplicationGroupHasPermissionToDeleteRepoWithAllowInHerited_WhenEvaluating_ThenFalse()
        {
            var client = Substitute.For <IVstsRestClient>();

            InitializeLookupData(client, PermissionId.AllowInherited);

            var rule = new NobodyCanDeleteTheRepository(client);

            (await rule.EvaluateAsync("", RepositoryId)).ShouldBe(false);
        }
        public async Task IgnoreGroupsProjectCollectionAdminAndProjectCollectionServiceAccounts()
        {
            var client = Substitute.For <IVstsRestClient>();

            var applicationGroup1 = new ApplicationGroup
            {
                FriendlyDisplayName = "Project Collection Administrators",
                DisplayName         = "blblblablaaProject Collection Administrators",
                TeamFoundationId    = "11"
            };

            var applicationGroup2 = new ApplicationGroup
            {
                FriendlyDisplayName = "Project Collection Service Accounts",
                DisplayName         = "blblblablaaProject Collection Service Accounts",
                TeamFoundationId    = "22"
            };

            var applicationGroup3 = new ApplicationGroup
            {
                FriendlyDisplayName = "Dit is een test",
                DisplayName         = "blblblablaaDit is een testy",
                TeamFoundationId    = "33"
            };

            var applicationGroups = new ApplicationGroups
            {
                Identities = new[] { applicationGroup1, applicationGroup2, applicationGroup3 }
            };

            InitializeLookupData(client, PermissionId.Deny);

            client.GetAsync(Arg.Any <IVstsRequest <ApplicationGroups> >()).Returns(applicationGroups);

            var rule = new NobodyCanDeleteTheRepository(client);

            (await rule.EvaluateAsync("", RepositoryId)).ShouldBe(true);


            await client
            .DidNotReceive()
            .GetAsync(Arg.Is <IVstsRequest <PermissionsSetId> >(x =>
                                                                x.QueryParams.Contains(new KeyValuePair <string, object>("tfid", "11"))));

            await client
            .DidNotReceive()
            .GetAsync(Arg.Is <IVstsRequest <PermissionsSetId> >(x =>
                                                                x.QueryParams.Contains(new KeyValuePair <string, object>("tfid", "22"))));

            await client
            .Received()
            .GetAsync(Arg.Is <IVstsRequest <PermissionsSetId> >(x =>
                                                                x.QueryParams.Contains(new KeyValuePair <string, object>("tfid", "33"))));
        }
        public async Task GivenPermissionIsNotSet_WhenFixPermission_IsNotUpdated()
        {
            var client = Substitute.For <IVstsRestClient>();

            InitializeLookupData(client, PermissionId.NotSet);


            var rule = new NobodyCanDeleteTheRepository(client);
            await rule.ReconcileAsync("TAS", "123");

            await client
            .DidNotReceive()
            .PostAsync(Arg.Any <IVstsRequest <Permissions.UpdateWrapper, object> >(),
                       Arg.Any <Permissions.UpdateWrapper>());
        }
        public async Task GivenPermissionIsAllowWhenFixPermissionIsUpdatedToDeny()
        {
            var client = Substitute.For <IVstsRestClient>();

            InitializeLookupData(client, PermissionId.Allow);

            var rule = new NobodyCanDeleteTheRepository(client);
            await rule.ReconcileAsync("TAS", "123");

            await client
            .Received()
            .PostAsync(Arg.Any <IVstsRequest <Permissions.UpdateWrapper, object> >(),
                       Arg.Is <Permissions.UpdateWrapper>(x =>
                                                          x.UpdatePackage.Contains("123") &&
                                                          x.UpdatePackage.Contains(@"PermissionId"":2")));
        }