public void TestConstructor()
        {
            MockAppConfig config;

            // Tests if the constructor works when JSON secrets file is not provided.
            config = new Mocks.MockAppConfig();
            config.MockReadSettings(tblSettingsNoSecretJson);
            provider = new OAuth2ProviderForServiceAccounts(config);

            Assert.AreEqual(provider.ClientId, config.OAuth2ClientId);
            Assert.AreEqual(provider.ClientSecret, config.OAuth2ClientSecret);
            Assert.AreEqual(provider.AccessToken, config.OAuth2AccessToken);
            Assert.AreEqual(provider.Scope, config.OAuth2Scope);
            Assert.AreEqual(provider.ServiceAccountEmail, config.OAuth2ServiceAccountEmail);
            Assert.AreEqual(provider.PrnEmail, config.OAuth2PrnEmail);
            Assert.AreEqual(provider.JwtCertificatePath, config.OAuth2CertificatePath);
            Assert.AreEqual(provider.JwtCertificatePassword, config.OAuth2CertificatePassword);
            Assert.IsNullOrEmpty(provider.JwtPrivateKey);

            // Tests if the constructor works when JSON secrets file is provided.
            config = new Mocks.MockAppConfig();
            config.MockReadSettings(tblSettingsWithSecretJson);
            provider = new OAuth2ProviderForServiceAccounts(config);

            Assert.AreEqual(provider.ClientId, config.OAuth2ClientId);
            Assert.AreEqual(provider.ClientSecret, config.OAuth2ClientSecret);
            Assert.AreEqual(provider.AccessToken, config.OAuth2AccessToken);
            Assert.AreEqual(provider.Scope, config.OAuth2Scope);
            Assert.AreEqual(provider.ServiceAccountEmail, config.OAuth2ServiceAccountEmail);
            Assert.AreEqual(provider.PrnEmail, config.OAuth2PrnEmail);
            Assert.IsNullOrEmpty(provider.JwtCertificatePath);
            Assert.IsNullOrEmpty(provider.JwtCertificatePassword);
            Assert.AreEqual(provider.JwtPrivateKey, config.OAuth2PrivateKey);
        }
        public void TestGenerateAccessTokenForServiceAccountsWithSecretsFile()
        {
            MockAppConfig config = new Mocks.MockAppConfig();

            config.MockReadSettings(tblSettingsWithSecretJson);
            provider = new OAuth2ProviderForServiceAccounts(config);

            TestUtils.ValidateRequiredParameters(provider, new string[] { "ServiceAccountEmail",
                                                                          "Scope", "JwtPrivateKey" },
                                                 delegate() {
                provider.GenerateAccessTokenForServiceAccount();
            }
                                                 );
            oauth2RequestInterceptor.RequestType =
                OAuth2RequestInterceptor.OAuth2RequestType.FetchAccessTokenForServiceAccount;
            WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri,
                                                                           WebHeaderCollection headers, String body) {
                Assert.AreEqual(SERVICE_ACCOUNT_REQUEST_WITH_SECRETS_FILE, body);
            };
            try {
                oauth2RequestInterceptor.BeforeSendResponse += callback;
                provider.GenerateAccessTokenForServiceAccount();
                Assert.AreEqual(provider.AccessToken, OAuth2RequestInterceptor.TEST_ACCESS_TOKEN);
                Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE);
                Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN);
            } finally {
                oauth2RequestInterceptor.BeforeSendResponse -= callback;
            }
        }
        public void TestGenerateAccessTokenForServiceAccounts()
        {
            MockAppConfig config = new Mocks.MockAppConfig();

            config.MockReadSettings(dictSettings);
            provider = new OAuth2ProviderForServiceAccounts(config)
            {
                HttpClientFactory = mockHttpClientFactory,
                Clock             = mockClock
            };

            TestUtils.ValidateRequiredParameters(provider, new string[] { "ServiceAccountEmail",
                                                                          "Scope", "JwtPrivateKey" },
                                                 delegate() {
                provider.GenerateAccessTokenForServiceAccount();
            }
                                                 );

            provider.GenerateAccessTokenForServiceAccount();
            Assert.AreEqual(mockHttpClientFactory.messageHandler.LastRequest, SERVICE_ACCOUNT_REQUEST);
            Assert.AreEqual(provider.AccessToken, OAuth2RequestInterceptor.TEST_ACCESS_TOKEN);
            Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE);
            Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN);

            // Test no impersonation with empty string.
            config.SetPropertyFieldForTests("OAuth2PrnEmail", "");
            provider.GenerateAccessTokenForServiceAccount();
            Assert.AreEqual(mockHttpClientFactory.messageHandler.LastRequest,
                            SERVICE_ACCOUNT_REQUEST_NO_PRN);

            // Test no impersonation with null.
            config.SetPropertyFieldForTests("OAuth2PrnEmail", null);
            provider.GenerateAccessTokenForServiceAccount();
            Assert.AreEqual(mockHttpClientFactory.messageHandler.LastRequest,
                            SERVICE_ACCOUNT_REQUEST_NO_PRN);
        }