|
12 | 12 | using System.Threading.Tasks; |
13 | 13 | using System.Web; |
14 | 14 | using System.Web.Mvc; |
| 15 | + |
15 | 16 | using Moq; |
| 17 | + |
16 | 18 | using NuGet.Services.Entities; |
17 | 19 | using NuGet.Services.Messaging.Email; |
| 20 | + |
18 | 21 | using NuGetGallery.Areas.Admin; |
19 | 22 | using NuGetGallery.Areas.Admin.Models; |
20 | 23 | using NuGetGallery.Areas.Admin.ViewModels; |
|
24 | 27 | using NuGetGallery.Infrastructure.Authentication; |
25 | 28 | using NuGetGallery.Infrastructure.Mail.Messages; |
26 | 29 | using NuGetGallery.Security; |
| 30 | + |
27 | 31 | using Xunit; |
28 | 32 |
|
29 | 33 | namespace NuGetGallery |
@@ -3704,43 +3708,115 @@ public void DeleteHappyAccount(bool withPendingIssues) |
3704 | 3708 |
|
3705 | 3709 | public class ThePackagesAction : TestContainer |
3706 | 3710 | { |
3707 | | - [Fact] |
3708 | | - public void UsesProperIconUrl() |
| 3711 | + private UsersController _testController; |
| 3712 | + private User _testUser; |
| 3713 | + private string userName = "RegularUser"; |
| 3714 | + private Fakes _fakes; |
| 3715 | + |
| 3716 | + public ThePackagesAction() |
3709 | 3717 | { |
3710 | | - string userName = "RegularUser"; |
3711 | | - var controller = GetController<UsersController>(); |
3712 | | - var fakes = Get<Fakes>(); |
3713 | | - var testUser = fakes.CreateUser(userName); |
3714 | | - testUser.IsDeleted = false; |
3715 | | - testUser.Key = 1; |
3716 | | - controller.SetCurrentUser(testUser); |
| 3718 | + _testController = GetController<UsersController>(); |
| 3719 | + _fakes = Get<Fakes>(); |
| 3720 | + _testUser = _fakes.CreateUser(userName); |
| 3721 | + _testUser.IsDeleted = false; |
| 3722 | + _testUser.Key = 1; |
| 3723 | + _testController.SetCurrentUser(_testUser); |
| 3724 | + } |
3717 | 3725 |
|
| 3726 | + private PackageRegistration CreatePackageRegistration(string Id, int Key, string Version, string Description) |
| 3727 | + { |
3718 | 3728 | var packageRegistration = new PackageRegistration(); |
3719 | | - packageRegistration.Owners.Add(testUser); |
| 3729 | + packageRegistration.Id = Id; |
| 3730 | + packageRegistration.Owners.Add(_testUser); |
3720 | 3731 |
|
3721 | | - var userPackage = new Package() |
| 3732 | + var userPackage1 = new Package() |
3722 | 3733 | { |
3723 | | - Description = "TestPackage", |
3724 | | - Key = 1, |
3725 | | - Version = "1.0.0", |
| 3734 | + Key = Key, |
| 3735 | + Version = Version, |
3726 | 3736 | PackageRegistration = packageRegistration, |
| 3737 | + Description = Description |
3727 | 3738 | }; |
3728 | | - packageRegistration.Packages.Add(userPackage); |
| 3739 | + packageRegistration.Packages.Add(userPackage1); |
| 3740 | + return packageRegistration; |
| 3741 | + } |
| 3742 | + |
| 3743 | + [Fact] |
| 3744 | + public void PackagesAreSortedById() |
| 3745 | + { |
| 3746 | + PackageRegistration packageRegistration1 = CreatePackageRegistration("Company.ZebraPackage", 1, "1.0.0", "last"); |
| 3747 | + PackageRegistration packageRegistration2 = CreatePackageRegistration("Company.AlphaPackage", 1, "1.0.0", "first"); |
| 3748 | + PackageRegistration packageRegistration3 = CreatePackageRegistration("Company.NormalPackage", 1, "1.0.0", "middle"); |
3729 | 3749 |
|
| 3750 | + var userPackages = new List<Package>() { |
| 3751 | + packageRegistration1.Packages.First() , |
| 3752 | + packageRegistration2.Packages.First(), |
| 3753 | + packageRegistration3.Packages.First() |
| 3754 | + }; |
| 3755 | + |
| 3756 | + GetMock<IUserService>() |
| 3757 | + .Setup(stub => stub.FindByUsername(userName, false)) |
| 3758 | + .Returns(_testUser); |
| 3759 | + |
| 3760 | + GetMock<IPackageService>() |
| 3761 | + .Setup(stub => stub.FindPackagesByAnyMatchingOwner(_testUser, It.IsAny<bool>(), false)) |
| 3762 | + .Returns(userPackages); |
| 3763 | + |
| 3764 | + var model = ResultAssert.IsView<ManagePackagesViewModel>(_testController.Packages()); |
| 3765 | + |
| 3766 | + Assert.Equal("Company.AlphaPackage", model.ListedPackages.ToArray()[0].Id); |
| 3767 | + Assert.Equal("Company.NormalPackage", model.ListedPackages.ToArray()[1].Id); |
| 3768 | + Assert.Equal("Company.ZebraPackage", model.ListedPackages.ToArray()[2].Id); |
| 3769 | + } |
| 3770 | + |
| 3771 | + [Fact] |
| 3772 | + public void PackagesVersionSortOrderIsSetBySemVer() |
| 3773 | + { |
| 3774 | + PackageRegistration packageRegistration1 = CreatePackageRegistration("Company.ZebraPackage", 1, "1.0.0", "middle"); |
| 3775 | + PackageRegistration packageRegistration2 = CreatePackageRegistration("Company.NormalPackage", 1, "0.0.1", "first"); |
| 3776 | + PackageRegistration packageRegistration3 = CreatePackageRegistration("Company.AlphaPackage", 1, "1.1.0", "last"); |
| 3777 | + |
| 3778 | + var userPackages = new List<Package>() { |
| 3779 | + packageRegistration1.Packages.First() , |
| 3780 | + packageRegistration2.Packages.First(), |
| 3781 | + packageRegistration3.Packages.First() |
| 3782 | + }; |
| 3783 | + |
| 3784 | + GetMock<IUserService>() |
| 3785 | + .Setup(stub => stub.FindByUsername(userName, false)) |
| 3786 | + .Returns(_testUser); |
| 3787 | + |
| 3788 | + GetMock<IPackageService>() |
| 3789 | + .Setup(stub => stub.FindPackagesByAnyMatchingOwner(_testUser, It.IsAny<bool>(), false)) |
| 3790 | + .Returns(userPackages); |
| 3791 | + |
| 3792 | + var model = ResultAssert.IsView<ManagePackagesViewModel>(_testController.Packages()); |
| 3793 | + |
| 3794 | + // The "VersionSortOrder" should be set according to Semantic Version sort order, not package Id |
| 3795 | + Assert.Equal(0, model.ListedPackages.First(x => x.Version == "0.0.1").VersionSortOrder); |
| 3796 | + Assert.Equal(1, model.ListedPackages.First(x => x.Version == "1.0.0").VersionSortOrder); |
| 3797 | + Assert.Equal(2, model.ListedPackages.First(x => x.Version == "1.1.0").VersionSortOrder); |
| 3798 | + } |
| 3799 | + |
| 3800 | + [Fact] |
| 3801 | + public void UsesProperIconUrl() |
| 3802 | + { |
| 3803 | + PackageRegistration packageRegistration1 = CreatePackageRegistration("TestPackage", 1, "1.0.0", "TestPackage"); |
| 3804 | + Package userPackage = packageRegistration1.Packages.First(); |
3730 | 3805 | var userPackages = new List<Package>() { userPackage }; |
3731 | 3806 |
|
3732 | 3807 | const string iconUrl = "https://some.test/icon"; |
| 3808 | + |
3733 | 3809 | GetMock<IIconUrlProvider>() |
3734 | 3810 | .Setup(iup => iup.GetIconUrlString(It.IsAny<Package>())) |
3735 | 3811 | .Returns(iconUrl); |
3736 | 3812 | GetMock<IUserService>() |
3737 | 3813 | .Setup(stub => stub.FindByUsername(userName, false)) |
3738 | | - .Returns(testUser); |
| 3814 | + .Returns(_testUser); |
3739 | 3815 | GetMock<IPackageService>() |
3740 | | - .Setup(stub => stub.FindPackagesByAnyMatchingOwner(testUser, It.IsAny<bool>(), false)) |
| 3816 | + .Setup(stub => stub.FindPackagesByAnyMatchingOwner(_testUser, It.IsAny<bool>(), false)) |
3741 | 3817 | .Returns(userPackages); |
3742 | 3818 |
|
3743 | | - var model = ResultAssert.IsView<ManagePackagesViewModel>(controller.Packages()); |
| 3819 | + var model = ResultAssert.IsView<ManagePackagesViewModel>(_testController.Packages()); |
3744 | 3820 |
|
3745 | 3821 | GetMock<IIconUrlProvider>() |
3746 | 3822 | .Verify(iup => iup.GetIconUrlString(userPackage), Times.AtLeastOnce); |
|
0 commit comments