Skip to content

Commit 94e4009

Browse files
authored
Add functional tests on Gallery for Advanced Search (#8115)
* Add some functional tests * Fix PR comments * Remove unnecessary pivot * Fix PR comments * Encoded colon
1 parent a83a8eb commit 94e4009

3 files changed

Lines changed: 144 additions & 0 deletions

File tree

tests/NuGetGallery.FunctionalTests.Core/Constants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public static class Constants
3232
public const string StatsPageDefaultText = "Statistics last updated";
3333
public const string ContactOwnersText = "Your message has been sent to the owners of";
3434
public const string UnListedPackageText = "This package is unlisted and hidden from package listings";
35+
public const string TestAccount = "NugetTestAccount";
3536
public const string TestPackageId = "BaseTestPackage";
37+
public const string TestPackageIdDotNetTool = "BaseTestPackage.DotnetTool";
38+
public const string TestPackageIdARandomType = "BaseTestPackage.ARandomType";
39+
public const string TestPackageIdTemplate = "BaseTestPackage.Template";
3640
public const string TestPackageIdWithPrereleases = "BaseTestPackage.SearchFilters";
3741
public const string TestPackageIdNoStable = "BaseTestPackage.NoStable";
3842
public const string TestOrganizationCollaboratorPackageId = "BaseTestOrganizationCollaboratorPackage";

tests/NuGetGallery.FunctionalTests/NuGetGallery.FunctionalTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="ErrorHandling\SimulatedErrorType.cs" />
5454
<Compile Include="ErrorHandling\TestResponse.cs" />
5555
<Compile Include="PackageRetrieval\LatestTests.cs" />
56+
<Compile Include="PackageRetrieval\PackageListTests.cs" />
5657
<Compile Include="ProfileTests.cs" />
5758
<Compile Include="ODataFeeds\CuratedFeedTest.cs" />
5859
<Compile Include="ODataFeeds\SearchTest.cs" />
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Text.RegularExpressions;
8+
using System.Threading.Tasks;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
using System.Linq;
12+
using System.Collections.Generic;
13+
14+
namespace NuGetGallery.FunctionalTests.PackageRetrieval
15+
{
16+
public class PackageListTests : GalleryTestBase
17+
{
18+
private static readonly TimeSpan RegexTimeout = TimeSpan.FromMinutes(1);
19+
private static readonly Regex TotalDownloadsExpr = new Regex(@"((\d+[,]?)+) total download[s]?", RegexOptions.None, RegexTimeout);
20+
private static readonly Regex LastUpdatedExpr = new Regex(@"<span data-datetime=""(.+)"">", RegexOptions.None, RegexTimeout);
21+
22+
public PackageListTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
23+
{
24+
}
25+
26+
[Theory]
27+
[Priority(2)]
28+
[Category("P2Tests")]
29+
[InlineData("totaldownloads-desc", true)]
30+
[InlineData("tOtAlDoWnLoadS-DEsc", true)]
31+
[InlineData("totaldownloads-asc", false)]
32+
[InlineData("totaldowNLOADS-ASC", false)]
33+
public async Task MakeSureSortedByDownloadsWork(
34+
string sortBy = "",
35+
bool expectDescending = true)
36+
{
37+
var sortByParam = string.IsNullOrEmpty(sortBy) ? string.Empty : $"&sortBy={sortBy}";
38+
// Arrange
39+
var feedUrl = new Uri(
40+
new Uri(UrlHelper.BaseUrl),
41+
$"/packages?q=owner%3A{Constants.TestAccount}{sortByParam}");
42+
43+
// Act
44+
using (var httpClient = new HttpClient())
45+
using (var response = await httpClient.GetAsync(feedUrl))
46+
{
47+
// Assert
48+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
49+
AssertIsHtml(response);
50+
51+
var content = await response.Content.ReadAsStringAsync();
52+
var downloads = GetPackagesDownloads(content);
53+
var expectedDownloads = expectDescending ? downloads.OrderByDescending(x => x) : downloads.Select(x => x);
54+
Assert.True(downloads.Count > 1);
55+
Assert.Equal(expectedDownloads, downloads); // The downloads are sorted as expected
56+
}
57+
}
58+
59+
[Theory]
60+
[InlineData("created-desc")]
61+
[InlineData("cReAtEd-DeSc")]
62+
public async Task MakeSureLastUpdatedSortingWorks(string sortBy)
63+
{
64+
var sortByParam = string.IsNullOrEmpty(sortBy) ? string.Empty : $"&sortBy={sortBy}";
65+
// Arrange
66+
var feedUrl = new Uri(
67+
new Uri(UrlHelper.BaseUrl),
68+
$"/packages?q=owner%3A{Constants.TestAccount}{sortByParam}");
69+
70+
// Act
71+
using (var httpClient = new HttpClient())
72+
using (var response = await httpClient.GetAsync(feedUrl))
73+
{
74+
// Assert
75+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
76+
AssertIsHtml(response);
77+
78+
var content = await response.Content.ReadAsStringAsync();
79+
var updateDates = GetPackagesUpdateDates(content);
80+
var expectedDates = updateDates.OrderByDescending(x => x);
81+
Assert.Equal(expectedDates, updateDates); // The "recently updated" dates are sorted as expected
82+
}
83+
}
84+
85+
[Theory]
86+
[Priority(2)]
87+
[Category("P2Tests")]
88+
[InlineData(Constants.TestPackageId, "dependency")]
89+
[InlineData(Constants.TestPackageIdDotNetTool, "dotnettool")]
90+
[InlineData(Constants.TestPackageIdARandomType, "ARandomType")]
91+
[InlineData(Constants.TestPackageIdTemplate, "template")]
92+
public async Task ExpectPackageTypeFilterToWork(
93+
string id,
94+
string packageType = "")
95+
{
96+
var packageTypeParam = string.IsNullOrEmpty(packageType) ? string.Empty : $"&packageType={packageType}";
97+
// Arrange
98+
var feedUrl = new Uri(
99+
new Uri(UrlHelper.BaseUrl),
100+
$"/packages?q=packageid%3A{Uri.EscapeUriString(id)}+owner%3A{Constants.TestAccount}{packageTypeParam}");
101+
102+
// Act
103+
using (var httpClient = new HttpClient())
104+
using (var response = await httpClient.GetAsync(feedUrl))
105+
{
106+
// Assert
107+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
108+
AssertIsHtml(response);
109+
110+
var content = await response.Content.ReadAsStringAsync();
111+
Assert.Contains($"/packages/{id}", content);
112+
}
113+
}
114+
115+
private static List<long> GetPackagesDownloads(string content)
116+
{
117+
return TotalDownloadsExpr
118+
.Matches(content)
119+
.Cast<Match>()
120+
.Select(x => long.Parse(x.Groups[1].Value.Replace(",", ""))).ToList();
121+
}
122+
123+
private static List<DateTime> GetPackagesUpdateDates(string content)
124+
{
125+
var dates = LastUpdatedExpr
126+
.Matches(content)
127+
.Cast<Match>()
128+
.ToList();
129+
130+
return dates.Select(x => DateTime.Parse(x.Groups[1].Value)).ToList();
131+
}
132+
133+
private static void AssertIsHtml(HttpResponseMessage response)
134+
{
135+
var contentType = Assert.Single(response.Content.Headers.GetValues("Content-Type"));
136+
Assert.Equal("text/html; charset=utf-8", contentType);
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)