Skip to content

Commit 2d6c527

Browse files
authored
Functional tests for license expression/file and reasoning phrase. (#6778)
* functional tests for license * Some small changes * Some updates * Refactor * nit change * nit changes * delete empty whitespace
1 parent 24fb2a3 commit 2d6c527

6 files changed

Lines changed: 182 additions & 2 deletions

File tree

tests/NuGetGallery.FunctionalTests.Core/Helpers/NuspecHelper.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task<string> CreateDefaultNuspecFile(string packageName, string ver
4444
}
4545

4646
Directory.CreateDirectory(packageDir);
47-
47+
4848
var commandlineHelper = new CommandlineHelper(TestOutputHelper);
4949
await commandlineHelper.SpecPackageAsync(packageName, packageDir);
5050

@@ -79,7 +79,27 @@ public async Task<string> CreateDefaultNuspecFile(string packageName, string ver
7979
}
8080
return filePath;
8181
}
82+
83+
/// <summary>
84+
/// Given a nupsec file path, add the license expression attribute.
85+
/// </summary>
86+
/// <param name="nuspecFilePath"></param>
87+
/// <param name="licenseExpression"></param>
88+
public static void AddLicenseExpression(string nuspecFilePath, string licenseExpression)
89+
{
90+
UpdateNuspecFile(nuspecFilePath, "</metadata>", $"<license type='expression'>{licenseExpression}</license></metadata>");
91+
}
8292

93+
/// <summary>
94+
/// Given a nupsec file path, add the license file attribute.
95+
/// </summary>
96+
/// <param name="nuspecFilePath"></param>
97+
/// <param name="licenseFile"></param>
98+
public static void AddLicenseFile(string nuspecFilePath, string licenseFile)
99+
{
100+
UpdateNuspecFile(nuspecFilePath, "</metadata>", $"<license type='file'>{licenseFile}</license></metadata>");
101+
}
102+
83103
/// <summary>
84104
/// Given a nupsec file path, add the windows 8 tag to it.
85105
/// </summary>

tests/NuGetGallery.FunctionalTests.Core/Helpers/PackageCreationHelper.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,47 @@ public async Task<string> CreatePackageWithTargetFramework(string packageName, s
6868
return await CreatePackageWithTargetFrameworkInternal(nuspecFileFullPath, frameworkVersion);
6969
}
7070

