public void CanExportConfig()
        {
            string key           = "key1";
            var    config        = new SimpleTypedConfig <bool>(key, "", true);
            string configPath    = Path.GetTempFileName();
            var    dataStore     = new MockDataStore();
            var    configManager = GetConfigManager(
                new InitSettings()
            {
                DataStore = dataStore,
                Path      = configPath
            },
                config);

            // update config to false
            configManager.UpdateConfig(key, false, ConfigScope.CurrentUser);

            // export config file
            string path = Path.GetTempFileName();

            Assert.False(dataStore.FileExists(path));
            new JsonConfigHelper(configPath, dataStore).ExportConfigFile(path);

            // config file should be exported and be correct
            Assert.True(dataStore.FileExists(path));
            var json = JsonUtilities.DeserializeJson(dataStore.ReadFileAsText(path), true);

            Assert.True(json.ContainsKey(ConfigFilter.GlobalAppliesTo));
            var jsonConfig = json.GetProperty(ConfigFilter.GlobalAppliesTo) as IDictionary <string, object>;

            Assert.NotNull(jsonConfig);
            Assert.False((bool)jsonConfig.GetProperty(key));
        }
        public void ProfileSaveDoesNotSerializeContext()
        {
            var dataStore   = new MockDataStore();
            var profilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.Instance.ProfileFile);
            var profile     = new AzureSMProfile(profilePath);

            AzureSession.Instance.DataStore = dataStore;
            var tenant      = Guid.NewGuid().ToString();
            var environment = new AzureEnvironment
            {
                Name            = "testCloud",
                ActiveDirectory = new Uri("http://contoso.com")
            };
            var account = new AzureAccount
            {
                Id   = "*****@*****.**",
                Type = AzureAccount.AccountType.User,
            };

            account.SetTenants(tenant);
            var sub = new AzureSubscription
            {
                Id   = new Guid().ToString(),
                Name = "Contoso Test Subscription",
            };

            sub.SetAccount(account.Id);
            sub.SetEnvironment(environment.Name);
            sub.SetTenant(tenant);
            profile.EnvironmentTable[environment.Name] = environment;
            profile.AccountTable[account.Id]           = account;
            profile.SubscriptionTable[sub.GetId()]     = sub;

            profile.Save();

            var    profileFile     = profile.ProfilePath;
            string profileContents = dataStore.ReadFileAsText(profileFile);
            var    readProfile     = JsonConvert.DeserializeObject <Dictionary <string, object> >(profileContents);

            Assert.False(readProfile.ContainsKey("DefaultContext"));
            AzureSMProfile parsedProfile = new AzureSMProfile();
            var            serializer    = new JsonProfileSerializer();

            Assert.True(serializer.Deserialize(profileContents, parsedProfile));
            Assert.NotNull(parsedProfile);
            Assert.NotNull(parsedProfile.Environments);
            Assert.True(parsedProfile.EnvironmentTable.ContainsKey(environment.Name));
            Assert.NotNull(parsedProfile.Accounts);
            Assert.True(parsedProfile.AccountTable.ContainsKey(account.Id));
            Assert.NotNull(parsedProfile.Subscriptions);
            Assert.True(parsedProfile.SubscriptionTable.ContainsKey(sub.GetId()));
        }
        public void ProfileSaveDoesNotSerializeContext()
        {
            var dataStore      = new MockDataStore();
            var currentProfile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));

            AzureSession.DataStore = dataStore;
            var client      = new ProfileClient(currentProfile);
            var tenant      = Guid.NewGuid().ToString();
            var environment = new AzureEnvironment
            {
                Name      = "testCloud",
                Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } }
            };
            var account = new AzureAccount
            {
                Id         = "*****@*****.**",
                Type       = AzureAccount.AccountType.User,
                Properties = { { AzureAccount.Property.Tenants, tenant } }
            };
            var sub = new AzureSubscription
            {
                Account     = account.Id,
                Environment = environment.Name,
                Id          = new Guid(),
                Name        = "Contoso Test Subscription",
                Properties  = { { AzureSubscription.Property.Tenants, tenant } }
            };

            client.AddOrSetEnvironment(environment);
            client.AddOrSetAccount(account);
            client.AddOrSetSubscription(sub);

            currentProfile.Save();

            var    profileFile     = currentProfile.ProfilePath;
            string profileContents = dataStore.ReadFileAsText(profileFile);
            var    readProfile     = JsonConvert.DeserializeObject <Dictionary <string, object> >(profileContents);

            Assert.False(readProfile.ContainsKey("Context"));
            AzureProfile parsedProfile = new AzureProfile();
            var          serializer    = new JsonProfileSerializer();

            Assert.True(serializer.Deserialize(profileContents, parsedProfile));
            Assert.NotNull(parsedProfile);
            Assert.NotNull(parsedProfile.Environments);
            Assert.True(parsedProfile.Environments.ContainsKey(environment.Name));
            Assert.NotNull(parsedProfile.Accounts);
            Assert.True(parsedProfile.Accounts.ContainsKey(account.Id));
            Assert.NotNull(parsedProfile.Subscriptions);
            Assert.True(parsedProfile.Subscriptions.ContainsKey(sub.Id));
        }
        public void SavingProfileWorks()
        {
            string expected  = @"{
  ""Environments"": {
    ""testCloud"": {
      ""Name"": ""testCloud"",
      ""OnPremise"": false,
      ""Endpoints"": {
        ""ActiveDirectory"": ""http://contoso.com""
      }
    }
  },
  ""Context"": {
    ""Account"": {
      ""Id"": ""*****@*****.**"",
      ""Type"": 1,
      ""Properties"": {
        ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
      }
    },
    ""Subscription"": {
      ""Id"": ""00000000-0000-0000-0000-000000000000"",
      ""Name"": ""Contoso Test Subscription"",
      ""Environment"": ""testCloud"",
      ""Account"": ""*****@*****.**"",
      ""State"": ""Enabled"",
      ""Properties"": {
        ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
      }
    },
    ""Environment"": {
      ""Name"": ""testCloud"",
      ""OnPremise"": false,
      ""Endpoints"": {
        ""ActiveDirectory"": ""http://contoso.com""
      }
    },
    ""Tenant"": {
      ""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
      ""Domain"": ""contoso.com""
    },
    ""TokenCache"": ""AQIDBAUGCAkA""
  }
}";
            var    path      = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile);
            var    dataStore = new MockDataStore();

            AzureSession.DataStore = dataStore;
            AzureRMProfile profile     = new AzureRMProfile(path);
            var            tenantId    = new Guid("3c0ff8a7-e8bb-40e8-ae66-271343379af6");
            var            environment = new AzureEnvironment
            {
                Name      = "testCloud",
                Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } }
            };
            var account = new AzureAccount
            {
                Id         = "*****@*****.**",
                Type       = AzureAccount.AccountType.User,
                Properties = { { AzureAccount.Property.Tenants, tenantId.ToString() } }
            };
            var sub = new AzureSubscription
            {
                Account     = account.Id,
                Environment = environment.Name,
                Id          = new Guid(),
                Name        = "Contoso Test Subscription",
                State       = "Enabled",
                Properties  = { { AzureSubscription.Property.Tenants, tenantId.ToString() } }
            };
            var tenant = new AzureTenant
            {
                Id     = tenantId,
                Domain = "contoso.com"
            };

            profile.Context = new AzureContext(sub, account, environment, tenant);
            profile.Environments[environment.Name] = environment;
            profile.Context.TokenCache             = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 };
            profile.Save();
            string actual = dataStore.ReadFileAsText(path);

            Assert.Equal(expected, actual);
        }
        public void SavingProfileWorks()
        {
            string expected  = @"{
  ""EnvironmentTable"": {
    ""testCloud"": {
      ""Name"": ""testCloud"",
      ""OnPremise"": false,
      ""ServiceManagementUrl"": null,
      ""ResourceManagerUrl"": null,
      ""ManagementPortalUrl"": null,
      ""PublishSettingsFileUrl"": null,
      ""ActiveDirectoryAuthority"": ""http://contoso.com"",
      ""GalleryUrl"": null,
      ""GraphUrl"": null,
      ""ActiveDirectoryServiceEndpointResourceId"": null,
      ""StorageEndpointSuffix"": null,
      ""SqlDatabaseDnsSuffix"": null,
      ""TrafficManagerDnsSuffix"": null,
      ""AzureKeyVaultDnsSuffix"": null,
      ""AzureKeyVaultServiceEndpointResourceId"": null,
      ""GraphEndpointResourceId"": null,
      ""DataLakeEndpointResourceId"": null,
      ""AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix"": null,
      ""AzureDataLakeStoreFileSystemEndpointSuffix"": null,
      ""AdTenant"": null,
      ""VersionProfiles"": [],
      ""ExtendedProperties"": {}
    }
  },
  ""Contexts"": {
    ""Default"": {
      ""Account"": {
        ""Id"": ""*****@*****.**"",
        ""Credential"": null,
        ""Type"": ""User"",
        ""TenantMap"": {},
        ""ExtendedProperties"": {
          ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6""
        }
      },
      ""Tenant"": {
        ""Id"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
        ""Directory"": ""contoso.com"",
        ""ExtendedProperties"": {}
      },
      ""Subscription"": {
        ""Id"": ""00000000-0000-0000-0000-000000000000"",
        ""Name"": ""Contoso Test Subscription"",
        ""State"": ""Enabled"",
        ""ExtendedProperties"": {
          ""Account"": ""*****@*****.**"",
          ""Tenants"": ""3c0ff8a7-e8bb-40e8-ae66-271343379af6"",
          ""Environment"": ""testCloud""
        }
      },
      ""Environment"": {
        ""Name"": ""testCloud"",
        ""OnPremise"": false,
        ""ServiceManagementUrl"": null,
        ""ResourceManagerUrl"": null,
        ""ManagementPortalUrl"": null,
        ""PublishSettingsFileUrl"": null,
        ""ActiveDirectoryAuthority"": ""http://contoso.com"",
        ""GalleryUrl"": null,
        ""GraphUrl"": null,
        ""ActiveDirectoryServiceEndpointResourceId"": null,
        ""StorageEndpointSuffix"": null,
        ""SqlDatabaseDnsSuffix"": null,
        ""TrafficManagerDnsSuffix"": null,
        ""AzureKeyVaultDnsSuffix"": null,
        ""AzureKeyVaultServiceEndpointResourceId"": null,
        ""GraphEndpointResourceId"": null,
        ""DataLakeEndpointResourceId"": null,
        ""AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix"": null,
        ""AzureDataLakeStoreFileSystemEndpointSuffix"": null,
        ""AdTenant"": null,
        ""VersionProfiles"": [],
        ""ExtendedProperties"": {}
      },
      ""VersionProfile"": null,
      ""TokenCache"": {
        ""CacheData"": ""AgAAAAAAAAA=""
      },
      ""ExtendedProperties"": {}
    }
  },
  ""ExtendedProperties"": {}
}";
            var    path      = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.Instance.ProfileFile);
            var    dataStore = new MockDataStore();

            AzureSession.Instance.DataStore = dataStore;
            AzureRmProfile profile     = new AzureRmProfile(path);
            var            tenantId    = new Guid("3c0ff8a7-e8bb-40e8-ae66-271343379af6");
            var            environment = new AzureEnvironment
            {
                Name = "testCloud",
                ActiveDirectoryAuthority = "http://contoso.com"
            };
            var account = new AzureAccount
            {
                Id   = "*****@*****.**",
                Type = AzureAccount.AccountType.User,
            };

            account.SetTenants(tenantId.ToString());
            var sub = new AzureSubscription
            {
                Id    = new Guid().ToString(),
                Name  = "Contoso Test Subscription",
                State = "Enabled",
            };

            sub.SetAccount(account.Id);
            sub.SetEnvironment(environment.Name);
            sub.SetTenant(tenantId.ToString());
            var tenant = new AzureTenant
            {
                Id        = tenantId.ToString(),
                Directory = "contoso.com"
            };

            profile.DefaultContext = new AzureContext(sub, account, environment, tenant);
            profile.EnvironmentTable[environment.Name] = environment;
            profile.DefaultContext.TokenCache          = new AuthenticationStoreTokenCache(new AzureTokenCache {
                CacheData = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 }
            });
            profile.Save();
            string actual = dataStore.ReadFileAsText(path);

            Assert.Equal(expected, actual);
        }