|
| 1 | +using AwesomeAssertions; |
| 2 | +using Gotenberg.Sharp.API.Client.Domain.Builders; |
| 3 | +using Gotenberg.Sharp.API.Client.Domain.Settings; |
| 4 | +using Gotenberg.Sharp.API.Client.Extensions; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace GotenbergSharpClient.Tests; |
| 10 | + |
| 11 | +[TestFixture] |
| 12 | +public class BasicAuthTests |
| 13 | +{ |
| 14 | + private const string GotenbergUrl = "http://localhost:3000"; |
| 15 | + private const string TestUsername = "testuser"; |
| 16 | + private const string TestPassword = "testpass"; |
| 17 | + |
| 18 | + [Test] |
| 19 | + public async Task Client_WithBasicAuth_ShouldIncludeAuthorizationHeader() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + var services = new ServiceCollection(); |
| 23 | + |
| 24 | + services.AddOptions<GotenbergSharpClientOptions>() |
| 25 | + .Configure(options => |
| 26 | + { |
| 27 | + options.ServiceUrl = new Uri(GotenbergUrl); |
| 28 | + options.BasicAuthUsername = TestUsername; |
| 29 | + options.BasicAuthPassword = TestPassword; |
| 30 | + }); |
| 31 | + |
| 32 | + services.AddGotenbergSharpClient(); |
| 33 | + |
| 34 | + var serviceProvider = services.BuildServiceProvider(); |
| 35 | + var client = serviceProvider.GetRequiredService<Gotenberg.Sharp.API.Client.GotenbergSharpClient>(); |
| 36 | + |
| 37 | + // Act - Create a simple HTML to PDF request |
| 38 | + var builder = new HtmlRequestBuilder() |
| 39 | + .AddDocument(doc => doc.SetBody("<html><body><h1>Basic Auth Test</h1></body></html>")); |
| 40 | + |
| 41 | + // Assert - This will fail if auth is not properly configured or Gotenberg rejects the request |
| 42 | + var result = await client.HtmlToPdfAsync(builder); |
| 43 | + |
| 44 | + result.Should().NotBeNull("Basic auth should be properly configured and accepted by Gotenberg"); |
| 45 | + result.Length.Should().BeGreaterThan(0); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + public async Task Client_WithoutBasicAuth_ShouldFailWhenGotenbergRequiresAuth() |
| 50 | + { |
| 51 | + // Arrange |
| 52 | + var services = new ServiceCollection(); |
| 53 | + |
| 54 | + services.AddOptions<GotenbergSharpClientOptions>() |
| 55 | + .Configure(options => |
| 56 | + { |
| 57 | + options.ServiceUrl = new Uri(GotenbergUrl); |
| 58 | + // Deliberately not setting BasicAuthUsername or BasicAuthPassword |
| 59 | + }); |
| 60 | + |
| 61 | + services.AddGotenbergSharpClient(); |
| 62 | + |
| 63 | + var serviceProvider = services.BuildServiceProvider(); |
| 64 | + var client = serviceProvider.GetRequiredService<Gotenberg.Sharp.API.Client.GotenbergSharpClient>(); |
| 65 | + |
| 66 | + // Act |
| 67 | + var builder = new HtmlRequestBuilder() |
| 68 | + .AddDocument(doc => doc.SetBody("<html><body><h1>No Auth Test</h1></body></html>")); |
| 69 | + |
| 70 | + // Assert - Should fail with 401 Unauthorized when Gotenberg requires auth |
| 71 | + var act = () => client.HtmlToPdfAsync(builder); |
| 72 | + |
| 73 | + await act.Should().ThrowAsync<Exception>("Gotenberg should reject requests without proper authentication"); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public async Task Client_WithInvalidCredentials_ShouldFailWithUnauthorized() |
| 78 | + { |
| 79 | + // Arrange |
| 80 | + var services = new ServiceCollection(); |
| 81 | + |
| 82 | + services.AddOptions<GotenbergSharpClientOptions>() |
| 83 | + .Configure(options => |
| 84 | + { |
| 85 | + options.ServiceUrl = new Uri(GotenbergUrl); |
| 86 | + options.BasicAuthUsername = "wronguser"; |
| 87 | + options.BasicAuthPassword = "wrongpassword"; |
| 88 | + }); |
| 89 | + |
| 90 | + services.AddGotenbergSharpClient(); |
| 91 | + |
| 92 | + var serviceProvider = services.BuildServiceProvider(); |
| 93 | + var client = serviceProvider.GetRequiredService<Gotenberg.Sharp.API.Client.GotenbergSharpClient>(); |
| 94 | + |
| 95 | + // Act |
| 96 | + var builder = new HtmlRequestBuilder() |
| 97 | + .AddDocument(doc => doc.SetBody("<html><body><h1>Invalid Auth Test</h1></body></html>")); |
| 98 | + |
| 99 | + // Assert - Should fail with 401 Unauthorized |
| 100 | + var act = () => client.HtmlToPdfAsync(builder); |
| 101 | + |
| 102 | + await act.Should().ThrowAsync<Exception>("Invalid credentials should be rejected"); |
| 103 | + } |
| 104 | + |
| 105 | + [Test] |
| 106 | + public void GotenbergSharpClientOptions_BasicAuthProperties_ShouldBeNullableAndOptional() |
| 107 | + { |
| 108 | + // Arrange & Act |
| 109 | + var options = new GotenbergSharpClientOptions(); |
| 110 | + |
| 111 | + // Assert |
| 112 | + options.BasicAuthUsername.Should().BeNull("BasicAuthUsername should be nullable and default to null"); |
| 113 | + options.BasicAuthPassword.Should().BeNull("BasicAuthPassword should be nullable and default to null"); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void GotenbergSharpClientOptions_WithBasicAuthSet_ShouldRetainValues() |
| 118 | + { |
| 119 | + // Arrange |
| 120 | + var options = new GotenbergSharpClientOptions |
| 121 | + { |
| 122 | + BasicAuthUsername = TestUsername, |
| 123 | + BasicAuthPassword = TestPassword |
| 124 | + }; |
| 125 | + |
| 126 | + // Assert |
| 127 | + options.BasicAuthUsername.Should().Be(TestUsername); |
| 128 | + options.BasicAuthPassword.Should().Be(TestPassword); |
| 129 | + } |
| 130 | +} |
0 commit comments