|
| 1 | +using Microsoft.Extensions.Configuration; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Microsoft.Extensions.Options; |
| 4 | +using Moq; |
| 5 | +using Moq.Protected; |
| 6 | +using Newtonsoft.Json; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Text; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace Autodesk.Forge.Core.Test |
| 15 | +{ |
| 16 | + public class TestAPSConfiguration |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Tests using APS__ClientId and APS__ClientSecret environment variables dotnet core style |
| 20 | + /// </summary> |
| 21 | + [Fact] |
| 22 | + public void TestAPSConfigFromEnvironmentVariables_DoubleUnderscoreFormat() |
| 23 | + { |
| 24 | + Environment.SetEnvironmentVariable("APS__ClientId", "bla"); |
| 25 | + Environment.SetEnvironmentVariable("APS__ClientSecret", "blabla"); |
| 26 | + var configuration = new ConfigurationBuilder() |
| 27 | + .AddEnvironmentVariables() |
| 28 | + .Build(); |
| 29 | + |
| 30 | + var services = new ServiceCollection(); |
| 31 | + services.AddForgeService(configuration); |
| 32 | + var serviceProvider = services.BuildServiceProvider(); |
| 33 | + |
| 34 | + var config = serviceProvider.GetRequiredService<IOptions<ForgeConfiguration>>(); |
| 35 | + Assert.Equal("bla", config.Value.ClientId); |
| 36 | + Assert.Equal("blabla", config.Value.ClientSecret); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Tests using APS_CLIENT_ID and APS_CLIENT_SECRET environment variables |
| 41 | + /// </summary> |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void TestAPSConfigFromEnvironmentVariables_UnderscoreFormat() |
| 45 | + { |
| 46 | + Environment.SetEnvironmentVariable("APS_CLIENT_ID", "bla"); |
| 47 | + Environment.SetEnvironmentVariable("APS_CLIENT_SECRET", "blabla"); |
| 48 | + var configuration = new ConfigurationBuilder() |
| 49 | + .AddAPSAlternativeEnvironmentVariables() |
| 50 | + .Build(); |
| 51 | + var services = new ServiceCollection(); |
| 52 | + services.AddForgeService(configuration); |
| 53 | + var serviceProvider = services.BuildServiceProvider(); |
| 54 | + var config = serviceProvider.GetRequiredService<IOptions<ForgeConfiguration>>(); |
| 55 | + Assert.Equal("bla", config.Value.ClientId); |
| 56 | + Assert.Equal("blabla", config.Value.ClientSecret); |
| 57 | + |
| 58 | + } |
| 59 | + /// <summary> |
| 60 | + /// Tests loading APS configuration values from JSON with ClientId and ClientSecret |
| 61 | + /// </summary> |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void TestAPSConfigFromJson() |
| 65 | + { |
| 66 | + var json = @" |
| 67 | + { |
| 68 | + ""APS"" : { |
| 69 | + ""ClientId"" : ""bla"", |
| 70 | + ""ClientSecret"" : ""blabla"" |
| 71 | + } |
| 72 | + }"; |
| 73 | + var configuration = new ConfigurationBuilder() |
| 74 | + .AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) |
| 75 | + .Build(); |
| 76 | + |
| 77 | + var services = new ServiceCollection(); |
| 78 | + services.AddForgeService(configuration); |
| 79 | + var serviceProvider = services.BuildServiceProvider(); |
| 80 | + |
| 81 | + var config = serviceProvider.GetRequiredService<IOptions<ForgeConfiguration>>(); |
| 82 | + Assert.Equal("bla", config.Value.ClientId); |
| 83 | + Assert.Equal("blabla", config.Value.ClientSecret); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Tests loading APS configuration values from JSON with additional agent configurations |
| 88 | + /// </summary> |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void TestAPSConfigFromJsonWithAgents() |
| 92 | + { |
| 93 | + var json = @" |
| 94 | + { |
| 95 | + ""APS"" : { |
| 96 | + ""ClientId"" : ""bla"", |
| 97 | + ""ClientSecret"" : ""blabla"", |
| 98 | + ""Agents"" : { |
| 99 | + ""user1"" : { |
| 100 | + ""ClientId"" : ""user1-bla"", |
| 101 | + ""ClientSecret"" : ""user1-blabla"" |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + }"; |
| 106 | + var configuration = new ConfigurationBuilder() |
| 107 | + .AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) |
| 108 | + .Build(); |
| 109 | + |
| 110 | + var services = new ServiceCollection(); |
| 111 | + services.AddForgeService(configuration); |
| 112 | + var serviceProvider = services.BuildServiceProvider(); |
| 113 | + |
| 114 | + var config = serviceProvider.GetRequiredService<IOptions<ForgeConfiguration>>(); |
| 115 | + Assert.Equal("bla", config.Value.ClientId); |
| 116 | + Assert.Equal("blabla", config.Value.ClientSecret); |
| 117 | + Assert.Equal("user1-bla", config.Value.Agents["user1"].ClientId); |
| 118 | + Assert.Equal("user1-blabla", config.Value.Agents["user1"].ClientSecret); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Tests APS configuration for user agent "user1" and checks proper handling of authentication and request headers. |
| 123 | + /// </summary> |
| 124 | + [Fact] |
| 125 | + public async Task TestAPSUserAgent() |
| 126 | + { |
| 127 | + var json = @" |
| 128 | + { |
| 129 | + ""APS"" : { |
| 130 | + ""ClientId"" : ""bla"", |
| 131 | + ""ClientSecret"" : ""blabla"", |
| 132 | + ""Agents"" : { |
| 133 | + ""user1"" : { |
| 134 | + ""ClientId"" : ""user1-bla"", |
| 135 | + ""ClientSecret"" : ""user1-blabla"" |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + }"; |
| 140 | + var configuration = new ConfigurationBuilder() |
| 141 | + .AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))) |
| 142 | + .Build(); |
| 143 | + |
| 144 | + var sink = new Mock<HttpMessageHandler>(MockBehavior.Strict); |
| 145 | + var services = new ServiceCollection(); |
| 146 | + services.AddForgeService("user1", configuration).ConfigurePrimaryHttpMessageHandler(() => sink.Object); |
| 147 | + var serviceProvider = services.BuildServiceProvider(); |
| 148 | + var config = serviceProvider.GetRequiredService<IOptions<ForgeConfiguration>>().Value; |
| 149 | + var req = new HttpRequestMessage(); |
| 150 | + req.RequestUri = new Uri("http://example.com"); |
| 151 | + req.Options.Set(ForgeConfiguration.ScopeKey, "somescope"); |
| 152 | + |
| 153 | + string user = null; |
| 154 | + sink.Protected().As<HttpMessageInvoker>().Setup(o => o.SendAsync(It.Is<HttpRequestMessage>(r => r.RequestUri == config.AuthenticationAddress), It.IsAny<CancellationToken>())) |
| 155 | + .ReturnsAsync(new HttpResponseMessage() |
| 156 | + { |
| 157 | + Content = new StringContent(JsonConvert.SerializeObject(new Dictionary<string, string> { { "token_type", "Bearer" }, { "access_token", "blablabla" }, { "expires_in", "3" } })), |
| 158 | + StatusCode = System.Net.HttpStatusCode.OK |
| 159 | + }); |
| 160 | + sink.Protected().As<HttpMessageInvoker>().Setup(o => o.SendAsync(It.Is<HttpRequestMessage>(r => r.RequestUri == req.RequestUri), It.IsAny<CancellationToken>())) |
| 161 | + .Callback<HttpRequestMessage, CancellationToken>((r, ct) => |
| 162 | + { |
| 163 | + r.Options.TryGetValue(ForgeConfiguration.AgentKey, out user); |
| 164 | + }) |
| 165 | + .ReturnsAsync(new HttpResponseMessage() |
| 166 | + { |
| 167 | + StatusCode = System.Net.HttpStatusCode.OK |
| 168 | + }); |
| 169 | + |
| 170 | + |
| 171 | + var clientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>(); |
| 172 | + var client = clientFactory.CreateClient("user1"); |
| 173 | + var resp = await client.SendAsync(req, CancellationToken.None); |
| 174 | + |
| 175 | + sink.Protected().As<HttpMessageInvoker>().Verify(o => o.SendAsync(It.Is<HttpRequestMessage>(r => r.RequestUri == config.AuthenticationAddress), It.IsAny<CancellationToken>()), Times.Once()); |
| 176 | + sink.Protected().As<HttpMessageInvoker>().Verify(o => o.SendAsync(It.Is<HttpRequestMessage>(r => r.RequestUri == req.RequestUri), It.IsAny<CancellationToken>()), Times.Once()); |
| 177 | + Assert.Equal("user1", user); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments