|
| 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 NuGet.Services.Entities; |
| 6 | +using NuGetGallery.Authentication; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace NuGetGallery.Infrastructure.Authentication |
| 10 | +{ |
| 11 | + public class CredentialBuilderFacts |
| 12 | + { |
| 13 | + public class TheCreateShortLivedApiKeyMethod : CredentialBuilderFacts |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void CreatesShortLivedApiKey() |
| 17 | + { |
| 18 | + // Act |
| 19 | + var credential = Target.CreateShortLivedApiKey(Expiration, Policy, out var plaintextApiKey); |
| 20 | + |
| 21 | + // Assert |
| 22 | + Assert.Null(credential.User); |
| 23 | + Assert.Equal(default, credential.UserKey); |
| 24 | + Assert.StartsWith("oy2", plaintextApiKey, StringComparison.Ordinal); |
| 25 | + Assert.Equal(CredentialTypes.ApiKey.V4, credential.Type); |
| 26 | + Assert.Equal("Short-lived API key generated via a federated credential", credential.Description); |
| 27 | + Assert.Equal(Expiration.Ticks, credential.ExpirationTicks); |
| 28 | + Assert.Null(credential.User); |
| 29 | + |
| 30 | + var scope = Assert.Single(credential.Scopes); |
| 31 | + Assert.Equal(NuGetScopes.All, scope.AllowedAction); |
| 32 | + Assert.Equal(NuGetPackagePattern.AllInclusivePattern, scope.Subject); |
| 33 | + Assert.Same(Policy.PackageOwner, scope.Owner); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void RejectsMissingPackageOwner() |
| 38 | + { |
| 39 | + // Arrange |
| 40 | + Policy.PackageOwner = null; |
| 41 | + |
| 42 | + // Act |
| 43 | + Assert.Throws<ArgumentException>(() => Target.CreateShortLivedApiKey(Expiration, Policy, out var plaintextApiKey)); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory] |
| 47 | + [InlineData(-1)] |
| 48 | + [InlineData(0)] |
| 49 | + [InlineData(61)] |
| 50 | + public void RejectsOutOfRangeExpiration(int expirationMinutes) |
| 51 | + { |
| 52 | + // Arrange |
| 53 | + Expiration = TimeSpan.FromMinutes(expirationMinutes); |
| 54 | + |
| 55 | + // Act |
| 56 | + Assert.Throws<ArgumentOutOfRangeException>(() => Target.CreateShortLivedApiKey(Expiration, Policy, out var plaintextApiKey)); |
| 57 | + } |
| 58 | + |
| 59 | + public FederatedCredentialPolicy Policy { get; } |
| 60 | + |
| 61 | + public TheCreateShortLivedApiKeyMethod() |
| 62 | + { |
| 63 | + Policy = new FederatedCredentialPolicy |
| 64 | + { |
| 65 | + Key = 23, |
| 66 | + PackageOwner = new User { Key = 42 }, |
| 67 | + CreatedBy = new User { Key = 43 }, |
| 68 | + }; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public TimeSpan Expiration { get; set; } |
| 73 | + |
| 74 | + public CredentialBuilder Target { get; } |
| 75 | + |
| 76 | + public CredentialBuilderFacts() |
| 77 | + { |
| 78 | + Expiration = TimeSpan.FromMinutes(13); |
| 79 | + |
| 80 | + Target = new CredentialBuilder(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments