Browse Source

Add unit tests for the MongoDB stores resolvers

pull/649/head
Kévin Chalet 8 years ago
parent
commit
7cf18dc1ea
  1. 80
      test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictApplicationStoreResolverTests.cs
  2. 80
      test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictAuthorizationStoreResolverTests.cs
  3. 80
      test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictScopeStoreResolverTests.cs
  4. 80
      test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictTokenStoreResolverTests.cs

80
test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictApplicationStoreResolverTests.cs

@ -0,0 +1,80 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Text;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using OpenIddict.Abstractions;
using OpenIddict.MongoDb.Models;
using Xunit;
namespace OpenIddict.MongoDb.Tests
{
public class OpenIddictApplicationStoreResolverTests
{
[Fact]
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictApplicationStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<CustomApplication>());
}
[Fact]
public void Get_ThrowsAnExceptionForInvalidEntityType()
{
// Arrange
var services = new ServiceCollection();
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictApplicationStoreResolver(provider);
// Act and assert
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomApplication>());
Assert.Equal(new StringBuilder()
.AppendLine("The specified application type is not compatible with the MongoDB stores.")
.Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictApplication' ")
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ")
.Append("that inherits from the 'OpenIddictApplication' entity.")
.ToString(), exception.Message);
}
[Fact]
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictApplicationStore<CustomApplication>>());
services.AddSingleton(CreateStore());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictApplicationStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<MyApplication>());
}
private static OpenIddictApplicationStore<MyApplication> CreateStore()
=> new Mock<OpenIddictApplicationStore<MyApplication>>(
Mock.Of<IMemoryCache>(),
Mock.Of<IOpenIddictMongoDbContext>(),
Mock.Of<IOptionsMonitor<OpenIddictMongoDbOptions>>()).Object;
public class CustomApplication { }
public class MyApplication : OpenIddictApplication { }
}
}

80
test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictAuthorizationStoreResolverTests.cs

@ -0,0 +1,80 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Text;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using OpenIddict.Abstractions;
using OpenIddict.MongoDb.Models;
using Xunit;
namespace OpenIddict.MongoDb.Tests
{
public class OpenIddictAuthorizationStoreResolverTests
{
[Fact]
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictAuthorizationStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<CustomAuthorization>());
}
[Fact]
public void Get_ThrowsAnExceptionForInvalidEntityType()
{
// Arrange
var services = new ServiceCollection();
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictAuthorizationStoreResolver(provider);
// Act and assert
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomAuthorization>());
Assert.Equal(new StringBuilder()
.AppendLine("The specified authorization type is not compatible with the MongoDB stores.")
.Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictAuthorization' ")
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ")
.Append("that inherits from the 'OpenIddictAuthorization' entity.")
.ToString(), exception.Message);
}
[Fact]
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictAuthorizationStore<CustomAuthorization>>());
services.AddSingleton(CreateStore());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictAuthorizationStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<MyAuthorization>());
}
private static OpenIddictAuthorizationStore<MyAuthorization> CreateStore()
=> new Mock<OpenIddictAuthorizationStore<MyAuthorization>>(
Mock.Of<IMemoryCache>(),
Mock.Of<IOpenIddictMongoDbContext>(),
Mock.Of<IOptionsMonitor<OpenIddictMongoDbOptions>>()).Object;
public class CustomAuthorization { }
public class MyAuthorization : OpenIddictAuthorization { }
}
}

80
test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictScopeStoreResolverTests.cs

@ -0,0 +1,80 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Text;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using OpenIddict.Abstractions;
using OpenIddict.MongoDb.Models;
using Xunit;
namespace OpenIddict.MongoDb.Tests
{
public class OpenIddictScopeStoreResolverTests
{
[Fact]
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictScopeStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<CustomScope>());
}
[Fact]
public void Get_ThrowsAnExceptionForInvalidEntityType()
{
// Arrange
var services = new ServiceCollection();
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictScopeStoreResolver(provider);
// Act and assert
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomScope>());
Assert.Equal(new StringBuilder()
.AppendLine("The specified scope type is not compatible with the MongoDB stores.")
.Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictScope' ")
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ")
.Append("that inherits from the 'OpenIddictScope' entity.")
.ToString(), exception.Message);
}
[Fact]
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictScopeStore<CustomScope>>());
services.AddSingleton(CreateStore());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictScopeStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<MyScope>());
}
private static OpenIddictScopeStore<MyScope> CreateStore()
=> new Mock<OpenIddictScopeStore<MyScope>>(
Mock.Of<IMemoryCache>(),
Mock.Of<IOpenIddictMongoDbContext>(),
Mock.Of<IOptionsMonitor<OpenIddictMongoDbOptions>>()).Object;
public class CustomScope { }
public class MyScope : OpenIddictScope { }
}
}

80
test/OpenIddict.MongoDb.Tests/Resolvers/OpenIddictTokenStoreResolverTests.cs

@ -0,0 +1,80 @@
/*
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* See https://github.com/openiddict/openiddict-core for more information concerning
* the license and the contributors participating to this project.
*/
using System;
using System.Text;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using OpenIddict.Abstractions;
using OpenIddict.MongoDb.Models;
using Xunit;
namespace OpenIddict.MongoDb.Tests
{
public class OpenIddictTokenStoreResolverTests
{
[Fact]
public void Get_ReturnsCustomStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictTokenStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<CustomToken>());
}
[Fact]
public void Get_ThrowsAnExceptionForInvalidEntityType()
{
// Arrange
var services = new ServiceCollection();
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictTokenStoreResolver(provider);
// Act and assert
var exception = Assert.Throws<InvalidOperationException>(() => resolver.Get<CustomToken>());
Assert.Equal(new StringBuilder()
.AppendLine("The specified token type is not compatible with the MongoDB stores.")
.Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictToken' ")
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ")
.Append("that inherits from the 'OpenIddictToken' entity.")
.ToString(), exception.Message);
}
[Fact]
public void Get_ReturnsDefaultStoreCorrespondingToTheSpecifiedTypeWhenAvailable()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(Mock.Of<IOpenIddictTokenStore<CustomToken>>());
services.AddSingleton(CreateStore());
var provider = services.BuildServiceProvider();
var resolver = new OpenIddictTokenStoreResolver(provider);
// Act and assert
Assert.NotNull(resolver.Get<MyToken>());
}
private static OpenIddictTokenStore<MyToken> CreateStore()
=> new Mock<OpenIddictTokenStore<MyToken>>(
Mock.Of<IMemoryCache>(),
Mock.Of<IOpenIddictMongoDbContext>(),
Mock.Of<IOptionsMonitor<OpenIddictMongoDbOptions>>()).Object;
public class CustomToken { }
public class MyToken : OpenIddictToken { }
}
}
Loading…
Cancel
Save