|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 16 | +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; |
| 17 | +using Microsoft.Azure.Commands.Common.Authentication.Models; |
| 18 | +using Microsoft.Azure.Commands.Profile; |
| 19 | +using Microsoft.Azure.Commands.Profile.Models; |
| 20 | +using Microsoft.Azure.Commands.ScenarioTest; |
| 21 | +using Microsoft.Azure.Commands.TestFx.Mocks; |
| 22 | +using Microsoft.Azure.ServiceManagement.Common.Models; |
| 23 | +using Microsoft.WindowsAzure.Commands.Common; |
| 24 | +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; |
| 25 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 26 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 27 | + |
| 28 | +using Moq; |
| 29 | + |
| 30 | +using System; |
| 31 | +using System.Linq; |
| 32 | +using System.Security; |
| 33 | + |
| 34 | +using Xunit; |
| 35 | +using Xunit.Abstractions; |
| 36 | + |
| 37 | +namespace Microsoft.Azure.Commands.ResourceManager.Common.Test |
| 38 | +{ |
| 39 | + public class AccessTokenCmdletTests |
| 40 | + { |
| 41 | + private GetAzureRmAccessTokenCommand cmdlet; |
| 42 | + private Mock<IAuthenticationFactory> factoryMock = new Mock<IAuthenticationFactory>(); |
| 43 | + private MockCommandRuntime mockedCommandRuntime; |
| 44 | + private IAuthenticationFactory previousFactory = null; |
| 45 | + |
| 46 | + private string tenantId = Guid.NewGuid().ToString(); |
| 47 | + |
| 48 | + public AccessTokenCmdletTests(ITestOutputHelper output) |
| 49 | + { |
| 50 | + TestExecutionHelpers.SetUpSessionAndProfile(); |
| 51 | + XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output)); |
| 52 | + |
| 53 | + var defaultContext = new AzureContext( |
| 54 | + new AzureSubscription() |
| 55 | + { |
| 56 | + Id = Guid.NewGuid().ToString(), |
| 57 | + Name = "Test subscription" |
| 58 | + }, |
| 59 | + new AzureAccount() |
| 60 | + { |
| 61 | + Id = "admin@contoso.com", |
| 62 | + Type = AzureAccount.AccountType.User |
| 63 | + }, |
| 64 | + AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], |
| 65 | + new AzureTenant() |
| 66 | + { |
| 67 | + Id = tenantId |
| 68 | + }); |
| 69 | + |
| 70 | + mockedCommandRuntime = new MockCommandRuntime(); |
| 71 | + cmdlet = new GetAzureRmAccessTokenCommand() |
| 72 | + { |
| 73 | + CommandRuntime = mockedCommandRuntime, |
| 74 | + DefaultProfile = new AzureRmProfile() |
| 75 | + }; |
| 76 | + cmdlet.DefaultProfile.DefaultContext = defaultContext; |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 81 | + public void TestGetAccessTokenAsPlainText() |
| 82 | + { |
| 83 | + // Setup |
| 84 | + cmdlet.TenantId = tenantId; |
| 85 | + var fakeToken = "eyfaketoken.eyfaketoken"; |
| 86 | + |
| 87 | + var expected = new PSAccessToken { |
| 88 | + UserId = "faker@contoso.com", |
| 89 | + TenantId = cmdlet.TenantId, |
| 90 | + Token = fakeToken |
| 91 | + }; |
| 92 | + |
| 93 | + factoryMock.Setup(t => t.Authenticate( |
| 94 | + It.IsAny<IAzureAccount>(), |
| 95 | + It.IsAny<IAzureEnvironment>(), |
| 96 | + It.IsAny<string>(), |
| 97 | + It.IsAny<SecureString>(), |
| 98 | + It.IsAny<string>(), |
| 99 | + It.IsAny<Action<string>>(), |
| 100 | + It.IsAny<IAzureTokenCache>(), |
| 101 | + It.IsAny<string>())).Returns(new MockAccessToken |
| 102 | + { |
| 103 | + UserId = expected.UserId, |
| 104 | + LoginType = LoginType.OrgId, |
| 105 | + AccessToken = expected.Token, |
| 106 | + TenantId = expected.TenantId |
| 107 | + }); |
| 108 | + previousFactory = AzureSession.Instance.AuthenticationFactory; |
| 109 | + AzureSession.Instance.AuthenticationFactory = factoryMock.Object; |
| 110 | + |
| 111 | + // Act |
| 112 | + cmdlet.InvokeBeginProcessing(); |
| 113 | + cmdlet.ExecuteCmdlet(); |
| 114 | + cmdlet.InvokeEndProcessing(); |
| 115 | + |
| 116 | + //Verify |
| 117 | + Assert.Single(mockedCommandRuntime.OutputPipeline); |
| 118 | + var outputPipeline = mockedCommandRuntime.OutputPipeline; |
| 119 | + Assert.Equal(expected.TenantId, ((PSAccessToken)outputPipeline.First()).TenantId); |
| 120 | + Assert.Equal(expected.UserId, ((PSAccessToken)outputPipeline.First()).UserId); |
| 121 | + Assert.Equal("Bearer", ((PSAccessToken)outputPipeline.First()).Type); |
| 122 | + Assert.Equal(expected.Token, ((PSAccessToken)outputPipeline.First()).Token); |
| 123 | + |
| 124 | + AzureSession.Instance.AuthenticationFactory = previousFactory; |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 129 | + public void TestGetAccessTokenAsSecureString() |
| 130 | + { |
| 131 | + // Setup |
| 132 | + cmdlet.TenantId = tenantId; |
| 133 | + cmdlet.AsSecureString = true; |
| 134 | + var fakeToken = "eyfaketoken.eyfaketoken"; |
| 135 | + |
| 136 | + var expected = new PSSecureAccessToken(); |
| 137 | + expected.UserId = "faker@contoso.com"; |
| 138 | + expected.TenantId = cmdlet.TenantId; |
| 139 | + expected.Token = fakeToken.ConvertToSecureString(); |
| 140 | + |
| 141 | + |
| 142 | + factoryMock.Setup(t => t.Authenticate( |
| 143 | + It.IsAny<IAzureAccount>(), |
| 144 | + It.IsAny<IAzureEnvironment>(), |
| 145 | + It.IsAny<string>(), |
| 146 | + It.IsAny<SecureString>(), |
| 147 | + It.IsAny<string>(), |
| 148 | + It.IsAny<Action<string>>(), |
| 149 | + It.IsAny<IAzureTokenCache>(), |
| 150 | + It.IsAny<string>())).Returns(new MockAccessToken |
| 151 | + { |
| 152 | + UserId = expected.UserId, |
| 153 | + LoginType = LoginType.OrgId, |
| 154 | + AccessToken = fakeToken, |
| 155 | + TenantId = expected.TenantId |
| 156 | + }); |
| 157 | + previousFactory = AzureSession.Instance.AuthenticationFactory; |
| 158 | + AzureSession.Instance.AuthenticationFactory = factoryMock.Object; |
| 159 | + |
| 160 | + // Act |
| 161 | + cmdlet.InvokeBeginProcessing(); |
| 162 | + cmdlet.ExecuteCmdlet(); |
| 163 | + cmdlet.InvokeEndProcessing(); |
| 164 | + |
| 165 | + //Verify |
| 166 | + Assert.Single(mockedCommandRuntime.OutputPipeline); |
| 167 | + var outputPipeline = mockedCommandRuntime.OutputPipeline; |
| 168 | + Assert.Equal(expected.TenantId, ((PSSecureAccessToken)outputPipeline.First()).TenantId); |
| 169 | + Assert.Equal(expected.UserId, ((PSSecureAccessToken)outputPipeline.First()).UserId); |
| 170 | + Assert.Equal("Bearer", ((PSSecureAccessToken)outputPipeline.First()).Type); |
| 171 | + var expectedToken = expected.Token.ConvertToString(); |
| 172 | + var actualToken = ((PSSecureAccessToken)outputPipeline.First()).Token.ConvertToString(); |
| 173 | + Assert.Equal(expectedToken, actualToken); |
| 174 | + |
| 175 | + AzureSession.Instance.AuthenticationFactory = previousFactory; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments