Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions src/Storage.MongoDB/BewitAuthorizationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,67 @@ public static void UseMongoPersistence(
this BewitPayloadContext context,
MongoNonceOptions options)
{
options.Validate();

if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

options.Validate(requireConnectionString: true);

context.SetRepository(() =>
{
var client = new MongoClient(options.ConnectionString);
IMongoDatabase database = client.GetDatabase(options.DatabaseName);
return new MongoNonceRepository(database, options);
var client = new MongoClient(GetRequiredConnectionString(options));
return client.GetRepository(options);
});
}

public static void UseMongoPersistence(
this BewitPayloadContext context,
IMongoClient client,
MongoNonceOptions options)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

options.Validate(requireConnectionString: false);

context.SetRepository(() => client.GetRepository(options));
}

private static MongoNonceRepository GetRepository(this IMongoClient client, MongoNonceOptions options)
{
IMongoDatabase database = client.GetDatabase(options.DatabaseName);
return new MongoNonceRepository(database, options);
}

private static string GetRequiredConnectionString(MongoNonceOptions options)
{
if (string.IsNullOrWhiteSpace(options.ConnectionString))
{
throw new ArgumentException(
"Value cannot be null or whitespace.",
nameof(options.ConnectionString));
}

return options.ConnectionString;
}

}
}
14 changes: 10 additions & 4 deletions src/Storage.MongoDB/BewitMongoOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
using System;
using MongoDB.Driver.Core.Configuration;

namespace Bewit.Storage.MongoDB
{
internal static class BewitMongoOptionsExtensions
{
internal static void Validate(this MongoNonceOptions options)
internal static void Validate(
this MongoNonceOptions options,
bool requireConnectionString = true)
{
if (string.IsNullOrWhiteSpace(options.ConnectionString))
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

if (requireConnectionString && string.IsNullOrWhiteSpace(options.ConnectionString))
{
throw new ArgumentException(
"Value cannot be null or whitespace.",
nameof(ConnectionString));
nameof(options.ConnectionString));
}

if (string.IsNullOrWhiteSpace(options.DatabaseName))
Expand Down
11 changes: 5 additions & 6 deletions src/Storage.MongoDB/MongoNonceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

#nullable enable
namespace Bewit.Storage.MongoDB
{
public class MongoNonceOptions
{
/// <summary>
/// Connection string for the MongoDB instance.
/// Mandatory.
/// Required only when using connection-string based UseMongoPersistence overloads.
/// </summary>
public string ConnectionString { get; set; } //mandatory
public string? ConnectionString { get; set; }

/// <summary>
/// Name of the Database in the MongoDB instance.
/// Mandatory.
/// Mandatory for all UseMongoPersistence overloads.
/// </summary>
public string DatabaseName { get; set; } //mandatory

public string DatabaseName { get; set; } = null!;
/// <summary>
/// Name of the Bewit Collection in the Mongo database.
/// Optional.
Expand All @@ -27,7 +27,6 @@ public class MongoNonceOptions
/// </summary>
public NonceUsage NonceUsage { get; set; } = NonceUsage.OneTime;


public int RecordExpireAfterDays { get; set; } = 365 * 2;
}
}
66 changes: 66 additions & 0 deletions test/Storage.MongoDB.Tests/BewitAuthorizationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,71 @@ public void UseMongoPersistence_WithBuilderNull_ShouldThrowArgumentNullException
//Assert
useRepository.Should().Throw<ArgumentNullException>();
}

[Fact]
public void UseMongoPersistence_WithMongoClientAndMandatoryParameters_ShouldInitAndReturnMongoNonceRepository()
{
//Arrange
var builder = new BewitPayloadContext(typeof(object));
var client = new MongoClient(_mongoResource.ConnectionString);
IMongoCollection<Foo> collection = _mongoResource.CreateCollection<Foo>();

//Act
builder.UseMongoPersistence(
client,
new MongoNonceOptions
{
DatabaseName = collection.Database.DatabaseNamespace.DatabaseName
}
);

//Assert
builder.Should().Be(builder);
builder.CreateRepository.Should().NotBeNull();
}

[Fact]
public void UseMongoPersistence_WithMongoClientAndNullContext_ShouldThrowArgumentNullException()
{
//Arrange
BewitPayloadContext context = null;
var client = new MongoClient(_mongoResource.ConnectionString);
IMongoCollection<Foo> collection = _mongoResource.CreateCollection<Foo>();

//Act
Action useRepository = ()
=> context.UseMongoPersistence(
client,
new MongoNonceOptions
{
DatabaseName = collection.Database.DatabaseNamespace.DatabaseName
}
);

//Assert
useRepository.Should().Throw<ArgumentNullException>();
}

[Fact]
public void UseMongoPersistence_WithMongoClientNull_ShouldThrowArgumentNullException()
{
//Arrange
var builder = new BewitPayloadContext(typeof(object));
IMongoClient client = null;
IMongoCollection<Foo> collection = _mongoResource.CreateCollection<Foo>();

//Act
Action useRepository = ()
=> builder.UseMongoPersistence(
client,
new MongoNonceOptions
{
DatabaseName = collection.Database.DatabaseNamespace.DatabaseName
}
);

//Assert
useRepository.Should().Throw<ArgumentNullException>();
}
}
}