Skip to content

Commit

Permalink
test: add credentials test
Browse files Browse the repository at this point in the history
Add tests for multiple target clusters, we had issues with connecting
different camunda cloud clusters
  • Loading branch information
ChrisKujawa committed May 8, 2021
1 parent e662190 commit 8a5cb03
Showing 1 changed file with 75 additions and 10 deletions.
85 changes: 75 additions & 10 deletions Client.UnitTests/CamundaCloudTokenProviderTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
Expand All @@ -21,14 +22,23 @@ public class CamundaCloudTokenProviderTest
private static long ExpiresIn { get; set; }
private static string Token { get; set; }

private static string _requestUri;
private static string _clientId;
private static string _clientSecret;
private static string _audience;

[SetUp]
public void Init()
{
_requestUri = "https://local.de";
_clientId = "ID";
_clientSecret = "SECRET";
_audience = "AUDIENCE";
TokenProvider = new CamundaCloudTokenProviderBuilder()
.UseAuthServer("https://local.de")
.UseClientId("ID")
.UseClientSecret("SECRET")
.UseAudience("AUDIENCE")
.UseAuthServer(_requestUri)
.UseClientId(_clientId)
.UseClientSecret(_clientSecret)
.UseAudience(_audience)
.Build();

MessageHandlerStub = new HttpMessageHandlerStub();
Expand All @@ -50,16 +60,17 @@ private class HttpMessageHandlerStub : HttpMessageHandler
{
public int RequestCount { get; set; }
private bool _disposed = false;

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
CheckDisposed();
Assert.AreEqual(request.RequestUri, "https://local.de");
Assert.AreEqual(request.RequestUri, _requestUri);
var content = await request.Content.ReadAsStringAsync();
var jsonObject = JObject.Parse(content);
Assert.AreEqual((string)jsonObject["client_id"], "ID");
Assert.AreEqual((string)jsonObject["client_secret"], "SECRET");
Assert.AreEqual((string)jsonObject["audience"], "AUDIENCE");
Assert.AreEqual((string)jsonObject["client_id"], _clientId);
Assert.AreEqual((string)jsonObject["client_secret"], _clientSecret);
Assert.AreEqual((string)jsonObject["audience"], _audience);

RequestCount++;
var responseMessage = new HttpResponseMessage(HttpStatusCode.OK)
Expand Down Expand Up @@ -117,8 +128,39 @@ public async Task ShouldStoreCredentials()
Assert.AreEqual(1, files.Length);
var tokenFile = files[0];
var content = File.ReadAllText(tokenFile);
var fileToken = JsonConvert.DeserializeObject<CamundaCloudTokenProvider.AccessToken>(content);
Assert.AreEqual(token, fileToken.Token);
var credentials = JsonConvert.DeserializeObject<Dictionary<string, CamundaCloudTokenProvider.AccessToken>>(content);
Assert.AreEqual(credentials["AUDIENCE"].Token, token);
}

[Test]
public async Task ShouldStoreMultipleCredentials()
{
// given
await TokenProvider.GetAccessTokenForRequestAsync();
var otherProvider = new CamundaCloudTokenProviderBuilder()
.UseAuthServer(_requestUri)
.UseClientId(_clientId = "OTHERID")
.UseClientSecret(_clientSecret = "OTHERSECRET")
.UseAudience(_audience = "OTHER_AUDIENCE")
.Build();
otherProvider.SetHttpMessageHandler(MessageHandlerStub);
otherProvider.TokenStoragePath = TokenStoragePath;
Token = "OTHER_TOKEN";

// when
var token = await otherProvider.GetAccessTokenForRequestAsync();

// then
Assert.AreEqual("OTHER_TOKEN", token);
var files = Directory.GetFiles(TokenStoragePath);
Assert.AreEqual(1, files.Length);
var tokenFile = files[0];
var content = File.ReadAllText(tokenFile);
var credentials = JsonConvert.DeserializeObject<Dictionary<string, CamundaCloudTokenProvider.AccessToken>>(content);

Assert.AreEqual(credentials.Count, 2);
Assert.AreEqual(token, credentials["OTHER_AUDIENCE"].Token);
Assert.AreEqual("REQUESTED_TOKEN", credentials["AUDIENCE"].Token);
}

[Test]
Expand Down Expand Up @@ -199,6 +241,29 @@ public async Task ShouldUseCachedFile()
Assert.AreEqual(0, MessageHandlerStub.RequestCount);
}

[Test]
public async Task ShouldNotUseCachedFileForOtherAudience()
{
// given
Token = "STORED_TOKEN";
await TokenProvider.GetAccessTokenForRequestAsync();
var otherProvider = new CamundaCloudTokenProviderBuilder()
.UseAuthServer(_requestUri)
.UseClientId(_clientId = "OTHERID")
.UseClientSecret(_clientSecret = "OTHERSECRET")
.UseAudience(_audience = "OTHER_AUDIENCE")
.Build();
otherProvider.SetHttpMessageHandler(MessageHandlerStub);
otherProvider.TokenStoragePath = TokenStoragePath;
Token = "OTHER_TOKEN";

// when
var token = await otherProvider.GetAccessTokenForRequestAsync();

// then
Assert.AreEqual("OTHER_TOKEN", token);
}

[Test]
public async Task ShouldRequestWhenCachedFileExpired()
{
Expand Down

0 comments on commit 8a5cb03

Please sign in to comment.