Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the vulnerability order inside the dictionary cache #8780

Merged
merged 4 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private DisplayPackageViewModel SetupCommon(
&& packageKeyToVulnerabilities.TryGetValue(package.Key, out var vulnerabilities)
&& vulnerabilities != null && vulnerabilities.Any())
{
viewModel.Vulnerabilities = vulnerabilities;
viewModel.Vulnerabilities = vulnerabilities.OrderByDescending(vul => vul.Severity).ToList().AsReadOnly();
maxVulnerabilitySeverity = viewModel.Vulnerabilities.Max(v => v.Severity); // cache for messaging
viewModel.MaxVulnerabilitySeverity = maxVulnerabilitySeverity.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,36 @@ public void DeprecationFieldsAreSetAsExpected(
Assert.Null(versionModel.CustomMessage);
}

[Fact]
public void VulnerabilitiesDisplayedInOrder()
{
var package = CreateTestPackage("1.0.0");

var packageKeyToVulnerabilities = new Dictionary<int, IReadOnlyList<PackageVulnerability>>
{
{ package.Key, new List<PackageVulnerability>
{
new PackageVulnerability { Key = 1, Severity = PackageVulnerabilitySeverity.High },
new PackageVulnerability { Key = 2, Severity = PackageVulnerabilitySeverity.Low },
new PackageVulnerability { Key = 3, Severity = PackageVulnerabilitySeverity.Critical },
}
}
};

// Act
var model = CreateDisplayPackageViewModel(
package,
currentUser: null,
packageKeyToVulnerabilities: packageKeyToVulnerabilities,
readmeHtml: null);

// Assert
var versionModel = model.PackageVersions.Single();
Assert.Null(versionModel.CustomMessage);
Assert.NotNull(model.Vulnerabilities);
Assert.Equal(model.Vulnerabilities, packageKeyToVulnerabilities[package.Key].OrderByDescending(p => p.Severity).ToList().AsReadOnly());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried someone could break this logic if they re-order the PackageVulnerabilitySeverity enum. To prevent this mistake, we could manually verify the order here:

Suggested change
Assert.Equal(model.Vulnerabilities, packageKeyToVulnerabilities[package.Key].OrderByDescending(p => p.Severity).ToList().AsReadOnly());
Assert.Equal(PackageVulnerabilitySeverity.Critical, model.Vulnerabilities[0].Severity);
Assert.Equal(PackageVulnerabilitySeverity.High, model.Vulnerabilities[1].Severity);
Assert.Equal(PackageVulnerabilitySeverity.Low, model.Vulnerabilities[2].Severity);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure :)

}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down