|
| 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.WindowsAzure.Commands.Utilities.Common; |
| 16 | +using Microsoft.Azure.Commands.Profile; |
| 17 | +using Microsoft.Azure.Commands.ResourceManager.Common; |
| 18 | +using Microsoft.Azure.Common.Authentication; |
| 19 | +using Microsoft.Azure.Common.Authentication.Models; |
| 20 | +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; |
| 21 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 22 | +using System.Linq; |
| 23 | +using Xunit; |
| 24 | +using System; |
| 25 | +using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; |
| 26 | +using System.Management.Automation; |
| 27 | + |
| 28 | +namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test |
| 29 | +{ |
| 30 | + public class ContextCmdletTestsLive |
| 31 | + { |
| 32 | + private MemoryDataStore dataStore; |
| 33 | + private MockCommandRuntime commandRuntimeMock; |
| 34 | + |
| 35 | + public ContextCmdletTestsLive() |
| 36 | + { |
| 37 | + dataStore = new MemoryDataStore(); |
| 38 | + AzureSession.DataStore = dataStore; |
| 39 | + commandRuntimeMock = new MockCommandRuntime(); |
| 40 | + AzureRMCmdlet.DefaultProfile = new AzureRMProfile(); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + [Trait(Category.AcceptanceType, Category.LiveOnly)] |
| 45 | + public void SelectAzureContextWithSubscriptionAndTenant() |
| 46 | + { |
| 47 | + var cmdlt = new SelectAzureRMContextCommand(); |
| 48 | + // Setup |
| 49 | + cmdlt.CommandRuntime = commandRuntimeMock; |
| 50 | + cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c"; |
| 51 | + cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47"; |
| 52 | + |
| 53 | + // Act |
| 54 | + Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null); |
| 55 | + cmdlt.InvokeBeginProcessing(); |
| 56 | + cmdlt.ExecuteCmdlet(); |
| 57 | + cmdlt.InvokeEndProcessing(); |
| 58 | + |
| 59 | + // Verify |
| 60 | + Assert.True(commandRuntimeMock.OutputPipeline.Count == 2); |
| 61 | + var context = (AzureContext)commandRuntimeMock.OutputPipeline[1]; |
| 62 | + Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString()); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + [Trait(Category.AcceptanceType, Category.LiveOnly)] |
| 67 | + public void SelectAzureContextWithSubscriptionAndNoTenant() |
| 68 | + { |
| 69 | + var cmdlt = new SelectAzureRMContextCommand(); |
| 70 | + // Setup |
| 71 | + cmdlt.CommandRuntime = commandRuntimeMock; |
| 72 | + cmdlt.SubscriptionId = "db1ab6f0-4769-4b27-930e-01e2ef9c123c"; |
| 73 | + |
| 74 | + // Act |
| 75 | + Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null); |
| 76 | + cmdlt.InvokeBeginProcessing(); |
| 77 | + cmdlt.ExecuteCmdlet(); |
| 78 | + cmdlt.InvokeEndProcessing(); |
| 79 | + |
| 80 | + // Verify |
| 81 | + Assert.True(commandRuntimeMock.OutputPipeline.Count == 2); |
| 82 | + var context = (AzureContext)commandRuntimeMock.OutputPipeline[1]; |
| 83 | + Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString()); |
| 84 | + Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString()); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + [Trait(Category.AcceptanceType, Category.LiveOnly)] |
| 89 | + public void SelectAzureContextWithNoSubscriptionAndTenant() |
| 90 | + { |
| 91 | + var cmdlt = new SelectAzureRMContextCommand(); |
| 92 | + // Setup |
| 93 | + cmdlt.CommandRuntime = commandRuntimeMock; |
| 94 | + cmdlt.Tenant = "72f988bf-86f1-41af-91ab-2d7cd011db47"; |
| 95 | + |
| 96 | + // Act |
| 97 | + Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null); |
| 98 | + cmdlt.InvokeBeginProcessing(); |
| 99 | + cmdlt.ExecuteCmdlet(); |
| 100 | + cmdlt.InvokeEndProcessing(); |
| 101 | + |
| 102 | + // Verify |
| 103 | + Assert.True(commandRuntimeMock.OutputPipeline.Count == 2); |
| 104 | + var context = (AzureContext)commandRuntimeMock.OutputPipeline[1]; |
| 105 | + Assert.Null(context.Subscription); |
| 106 | + Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString()); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + [Trait(Category.AcceptanceType, Category.LiveOnly)] |
| 111 | + public void SelectAzureContextWithNoSubscriptionAndNoTenant() |
| 112 | + { |
| 113 | + var cmdlt = new SelectAzureRMContextCommand(); |
| 114 | + // Setup |
| 115 | + cmdlt.CommandRuntime = commandRuntimeMock; |
| 116 | + |
| 117 | + // Act |
| 118 | + Login("2c224e7e-3ef5-431d-a57b-e71f4662e3a6", null); |
| 119 | + cmdlt.InvokeBeginProcessing(); |
| 120 | + |
| 121 | + // Verify |
| 122 | + Assert.Throws<PSNotSupportedException>(() => cmdlt.ExecuteCmdlet()); |
| 123 | + cmdlt.InvokeEndProcessing(); |
| 124 | + } |
| 125 | + |
| 126 | + private void Login(string subscriptionId, string tenantId) |
| 127 | + { |
| 128 | + var cmdlt = new LoginAzureRMAccountCommand(); |
| 129 | + // Setup |
| 130 | + cmdlt.CommandRuntime = commandRuntimeMock; |
| 131 | + cmdlt.SubscriptionId = subscriptionId; |
| 132 | + cmdlt.Tenant = tenantId; |
| 133 | + |
| 134 | + // Act |
| 135 | + cmdlt.InvokeBeginProcessing(); |
| 136 | + cmdlt.ExecuteCmdlet(); |
| 137 | + cmdlt.InvokeEndProcessing(); |
| 138 | + |
| 139 | + Assert.NotNull(AzureRMCmdlet.DefaultProfile.Context); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments