|
| 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.Models; |
| 17 | +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; |
| 18 | +using System; |
| 19 | +using System.IO; |
| 20 | +using System.Runtime.Serialization.Formatters.Binary; |
| 21 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace Common.Authentication.Test |
| 25 | +{ |
| 26 | + public class AzureRMProfileTests |
| 27 | + { |
| 28 | + [Fact] |
| 29 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 30 | + public void ProfileSerializeDeserializeWorks() |
| 31 | + { |
| 32 | + var dataStore = new MockDataStore(); |
| 33 | + AzureSession.DataStore = dataStore; |
| 34 | + var profilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile); |
| 35 | + var currentProfile = new AzureRMProfile(profilePath); |
| 36 | + var tenantId = Guid.NewGuid().ToString(); |
| 37 | + var environment = new AzureEnvironment |
| 38 | + { |
| 39 | + Name = "testCloud", |
| 40 | + Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } } |
| 41 | + }; |
| 42 | + var account = new AzureAccount |
| 43 | + { |
| 44 | + |
| 45 | + Type = AzureAccount.AccountType.User, |
| 46 | + Properties = { { AzureAccount.Property.Tenants, tenantId } } |
| 47 | + }; |
| 48 | + var sub = new AzureSubscription |
| 49 | + { |
| 50 | + Account = account.Id, |
| 51 | + Environment = environment.Name, |
| 52 | + Id = new Guid(), |
| 53 | + Name = "Contoso Test Subscription", |
| 54 | + Properties = { { AzureSubscription.Property.Tenants, tenantId } } |
| 55 | + }; |
| 56 | + var tenant = new AzureTenant |
| 57 | + { |
| 58 | + Id = new Guid(tenantId), |
| 59 | + Domain = "contoso.com" |
| 60 | + }; |
| 61 | + |
| 62 | + currentProfile.Context = new AzureContext(sub, account, environment, tenant); |
| 63 | + currentProfile.Environments[environment.Name] = environment; |
| 64 | + currentProfile.Context.TokenCache = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 }; |
| 65 | + |
| 66 | + AzureRMProfile deserializedProfile; |
| 67 | + // Round-trip the exception: Serialize and de-serialize with a BinaryFormatter |
| 68 | + BinaryFormatter bf = new BinaryFormatter(); |
| 69 | + using (MemoryStream ms = new MemoryStream()) |
| 70 | + { |
| 71 | + // "Save" object state |
| 72 | + bf.Serialize(ms, currentProfile); |
| 73 | + |
| 74 | + // Re-use the same stream for de-serialization |
| 75 | + ms.Seek(0, 0); |
| 76 | + |
| 77 | + // Replace the original exception with de-serialized one |
| 78 | + deserializedProfile = (AzureRMProfile)bf.Deserialize(ms); |
| 79 | + } |
| 80 | + Assert.NotNull(deserializedProfile); |
| 81 | + var jCurrentProfile = currentProfile.ToString(); |
| 82 | + var jDeserializedProfile = deserializedProfile.ToString(); |
| 83 | + Assert.Equal(jCurrentProfile, jDeserializedProfile); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 88 | + public void SavingProfileWorks() |
| 89 | + { |
| 90 | + string expected = @"{ |
| 91 | + ""Environments"": { |
| 92 | + ""testCloud"": { |
| 93 | + ""Name"": ""testCloud"", |
| 94 | + ""OnPremise"": false, |
| 95 | + ""Endpoints"": { |
| 96 | + ""ActiveDirectory"": ""http://contoso.com"" |
| 97 | + } |
| 98 | + } |
| 99 | + }, |
| 100 | + ""Context"": { |
| 101 | + ""Account"": { |
| 102 | + |
| 103 | + ""Type"": 1, |
| 104 | + ""Properties"": { |
| 105 | + ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"" |
| 106 | + } |
| 107 | + }, |
| 108 | + ""Subscription"": { |
| 109 | + ""Id"": ""00000000-0000-0000-0000-000000000000"", |
| 110 | + ""Name"": ""Contoso Test Subscription"", |
| 111 | + ""Environment"": ""testCloud"", |
| 112 | + ""Account"": ""[email protected]"", |
| 113 | + ""State"": ""Enabled"", |
| 114 | + ""Properties"": { |
| 115 | + ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"" |
| 116 | + } |
| 117 | + }, |
| 118 | + ""Environment"": { |
| 119 | + ""Name"": ""testCloud"", |
| 120 | + ""OnPremise"": false, |
| 121 | + ""Endpoints"": { |
| 122 | + ""ActiveDirectory"": ""http://contoso.com"" |
| 123 | + } |
| 124 | + }, |
| 125 | + ""Tenant"": { |
| 126 | + ""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"", |
| 127 | + ""Domain"": ""contoso.com"" |
| 128 | + }, |
| 129 | + ""TokenCache"": ""AQIDBAUGCAkA"" |
| 130 | + } |
| 131 | +}"; |
| 132 | + var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile); |
| 133 | + var dataStore = new MockDataStore(); |
| 134 | + AzureSession.DataStore = dataStore; |
| 135 | + AzureRMProfile profile = new AzureRMProfile(path); |
| 136 | + var tenantId = new Guid("3c0ff8a7-e8bb-40e8-ae66-271343379af6"); |
| 137 | + var environment = new AzureEnvironment |
| 138 | + { |
| 139 | + Name = "testCloud", |
| 140 | + Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } } |
| 141 | + }; |
| 142 | + var account = new AzureAccount |
| 143 | + { |
| 144 | + |
| 145 | + Type = AzureAccount.AccountType.User, |
| 146 | + Properties = { { AzureAccount.Property.Tenants, tenantId.ToString() } } |
| 147 | + }; |
| 148 | + var sub = new AzureSubscription |
| 149 | + { |
| 150 | + Account = account.Id, |
| 151 | + Environment = environment.Name, |
| 152 | + Id = new Guid(), |
| 153 | + Name = "Contoso Test Subscription", |
| 154 | + State = "Enabled", |
| 155 | + Properties = { { AzureSubscription.Property.Tenants, tenantId.ToString() } } |
| 156 | + }; |
| 157 | + var tenant = new AzureTenant |
| 158 | + { |
| 159 | + Id = tenantId, |
| 160 | + Domain = "contoso.com" |
| 161 | + }; |
| 162 | + profile.Context = new AzureContext(sub, account, environment, tenant); |
| 163 | + profile.Environments[environment.Name] = environment; |
| 164 | + profile.Context.TokenCache = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 }; |
| 165 | + profile.Save(); |
| 166 | + string actual = dataStore.ReadFileAsText(path); |
| 167 | + Assert.Equal(expected, actual); |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 172 | + public void LoadingProfileWorks() |
| 173 | + { |
| 174 | + string contents = @"{ |
| 175 | + ""Environments"": { |
| 176 | + ""testCloud"": { |
| 177 | + ""Name"": ""testCloud"", |
| 178 | + ""OnPremise"": false, |
| 179 | + ""Endpoints"": { |
| 180 | + ""ActiveDirectory"": ""http://contoso.com"" |
| 181 | + } |
| 182 | + } |
| 183 | + }, |
| 184 | + ""Context"": { |
| 185 | + ""TokenCache"": ""AQIDBAUGCAkA"", |
| 186 | + ""Account"": { |
| 187 | + |
| 188 | + ""Type"": 1, |
| 189 | + ""Properties"": { |
| 190 | + ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"" |
| 191 | + } |
| 192 | + }, |
| 193 | + ""Subscription"": { |
| 194 | + ""Id"": ""00000000-0000-0000-0000-000000000000"", |
| 195 | + ""Name"": ""Contoso Test Subscription"", |
| 196 | + ""Environment"": ""testCloud"", |
| 197 | + ""Account"": ""[email protected]"", |
| 198 | + ""Properties"": { |
| 199 | + ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"" |
| 200 | + } |
| 201 | + }, |
| 202 | + ""Environment"": { |
| 203 | + ""Name"": ""testCloud"", |
| 204 | + ""OnPremise"": false, |
| 205 | + ""Endpoints"": { |
| 206 | + ""ActiveDirectory"": ""http://contoso.com"" |
| 207 | + } |
| 208 | + }, |
| 209 | + ""Tenant"": { |
| 210 | + ""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"", |
| 211 | + ""Domain"": ""contoso.com"" |
| 212 | + } |
| 213 | + } |
| 214 | +}"; |
| 215 | + var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile); |
| 216 | + var dataStore = new MockDataStore(); |
| 217 | + AzureSession.DataStore = dataStore; |
| 218 | + dataStore.WriteFile(path, contents); |
| 219 | + var profile = new AzureRMProfile(path); |
| 220 | + Assert.Equal(4, profile.Environments.Count); |
| 221 | + Assert.Equal("3c0ff8a7-e8bb-40e8-ae66-271343379af6", profile.Context.Tenant.Id.ToString()); |
| 222 | + Assert.Equal("contoso.com", profile.Context.Tenant.Domain); |
| 223 | + Assert.Equal("00000000-0000-0000-0000-000000000000", profile.Context.Subscription.Id.ToString()); |
| 224 | + Assert.Equal("testCloud", profile.Context.Environment.Name); |
| 225 | + Assert.Equal("[email protected]", profile.Context.Account.Id); |
| 226 | + Assert.Equal(new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 }, profile.Context.TokenCache); |
| 227 | + Assert.Equal(path, profile.ProfilePath); |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments