public void FromConfig_ServiceAccountCredential()
        {
            IConfiguration           config = BuildConfigurationForCredential(DummyServiceAccountCredentialConfigContents);
            JsonCredentialParameters credentialParameters = config.GetSection("GoogleCredential").Get <JsonCredentialParameters>();
            GoogleCredential         credential           = GoogleCredential.FromJsonParameters(credentialParameters);

            var serviceCred = Assert.IsType <ServiceAccountCredential>(credential.UnderlyingCredential);

            Assert.True(credential.IsCreateScopedRequired);
            Assert.Equal("CLIENT_EMAIL", serviceCred.Id);
            Assert.Equal("PROJECT_ID", serviceCred.ProjectId);
        }
        public void FromConfig_UserCredential()
        {
            IConfiguration           config = BuildConfigurationForCredential(DummyUserCredentialConfigContents);
            JsonCredentialParameters credentialParameters = config.GetSection("GoogleCredential").Get <JsonCredentialParameters>();
            GoogleCredential         credential           = GoogleCredential.FromJsonParameters(credentialParameters);

            var userCred = Assert.IsType <UserCredential>(credential.UnderlyingCredential);

            Assert.False(credential.IsCreateScopedRequired);
            Assert.Equal("REFRESH_TOKEN", userCred.Token.RefreshToken);
            var flow = (GoogleAuthorizationCodeFlow)userCred.Flow;

            Assert.Equal("CLIENT_ID", flow.ClientSecrets.ClientId);
            Assert.Equal("CLIENT_SECRET", flow.ClientSecrets.ClientSecret);
            Assert.Equal("PROJECT_ID", flow.ProjectId);
            Assert.Equal("QUOTA_PROJECT", userCred.QuotaProject);
        }
        public void FromConfig_WrongTypesUserCredential()
        {
            IConfiguration           config = BuildConfigurationForCredential(WrongTypesUserCredentialConfigContents);
            JsonCredentialParameters credentialParameters = config.GetSection("GoogleCredential").Get <JsonCredentialParameters>();

            // These are parsed as string. Nothing we can do our side.
            Assert.Equal("1", credentialParameters.ClientId);
            Assert.Equal("2", credentialParameters.ClientSecret);
            Assert.Equal("True", credentialParameters.RefreshToken);
            Assert.Equal("False", credentialParameters.ProjectId);

            GoogleCredential credential = GoogleCredential.FromJsonParameters(credentialParameters);

            var userCred = Assert.IsType <UserCredential>(credential.UnderlyingCredential);

            Assert.False(credential.IsCreateScopedRequired);
            Assert.Equal("True", userCred.Token.RefreshToken);
            var flow = (GoogleAuthorizationCodeFlow)userCred.Flow;

            Assert.Equal("1", flow.ClientSecrets.ClientId);
            Assert.Equal("2", flow.ClientSecrets.ClientSecret);
            Assert.Equal("False", flow.ProjectId);
            Assert.Equal("QUOTA_PROJECT", userCred.QuotaProject);
        }
        public void FromConfig_NotAJson()
        {
            IConfiguration config = new ConfigurationBuilder().AddInMemoryCollection(new KeyValuePair <string, string>[]
            {
                new KeyValuePair <string, string>("GoogleCredential", "NotAJson")
            }).Build();

            JsonCredentialParameters credentialParameters = config.GetSection("GoogleCredential").Get <JsonCredentialParameters>();
            ArgumentNullException    ex = Assert.Throws <ArgumentNullException>(() => GoogleCredential.FromJsonParameters(credentialParameters));

            Assert.Equal("credentialParameters", ex.ParamName);
        }
        public void FromConfig_IncompleteUserCredential()
        {
            IConfiguration            config = BuildConfigurationForCredential(IncompleteUserCredentialConfigContents);
            JsonCredentialParameters  credentialParameters = config.GetSection("GoogleCredential").Get <JsonCredentialParameters>();
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => GoogleCredential.FromJsonParameters(credentialParameters));

            Assert.Contains("does not represent a valid user credential", ex.Message);
        }
        public void FromConfig_Untyped()
        {
            IConfiguration            config = BuildConfigurationForCredential(UntypedCredentialConfigContents);
            JsonCredentialParameters  credentialParameters = config.GetSection("GoogleCredential").Get <JsonCredentialParameters>();
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => GoogleCredential.FromJsonParameters(credentialParameters));

            Assert.Contains("Unrecognized credential type", ex.Message);
        }