public async Task NormalOwnerCannotRemoveNamespaceOwner()
            {
                var namespaceOwner = new User {
                    Key = 100, Username = "******"
                };
                var nonNamespaceOwner = new User {
                    Key = 101, Username = "******"
                };
                var package = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = true, Owners = new List <User> {
                        namespaceOwner, nonNamespaceOwner
                    }
                };
                var existingNamespace1 = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);

                namespaceOwner.ReservedNamespaces.Add(existingNamespace1);
                package.ReservedNamespaces.Add(existingNamespace1);
                existingNamespace1.Owners.Add(namespaceOwner);
                existingNamespace1.PackageRegistrations.Add(package);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.RemovePackageOwnerAsync(packageRegistration: package, requestingOwner: nonNamespaceOwner, ownerToBeRemoved: namespaceOwner));
            }
            public async Task RemovingOneNamespaceOwnerDoesNotRemoveVerifiedFlag()
            {
                var existingOwner1 = new User {
                    Key = 100, Username = "******"
                };
                var existingOwner2 = new User {
                    Key = 101, Username = "******"
                };
                var existingNamespace = new ReservedNamespace {
                    Value = "microsoft.aspnet.", IsSharedNamespace = false, IsPrefix = true, Owners = new HashSet <User> {
                        existingOwner1, existingOwner2
                    }
                };
                var package = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = true, Owners = new List <User> {
                        existingOwner1, existingOwner2
                    }
                };

                existingOwner1.ReservedNamespaces.Add(existingNamespace);
                existingOwner2.ReservedNamespaces.Add(existingNamespace);
                package.ReservedNamespaces.Add(existingNamespace);
                existingNamespace.PackageRegistrations.Add(package);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await service.RemovePackageOwnerAsync(package, existingOwner2, existingOwner1);

                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.Is <IReadOnlyCollection <PackageRegistration> >(pr => pr.First() == package), false, false), Times.Never);
                reservedNamespaceService.Verify(x => x.RemovePackageRegistrationFromNamespace(It.IsAny <ReservedNamespace>(), It.IsAny <PackageRegistration>()), Times.Never);
                Assert.True(package.IsVerified);
            }
