|
| 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.Abstractions.Core; |
| 18 | +using Microsoft.Azure.Commands.ResourceManager.Common; |
| 19 | + |
| 20 | +using Moq; |
| 21 | + |
| 22 | +using System; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.Linq; |
| 25 | +using System.Security; |
| 26 | +using System.Text.RegularExpressions; |
| 27 | + |
| 28 | +using Xunit; |
| 29 | + |
| 30 | +namespace Microsoft.Azure.Commands.ResourceManager.Test |
| 31 | +{ |
| 32 | + public class AzureRMCmdletAuthenticationUnitTests |
| 33 | + { |
| 34 | + |
| 35 | + private static readonly AzureAccount azureAccount = new AzureAccount() |
| 36 | + { |
| 37 | + Id = "fakeUserId", |
| 38 | + Type = AzureAccount.AccountType.User |
| 39 | + }; |
| 40 | + |
| 41 | + private static IAzureSession CreateAuthenticationFactory() |
| 42 | + { |
| 43 | + var session = new Mock<IAzureSession>(); |
| 44 | + var authFactory = new Mock<IAuthenticationFactory>(); |
| 45 | + authFactory.Setup(f => f.Authenticate( |
| 46 | + It.IsAny<IAzureAccount>(), |
| 47 | + It.IsAny<IAzureEnvironment>(), |
| 48 | + It.IsAny<string>(), |
| 49 | + It.IsAny<SecureString>(), |
| 50 | + It.IsAny<string>(), |
| 51 | + It.IsAny<Action<string>>(), |
| 52 | + It.IsAny<string>())).Returns<IAzureAccount, IAzureEnvironment, string, SecureString, string, Action<string>, string> |
| 53 | + ((a, e, t, w, b, p, r) => new MockAccessToken(azureAccount.Id, t, azureAccount.Type, t)); |
| 54 | + session.SetupProperty(m => m.AuthenticationFactory, authFactory.Object); |
| 55 | + return session.Object; |
| 56 | + } |
| 57 | + |
| 58 | + private static AzureRmProfileProvider CreateAzureRmProfile() |
| 59 | + { |
| 60 | + var context = new Mock<IAzureContext>(); |
| 61 | + context.SetupProperty(m => m.Account, azureAccount); |
| 62 | + |
| 63 | + var profile = new Mock<IAzureContextContainer>(); |
| 64 | + profile.SetupProperty(m => m.DefaultContext, context.Object); |
| 65 | + |
| 66 | + var profileProvider = new Mock<AzureRmProfileProvider>(); |
| 67 | + profileProvider.SetupProperty(m => m.Profile, profile.Object); |
| 68 | + return profileProvider.Object; |
| 69 | + } |
| 70 | + |
| 71 | + private static void InitializeSessionAndProfile() |
| 72 | + { |
| 73 | + AzureSession.Initialize(CreateAuthenticationFactory, true); |
| 74 | + AzureSession.Modify(s => s.ARMContextSaveMode = ContextSaveMode.Process); |
| 75 | + AzureRmProfileProvider.SetInstance(() => CreateAzureRmProfile(), true); |
| 76 | + } |
| 77 | + |
| 78 | + private static void Dispose() |
| 79 | + { |
| 80 | + AzureRmProfileProvider.SetInstance(() => null, true); |
| 81 | + AzureSession.Modify(s => s = null); |
| 82 | + } |
| 83 | + |
| 84 | + public class MockCmdlet : AzureRMCmdlet |
| 85 | + { |
| 86 | + public IDictionary<string, IList<string>> GetAuxilaryAuthHeaderByTenatIds(IEnumerable<string> tenantIds) |
| 87 | + { |
| 88 | + return base.GetAuxiliaryAuthHeaderFromTenantIds(tenantIds); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void TestGetAuxHeaderByTenantIds() |
| 95 | + { |
| 96 | + try |
| 97 | + { |
| 98 | + InitializeSessionAndProfile(); |
| 99 | + var cmdlet = new MockCmdlet(); |
| 100 | + var tenants = new List<string>() { "tenant0", "tenant1", "tenant2" }; |
| 101 | + |
| 102 | + var header = cmdlet.GetAuxilaryAuthHeaderByTenatIds(tenants); |
| 103 | + |
| 104 | + Assert.Equal(1, header.Count); |
| 105 | + var h = header.First(); |
| 106 | + Assert.Equal("x-ms-authorization-auxiliary", h.Key); |
| 107 | + Assert.Single(h.Value); |
| 108 | + var tokens = h.Value.First().Split(';'); |
| 109 | + var regex = new Regex(@"Bearer ([0-9a-zA-Z]+)"); |
| 110 | + for (int i = 0; i < tenants.Count; ++i) |
| 111 | + { |
| 112 | + var match = regex.Match(tokens[i]); |
| 113 | + Assert.True(match.Success); |
| 114 | + match.Groups[1].Value.StartsWith(tenants[i]); |
| 115 | + } |
| 116 | + } |
| 117 | + finally |
| 118 | + { |
| 119 | + Dispose(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void TestGetAuxHeaderByEmptyTenantIds() |
| 125 | + { |
| 126 | + try |
| 127 | + { |
| 128 | + InitializeSessionAndProfile(); |
| 129 | + var cmdlet = new MockCmdlet(); |
| 130 | + var tenants = new List<string>(); |
| 131 | + |
| 132 | + var header = cmdlet.GetAuxilaryAuthHeaderByTenatIds(tenants); |
| 133 | + |
| 134 | + Assert.Null(header); |
| 135 | + } |
| 136 | + finally |
| 137 | + { |
| 138 | + Dispose(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void TestGetAuxHeaderByExcessTenantIds() |
| 144 | + { |
| 145 | + try |
| 146 | + { |
| 147 | + InitializeSessionAndProfile(); |
| 148 | + var cmdlet = new MockCmdlet(); |
| 149 | + var tenants = new List<string>() { "tenant0", "tenant1", "tenant2", "tenants3" }; |
| 150 | + |
| 151 | + Assert.Throws<ArgumentException>(() => cmdlet.GetAuxilaryAuthHeaderByTenatIds(tenants)); |
| 152 | + } |
| 153 | + finally |
| 154 | + { |
| 155 | + Dispose(); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments