| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using Google; |
| | using Google.Apis.Auth.OAuth2; |
| | using Google.Apis.BigtableAdmin.v2; |
| | using Google.Apis.BigtableAdmin.v2.Data; |
| | using Google.Apis.Services; |
| | using Google.Apis.Storage.v1; |
| | using Google.Apis.Storage.v1.Data; |
| | using IntegrationTests.Utils; |
| | using System; |
| | using System.Net; |
| | using System.Net.Http; |
| | using System.Net.Http.Headers; |
| | using System.Threading.Tasks; |
| | using Xunit; |
| |
|
| | namespace IntegrationTests |
| | { |
| | public class ServiceAccountTests |
| | { |
| | [Fact] |
| | public async Task ServiceCredential() |
| | { |
| | |
| | |
| |
|
| | GoogleCredential credential = Helper.GetServiceCredential().CreateScoped(StorageService.Scope.DevstorageFullControl); |
| |
|
| | StorageService client = new StorageService(new BaseClientService.Initializer |
| | { |
| | HttpClientInitializer = credential, |
| | ApplicationName = "IntegrationTest" |
| | }); |
| |
|
| | |
| | var ok = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync(); |
| | Assert.NotNull(ok); |
| |
|
| | |
| | Buckets buckets = client.Buckets.List(Helper.GetProjectId()).Execute(); |
| |
|
| | |
| | Assert.NotNull(buckets.Items); |
| | } |
| |
|
| | [Fact] |
| | public async Task JwtAccessToken_Http() |
| | { |
| | |
| |
|
| | ServiceAccountCredential credential = Helper.GetServiceCredential().CreateScoped().UnderlyingCredential as ServiceAccountCredential; |
| | string token = await credential.GetAccessTokenForRequestAsync("https://bigtableadmin.googleapis.com/"); |
| |
|
| | using HttpClient client = new HttpClient(); |
| | HttpRequestMessage request = new HttpRequestMessage(); |
| | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); |
| | request.RequestUri = new Uri($"https://bigtableadmin.googleapis.com/v2/projects/{Helper.GetProjectId()}/instances"); |
| |
|
| | HttpResponseMessage response = await client.SendAsync(request); |
| | Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| | } |
| |
|
| | [Theory] |
| | [InlineData(true, null)] |
| | [InlineData(false, null)] |
| | [InlineData(true, new[] { "https://www.googleapis.com/auth/bigtable.admin" })] |
| | public void JwtAccessToken_RestClient(bool useJwtAccessWithScopes, string[] scopes) |
| | { |
| | |
| |
|
| | GoogleCredential credential = useJwtAccessWithScopes |
| | ? Helper.GetServiceCredentialWithJwtFlag().CreateScoped(scopes) |
| | : Helper.GetServiceCredential().CreateScoped(scopes); |
| |
|
| | BigtableAdminService service = new BigtableAdminService(new BaseClientService.Initializer |
| | { |
| | HttpClientInitializer = credential |
| | }); |
| | |
| | ListInstancesResponse response = service.Projects.Instances.List($"projects/{Helper.GetProjectId()}").Execute(); |
| |
|
| | Assert.NotNull(response); |
| | } |
| |
|
| | [Fact] |
| | public void JwtAccessToken_WrongScope_RestClient() |
| | { |
| | |
| |
|
| | GoogleCredential credential = Helper.GetServiceCredentialWithJwtFlag().CreateScoped(new[] { "https://www.googleapis.com/auth/admob.readonly" }); |
| |
|
| | BigtableAdminService service = new BigtableAdminService(new BaseClientService.Initializer |
| | { |
| | HttpClientInitializer = credential |
| | }); |
| |
|
| | string expectedError = "Request had invalid authentication credentials."; |
| |
|
| | var exception = Assert.Throws<GoogleApiException>(() => |
| | service.Projects.Instances.List($"projects/{Helper.GetProjectId()}").Execute()); |
| | Assert.Contains(expectedError, exception.Message); |
| | } |
| | } |
| | } |
| |
|