Exemple #3
0
        public async Task AddReservedNamespaceAsync(ReservedNamespace newNamespace)
        {
            if (newNamespace == null)
            {
                throw new ArgumentNullException(nameof(newNamespace));
            }

            ValidateNamespace(newNamespace.Value);

            var matchingReservedNamespaces = FindAllReservedNamespacesForPrefix(prefix: newNamespace.Value, getExactMatches: !newNamespace.IsPrefix);

            if (matchingReservedNamespaces.Any())
            {
                throw new InvalidOperationException(Strings.ReservedNamespace_NamespaceNotAvailable);
            }

            // Mark the new namespace as shared if it matches any liberal namespace which is a shared
            // namespace. For eg: A.B.* is a shared namespace, when reserving A.B.C.* namespace,
            // make it a shared namespace. This ensures that all namespaces under a shared
            // namespace are also shared to keep the data consistent.
            if (!newNamespace.IsSharedNamespace && ShouldForceSharedNamespace(newNamespace.Value))
            {
                newNamespace.IsSharedNamespace = true;
            }

            ReservedNamespaceRepository.InsertOnCommit(newNamespace);
            await ReservedNamespaceRepository.CommitChangesAsync();
        }
            public async Task AddingOwnerMarksPackageVerifiedForMatchingNamespace()
            {
                var package = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = false
                };
                var pendingOwner = new User {
                    Key = 100, Username = "******"
                };
                var existingNamespace = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);

                pendingOwner.ReservedNamespaces.Add(existingNamespace);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await service.AddPackageOwnerAsync(package, pendingOwner);

                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.Is <IReadOnlyCollection <PackageRegistration> >(pr => pr.First() == package), true, true));
                packageOwnerRequestService.Verify(x => x.GetPackageOwnershipRequests(It.IsAny <PackageRegistration>(), It.IsAny <User>(), It.IsAny <User>()));
                packageOwnerRequestService.Verify(x => x.DeletePackageOwnershipRequest(It.IsAny <PackageOwnerRequest>(), true));
                reservedNamespaceService.Verify(x => x.AddPackageRegistrationToNamespace(It.IsAny <string>(), It.IsAny <PackageRegistration>()), Times.Once);
                Assert.True(package.IsVerified);
            }
            public async Task RemovingNamespaceOwnerRemovesPackageVerified()
            {
                var existingOwner1 = new User {
                    Key = 100, Username = "******"
                };
                var existingNamespace = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);
                var package           = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = true, Owners = new List <User> {
                        existingOwner1
                    }
                };

                existingOwner1.ReservedNamespaces.Add(existingNamespace);
                package.ReservedNamespaces.Add(existingNamespace);
                existingNamespace.Owners.Add(existingOwner1);
                existingNamespace.PackageRegistrations.Add(package);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await service.RemovePackageOwnerAsync(package, existingOwner1, existingOwner1);

                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.Is <IReadOnlyCollection <PackageRegistration> >(pr => pr.First() == package), It.Is <bool>(b => b == false)));
                reservedNamespaceService.Verify(x => x.RemovePackageRegistrationFromNamespace(It.IsAny <string>(), It.IsAny <PackageRegistration>()), Times.Once);
                Assert.False(package.IsVerified);
            }
 public ReservedNamespaceListItemViewModel(ReservedNamespace reservedNamespace)
 {
     Value    = reservedNamespace.Value;
     IsPublic = reservedNamespace.IsSharedNamespace;
     IsPrefix = reservedNamespace.IsPrefix;
     Owners   = reservedNamespace.Owners;
 }
            public async Task NonNamespaceOwnerCanRemoveOtherSimilarOwners()
            {
                var existingOwner1 = new User {
                    Key = 100, Username = "******"
                };
                var existingOwner2 = new User {
                    Key = 101, Username = "******"
                };
                var existingOwner3 = new User {
                    Key = 102, Username = "******"
                };
                var package = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = true, Owners = new List <User> {
                        existingOwner1, existingOwner2, existingOwner3
                    }
                };
                var existingNamespace1 = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);

                existingOwner3.ReservedNamespaces.Add(existingNamespace1);
                package.ReservedNamespaces.Add(existingNamespace1);
                existingNamespace1.Owners.Add(existingOwner3);
                existingNamespace1.PackageRegistrations.Add(package);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await service.RemovePackageOwnerAsync(package, existingOwner1, existingOwner2);

                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.Is <IReadOnlyCollection <PackageRegistration> >(pr => pr.First() == package), false, false), Times.Never);
                reservedNamespaceService.Verify(x => x.RemovePackageRegistrationFromNamespace(existingNamespace1, package), Times.Never);
                Assert.True(package.IsVerified);
            }
            private async Task RemovingNamespaceOwnerRemovesPackageVerified(User owner, User requestingUser)
            {
                var existingNamespace = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);
                var package           = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = true, Owners = new List <User> {
                        owner
                    }
                };

                owner.ReservedNamespaces.Add(existingNamespace);
                package.ReservedNamespaces.Add(existingNamespace);
                existingNamespace.Owners.Add(owner);
                existingNamespace.PackageRegistrations.Add(package);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await service.RemovePackageOwnerAsync(package, requestingUser, owner);

                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.Is <IReadOnlyCollection <PackageRegistration> >(pr => pr.First() == package), false, false));
                reservedNamespaceService.Verify(x => x.RemovePackageRegistrationFromNamespace(It.IsAny <ReservedNamespace>(), It.IsAny <PackageRegistration>()), Times.Once);
                Assert.False(package.IsVerified);
            }
Exemple #9
0
        /// <summary>
        /// This method fetches the reserved namespace matching the prefix and removes the
        /// package registration entry from the reserved namespace, the provided package registration
        /// should be an entry in the database. It is the caller's responsibility to commit the
        /// changes to the entity context.
        /// </summary>
        /// <param name="prefix">The prefix value of the reserved namespace to modify</param>
        /// <param name="packageRegistration">The package registration entity to be removed.</param>
        /// <returns>Awaitable task</returns>
        public void RemovePackageRegistrationFromNamespace(ReservedNamespace reservedNamespace, PackageRegistration packageRegistration)
        {
            if (reservedNamespace == null)
            {
                throw new ArgumentNullException(nameof(reservedNamespace));
            }

            if (packageRegistration == null)
            {
                throw new ArgumentNullException(nameof(packageRegistration));
            }

            reservedNamespace.PackageRegistrations.Remove(packageRegistration);
            packageRegistration.ReservedNamespaces.Remove(reservedNamespace);
        }
