|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Data.Entity; |
6 | 7 | using System.Globalization; |
7 | 8 | using System.Linq; |
8 | 9 | using System.Security.Claims; |
@@ -2382,6 +2383,44 @@ public async Task SavesChangesInTheDataStore() |
2382 | 2383 | authService.Entities.VerifyCommitChanges(); |
2383 | 2384 | } |
2384 | 2385 |
|
| 2386 | + /// <summary> |
| 2387 | + /// Needed to avoid collection modified exception caused by the entity context. |
| 2388 | + /// </summary> |
| 2389 | + [Fact] |
| 2390 | + public async Task CopiesScopeCollectionForDeletion() |
| 2391 | + { |
| 2392 | + // Arrange |
| 2393 | + var credentialBuilder = new CredentialBuilder(); |
| 2394 | + |
| 2395 | + var credScopes = |
| 2396 | + Enumerable.Range(0, 5) |
| 2397 | + .Select( |
| 2398 | + i => new Scope { AllowedAction = NuGetScopes.PackagePush, Key = i, Subject = "package" + i }).ToList(); |
| 2399 | + |
| 2400 | + var mockScopes = new Mock<DbSet<Scope>>(); |
| 2401 | + var dbContext = GetMock<IEntitiesContext>(); |
| 2402 | + dbContext.Setup(x => x.Scopes).Returns(mockScopes.Object); |
| 2403 | + mockScopes.Setup(x => x.Remove(It.IsAny<Scope>())).Callback<Scope>(x => credScopes.Remove(x)); |
| 2404 | + |
| 2405 | + var fakes = Get<Fakes>(); |
| 2406 | + var cred = credentialBuilder.CreateApiKey(null, out string plaintextApiKey); |
| 2407 | + var user = fakes.CreateUser("test", credentialBuilder.CreatePasswordCredential(Fakes.Password), cred); |
| 2408 | + var authService = Get<AuthenticationService>(); |
| 2409 | + |
| 2410 | + var newScopes = |
| 2411 | + Enumerable.Range(1, 2) |
| 2412 | + .Select( |
| 2413 | + i => new Scope { AllowedAction = NuGetScopes.PackageUnlist, Key = i * 10, Subject = "otherpackage" + i }).ToList(); |
| 2414 | + |
| 2415 | + cred.Scopes = credScopes; |
| 2416 | + |
| 2417 | + // Act |
| 2418 | + await authService.EditCredentialScopes(user, cred, newScopes); |
| 2419 | + |
| 2420 | + // Act |
| 2421 | + Assert.Empty(credScopes); |
| 2422 | + } |
| 2423 | + |
2385 | 2424 | [Fact] |
2386 | 2425 | public async Task WritesAuditRecordForTheEditedCredential() |
2387 | 2426 | { |
|
0 commit comments