|
3 | 3 |
|
4 | 4 | namespace AzureAuth.Test.Commands.Ado |
5 | 5 | { |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + |
6 | 9 | using FluentAssertions; |
7 | | - using Microsoft.Authentication.AzureAuth.Ado; |
| 10 | + using AzureAuth.Test; |
| 11 | + using Microsoft.Authentication.AzureAuth; |
8 | 12 | using Microsoft.Authentication.AzureAuth.Commands.Ado; |
| 13 | + using Microsoft.Authentication.MSALWrapper; |
| 14 | + using Microsoft.Extensions.DependencyInjection; |
| 15 | + using Microsoft.Extensions.Logging; |
| 16 | + using Microsoft.IdentityModel.JsonWebTokens; |
| 17 | + using Microsoft.Office.Lasso.Interfaces; |
| 18 | + using Microsoft.Office.Lasso.Telemetry; |
| 19 | + using Moq; |
| 20 | + using NLog.Extensions.Logging; |
| 21 | + using NLog.Targets; |
9 | 22 | using NUnit.Framework; |
10 | 23 |
|
11 | 24 | internal class CommandTokenTest |
12 | 25 | { |
| 26 | + private Mock<IEnv> mockEnv; |
| 27 | + private Mock<ITelemetryService> mockTelemetry; |
| 28 | + private Mock<IPublicClientAuth> mockPublicClientAuth; |
| 29 | + private IServiceProvider serviceProvider; |
| 30 | + private MemoryTarget logTarget; |
| 31 | + private CommandExecuteEventData eventData; |
| 32 | + |
| 33 | + [SetUp] |
| 34 | + public void SetUp() |
| 35 | + { |
| 36 | + this.mockEnv = new Mock<IEnv>(); |
| 37 | + this.mockEnv.Setup(e => e.Get(It.IsAny<string>())).Returns((string)null); |
| 38 | + this.mockTelemetry = new Mock<ITelemetryService>(); |
| 39 | + this.mockPublicClientAuth = new Mock<IPublicClientAuth>(); |
| 40 | + this.eventData = new CommandExecuteEventData(); |
| 41 | + |
| 42 | + // Setup in memory logging target with NLog - allows making assertions against what has been logged. |
| 43 | + var loggingConfig = new NLog.Config.LoggingConfiguration(); |
| 44 | + this.logTarget = new MemoryTarget("memory_target") |
| 45 | + { |
| 46 | + Layout = "${message}" // Define a simple layout so we don't get timestamps in messages. |
| 47 | + }; |
| 48 | + loggingConfig.AddTarget(this.logTarget); |
| 49 | + loggingConfig.AddRuleForAllLevels(this.logTarget); |
| 50 | + |
| 51 | + // Setup Dependency Injection container to provide logger. |
| 52 | + this.serviceProvider = new ServiceCollection() |
| 53 | + .AddLogging(loggingBuilder => |
| 54 | + { |
| 55 | + loggingBuilder.ClearProviders(); |
| 56 | + loggingBuilder.SetMinimumLevel(LogLevel.Trace); |
| 57 | + loggingBuilder.AddNLog(loggingConfig); |
| 58 | + }) |
| 59 | + .BuildServiceProvider(); |
| 60 | + } |
| 61 | + |
13 | 62 | [TestCase("foobar", CommandToken.OutputMode.Token, "foobar")] |
14 | 63 | [TestCase("foobar", CommandToken.OutputMode.HeaderValue, "Basic OmZvb2Jhcg==")] |
15 | 64 | [TestCase("foobar", CommandToken.OutputMode.Header, "Authorization: Basic OmZvb2Jhcg==")] |
16 | 65 | public void FormatToken_Basic(string input, CommandToken.OutputMode mode, string expected) |
17 | 66 | { |
18 | | - CommandToken.FormatToken(input, mode, Authorization.Basic).Should().Be(expected); |
| 67 | + CommandToken.FormatToken(input, mode, Microsoft.Authentication.AzureAuth.Ado.Authorization.Basic).Should().Be(expected); |
19 | 68 | } |
20 | 69 |
|
21 | 70 | [TestCase("foobar", CommandToken.OutputMode.Token, "foobar")] |
22 | 71 | [TestCase("foobar", CommandToken.OutputMode.HeaderValue, "Bearer foobar")] |
23 | 72 | [TestCase("foobar", CommandToken.OutputMode.Header, "Authorization: Bearer foobar")] |
24 | 73 | public void FormatToken_Bearer(string input, CommandToken.OutputMode mode, string expected) |
25 | 74 | { |
26 | | - CommandToken.FormatToken(input, mode, Authorization.Bearer).Should().Be(expected); |
| 75 | + CommandToken.FormatToken(input, mode, Microsoft.Authentication.AzureAuth.Ado.Authorization.Bearer).Should().Be(expected); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public void OnExecute_AzureAuthAdoPat_AlwaysUsed() |
| 80 | + { |
| 81 | + this.mockEnv.Setup(e => e.Get(EnvVars.AdoPat)).Returns("my-explicit-pat"); |
| 82 | + |
| 83 | + var command = new CommandToken(); |
| 84 | + var result = command.OnExecute( |
| 85 | + this.serviceProvider.GetService<ILogger<CommandToken>>(), |
| 86 | + this.mockEnv.Object, |
| 87 | + this.mockTelemetry.Object, |
| 88 | + this.mockPublicClientAuth.Object, |
| 89 | + this.eventData); |
| 90 | + |
| 91 | + result.Should().Be(0); |
| 92 | + this.mockPublicClientAuth.Verify( |
| 93 | + p => p.Token(It.IsAny<AuthParameters>(), It.IsAny<IEnumerable<AuthMode>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<TimeSpan>(), It.IsAny<EventData>()), |
| 94 | + Times.Never); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void OnExecute_AdoPipeline_UsesSystemAccessToken() |
| 99 | + { |
| 100 | + this.mockEnv.Setup(e => e.Get(EnvVars.TfBuild)).Returns("True"); |
| 101 | + this.mockEnv.Setup(e => e.Get(EnvVars.SystemAccessToken)).Returns("pipeline-token"); |
| 102 | + |
| 103 | + var command = new CommandToken(); |
| 104 | + var result = command.OnExecute( |
| 105 | + this.serviceProvider.GetService<ILogger<CommandToken>>(), |
| 106 | + this.mockEnv.Object, |
| 107 | + this.mockTelemetry.Object, |
| 108 | + this.mockPublicClientAuth.Object, |
| 109 | + this.eventData); |
| 110 | + |
| 111 | + result.Should().Be(0); |
| 112 | + this.mockPublicClientAuth.Verify( |
| 113 | + p => p.Token(It.IsAny<AuthParameters>(), It.IsAny<IEnumerable<AuthMode>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<TimeSpan>(), It.IsAny<EventData>()), |
| 114 | + Times.Never); |
| 115 | + } |
| 116 | + |
| 117 | + [Test] |
| 118 | + public void OnExecute_AdoPipeline_NoSystemAccessToken_ReturnsError() |
| 119 | + { |
| 120 | + this.mockEnv.Setup(e => e.Get(EnvVars.TfBuild)).Returns("True"); |
| 121 | + |
| 122 | + var command = new CommandToken(); |
| 123 | + var result = command.OnExecute( |
| 124 | + this.serviceProvider.GetService<ILogger<CommandToken>>(), |
| 125 | + this.mockEnv.Object, |
| 126 | + this.mockTelemetry.Object, |
| 127 | + this.mockPublicClientAuth.Object, |
| 128 | + this.eventData); |
| 129 | + |
| 130 | + result.Should().Be(1); |
| 131 | + this.logTarget.Logs.Should().Contain(l => l.Contains($"{EnvVars.SystemAccessToken} is not set")); |
| 132 | + } |
| 133 | + |
| 134 | + [Test] |
| 135 | + public void OnExecute_NotAdoPipeline_SystemAccessTokenSet_WarnsAndContinues() |
| 136 | + { |
| 137 | + this.mockEnv.Setup(e => e.Get(EnvVars.SystemAccessToken)).Returns("stale-token"); |
| 138 | + var fakeTokenResult = new TokenResult(new JsonWebToken(Fake.Token), Guid.NewGuid()); |
| 139 | + this.mockPublicClientAuth |
| 140 | + .Setup(p => p.Token(It.IsAny<AuthParameters>(), It.IsAny<IEnumerable<AuthMode>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<TimeSpan>(), It.IsAny<EventData>())) |
| 141 | + .Returns(fakeTokenResult); |
| 142 | + |
| 143 | + var command = new CommandToken(); |
| 144 | + var result = command.OnExecute( |
| 145 | + this.serviceProvider.GetService<ILogger<CommandToken>>(), |
| 146 | + this.mockEnv.Object, |
| 147 | + this.mockTelemetry.Object, |
| 148 | + this.mockPublicClientAuth.Object, |
| 149 | + this.eventData); |
| 150 | + |
| 151 | + result.Should().Be(0); |
| 152 | + this.logTarget.Logs.Should().Contain(l => l.Contains("does not appear to be an Azure DevOps Pipeline environment")); |
| 153 | + |
| 154 | + // Verify it fell through to AAD auth (ignored the SYSTEM_ACCESSTOKEN) |
| 155 | + this.mockPublicClientAuth.Verify( |
| 156 | + p => p.Token(It.IsAny<AuthParameters>(), It.IsAny<IEnumerable<AuthMode>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<TimeSpan>(), It.IsAny<EventData>()), |
| 157 | + Times.Once); |
| 158 | + } |
| 159 | + |
| 160 | + [Test] |
| 161 | + public void OnExecute_AdoPipeline_AzureAuthAdoPat_TakesPriority() |
| 162 | + { |
| 163 | + this.mockEnv.Setup(e => e.Get(EnvVars.AdoPat)).Returns("my-explicit-pat"); |
| 164 | + this.mockEnv.Setup(e => e.Get(EnvVars.TfBuild)).Returns("True"); |
| 165 | + this.mockEnv.Setup(e => e.Get(EnvVars.SystemAccessToken)).Returns("pipeline-token"); |
| 166 | + |
| 167 | + var command = new CommandToken(); |
| 168 | + var result = command.OnExecute( |
| 169 | + this.serviceProvider.GetService<ILogger<CommandToken>>(), |
| 170 | + this.mockEnv.Object, |
| 171 | + this.mockTelemetry.Object, |
| 172 | + this.mockPublicClientAuth.Object, |
| 173 | + this.eventData); |
| 174 | + |
| 175 | + result.Should().Be(0); |
| 176 | + this.logTarget.Logs.Should().Contain(l => l.Contains(EnvVars.AdoPat)); |
27 | 177 | } |
28 | 178 | } |
29 | 179 | } |
0 commit comments