Exemple #10
0
        public async Task AddReservedNamespaceAsync(ReservedNamespace newNamespace)
        {
            if (newNamespace == null)
            {
                throw new ArgumentNullException(nameof(newNamespace));
            }

            ValidateNamespace(newNamespace.Value);

            var matchingReservedNamespaces = FindAllReservedNamespacesForPrefix(prefix: newNamespace.Value, getExactMatches: !newNamespace.IsPrefix);

            if (matchingReservedNamespaces.Any())
            {
                throw new InvalidOperationException(Strings.ReservedNamespace_NamespaceNotAvailable);
            }

            ReservedNamespaceRepository.InsertOnCommit(newNamespace);
            await ReservedNamespaceRepository.CommitChangesAsync();
        }
            public async Task AddingOwnerAddsPackageRegistrationToMultipleNamespaces()
            {
                var package = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = false
                };
                var pendingOwner = new User {
                    Key = 100, Username = "******"
                };
                var existingNamespace  = new ReservedNamespace("microsoft.", isSharedNamespace: false, isPrefix: true);
                var existingNamespace2 = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);

                pendingOwner.ReservedNamespaces.Add(existingNamespace);
                pendingOwner.ReservedNamespaces.Add(existingNamespace2);

                var reservedNamespaceService = new Mock <IReservedNamespaceService>();

                var service = CreateService(reservedNamespaceService: reservedNamespaceService);
                await service.AddPackageOwnerAsync(package, pendingOwner);

                reservedNamespaceService.Verify(x => x.AddPackageRegistrationToNamespace(It.IsAny <string>(), It.IsAny <PackageRegistration>()), Times.Exactly(2));
            }
            public async Task AddingOwnerDoesNotMarkRegistrationVerifiedForAbsoluteNamespace()
            {
                var package = new PackageRegistration {
                    Key = 2, Id = "AbsolutePackage1", IsVerified = false
                };
                var pendingOwner = new User {
                    Key = 100, Username = "******"
                };
                var existingNamespace = new ReservedNamespace("Absolute", isSharedNamespace: false, isPrefix: false);

                pendingOwner.ReservedNamespaces.Add(existingNamespace);

                var packageService           = new Mock <IPackageService>();
                var reservedNamespaceService = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService);
                await service.AddPackageOwnerAsync(package, pendingOwner);

                reservedNamespaceService.Verify(x => x.AddPackageRegistrationToNamespace(It.IsAny <string>(), It.IsAny <PackageRegistration>()), Times.Never);
                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.IsAny <IReadOnlyCollection <PackageRegistration> >(), It.IsAny <bool>(), It.IsAny <bool>()), Times.Never);
            }
            public async Task AdminCanRemoveAnyOwner()
            {
                var existingOwner1 = new User {
                    Key = 100, Username = "******"
                };
                var package = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = true, Owners = new List <User> {
                        existingOwner1
                    }
                };
                var adminOwner = new User
                {
                    Key      = 101,
                    Username = "******",
                    Roles    = new List <Role>
                    {
                        new Role {
                            Name = CoreConstants.AdminRoleName
                        }
                    }
                };
                var existingNamespace1 = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);

                existingOwner1.ReservedNamespaces.Add(existingNamespace1);
                package.ReservedNamespaces.Add(existingNamespace1);
                package.Owners.Add(adminOwner);
                existingNamespace1.Owners.Add(existingOwner1);
                existingNamespace1.PackageRegistrations.Add(package);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await service.RemovePackageOwnerAsync(package, adminOwner, existingOwner1);

                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.Is <IReadOnlyCollection <PackageRegistration> >(pr => pr.First() == package), false, false), Times.Once);
                reservedNamespaceService.Verify(x => x.RemovePackageRegistrationFromNamespace(existingNamespace1, package), Times.Once);
                Assert.False(package.IsVerified);
            }
            public async Task MultipleNamespaceOwnersRemovalWorksCorrectly()
            {
                var package = new PackageRegistration {
                    Key = 2, Id = "Microsoft.Aspnet.Package1", IsVerified = true
                };
                var existingOwner1 = new User {
                    Key = 100, Username = "******"
                };
                var existingOwner2 = new User {
                    Key = 101, Username = "******"
                };
                var existingNamespace1 = new ReservedNamespace("microsoft.aspnet.", isSharedNamespace: false, isPrefix: true);
                var existingNamespace2 = new ReservedNamespace("microsoft.", isSharedNamespace: false, isPrefix: true);

                existingOwner1.ReservedNamespaces.Add(existingNamespace1);
                existingOwner2.ReservedNamespaces.Add(existingNamespace2);
                package.ReservedNamespaces.Add(existingNamespace1);
                package.ReservedNamespaces.Add(existingNamespace2);
                package.Owners.Add(existingOwner1);
                package.Owners.Add(existingOwner2);
                existingNamespace1.Owners.Add(existingOwner1);
                existingNamespace2.Owners.Add(existingOwner2);
                existingNamespace1.PackageRegistrations.Add(package);
                existingNamespace2.PackageRegistrations.Add(package);

                var packageService             = new Mock <IPackageService>();
                var packageOwnerRequestService = new Mock <IPackageOwnerRequestService>();
                var reservedNamespaceService   = new Mock <IReservedNamespaceService>();

                var service = CreateService(packageService: packageService, reservedNamespaceService: reservedNamespaceService, packageOwnerRequestService: packageOwnerRequestService);
                await service.RemovePackageOwnerAsync(package, existingOwner1, existingOwner2);

                packageService.Verify(x => x.UpdatePackageVerifiedStatusAsync(It.Is <IReadOnlyCollection <PackageRegistration> >(pr => pr.First() == package), false, false), Times.Never);
                reservedNamespaceService.Verify(x => x.RemovePackageRegistrationFromNamespace(existingNamespace2, package), Times.Once);
                Assert.True(package.IsVerified);
            }
