public static void SetEnvironmentSubscriptionId(TestEnvironment testEnv, string connectionString) { if (HttpMockServer.Mode == HttpRecorderMode.Playback) { if (HttpMockServer.Variables.ContainsKey(TestEnvironment.SubscriptionIdKey)) { testEnv.SubscriptionId = HttpMockServer.Variables[TestEnvironment.SubscriptionIdKey]; } else if (!string.IsNullOrEmpty(connectionString)) { IDictionary <string, string> parsedConnection = TestUtilities.ParseConnectionString(connectionString); if (!parsedConnection.ContainsKey(TestEnvironment.SubscriptionIdKey)) { throw new Exception( "Subscription ID is not present in the recorded mock or in environment variables."); } testEnv.SubscriptionId = parsedConnection[TestEnvironment.SubscriptionIdKey]; } else { throw new Exception( "Subscription ID is not present in the recorded mock or in environment variables."); } } else //Record or None { // Preserve/restore subscription ID HttpMockServer.Variables[TestEnvironment.SubscriptionIdKey] = testEnv.SubscriptionId; } }
/// <summary> /// Return test credentials and URI using AAD auth for an OrgID account. Use this method with caution, it may take a dependency on ADAL /// </summary> /// <returns>The test credentials, or null if the appropriate environment variable is not set.</returns> public static TestEnvironment GetTestEnvironment() { string connectionString = Environment.GetEnvironmentVariable(TestCSMOrgIdConnectionStringKey); TestEnvironment testEnv = new TestEnvironment(TestUtilities.ParseConnectionString(connectionString)); if (HttpMockServer.Mode == HttpRecorderMode.Playback) { testEnv.UserName = TestEnvironment.UserIdDefault; SetEnvironmentSubscriptionId(testEnv, connectionString); testEnv.TokenInfo[TokenAudience.Management] = new TokenCredentials(TestEnvironment.RawToken); testEnv.TokenInfo[TokenAudience.Graph] = new TokenCredentials(TestEnvironment.RawGraphToken); } else //Record or None { if (!string.IsNullOrEmpty(connectionString)) { IDictionary <string, string> parsedConnection = TestUtilities.ParseConnectionString(connectionString); foreach (var keyVal in CustomEnvValues) { parsedConnection[keyVal.Key] = keyVal.Value; } testEnv = new TestEnvironment(parsedConnection); if (parsedConnection.ContainsKey(TestEnvironment.RawToken)) { testEnv.TokenInfo[TokenAudience.Management] = new TokenCredentials(TestEnvironment.RawToken); testEnv.TokenInfo[TokenAudience.Graph] = new TokenCredentials(TestEnvironment.RawGraphToken); } else { string password = null; parsedConnection.TryGetValue(TestEnvironment.AADPasswordKey, out password); if (testEnv.UserName != null && password != null) { TestEnvironmentFactory.LoginUserAsync( testEnv.TokenInfo, testEnv.Tenant, testEnv.UserName, password, testEnv.Endpoints) .ConfigureAwait(false).GetAwaiter().GetResult(); } else if (testEnv.ServicePrincipal != null && password != null) { TestEnvironmentFactory.LoginServicePrincipalAsync( testEnv.TokenInfo, testEnv.Tenant, testEnv.ServicePrincipal, password, testEnv.Endpoints) .ConfigureAwait(false).GetAwaiter().GetResult(); } #if NET45 else { TestEnvironmentFactory.LoginWithPromptAsync( testEnv.TokenInfo, testEnv.Tenant, testEnv.Endpoints); } #endif } }//end-of-if connectionString present // Management Clients that are not subscription based should set "SubscriptionId=None" in // the connection string. if (testEnv.SubscriptionId == null || !testEnv.SubscriptionId.Equals("None", StringComparison.OrdinalIgnoreCase)) { //Getting subscriptions from server var subscriptions = ListSubscriptions( testEnv.BaseUri.ToString(), testEnv.TokenInfo[TokenAudience.Management]); if (subscriptions.Count == 0) { throw new Exception("Logged in account had no associated subscriptions. We are in " + testEnv.Endpoints.Name + " environment and the tenant is " + testEnv.Tenant + ". Please check if the subscription is in the " + "correct tenant and environment. You can set the envt. variable - " + "TEST_CSM_ORGID_AUTHENTICATION=SubscriptionId=<subscription-id>;" + "Environment=<Env-name>;Tenant=<tenant-id>"); } bool matchingSubscription = false; //SubscriptionId is provided in envt. variable if (testEnv.SubscriptionId != null) { matchingSubscription = subscriptions.Any(item => item.SubscriptionId == testEnv.SubscriptionId); if (!matchingSubscription) { throw new Exception("The provided SubscriptionId in the envt. variable - \"" + testEnv.SubscriptionId + "\" does not match the list of subscriptions associated with this account."); } } else { if (subscriptions.Count > 1) { throw new Exception("There are multiple subscriptions associated with the logged in account. " + "Please specify the subscription to use in the connection string. Please set " + "the envt. variable - TEST_CSM_ORGID_AUTHENTICATION=SubscriptionId=<subscription-id>"); } testEnv.SubscriptionId = subscriptions[0].SubscriptionId; } } if (testEnv.SubscriptionId == null) { throw new Exception("Subscription Id was not provided in environment variable. " + "Please set " + "the envt. variable - TEST_CSM_ORGID_AUTHENTICATION=SubscriptionId=<subscription-id>. " + "If SubscriptionId is not required for your management client then please set it to " + "'None' in the above mentioned envt. variable."); } SetEnvironmentSubscriptionId(testEnv, connectionString); } return(testEnv); }