Skip to content

Commit

Permalink
test: adjust IT to use old token provider
Browse files Browse the repository at this point in the history
Use CamundaCloudTokenProvider with OAuth SM test
Refactor a bit the ZeebeIntegrationTestHelper, remove booleans and make it more explicit
  • Loading branch information
ChrisKujawa committed Dec 15, 2023
1 parent 78ae3e2 commit 8620d5c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
8 changes: 5 additions & 3 deletions Client.IntegrationTests/OAuthIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Client.IntegrationTests;
[TestFixture]
public class OAuthIntegrationTest
{
private readonly ZeebeIntegrationTestHelper testHelper = ZeebeIntegrationTestHelper.Latest(true);
private readonly ZeebeIntegrationTestHelper testHelper = ZeebeIntegrationTestHelper.Latest().WithIdentity();

[OneTimeSetUp]
public async Task Setup()
Expand All @@ -24,7 +24,8 @@ public async Task Stop()
[Test]
public async Task ShouldSendRequestAndNotFailingWithAuthenticatedClient()
{
var topology = await testHelper.CreateAuthenticatedZeebeClient().TopologyRequest().Send();
var authenticatedZeebeClient = testHelper.CreateAuthenticatedZeebeClient();
var topology = await authenticatedZeebeClient.TopologyRequest().Send();
var gatewayVersion = topology.GatewayVersion;
Assert.AreEqual(ZeebeIntegrationTestHelper.LatestVersion, gatewayVersion);

Expand All @@ -36,11 +37,12 @@ public async Task ShouldSendRequestAndNotFailingWithAuthenticatedClient()
}

[Test]
public async Task ShouldFailWithUnauthenticatedClient()
public Task ShouldFailWithUnauthenticatedClient()
{
Assert.ThrowsAsync<RpcException>(code: async () =>
{
await testHelper.CreateZeebeClient().TopologyRequest().Send();
});
return Task.CompletedTask;
}
}
41 changes: 27 additions & 14 deletions Client.IntegrationTests/ZeebeIntegrationTestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
Expand All @@ -26,24 +27,23 @@ public class ZeebeIntegrationTestHelper

private readonly string version;
private readonly string audience;
private readonly bool withIdentity;
private bool withIdentity;
private int count = 1;
public readonly ILoggerFactory LoggerFactory;
private IContainer postgresContainer;
private IContainer keycloakContainer;
private IContainer identityContainer;

private ZeebeIntegrationTestHelper(string version, bool withIdentity = false)
private ZeebeIntegrationTestHelper(string version)
{
this.version = version;
this.withIdentity = withIdentity;
audience = Guid.NewGuid().ToString();
LoggerFactory = new NLogLoggerFactory();
}

public static ZeebeIntegrationTestHelper Latest(bool withIdentity = false)
public static ZeebeIntegrationTestHelper Latest()
{
return new ZeebeIntegrationTestHelper(LatestVersion, withIdentity);
return new ZeebeIntegrationTestHelper(LatestVersion);
}

public ZeebeIntegrationTestHelper WithPartitionCount(int count)
Expand All @@ -57,6 +57,12 @@ public static ZeebeIntegrationTestHelper OfVersion(string version)
return new ZeebeIntegrationTestHelper(version);
}

public ZeebeIntegrationTestHelper WithIdentity()
{
withIdentity = true;
return this;
}

public async Task<IZeebeClient> SetupIntegrationTest()
{
TestcontainersSettings.Logger = LoggerFactory.CreateLogger<ZeebeIntegrationTestHelper>();
Expand All @@ -74,11 +80,11 @@ public async Task<IZeebeClient> SetupIntegrationTest()

identityContainer = CreateIdentityContainer(network);
await identityContainer.StartAsync();
zeebeContainer = CreateZeebeContainer(true, network);
zeebeContainer = CreateZeebeContainer(network);
}
else
{
zeebeContainer = CreateZeebeContainer(false);
zeebeContainer = CreateZeebeContainer();
}

await zeebeContainer.StartAsync();
Expand Down Expand Up @@ -114,14 +120,15 @@ public async Task TearDownIntegrationTest()
zeebeContainer = null;
}

