Skip to content

Commit 935803e

Browse files
author
Scott Bommarito
authored
Unlisting a package when deprecating it should update LastEdited (#6955)
1 parent f09c4a5 commit 935803e

2 files changed

Lines changed: 72 additions & 21 deletions

File tree

src/NuGetGallery/Services/PackageDeprecationService.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ public class PackageDeprecationService : IPackageDeprecationService
1515
private readonly IEntityRepository<PackageDeprecation> _deprecationRepository;
1616
private readonly IEntityRepository<Cve> _cveRepository;
1717
private readonly IEntityRepository<Cwe> _cweRepository;
18+
private readonly IPackageService _packageService;
19+
private readonly IIndexingService _indexingService;
1820

1921
public PackageDeprecationService(
2022
IEntityRepository<PackageDeprecation> deprecationRepository,
2123
IEntityRepository<Cve> cveRepository,
22-
IEntityRepository<Cwe> cweRepository)
24+
IEntityRepository<Cwe> cweRepository,
25+
IPackageService packageService,
26+
IIndexingService indexingService)
2327
{
2428
_deprecationRepository = deprecationRepository ?? throw new ArgumentNullException(nameof(deprecationRepository));
2529
_cveRepository = cveRepository ?? throw new ArgumentNullException(nameof(cveRepository));
2630
_cweRepository = cweRepository ?? throw new ArgumentNullException(nameof(cweRepository));
31+
_packageService = packageService ?? throw new ArgumentNullException(nameof(packageService));
32+
_indexingService = indexingService ?? throw new ArgumentNullException(nameof(indexingService));
2733
}
2834

2935
public async Task UpdateDeprecation(
@@ -101,7 +107,7 @@ public async Task UpdateDeprecation(
101107

102108
if (shouldUnlist)
103109
{
104-
package.Listed = false;
110+
await _packageService.MarkPackageUnlistedAsync(package, commitChanges: false);
105111
}
106112
}
107113
}
@@ -116,6 +122,12 @@ public async Task UpdateDeprecation(
116122
}
117123

118124
await _deprecationRepository.CommitChangesAsync();
125+
126+
// Update the indexing of the packages we updated the deprecation information of.
127+
foreach (var package in packages)
128+
{
129+
_indexingService.UpdatePackage(package);
130+
}
119131
}
120132

121133
public async Task<IReadOnlyCollection<Cve>> GetOrCreateCvesByIdAsync(IEnumerable<string> ids, bool commitChanges)

tests/NuGetGallery.Facts/Services/PackageDeprecationServiceFacts.cs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ await Assert.ThrowsAsync<ArgumentNullException>(() =>
7777
false));
7878
}
7979

80-
[Fact]
81-
public async Task DeletesExistingDeprecationsIfStatusNotDeprecated()
80+
[Theory]
81+
[InlineData(false)]
82+
[InlineData(true)]
83+
public async Task DeletesExistingDeprecationsIfStatusNotDeprecated(bool shouldUnlist)
8284
{
8385
// Arrange
8486
var packageWithDeprecation1 = new Package
@@ -114,6 +116,20 @@ public async Task DeletesExistingDeprecationsIfStatusNotDeprecated()
114116
.Completes()
115117
.Verifiable();
116118

119+
var packageService = GetMock<IPackageService>();
120+
var indexingService = GetMock<IIndexingService>();
121+
foreach (var package in packages)
122+
{
123+
// When deleting deprecations, packages should not be unlisted because the option is hidden in the UI.
124+
packageService
125+
.Setup(x => x.MarkPackageUnlistedAsync(package, false))
126+
.Throws<InvalidOperationException>();
127+
128+
indexingService
129+
.Setup(x => x.UpdatePackage(package))
130+
.Verifiable();
131+
}
132+
117133
var service = Get<PackageDeprecationService>();
118134

119135
// Act
@@ -126,10 +142,11 @@ await service.UpdateDeprecation(
126142
null,
127143
null,
128144
null,
129-
false);
145+
shouldUnlist);
130146

131147
// Assert
132148
deprecationRepository.Verify();
149+
indexingService.Verify();
133150

134151
foreach (var package in packages)
135152
{
@@ -143,22 +160,27 @@ await service.UpdateDeprecation(
143160
public async Task ReplacesExistingDeprecations(bool shouldUnlist)
144161
{
145162
// Arrange
146-
var unlistedPackageWithoutDeprecation = new Package();
163+
var lastEdited = new DateTime(2019, 3, 4);
164+
165+
var unlistedPackageWithoutDeprecation = new Package
166+
{
167+
LastEdited = lastEdited
168+
};
147169

148170
var packageWithDeprecation1 = new Package
149171
{
150-
Listed = true,
151-
Deprecations = new List<PackageDeprecation> { new PackageDeprecation() }
172+
Deprecations = new List<PackageDeprecation> { new PackageDeprecation() },
173+
LastEdited = lastEdited
152174
};
153175

154176
var packageWithoutDeprecation1 = new Package
155177
{
156-
Listed = true,
178+
LastEdited = lastEdited
157179
};
158180

159181
var packageWithDeprecation2 = new Package
160182
{
161-
Listed = true,
183+
LastEdited = lastEdited,
162184
Deprecations = new List<PackageDeprecation>
163185
{
164186
new PackageDeprecation
@@ -176,12 +198,12 @@ public async Task ReplacesExistingDeprecations(bool shouldUnlist)
176198

177199
var packageWithoutDeprecation2 = new Package
178200
{
179-
Listed = true,
201+
LastEdited = lastEdited
180202
};
181203

182204
var packageWithDeprecation3 = new Package
183205
{
184-
Listed = true,
206+
LastEdited = lastEdited,
185207
Deprecations = new List<PackageDeprecation>
186208
{
187209
new PackageDeprecation
@@ -221,6 +243,30 @@ public async Task ReplacesExistingDeprecations(bool shouldUnlist)
221243
.Completes()
222244
.Verifiable();
223245

246+
var packageService = GetMock<IPackageService>();
247+
var indexingService = GetMock<IIndexingService>();
248+
foreach (var package in packages)
249+
{
250+
var unlistPackageSetup = packageService
251+
.Setup(x => x.MarkPackageUnlistedAsync(package, false));
252+
253+
if (shouldUnlist)
254+
{
255+
unlistPackageSetup
256+
.Completes()
257+
.Verifiable();
258+
}
259+
else
260+
{
261+
unlistPackageSetup
262+
.Throws<InvalidOperationException>();
263+
}
264+
265+
indexingService
266+
.Setup(x => x.UpdatePackage(package))
267+
.Verifiable();
268+
}
269+
224270
var service = Get<PackageDeprecationService>();
225271

226272
var status = (PackageDeprecationStatus)99;
@@ -272,6 +318,8 @@ await service.UpdateDeprecation(
272318

273319
// Assert
274320
deprecationRepository.Verify();
321+
packageService.Verify();
322+
indexingService.Verify();
275323

276324
foreach (var package in packages)
277325
{
@@ -283,15 +331,6 @@ await service.UpdateDeprecation(
283331
Assert.Equal(alternatePackageRegistration, deprecation.AlternatePackageRegistration);
284332
Assert.Equal(alternatePackage, deprecation.AlternatePackage);
285333
Assert.Equal(customMessage, deprecation.CustomMessage);
286-
287-
if (shouldUnlist)
288-
{
289-
Assert.False(package.Listed);
290-
}
291-
else if (package != unlistedPackageWithoutDeprecation)
292-
{
293-
Assert.True(package.Listed);
294-
}
295334
}
296335
}
297336
}

0 commit comments

Comments
 (0)