public void GetTenantWithTenantParameter()
        {
            // Setup
            cmdlet.TenantId = Guid.NewGuid().ToString();
            mockSubscriptionClient.Setup(f => f.ListAccountTenants(It.IsAny <IAccessToken>(), It.IsAny <IAzureEnvironment>())).Returns(
                (IAccessToken accessToken, IAzureEnvironment environment) =>
            {
                var result = new List <AzureTenant>();
                result.Add(new AzureTenant()
                {
                    Id = cmdlet.TenantId,
                    ExtendedProperties =
                    {
                        { "DisplayName",    "Microsoft"                                                   },
                        { "TenantCategory", "Home"                                                        },
                        { "Domains",        "test0.com,test1.com,test2.microsoft.com,test3.microsoft.com" },
                        { "TenantType",     "AAD"                                                         },
                        { "DefaultDomain",  "test0.com,test1.com"                                         }
                    }
                });
                result.Add(new AzureTenant()
                {
                    Id = defaultContext.Tenant.Id,
                    ExtendedProperties =
                    {
                        { "DisplayName",    "Macrohard"          },
                        { "TenantCategory", "Home"               },
                        { "Domains",        "test.macrohard.com" }
                    }
                });
                return(result);
            });

            // Act
            cmdlet.InvokeBeginProcessing();
            cmdlet.ExecuteCmdlet();
            cmdlet.InvokeEndProcessing();

            //Verify
            Assert.Single(OutputPipeline);
            Assert.Equal(cmdlet.TenantId, ((PSAzureTenant)OutputPipeline.First()).Id.ToString());
            Assert.Equal("Home", ((PSAzureTenant)OutputPipeline.First()).TenantCategory);
            Assert.Equal(4, ((PSAzureTenant)OutputPipeline.First()).Domains.Length);
            Assert.Equal("AAD", ((PSAzureTenant)OutputPipeline.First()).TenantType);
            Assert.Equal("test0.com,test1.com", ((PSAzureTenant)OutputPipeline.First()).DefaultDomain);
        }
Example #2
0
        public void SilentReauthenticateFailure()
        {
            try
            {
                // Setup
                InitializeSession();
                var mockAzureCredentialFactory = new Mock <AzureCredentialFactory>();
                mockAzureCredentialFactory.Setup(f => f.CreateSharedTokenCacheCredentials(It.IsAny <SharedTokenCacheCredentialOptions>())).Returns(() => new TokenCredentialMock(
                                                                                                                                                       (times) =>
                {
                    if (times < 1)
                    {
                        return(new ValueTask <AccessToken>(new AccessToken(fakeToken, DateTimeOffset.Now.AddHours(1))));
                    }
                    throw new CredentialUnavailableException("Exception from Azure Identity.");
                }
                                                                                                                                                       ));
                AzureSession.Instance.RegisterComponent(nameof(AzureCredentialFactory), () => mockAzureCredentialFactory.Object, true);
                AzureSession.Instance.ClientFactory.AddHandler(new HttpMockHandler(
                                                                   (times) =>
                {
                    var response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
                    {
                        Content = new StringContent(body401, Encoding.UTF8, "application/json"),
                    };
                    response.Headers.Add("WWW-Authenticate", WwwAuthenticateIP);
                    return(response);
                }));

                cmdlet.TenantId = Guid.NewGuid().ToString();

                // Act
                cmdlet.InvokeBeginProcessing();
                AuthenticationFailedException e = Assert.Throws <AuthenticationFailedException>(() => cmdlet.ExecuteCmdlet());
                string errorMessage             = $"Exception from Azure Identity.{Environment.NewLine}authorization_uri: https://login.windows.net/{Environment.NewLine}error: invalid_token{Environment.NewLine}error_description: Tenant IP Policy validate failed.{Environment.NewLine}claims: eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwidmFsdWUiOiIxNjEzOTgyNjA2In0sInhtc19ycF9pcGFkZHIiOnsidmFsdWUiOiIxNjcuMjIwLjI1NS40MSJ9fX0={Environment.NewLine}";
                Assert.Equal(errorMessage, e.Message);
                cmdlet.InvokeEndProcessing();
            }
            finally
            {
                //Dispose
                AzureSessionInitializer.CreateOrReplaceSession(new MemoryDataStore());
            }
        }