Skip to content

Commit

Permalink
Verify claims (#18)
Browse files Browse the repository at this point in the history
* Verify send claims

* Verify receive claims

* Restructure

* Removing nullable enable since for tests that doesn't really make sense

* More connection string tests

---------

Co-authored-by: Daniel Marbach <danielmarbach@users.noreply.github.com>
  • Loading branch information
danielmarbach and danielmarbach committed May 29, 2024
1 parent b459d32 commit 2eb3d24
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
50 changes: 50 additions & 0 deletions src/tests/ConnectionStringTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
using NUnit.Framework;

Expand All @@ -12,11 +13,60 @@ public async Task Should_establish_connection_with_manage_rights()
var client = new ServiceBusAdministrationClient(Environment.GetEnvironmentVariable("ASBConnectionString"));
await client.CreateQueueAsync("testqueue");
}

[Test]
public async Task Should_have_send_claims_with_manage_rights()
{
await CreateQueueWithManageRightsIfNotExists();

await using var client = new ServiceBusClient(Environment.GetEnvironmentVariable("ASBConnectionString"));
var sender = client.CreateSender("testqueue");
await sender.SendMessageAsync(new ServiceBusMessage(nameof(Should_have_send_claims_with_manage_rights)));
}

[Test]
public async Task Should_have_receive_claims_with_manage_rights()
{
await CreateQueueWithManageRightsIfNotExists();

await using var client = new ServiceBusClient(Environment.GetEnvironmentVariable("ASBConnectionString"));
var receiver = client.CreateReceiver("testqueue");
await receiver.ReceiveMessageAsync(TimeSpan.FromMilliseconds(500));
}

async Task CreateQueueWithManageRightsIfNotExists()
{
var serviceBusAdminClient = new ServiceBusAdministrationClient(Environment.GetEnvironmentVariable("ASBConnectionString"));
if (!await serviceBusAdminClient.QueueExistsAsync("testqueue"))
{
await serviceBusAdminClient.CreateQueueAsync("testqueue");
}
}

[Test]
public void Should_establish_connection_without_manage_rights()
{
var client = new ServiceBusAdministrationClient(Environment.GetEnvironmentVariable("ASBConnectionString_Restricted"));
Assert.ThrowsAsync<UnauthorizedAccessException>(async () => await client.CreateQueueAsync("testqueue"));
}

[Test]
public async Task Should_have_send_claims_without_manage_rights()
{
await CreateQueueWithManageRightsIfNotExists();

await using var client = new ServiceBusClient(Environment.GetEnvironmentVariable("ASBConnectionString_Restricted"));
var sender = client.CreateSender("testqueue");
await sender.SendMessageAsync(new ServiceBusMessage(nameof(Should_have_send_claims_without_manage_rights)));
}

[Test]
public async Task Should_have_receive_claims_without_manage_rights()
{
await CreateQueueWithManageRightsIfNotExists();

await using var client = new ServiceBusClient(Environment.GetEnvironmentVariable("ASBConnectionString_Restricted"));
var receiver = client.CreateReceiver("testqueue");
await receiver.ReceiveMessageAsync(TimeSpan.FromMilliseconds(500));
}
}
48 changes: 41 additions & 7 deletions src/tests/DefaultCredentialTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Azure.Core.Diagnostics;
using Azure.Identity;
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
Expand All @@ -9,14 +8,49 @@ namespace Tests;
[TestFixture]
public class DefaultCredentialTests
{
[Test]
public async Task Should_establish_connection_with_manage_rights()
string fullyQualifiedNamespace;

[SetUp]
public void Setup()
{
var connectionString = Environment.GetEnvironmentVariable("ASBConnectionString");

var properties = ServiceBusConnectionStringProperties.Parse(connectionString);

var client = new ServiceBusAdministrationClient(properties.FullyQualifiedNamespace, new DefaultAzureCredential());
await client.CreateQueueAsync("testqueuedefault");
fullyQualifiedNamespace = ServiceBusConnectionStringProperties.Parse(connectionString).FullyQualifiedNamespace;
}

[Test]
public async Task Should_establish_connection_with_manage_rights()
{
var serviceBusAdminClient = new ServiceBusAdministrationClient(fullyQualifiedNamespace, new DefaultAzureCredential());
await serviceBusAdminClient.CreateQueueAsync("testqueuedefault");
}

[Test]
public async Task Should_have_send_claims()
{
await CreateQueueIfNotExists();

await using var client = new ServiceBusClient(fullyQualifiedNamespace, new DefaultAzureCredential());
var sender = client.CreateSender("testqueuedefault");
await sender.SendMessageAsync(new ServiceBusMessage(nameof(Should_have_send_claims)));
}

[Test]
public async Task Should_have_receive_claims()
{
await CreateQueueIfNotExists();

await using var client = new ServiceBusClient(fullyQualifiedNamespace, new DefaultAzureCredential());
var receiver = client.CreateReceiver("testqueuedefault");
await receiver.ReceiveMessageAsync(TimeSpan.FromMilliseconds(500));
}

async Task CreateQueueIfNotExists()
{
var serviceBusAdminClient = new ServiceBusAdministrationClient(fullyQualifiedNamespace, new DefaultAzureCredential());
if (!await serviceBusAdminClient.QueueExistsAsync("testqueuedefault"))
{
await serviceBusAdminClient.CreateQueueAsync("testqueuedefault");
}
}
}
1 change: 0 additions & 1 deletion src/tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 2eb3d24

Please sign in to comment.