public void DiscoveryServiceHelper()
        {
            var authenticationContext  = new AuthenticationContext(OAuthConstants.ActiveDirectoryAuthenticationServiceUrl, false);
            var discoveryServiceHelper = new DiscoveryServiceHelper(
                AuthenticationTestBase.ClientId,
                AuthenticationTestBase.ReturnUrl,
                authenticationContext);

            var authenticationProvider = discoveryServiceHelper.AuthenticationProvider as AdalAuthenticationProvider;

            Assert.IsNotNull(authenticationProvider, "Unexpected authentication provider initialized.");

            var authenticationContextWrapper = authenticationProvider.AuthenticationContextWrapper as AuthenticationContextWrapper;

            Assert.IsNotNull(authenticationContextWrapper, "Unexpected authentication context wrapper initialized.");
            Assert.AreEqual(authenticationContext, authenticationContextWrapper.authenticationContext, "Unexpected authentication context.");

            Assert.IsNull(authenticationProvider.ClientCertificate, "Unexpected client certificate initialized.");
            Assert.AreEqual(AuthenticationTestBase.ClientId, authenticationProvider.ClientId, "Unexpected client ID initialized.");
            Assert.IsNull(authenticationProvider.ClientSecret, "Unexpected client secret initialized.");
            Assert.AreEqual(AuthenticationTestBase.ReturnUrl, authenticationProvider.ReturnUrl, "Unexpected return URL initialized.");
        }
        public void DiscoveryServiceHelper_ClientSecret()
        {
            var clientSecret           = "client secret";
            var discoveryServiceHelper = new DiscoveryServiceHelper(
                AuthenticationTestBase.ClientId,
                clientSecret,
                AuthenticationTestBase.ReturnUrl);

            var authenticationProvider = discoveryServiceHelper.AuthenticationProvider as AdalAuthenticationProvider;

            Assert.IsNotNull(authenticationProvider, "Unexpected authentication provider initialized.");

            var authenticationContextWrapper = authenticationProvider.AuthenticationContextWrapper as AuthenticationContextWrapper;

            Assert.IsNotNull(authenticationContextWrapper, "Unexpected authentication context wrapper initialized.");
            Assert.IsNotNull(authenticationContextWrapper.authenticationContext, "Unexpected authentication context.");

            Assert.IsNull(authenticationProvider.ClientCertificate, "Unexpected client certificate initialized.");
            Assert.AreEqual(AuthenticationTestBase.ClientId, authenticationProvider.ClientId, "Unexpected client ID initialized.");
            Assert.AreEqual(clientSecret, authenticationProvider.ClientSecret, "Unexpected client secret initialized.");
            Assert.AreEqual(AuthenticationTestBase.ReturnUrl, authenticationProvider.ReturnUrl, "Unexpected return URL initialized.");
        }
        public async Task <BusinessServiceInformation> AuthenticateWithDiscoveryServiceAsync(
            DiscoveryServiceResponse discoveryServiceResponse = null,
            string refreshToken = null)
        {
            bool refresh = refreshToken != null;

            var mockAuthenticationResult = new MockAuthenticationResult();

            mockAuthenticationResult.SetupGet(result => result.AccessToken).Returns("token");
            mockAuthenticationResult.SetupGet(result => result.AccessTokenType).Returns((string)null);
            mockAuthenticationResult.SetupGet(result => result.ExpiresOn).Returns(DateTimeOffset.UtcNow.AddHours(1));

            var mockAuthenticationContextWrapper = new MockAuthenticationContextWrapper();

            if (refresh)
            {
                mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenByRefreshTokenAsync(
                                                           It.Is <string>(token => token.Equals(refreshToken)),
                                                           It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)),
                                                           It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource))))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));
            }
            else
            {
                mockAuthenticationContextWrapper.Setup(wrapper => wrapper.AcquireTokenSilentAsync(
                                                           It.Is <string>(resource => resource.Equals(OAuthConstants.ActiveDirectoryDiscoveryResource)),
                                                           It.Is <string>(clientId => clientId.Equals(AuthenticationTestBase.ClientId)),
                                                           UserIdentifier.AnyUser))
                .Returns(Task.FromResult(mockAuthenticationResult.Object));
            }

            var authenticationProvider = new AdalAuthenticationProvider(
                AuthenticationTestBase.ClientId,
                AuthenticationTestBase.ReturnUrl,
                mockAuthenticationContextWrapper.Object);

            var discoveryServiceHelper = new DiscoveryServiceHelper(authenticationProvider);

            if (discoveryServiceResponse == null)
            {
                discoveryServiceResponse = new DiscoveryServiceResponse
                {
                    Value = new List <DiscoveryService>
                    {
                        new DiscoveryService
                        {
                            Capability         = "MyFiles",
                            ServiceApiVersion  = "v2.0",
                            ServiceEndpointUri = AuthenticationTestBase.ServiceEndpointUrl,
                            ServiceResourceId  = AuthenticationTestBase.ServiceResourceId,
                        }
                    }
                };
            }

            var requestBodyString = this.serializer.SerializeObject(discoveryServiceResponse);

            BusinessServiceInformation businessServiceInformation = null;

            using (var stringContent = new StringContent(requestBodyString))
            {
                this.httpResponseMessage.Content = stringContent;

                if (refresh)
                {
                    businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserWithRefreshTokenAsync(
                        refreshToken,
                        httpProvider : this.httpProvider.Object);
                }
                else
                {
                    businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserAsync(httpProvider : this.httpProvider.Object);
                }
            }

            return(businessServiceInformation);
        }