|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO.Abstractions; |
| 7 | +using System.IO.Abstractions.TestingHelpers; |
| 8 | +using Azure.DataApiBuilder.Auth; |
| 9 | +using Azure.DataApiBuilder.Service.Authorization; |
| 10 | +using Azure.DataApiBuilder.Service.Configurations; |
| 11 | +using Azure.DataApiBuilder.Service.Resolvers; |
| 12 | +using Azure.DataApiBuilder.Service.Services; |
| 13 | +using Azure.DataApiBuilder.Service.Services.MetadataProviders; |
| 14 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 15 | +using Microsoft.AspNetCore.TestHost; |
| 16 | +using Microsoft.Azure.Cosmos; |
| 17 | +using Microsoft.Extensions.DependencyInjection; |
| 18 | +using Microsoft.Extensions.Logging; |
| 19 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 20 | +using Moq; |
| 21 | + |
| 22 | +namespace Azure.DataApiBuilder.Service.Tests.CosmosTests |
| 23 | +{ |
| 24 | + [TestClass, TestCategory(TestCategory.COSMOSDBNOSQL)] |
| 25 | + public class CosmosClientTests : TestBase |
| 26 | + { |
| 27 | + [TestMethod] |
| 28 | + public void CosmosClientDefaultUserAgent() |
| 29 | + { |
| 30 | + CosmosClient client = _application.Services.GetService<CosmosClientProvider>().Client; |
| 31 | + // Validate results |
| 32 | + Assert.AreEqual(client.ClientOptions.ApplicationName, |
| 33 | + CosmosClientProvider.DEFAULT_APP_NAME); |
| 34 | + } |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void CosmosClientEnvUserAgent() |
| 38 | + { |
| 39 | + |
| 40 | + MockFileSystem fileSystem = new(new Dictionary<string, MockFileData>() |
| 41 | + { |
| 42 | + { @"../schema.gql", new MockFileData(TestBase.GRAPHQL_SCHEMA) } |
| 43 | + }); |
| 44 | + |
| 45 | + RuntimeConfigProvider runtimeConfigProvider = TestHelper.GetRuntimeConfigProvider(CosmosTestHelper.ConfigPath); |
| 46 | + ISqlMetadataProvider cosmosSqlMetadataProvider = new CosmosSqlMetadataProvider(runtimeConfigProvider, fileSystem); |
| 47 | + Mock<ILogger<AuthorizationResolver>> authorizationResolverLogger = new(); |
| 48 | + IAuthorizationResolver authorizationResolverCosmos = new AuthorizationResolver(runtimeConfigProvider, cosmosSqlMetadataProvider, authorizationResolverLogger.Object); |
| 49 | + string appName = "gql_dab_cosmos"; |
| 50 | + Environment.SetEnvironmentVariable(CosmosClientProvider.DAB_APP_NAME_ENV, appName); |
| 51 | + WebApplicationFactory<Startup> application = new WebApplicationFactory<Startup>() |
| 52 | + .WithWebHostBuilder(builder => |
| 53 | + { |
| 54 | + _ = builder.ConfigureTestServices(services => |
| 55 | + { |
| 56 | + services.AddSingleton<IFileSystem>(fileSystem); |
| 57 | + services.AddSingleton(runtimeConfigProvider); |
| 58 | + services.AddSingleton(authorizationResolverCosmos); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + CosmosClient client = application.Services.GetService<CosmosClientProvider>().Client; |
| 63 | + // Validate results |
| 64 | + Assert.AreEqual(client.ClientOptions.ApplicationName, appName); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | +} |
0 commit comments