private IContainer CreateZeebeContainer(bool withKeycloak, INetwork network = null)
private IContainer CreateZeebeContainer(INetwork network = null)
{
var containerBuilder = new ContainerBuilder()
.WithImage(new DockerImage("camunda", "zeebe", version))
.WithPortBinding(ZeebePort, true)
.WithOutputConsumer(Consume.RedirectStdoutAndStderrToConsole())
.WithEnvironment("ZEEBE_BROKER_CLUSTER_PARTITIONSCOUNT", count.ToString());

if (withKeycloak)
if (withIdentity)
{
containerBuilder = containerBuilder.WithEnvironment("ZEEBE_BROKER_GATEWAY_SECURITY_AUTHENTICATION_MODE",
"identity")
Expand Down Expand Up @@ -152,6 +159,7 @@ private IContainer CreatePostgresContainer(INetwork network)
.WithEnvironment("POSTGRES_PASSWORD", "#3]O?4RGj)DE7Z!9SA5")
.WithNetwork(network)
.WithAutoRemove(true)
.WithOutputConsumer(Consume.RedirectStdoutAndStderrToConsole())
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(5432));

return containerBuilder.Build();
Expand All @@ -160,7 +168,7 @@ private IContainer CreatePostgresContainer(INetwork network)
private IContainer CreateIdentityContainer(INetwork network)
{
var containerBuilder = new ContainerBuilder()
.WithImage(new DockerImage("camunda", "identity", "8.3.0"))
.WithImage(new DockerImage("camunda", "identity", version)) // identity and zeebe will have the same version
.WithName("integration-identity")
.WithExposedPort("8084")
.WithPortBinding("8084", "8084")
Expand All @@ -183,6 +191,7 @@ private IContainer CreateIdentityContainer(INetwork network)
.WithEnvironment("KEYCLOAK_CLIENTS_0_PERMISSIONS_0_DEFINITION", "write:*")
.WithEnvironment("RESOURCE_PERMISSIONS_ENABLED", "false")
.WithAutoRemove(true)
.WithOutputConsumer(Consume.RedirectStdoutAndStderrToConsole())
.WithNetwork(network);


Expand All @@ -202,6 +211,7 @@ private IContainer CreateKeyCloakContainer(INetwork network)
.WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", "admin")
.WithNetwork(network)
.WithAutoRemove(true)
.WithOutputConsumer(Consume.RedirectStdoutAndStderrToConsole())
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPort(8080).ForPath("/auth").ForStatusCode(HttpStatusCode.OK)));

Expand Down Expand Up @@ -231,9 +241,12 @@ public IZeebeClient CreateAuthenticatedZeebeClient()
.UseGatewayAddress(host)
.UseTransportEncryption()
.AllowUntrustedCertificates()
.UseAccessTokenSupplier(new OAuth2TokenProvider(
$"http://{keycloakContainer.Hostname}:{keycloakContainer.GetMappedPublicPort(8080)}/auth/realms/camunda-platform/protocol/openid-connect/token",
"zeebe", "sddh123865WUS)(1%!", audience)).Build();
.UseAccessTokenSupplier(
new CamundaCloudTokenProviderBuilder()
.UseAuthServer($"http://{keycloakContainer.Hostname}:{keycloakContainer.GetMappedPublicPort(8080)}/auth/realms/camunda-platform/protocol/openid-connect/token")
.UseClientId("zeebe")
.UseClientSecret("sddh123865WUS)(1%!")
.UseAudience(audience).Build()).Build();
}

private async Task AwaitBrokerReadiness()
Expand All @@ -249,7 +262,7 @@ private async Task AwaitBrokerReadiness()
{
try
{
var topology = await client.TopologyRequest().Send(TimeSpan.FromSeconds(1));
var topology = await zeebeClient.TopologyRequest().Send(TimeSpan.FromSeconds(1));
ready = topology.Brokers[0].Partitions.Count >= count;
topologyErrorLogger.LogInformation("Requested topology [retries {Retries}], got '{Topology}'", retries, topology);
}
Expand Down

0 comments on commit 8620d5c

Please sign in to comment.