Exemple #15
0
        private async Task <List <PackageRegistration> > DeleteOwnerFromReservedNamespaceImplAsync(string prefix, string username, ReservedNamespace namespaceToModify, bool commitChanges = true)
        {
            var userToRemove = UserService.FindByUsername(username)
                               ?? throw new InvalidOperationException(string.Format(
                                                                          CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_UserNotFound, username));

            if (!namespaceToModify.Owners.Contains(userToRemove))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, ServicesStrings.ReservedNamespace_UserNotAnOwner, username));
            }

            var packagesOwnedByUserMatchingPrefix = namespaceToModify
                                                    .PackageRegistrations
                                                    .Where(pr => pr
                                                           .Owners
                                                           .Any(pro => pro.Username == userToRemove.Username))
                                                    .ToList();

            namespaceToModify.Owners.Remove(userToRemove);

            // Remove verified mark for package registrations if the user to be removed is the only prefix owner
            // for the given package registration.
            var packageRegistrationsToMarkUnverified = packagesOwnedByUserMatchingPrefix
                                                       .Where(pr => !pr.Owners.Any(o =>
                                                                                   ActionsRequiringPermissions.AddPackageToReservedNamespace.CheckPermissionsOnBehalfOfAnyAccount(
                                                                                       o, new[] { namespaceToModify }) == PermissionsCheckResult.Allowed))
                                                       .ToList();

            if (packageRegistrationsToMarkUnverified.Any())
            {
                packageRegistrationsToMarkUnverified
                .ForEach(pr => namespaceToModify.PackageRegistrations.Remove(pr));

                await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsToMarkUnverified, isVerified : false, commitChanges : false);
            }

            if (commitChanges)
            {
                await ReservedNamespaceRepository.CommitChangesAsync();
            }

            return(packageRegistrationsToMarkUnverified);
        }
 /// <summary>
 /// Is <paramref name="currentUser"/> allowed to perform an action with a requirement of <paramref name="permissionsRequirement"/> on <paramref name="reservedNamespace"/>?
 /// </summary>
 public static bool IsRequirementSatisfied(PermissionsRequirement permissionsRequirement, User currentUser, ReservedNamespace reservedNamespace)
 {
     return(reservedNamespace.IsSharedNamespace || IsRequirementSatisfied(permissionsRequirement, currentUser, reservedNamespace.Owners));
 }
        private async Task <List <PackageRegistration> > DeleteOwnerFromReservedNamespaceImplAsync(string prefix, string username, ReservedNamespace namespaceToModify)
        {
            var userToRemove = UserService.FindByUsername(username)
                               ?? throw new InvalidOperationException(string.Format(
                                                                          CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserNotFound, username));

            if (!namespaceToModify.Owners.Contains(userToRemove))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings.ReservedNamespace_UserNotAnOwner, username));
            }

            var packagesOwnedByUserMatchingPrefix = namespaceToModify
                                                    .PackageRegistrations
                                                    .Where(pr => pr
                                                           .Owners
                                                           .Any(pro => pro.Username == userToRemove.Username))
                                                    .ToList();

            // Remove verified mark for package registrations if the user to be removed is the only prefix owner
            // for the given package registration.
            var packageRegistrationsToMarkUnverified = packagesOwnedByUserMatchingPrefix
                                                       .Where(pr => pr.Owners.Intersect(namespaceToModify.Owners).Count() == 1)
                                                       .ToList();

            if (packageRegistrationsToMarkUnverified.Any())
            {
                packageRegistrationsToMarkUnverified
                .ForEach(pr => namespaceToModify.PackageRegistrations.Remove(pr));

                await PackageService.UpdatePackageVerifiedStatusAsync(packageRegistrationsToMarkUnverified, isVerified : false);
            }

            namespaceToModify.Owners.Remove(userToRemove);
            await ReservedNamespaceRepository.CommitChangesAsync();

            return(packageRegistrationsToMarkUnverified);
        }