diff --git a/src/NuGetGallery/Controllers/UsersController.cs b/src/NuGetGallery/Controllers/UsersController.cs index e83a897a13..9b72d0c31f 100644 --- a/src/NuGetGallery/Controllers/UsersController.cs +++ b/src/NuGetGallery/Controllers/UsersController.cs @@ -123,17 +123,17 @@ public virtual ActionResult Thanks() public virtual ActionResult Packages() { var user = UserService.FindByUsername(Identity.Name); - var packages = PackageService.FindPackagesByOwner(user); + var packages = PackageService.FindPackagesByOwner(user, includeUnlisted: true) + .Select(p => new PackageViewModel(p) + { + DownloadCount = p.PackageRegistration.DownloadCount, + Version = null + }).ToList(); var model = new ManagePackagesViewModel - { - Packages = from p in packages - select new PackageViewModel(p) - { - DownloadCount = p.PackageRegistration.DownloadCount, - Version = null - }, - }; + { + Packages = packages + }; return View(model); } @@ -276,12 +276,12 @@ public virtual ActionResult Profiles(string username) return HttpNotFound(); } - var packages = (from p in PackageService.FindPackagesByOwner(user) - where p.Listed - orderby p.Version descending - group p by p.PackageRegistration.Id) - .Select(c => new PackageViewModel(c.First())) - .ToList(); + var packages = PackageService.FindPackagesByOwner(user, includeUnlisted: false) + .Select(p => new PackageViewModel(p) + { + DownloadCount = p.PackageRegistration.DownloadCount, + Version = null + }).ToList(); var model = new UserProfileModel(user) { diff --git a/src/NuGetGallery/Services/IPackageService.cs b/src/NuGetGallery/Services/IPackageService.cs index 1535fd4fcd..f77e4c299c 100644 --- a/src/NuGetGallery/Services/IPackageService.cs +++ b/src/NuGetGallery/Services/IPackageService.cs @@ -12,7 +12,7 @@ public interface IPackageService PackageRegistration FindPackageRegistrationById(string id); Package FindPackageByIdAndVersion(string id, string version, bool allowPrerelease = true); IQueryable GetPackagesForListing(bool includePrerelease); - IEnumerable FindPackagesByOwner(User user); + IEnumerable FindPackagesByOwner(User user, bool includeUnlisted); IEnumerable FindDependentPackages(Package package); /// diff --git a/src/NuGetGallery/Services/PackageService.cs b/src/NuGetGallery/Services/PackageService.cs index 00155b2572..32d3faaa75 100644 --- a/src/NuGetGallery/Services/PackageService.cs +++ b/src/NuGetGallery/Services/PackageService.cs @@ -155,20 +155,22 @@ public IQueryable GetPackagesForListing(bool includePrerelease) : packages.Where(p => p.IsLatestStable); } - public IEnumerable FindPackagesByOwner(User user) + public IEnumerable FindPackagesByOwner(User user, bool includeUnlisted) { // Like DisplayPackage we should prefer to show you information from the latest stable version, // but show you the latest version otherwise. - var latestStablePackageVersions = _packageRegistrationRepository.GetAll() - .Where(pr => pr.Owners.Where(owner => owner.Username == user.Username).Any()) - .Select(pr => pr.Packages.Where(p => p.IsLatestStable).FirstOrDefault()) + var latestStablePackageVersions = _packageRepository.GetAll() + .Where(p => + p.PackageRegistration.Owners.Any(owner => owner.Key == user.Key) + && p.IsLatestStable) .Include(p => p.PackageRegistration) .Include(p => p.PackageRegistration.Owners); - var latestPackageVersions = _packageRegistrationRepository.GetAll() - .Where(pr => pr.Owners.Where(owner => owner.Username == user.Username).Any()) - .Select(pr => pr.Packages.OrderByDescending(p => p.Version).FirstOrDefault()) + var latestPackageVersions = _packageRepository.GetAll() + .Where(p => + p.PackageRegistration.Owners.Any(owner => owner.Key == user.Key) + && p.IsLatest) .Include(p => p.PackageRegistration) .Include(p => p.PackageRegistration.Owners); @@ -179,10 +181,7 @@ public IEnumerable FindPackagesByOwner(User user) } foreach (var package in latestStablePackageVersions) { - if (package != null) - { - mergedResults[package.PackageRegistration.Id] = package; - } + mergedResults[package.PackageRegistration.Id] = package; } return mergedResults.Values; diff --git a/tests/NuGetGallery.Facts/Framework/TestContainer.cs b/tests/NuGetGallery.Facts/Framework/TestContainer.cs index d549e7b7cc..a5deaf3c60 100644 --- a/tests/NuGetGallery.Facts/Framework/TestContainer.cs +++ b/tests/NuGetGallery.Facts/Framework/TestContainer.cs @@ -35,6 +35,24 @@ protected TController GetController() where TController : Controlle return c; } + protected TService GetService() + { + var serviceInterfaces = typeof(TService).GetInterfaces(); + Kernel.Bind(serviceInterfaces).To(typeof(TService)); + return Get(); + } + + protected FakeEntitiesContext GetFakeContext() + { + var fakeContext = new FakeEntitiesContext(); + Kernel.Bind().ToConstant(fakeContext); + Kernel.Bind>().ToConstant(new EntityRepository(fakeContext)); + Kernel.Bind>().ToConstant(new EntityRepository(fakeContext)); + Kernel.Bind>().ToConstant(new EntityRepository(fakeContext)); + Kernel.Bind>().ToConstant(new EntityRepository(fakeContext)); + return fakeContext; + } + protected T Get() { if(typeof(Controller).IsAssignableFrom(typeof(T))) { diff --git a/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs b/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs index a69530cc58..495438e71d 100644 --- a/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs +++ b/tests/NuGetGallery.Facts/Services/PackageServiceFacts.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Moq; using NuGet; +using NuGetGallery.Framework; using NuGetGallery.Packaging; using Xunit; @@ -1151,6 +1152,110 @@ public void FindPackageReturnsTheLatestVersionIfNoLatestVersionIsAvailable() } } + public class TheFindPackagesByOwnerMethod : TestContainer + { + [Fact] + public void ReturnsAListedPackage() + { + var owner = new User { Username = "someone" }; + var packageRegistration = new PackageRegistration { Id = "theId", Owners = { owner }}; + var package = new Package { Version = "1.0", PackageRegistration = packageRegistration, Listed = true, IsLatest = true, IsLatestStable = true }; + packageRegistration.Packages.Add(package); + + var context = GetFakeContext(); + context.Users.Add(owner); + context.PackageRegistrations.Add(packageRegistration); + context.Packages.Add(package); + var service = Get(); + + var packages = service.FindPackagesByOwner(owner, includeUnlisted: false); + Assert.Equal(1, packages.Count()); + } + + [Fact] + public void ReturnsNoUnlistedPackagesWhenIncludeUnlistedIsFalse() + { + var owner = new User { Username = "someone" }; + var packageRegistration = new PackageRegistration { Id = "theId", Owners = { owner } }; + var package = new Package { Version = "1.0", PackageRegistration = packageRegistration, Listed = false, IsLatest = false, IsLatestStable = false }; + packageRegistration.Packages.Add(package); + + var context = GetFakeContext(); + context.Users.Add(owner); + context.PackageRegistrations.Add(packageRegistration); + context.Packages.Add(package); + var service = Get(); + + var packages = service.FindPackagesByOwner(owner, includeUnlisted: false); + Assert.Equal(0, packages.Count()); + } + + [Fact] + public void ReturnsAnUnlistedPackageWhenIncludeUnlistedIsTrue() + { + var owner = new User { Username = "someone" }; + var packageRegistration = new PackageRegistration { Id = "theId", Owners = { owner } }; + var package = new Package { Version = "1.0", PackageRegistration = packageRegistration, Listed = true, IsLatest = true, IsLatestStable = true }; + packageRegistration.Packages.Add(package); + + var context = GetFakeContext(); + context.Users.Add(owner); + context.PackageRegistrations.Add(packageRegistration); + context.Packages.Add(package); + var service = Get(); + + var packages = service.FindPackagesByOwner(owner, includeUnlisted: true); + Assert.Equal(1, packages.Count()); + } + + [Fact] + public void ReturnsAPackageForEachPackageRegistration() + { + var owner = new User { Username = "someone" }; + var packageRegistrationA = new PackageRegistration { Id = "idA", Owners = { owner } }; + var packageRegistrationB = new PackageRegistration { Id = "idB", Owners = { owner } }; + var packageA = new Package { Version = "1.0", PackageRegistration = packageRegistrationA, Listed = true, IsLatest = true, IsLatestStable = true }; + var packageB = new Package { Version = "1.0", PackageRegistration = packageRegistrationB, Listed = true, IsLatest = true, IsLatestStable = true }; + packageRegistrationA.Packages.Add(packageA); + packageRegistrationB.Packages.Add(packageB); + + var context = GetFakeContext(); + context.Users.Add(owner); + context.PackageRegistrations.Add(packageRegistrationA); + context.PackageRegistrations.Add(packageRegistrationB); + context.Packages.Add(packageA); + context.Packages.Add(packageB); + var service = Get(); + + var packages = service.FindPackagesByOwner(owner, includeUnlisted: false).ToList(); + Assert.Equal(2, packages.Count); + Assert.Contains(packageA, packages); + Assert.Contains(packageB, packages); + } + + [Fact] + public void ReturnsOnlyLatestStablePackageIfBothExist() + { + var owner = new User { Username = "someone" }; + var packageRegistration = new PackageRegistration { Id = "theId", Owners = { owner } }; + var latestPackage = new Package { Version = "2.0.0-alpha", PackageRegistration = packageRegistration, Listed = true, IsLatest = true }; + var latestStablePackage = new Package { Version = "1.0", PackageRegistration = packageRegistration, Listed = true, IsLatestStable = true }; + packageRegistration.Packages.Add(latestPackage); + packageRegistration.Packages.Add(latestStablePackage); + + var context = GetFakeContext(); + context.Users.Add(owner); + context.PackageRegistrations.Add(packageRegistration); + context.Packages.Add(latestPackage); + context.Packages.Add(latestStablePackage); + var service = Get(); + + var packages = service.FindPackagesByOwner(owner, includeUnlisted: false).ToList(); + Assert.Equal(1, packages.Count); + Assert.Contains(latestStablePackage, packages); + } + } + public class TheMarkPackageListedMethod { [Fact] diff --git a/tests/NuGetGallery.Facts/TestUtils/FakeEntitiesContext.cs b/tests/NuGetGallery.Facts/TestUtils/FakeEntitiesContext.cs index bf8af992a6..0fba1096ab 100644 --- a/tests/NuGetGallery.Facts/TestUtils/FakeEntitiesContext.cs +++ b/tests/NuGetGallery.Facts/TestUtils/FakeEntitiesContext.cs @@ -47,6 +47,18 @@ public IDbSet PackageRegistrations } } + public IDbSet Packages + { + get + { + return Set(); + } + set + { + throw new NotSupportedException(); + } + } + public IDbSet Users { get