71+
/// <summary>
72+
/// Creates a package with the license expression.
73+
/// </summary>
74+
/// <returns></returns>
75+
public async Task<string> CreatePackageWithLicenseExpression(string packageName, string packageVersion, string licenseUrl, string licenseExpression)
76+
{
77+
var nuspecHelper = new NuspecHelper(TestOutputHelper);
78+
var nuspecFileFullPath = await nuspecHelper.CreateDefaultNuspecFile(packageName, packageVersion, licenseUrl: licenseUrl);
79+
var nuspecDir = Path.GetDirectoryName(nuspecFileFullPath);
80+
81+
var nupkgFileFullPath = await CreatePackageInternal(nuspecFileFullPath);
82+
var nuspecFileName = packageName + ".nuspec";
83+
84+
NuspecHelper.AddLicenseExpression(nuspecFileFullPath, licenseExpression);
85+
ReplaceNuspecFileInNupkg(nupkgFileFullPath, nuspecFileFullPath, nuspecFileName);
86+
87+
return nupkgFileFullPath;
88+
}
89+
90+
/// <summary>
91+
/// Creates a package with the license file.
92+
/// </summary>
93+
/// <returns></returns>
94+
public async Task<string> CreatePackageWithLicenseFile(string packageName, string packageVersion, string licenseUrl, string licenseFile, string licenseFileName, string licenseFileContents)
95+
{
96+
var nuspecHelper = new NuspecHelper(TestOutputHelper);
97+
var nuspecFileFullPath = await nuspecHelper.CreateDefaultNuspecFile(packageName, packageVersion, licenseUrl: licenseUrl);
98+
var nuspecDir = Path.GetDirectoryName(nuspecFileFullPath);
99+
100+
var licenseFilePath = GetOrCreateFilePath(nuspecDir, licenseFileName);
101+
AddFile(licenseFilePath, licenseFileContents);
102+
103+
var nupkgFileFullPath = await CreatePackageInternal(nuspecFileFullPath);
104+
var nuspecFileName = packageName + ".nuspec";
105+
106+
NuspecHelper.AddLicenseFile(nuspecFileFullPath, licenseFile);
107+
ReplaceNuspecFileInNupkg(nupkgFileFullPath, nuspecFileFullPath, nuspecFileName);
108+
109+
return nupkgFileFullPath;
110+
}
111+
71112
/// <summary>
72113
/// Creates a package which will grow up to a huge size when extracted.
73114
/// </summary>
@@ -140,6 +181,56 @@ internal static void AddLib(string nuspecFileDir, string frameworkVersion)
140181
string source = "using System; namespace CodeDom { public class B {public static int k=7;}}";
141182
provider.CompileAssemblyFromSource(parameters, source);
142183
}
184+
185+
/// <summary>
186+
/// Get the file path given base directory and filename.
187+
/// </summary>
188+
/// <param name="baseDir"></param>
189+
/// <param name="filename"></param>
190+
internal static string GetOrCreateFilePath(string baseDir, string fileName)
191+
{
192+
var filePath = Path.Combine(baseDir, @fileName);
193+
var fileDir = Path.GetDirectoryName(filePath);
194+
if (!Directory.Exists(fileDir))
195+
{
196+
Directory.CreateDirectory(fileDir);
197+
}
198+
199+
return filePath;
200+
}
201+
202+
/// <summary>
203+
/// Adds the file in the filePath location.
204+
/// </summary>
205+
/// <param name="filePath"></param>
206+
/// <param name="fileContents"></param>
207+
internal static void AddFile(string filePath, string fileContents)
208+
{
209+
using (var streamWriter = new StreamWriter(filePath))
210+
{
211+
streamWriter.Write(fileContents);
212+
streamWriter.Close();
213+
}
214+
}
215+
216+
/// <summary>
217+
/// Replace the nuspec file in nupkg.
218+
/// </summary>
219+
/// <param name="nupkgFileFullPath"></param>
220+
/// <param name="nuspecFileFullPath"></param>
221+
/// <param name="nuspecFileName"></param>
222+
internal static void ReplaceNuspecFileInNupkg(string nupkgFileFullPath, string nuspecFileFullPath, string nuspecFileName)
223+
{
224+
using (var packageArchive = ZipFile.Open(nupkgFileFullPath, ZipArchiveMode.Update))
225+
{
226+
var nuspecEntry = packageArchive.GetEntry(nuspecFileName);
227+
if (nuspecEntry != null)
228+
{
229+
nuspecEntry.Delete();
230+
}
231+
packageArchive.CreateEntryFromFile(nuspecFileFullPath, nuspecFileName);
232+
}
233+
}
143234

