Browse Source

Backport the MongoDB changes to OpenIddict 1.x

pull/670/head
Kévin Chalet 8 years ago
parent
commit
105ede93b0
  1. 8
      src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs
  2. 67
      src/OpenIddict.MongoDb/OpenIddictMongoDbContext.cs
  3. 5
      src/OpenIddict.MongoDb/OpenIddictMongoDbOptions.cs
  4. 17
      test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbBuilderTests.cs
  5. 25
      test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbContextTests.cs

8
src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs

@ -57,6 +57,14 @@ namespace Microsoft.Extensions.DependencyInjection
return this;
}
/// <summary>
/// Disables initialization so that the MongoDB indexes used by OpenIddict
/// are not automatically created the first time the stores are invoked.
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/>.</returns>
public OpenIddictMongoDbBuilder DisableInitialization()
=> Configure(options => options.DisableInitialization = true);
/// <summary>
/// Configures OpenIddict to use the specified entity as the default application entity.
/// </summary>

67
src/OpenIddict.MongoDb/OpenIddictMongoDbContext.cs

@ -84,38 +84,41 @@ namespace OpenIddict.MongoDb
.ToString());
}
// Note: the cancellation token passed as a parameter is deliberately not used here to ensure
// the cancellation of a single store operation doesn't prevent the indexes from being created.
var applications = database.GetCollection<OpenIddictApplication>(options.ApplicationsCollectionName);
await applications.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictApplication>(
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.ClientId),
new CreateIndexOptions
{
Unique = true
}));
await applications.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictApplication>(
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.PostLogoutRedirectUris)));
await applications.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictApplication>(
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.RedirectUris)));
var scopes = database.GetCollection<OpenIddictScope>(options.ScopesCollectionName);
await scopes.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictScope>(
Builders<OpenIddictScope>.IndexKeys.Ascending(scope => scope.Name),
new CreateIndexOptions
{
Unique = true
}));
var tokens = database.GetCollection<OpenIddictToken>(options.TokensCollectionName);
await tokens.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictToken>(
Builders<OpenIddictToken>.IndexKeys.Ascending(token => token.ReferenceId),
new CreateIndexOptions<OpenIddictToken>
{
PartialFilterExpression = Builders<OpenIddictToken>.Filter.Exists(token => token.ReferenceId),
Unique = true
}));
if (!options.DisableInitialization)
{
// Note: the cancellation token passed as a parameter is deliberately not used here to ensure
// the cancellation of a single store operation doesn't prevent the indexes from being created.
var applications = database.GetCollection<OpenIddictApplication>(options.ApplicationsCollectionName);
await applications.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictApplication>(
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.ClientId),
new CreateIndexOptions
{
Unique = true
}));
await applications.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictApplication>(
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.PostLogoutRedirectUris)));
await applications.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictApplication>(
Builders<OpenIddictApplication>.IndexKeys.Ascending(application => application.RedirectUris)));
var scopes = database.GetCollection<OpenIddictScope>(options.ScopesCollectionName);
await scopes.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictScope>(
Builders<OpenIddictScope>.IndexKeys.Ascending(scope => scope.Name),
new CreateIndexOptions
{
Unique = true
}));
var tokens = database.GetCollection<OpenIddictToken>(options.TokensCollectionName);
await tokens.Indexes.CreateOneAsync(new CreateIndexModel<OpenIddictToken>(
Builders<OpenIddictToken>.IndexKeys.Ascending(token => token.ReferenceId),
new CreateIndexOptions<OpenIddictToken>
{
PartialFilterExpression = Builders<OpenIddictToken>.Filter.Exists(token => token.ReferenceId),
Unique = true
}));
}
return _database = database;
}

5
src/OpenIddict.MongoDb/OpenIddictMongoDbOptions.cs

@ -30,6 +30,11 @@ namespace OpenIddict.MongoDb
/// </summary>
public IMongoDatabase Database { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether automatic initialization should be disabled.
/// </summary>
public bool DisableInitialization { get; set; }
/// <summary>
/// Gets or sets the maximal duration given to the MongoDB client to initialize
/// the database and register the indexes used by the OpenIddict entities.

17
test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbBuilderTests.cs

@ -29,6 +29,23 @@ namespace OpenIddict.MongoDb.Tests
Assert.Equal("services", exception.ParamName);
}
[Fact]
public void DisableInitialization_InitializationIsCorrectlyDisabled()
{
// Arrange
var services = CreateServices();
var builder = CreateBuilder(services);
// Act
builder.DisableInitialization();
// Assert
var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<OpenIddictMongoDbOptions>>().Value;
Assert.True(options.DisableInitialization);
}
[Fact]
public void ReplaceDefaultApplicationEntity_EntityIsCorrectlySet()
{

25
test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbContextTests.cs

@ -161,6 +161,31 @@ namespace OpenIddict.MongoDb.Tests
Assert.Same(database.Object, await context.GetDatabaseAsync(CancellationToken.None));
}
[Fact]
public async Task GetDatabaseAsync_SkipsInitializationWhenDisabled()
{
// Arrange
var services = new ServiceCollection();
var provider = services.BuildServiceProvider();
var database = GetDatabase();
var options = Options.Create(new OpenIddictMongoDbOptions
{
Database = database.Object,
DisableInitialization = true
});
var context = new OpenIddictMongoDbContext(options, provider);
// Act
await context.GetDatabaseAsync(CancellationToken.None);
// Assert
database.Verify(mock => mock.GetCollection<OpenIddictApplication>(It.IsAny<string>(), It.IsAny<MongoCollectionSettings>()), Times.Never());
database.Verify(mock => mock.GetCollection<OpenIddictScope>(It.IsAny<string>(), It.IsAny<MongoCollectionSettings>()), Times.Never());
database.Verify(mock => mock.GetCollection<OpenIddictToken>(It.IsAny<string>(), It.IsAny<MongoCollectionSettings>()), Times.Never());
}
[Fact]
public async Task GetDatabaseAsync_ReturnsCachedDatabase()
{

Loading…
Cancel
Save