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()
 {
     TestUtils.ValidateRequiredParameters(provider, new string[] { "ServiceAccountEmail",
                                                                   "Scope", "PrnEmail", "JwtCertificatePath", "JwtCertificatePassword" },
                                          delegate() {
         provider.GenerateAccessTokenForServiceAccount();
     }
                                          );
     oauth2RequestInterceptor.RequestType =
         OAuth2RequestInterceptor.OAuth2RequestType.FetchAccessTokenForServiceAccount;
     WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri,
                                                                    WebHeaderCollection headers, String body) {
         Assert.AreEqual(SERVICE_ACCOUNT_REQUEST, 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);
        }