Exemple #1
0
        public async Task NegativeDeviceCodeTimeoutTestAsync()
        {
            MockHttpMessageHandler mockMessageHandler = new MockHttpMessageHandler(TestConstants.DefaultAuthorityHomeTenant)
            {
                Method          = HttpMethod.Get,
                Url             = TestConstants.DefaultAuthorityHomeTenant + "oauth2/devicecode",
                ResponseMessage = MockHelpers.CreateSuccessDeviceCodeResponseMessage("1")
            };

            HttpMessageHandlerFactory.AddMockHandler(mockMessageHandler);

            mockMessageHandler = new MockHttpMessageHandler(TestConstants.DefaultAuthorityHomeTenant)
            {
                Method          = HttpMethod.Post,
                Url             = TestConstants.DefaultAuthorityHomeTenant + "oauth2/token",
                ResponseMessage = MockHelpers.CreateFailureResponseMessage("{\"error\":\"authorization_pending\"," +
                                                                           "\"error_description\":\"AADSTS70016: Pending end-user authorization." +
                                                                           "\\r\\nTrace ID: f6c2c73f-a21d-474e-a71f-d8b121a58205\\r\\nCorrelation ID: " +
                                                                           "36fe3e82-442f-4418-b9f4-9f4b9295831d\\r\\nTimestamp: 2015-09-24 19:51:51Z\"," +
                                                                           "\"error_codes\":[70016],\"timestamp\":\"2015-09-24 19:51:51Z\",\"trace_id\":" +
                                                                           "\"f6c2c73f-a21d-474e-a71f-d8b121a58205\",\"correlation_id\":" +
                                                                           "\"36fe3e82-442f-4418-b9f4-9f4b9295831d\"}")
            };

            HttpMessageHandlerFactory.AddMockHandler(mockMessageHandler);

            mockMessageHandler = new MockHttpMessageHandler(TestConstants.DefaultAuthorityHomeTenant)
            {
                Method          = HttpMethod.Post,
                Url             = TestConstants.DefaultAuthorityHomeTenant + "oauth2/token",
                ResponseMessage = MockHelpers.CreateDeviceCodeExpirationErrorResponse()
            };
            HttpMessageHandlerFactory.AddMockHandler(mockMessageHandler);

            TokenCache            cache = new TokenCache();
            AuthenticationContext ctx   = new AuthenticationContext(TestConstants.DefaultAuthorityHomeTenant, cache);
            DeviceCodeResult      dcr   = await ctx.AcquireDeviceCodeAsync("some resource", "some authority").ConfigureAwait(false);

            Assert.IsNotNull(dcr);
            AuthenticationResult result;
            AdalServiceException ex = AssertException.TaskThrows <AdalServiceException>(async() => result = await ctx.AcquireTokenByDeviceCodeAsync(dcr).ConfigureAwait(false));

            Assert.IsTrue(ex.Message.Contains("Verification code expired"));
        }