144235
private async Task<string> CreatePackageInternal(string nuspecFileFullPath)
145236
{

tests/NuGetGallery.FunctionalTests.Core/NuGetGallery.FunctionalTests.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Reference Include="System.Core" />
5555
<Reference Include="Microsoft.CSharp" />
5656
<Reference Include="System.IO.Compression" />
57+
<Reference Include="System.IO.Compression.FileSystem" />
5758
<Reference Include="System.Net.Http" />
5859
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
5960
<HintPath>..\..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.Threading.Tasks;
6+
using Xunit;
7+
using Xunit.Abstractions;
8+
using System.ComponentModel;
9+
10+
namespace NuGetGallery.FunctionalTests.License
11+
{
12+
public class LicenseTests : GalleryTestBase
13+
{
14+
private readonly CommandlineHelper _commandlineHelper;
15+
private readonly PackageCreationHelper _packageCreationHelper;
16+
public LicenseTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
17+
{
18+
_commandlineHelper = new CommandlineHelper(TestOutputHelper);
19+
_packageCreationHelper = new PackageCreationHelper(testOutputHelper);
20+
}
21+
22+
[Fact]
23+
[Description("Push an invalidp package with license expression and verify uploading is blocked")]
24+
[Priority(2)]
25+
[Category("P2Tests")]
26+
public async Task UploadInValidPackageWithLicenseExpression()
27+
{
28+
// Arrange
29+
var packageName = $"TestPackageWithLicense.{DateTime.UtcNow.Ticks}";
30+
var packageVersion = "1.0.0";
31+
32+
var licenseUrl = "https://testNugetLicenseUrl";
33+
var licenseExpression = "MIT";
34+
var expectedErrorMessage = "when a license expression is specified, <licenseUrl> must be set to";
35+
36+
// Act
37+
string packageFullPath = await _packageCreationHelper.CreatePackageWithLicenseExpression(packageName, packageVersion, licenseUrl, licenseExpression);
38+
39+
var processResult = await _commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl);
40+
41+
// Assert
42+
Assert.True(processResult.ExitCode == 1, Constants.UploadFailureMessage);
43+
Assert.Contains(expectedErrorMessage, processResult.StandardError);
44+
}
45+
46+
[Theory]
47+
[Description("Push an invalid package with license file and verify uploading is blocked")]
48+
[Priority(2)]
49+
[Category("P2Tests")]
50+
[InlineData("https://testNugetLicenseUrl", "license.txt", "license.txt", "It's a license", "when a license file is packaged, <licenseUrl> must be set to ")]
51+
[InlineData("https://aka.ms/deprecateLicenseUrl", "licensefolder\\license.txt", "license.txt", "It's a license", "does not exist in the package")]
52+
public async Task UploadInValidPackageWithLicenseFile(string licenseUrl, string licenseFile, string licenseFileName, string licenseFileContents, string expectedErrorMessage)
53+
{
54+
var packageName = $"TestPackageWithLicense.{DateTime.UtcNow.Ticks}";
55+
string packageVersion = "1.0.0";
56+
string packageFullPath = await _packageCreationHelper.CreatePackageWithLicenseFile(packageName, packageVersion, licenseUrl, licenseFile, licenseFileName, licenseFileContents);
57+
58+
var processResult = await _commandlineHelper.UploadPackageAsync(packageFullPath, UrlHelper.V2FeedPushSourceUrl);
59+
60+
Assert.True(processResult.ExitCode == 1, Constants.UploadFailureMessage);
61+
Assert.Contains(expectedErrorMessage, processResult.StandardError);
62+
}
63+
}
64+
}

tests/NuGetGallery.FunctionalTests/NuGetGallery.FunctionalTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="WebPages\LinksTests.cs" />
9898
<Compile Include="Properties\AssemblyInfo.cs" />
9999
<Compile Include="ApiManagement\CachingTests.cs" />
100+
<Compile Include="License\LicenseTests.cs" />
100101
</ItemGroup>
101102
<ItemGroup>
102103
<None Include="app.config">

tests/NuGetGallery.FunctionalTests/TyposquattingCheck/TyposquattingCheckForUploadPackageTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
4+
using System.ComponentModel;
55
using System.Threading.Tasks;
66
using Xunit;
77
using Xunit.Abstractions;
@@ -20,6 +20,9 @@ public TyposquattingCheckForUploadPackageTests(ITestOutputHelper testOutputHelpe
2020
}
2121

2222
[TyposquattingTestFact]
23+
[Description("Push a package with a typosquatting Id and verify uploading is blocked")]
24+
[Priority(2)]
25+
[Category("P2Tests")]
2326
public async Task UploadTyposquattingPackageAndBlockUser()
2427
{
2528
var packageId = "newtonsoft-json";

0 commit comments

Comments
 (0)