Browse Source

Introduce unit tests for OpenIddict, OpenIddict.EntityFramework and OpenIddict.Mvc

pull/267/head
Kévin Chalet 9 years ago
parent
commit
e23950f44e
  1. 5
      test/OpenIddict.Core.Tests/OpenIddictExtensionsTests.cs
  2. 54
      test/OpenIddict.EntityFramework.Tests/OpenIddictExtensionsTests.cs
  3. 3
      test/OpenIddict.EntityFramework.Tests/Placeholder.cs
  4. 27
      test/OpenIddict.Mvc.Tests/OpenIddictExtensionsTests.cs
  5. 192
      test/OpenIddict.Mvc.Tests/OpenIddictModelBinderTests.cs
  6. 3
      test/OpenIddict.Mvc.Tests/Placeholder.cs
  7. 60
      test/OpenIddict.Tests/OpenIddictExtensionsTests.cs
  8. 3
      test/OpenIddict.Tests/Placeholder.cs

5
test/OpenIddict.Core.Tests/OpenIddictExtensionsTests.cs

@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
@ -14,7 +13,7 @@ using Xunit;
namespace OpenIddict.Core.Tests {
public class OpenIddictExtensionsTests {
[Fact]
public void AddOpenIddict_ProviderIsRegistered() {
public void AddOpenIddict_RegistersProvider() {
// Arrange
var services = new ServiceCollection();
@ -35,7 +34,7 @@ namespace OpenIddict.Core.Tests {
[InlineData(typeof(OpenIddictScopeManager<object>))]
[InlineData(typeof(OpenIddictTokenManager<object>))]
[InlineData(typeof(OpenIddictServices<object, object, object, object>))]
public void AddOpenIddict_BasicServicesAreRegistered(Type type) {
public void AddOpenIddict_RegistersCoreServices(Type type) {
// Arrange
var services = new ServiceCollection();

54
test/OpenIddict.EntityFramework.Tests/OpenIddictExtensionsTests.cs

@ -0,0 +1,54 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace OpenIddict.EntityFramework.Tests {
public class OpenIddictExtensionsTests {
[Theory]
[InlineData(typeof(OpenIddictApplicationStore<OpenIddictApplication<Guid>, OpenIddictToken<Guid>, OpenIddictDbContext, Guid>))]
[InlineData(typeof(OpenIddictAuthorizationStore<OpenIddictAuthorization<Guid>, OpenIddictToken<Guid>, OpenIddictDbContext, Guid>))]
[InlineData(typeof(OpenIddictScopeStore<OpenIddictScope<Guid>, OpenIddictDbContext, Guid>))]
[InlineData(typeof(OpenIddictTokenStore<OpenIddictToken<Guid>, OpenIddictAuthorization<Guid>, OpenIddictDbContext, Guid>))]
public void AddEntityFramework_RegistersEntityFrameworkStores(Type type) {
// Arrange
var services = new ServiceCollection();
var builder = new OpenIddictBuilder(services) {
ApplicationType = typeof(OpenIddictApplication<Guid>),
AuthorizationType = typeof(OpenIddictAuthorization<Guid>),
ScopeType = typeof(OpenIddictScope<Guid>),
TokenType = typeof(OpenIddictToken<Guid>)
};
// Act
builder.AddEntityFramework<OpenIddictDbContext, Guid>();
// Assert
Assert.Contains(services, service => service.ImplementationType == type);
}
[Theory]
[InlineData(typeof(OpenIddictApplicationStore<OpenIddictApplication, OpenIddictToken, OpenIddictDbContext, string>))]
[InlineData(typeof(OpenIddictAuthorizationStore<OpenIddictAuthorization, OpenIddictToken, OpenIddictDbContext, string>))]
[InlineData(typeof(OpenIddictScopeStore<OpenIddictScope, OpenIddictDbContext, string>))]
[InlineData(typeof(OpenIddictTokenStore<OpenIddictToken, OpenIddictAuthorization, OpenIddictDbContext, string>))]
public void AddEntityFramework_KeyTypeDefaultsToString(Type type) {
// Arrange
var services = new ServiceCollection();
var builder = new OpenIddictBuilder(services) {
ApplicationType = typeof(OpenIddictApplication),
AuthorizationType = typeof(OpenIddictAuthorization),
ScopeType = typeof(OpenIddictScope),
TokenType = typeof(OpenIddictToken)
};
// Act
builder.AddEntityFramework<OpenIddictDbContext>();
// Assert
Assert.Contains(services, service => service.ImplementationType == type);
}
}
}

3
test/OpenIddict.EntityFramework.Tests/Placeholder.cs

@ -1,3 +0,0 @@
namespace OpenIddict.EntityFramework.Tests {
public class Placeholder { }
}

27
test/OpenIddict.Mvc.Tests/OpenIddictExtensionsTests.cs

@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;
namespace OpenIddict.Mvc.Tests {
public class OpenIddictExtensionsTests {
[Fact]
public void AddMvcBinders_RegistersModelBinderProvider() {
// Arrange
var services = new ServiceCollection();
services.AddOptions();
var builder = new OpenIddictBuilder(services);
// Act
builder.AddMvcBinders();
var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<MvcOptions>>();
// Assert
Assert.Contains(options.Value.ModelBinderProviders, binder => binder is OpenIddictModelBinder);
}
}
}

192
test/OpenIddict.Mvc.Tests/OpenIddictModelBinderTests.cs

@ -0,0 +1,192 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AspNet.Security.OpenIdConnect.Extensions;
using AspNet.Security.OpenIdConnect.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Moq;
using Xunit;
namespace OpenIddict.Mvc.Tests {
public class OpenIddictModelBinderTests {
[Theory]
[InlineData(typeof(object))]
[InlineData(typeof(IList<int>))]
[InlineData(typeof(int[]))]
public async Task BindModelAsync_ThrowsAnExceptionForUnsupportedTypes(Type type) {
// Arrange
var binder = new OpenIddictModelBinder();
var provider = new EmptyModelMetadataProvider();
var context = new DefaultModelBindingContext {
ModelMetadata = provider.GetMetadataForType(type)
};
// Act and assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(delegate {
return binder.BindModelAsync(context);
});
Assert.Equal("The specified model type is not supported by this binder.", exception.Message);
}
[Fact]
public async Task BindModelAsync_ThrowsAnExceptionWhenRequestCannotBeFound() {
// Arrange
var binder = new OpenIddictModelBinder();
var provider = new EmptyModelMetadataProvider();
var context = new DefaultModelBindingContext {
ActionContext = new ActionContext() {
HttpContext = new DefaultHttpContext(),
},
ModelMetadata = provider.GetMetadataForType(typeof(OpenIdConnectRequest))
};
// Act and assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(delegate {
return binder.BindModelAsync(context);
});
Assert.Equal("The OpenID Connect request cannot be retrieved from the ASP.NET context. " +
"Make sure that 'app.UseOpenIddict()' is called before 'app.UseMvc()' and " +
"that the action route corresponds to the endpoint path registered via " +
"'services.AddOpenIddict().Enable[...]Endpoint(...)'.", exception.Message);
}
[Fact]
public async Task BindModelAsync_ReturnsNullWhenResponseCannotBeFound() {
// Arrange
var binder = new OpenIddictModelBinder();
var provider = new EmptyModelMetadataProvider();
var context = new DefaultModelBindingContext {
ActionContext = new ActionContext() {
HttpContext = new DefaultHttpContext(),
},
ModelMetadata = provider.GetMetadataForType(typeof(OpenIdConnectResponse)),
ValidationState = new ValidationStateDictionary()
};
// Act
await binder.BindModelAsync(context);
// Assert
Assert.True(context.Result.IsModelSet);
Assert.Null(context.Result.Model);
}
[Fact]
public async Task BindModelAsync_ReturnsAmbientRequest() {
// Arrange
var binder = new OpenIddictModelBinder();
var provider = new EmptyModelMetadataProvider();
var request = new OpenIdConnectRequest();
var features = new FeatureCollection();
features.Set(new OpenIdConnectServerFeature {
Request = request
});
var context = new DefaultModelBindingContext {
ActionContext = new ActionContext() {
HttpContext = new DefaultHttpContext(features),
},
ModelMetadata = provider.GetMetadataForType(typeof(OpenIdConnectRequest)),
ValidationState = new ValidationStateDictionary()
};
// Act
await binder.BindModelAsync(context);
// Assert
Assert.True(context.Result.IsModelSet);
Assert.Same(request, context.Result.Model);
Assert.True(context.ValidationState[request].SuppressValidation);
}
[Fact]
public async Task BindModelAsync_ReturnsAmbientResponse() {
// Arrange
var binder = new OpenIddictModelBinder();
var provider = new EmptyModelMetadataProvider();
var response = new OpenIdConnectResponse();
var features = new FeatureCollection();
features.Set(new OpenIdConnectServerFeature {
Response = response
});
var context = new DefaultModelBindingContext {
ActionContext = new ActionContext() {
HttpContext = new DefaultHttpContext(features),
},
ModelMetadata = provider.GetMetadataForType(typeof(OpenIdConnectResponse)),
ValidationState = new ValidationStateDictionary()
};
// Act
await binder.BindModelAsync(context);
// Assert
Assert.True(context.Result.IsModelSet);
Assert.Same(response, context.Result.Model);
Assert.True(context.ValidationState[response].SuppressValidation);
}
[Theory]
[InlineData(typeof(object))]
[InlineData(typeof(IList<int>))]
[InlineData(typeof(int[]))]
public void GetBinder_ReturnsNullForUnsupportedTypes(Type type) {
// Arrange
var provider = new OpenIddictModelBinder();
var metadata = new Mock<ModelMetadata>(ModelMetadataIdentity.ForType(type));
var context = new Mock<ModelBinderProviderContext>();
context.Setup(mock => mock.Metadata)
.Returns(metadata.Object);
// Act
var result = provider.GetBinder(context.Object);
// Assert
Assert.Null(result);
}
[Theory]
[InlineData(typeof(OpenIdConnectRequest))]
[InlineData(typeof(OpenIdConnectResponse))]
public void GetBinder_ReturnsNonNullForSupportedTypes(Type type) {
// Arrange
var binder = new OpenIddictModelBinder();
var metadata = new Mock<ModelMetadata>(ModelMetadataIdentity.ForType(type));
var context = new Mock<ModelBinderProviderContext>();
context.Setup(mock => mock.Metadata)
.Returns(metadata.Object);
// Act
var result = binder.GetBinder(context.Object);
// Assert
Assert.Same(binder, result);
}
}
}

3
test/OpenIddict.Mvc.Tests/Placeholder.cs

@ -1,3 +0,0 @@
namespace OpenIddict.Mvc.Tests {
public class Placeholder { }
}

60
test/OpenIddict.Tests/OpenIddictExtensionsTests.cs

@ -0,0 +1,60 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace OpenIddict.Tests {
public class OpenIddictExtensionsTests {
[Theory]
[InlineData(typeof(OpenIddictApplicationManager<OpenIddictApplication<Guid>>))]
[InlineData(typeof(OpenIddictAuthorizationManager<OpenIddictAuthorization<Guid>>))]
[InlineData(typeof(OpenIddictScopeManager<OpenIddictScope<Guid>>))]
[InlineData(typeof(OpenIddictTokenManager<OpenIddictToken<Guid>>))]
public void AddOpenIddict_RegistersCoreManagers(Type type) {
// Arrange
var services = new ServiceCollection();
// Act
services.AddOpenIddict<OpenIddictDbContext, Guid>();
// Assert
Assert.Contains(services, service => service.ImplementationType == type);
}
[Theory]
[InlineData(typeof(OpenIddictApplicationStore<OpenIddictApplication<Guid>, OpenIddictToken<Guid>, OpenIddictDbContext, Guid>))]
[InlineData(typeof(OpenIddictAuthorizationStore<OpenIddictAuthorization<Guid>, OpenIddictToken<Guid>, OpenIddictDbContext, Guid>))]
[InlineData(typeof(OpenIddictScopeStore<OpenIddictScope<Guid>, OpenIddictDbContext, Guid>))]
[InlineData(typeof(OpenIddictTokenStore<OpenIddictToken<Guid>, OpenIddictAuthorization<Guid>, OpenIddictDbContext, Guid>))]
public void AddOpenIddict_RegistersEntityFrameworkStores(Type type) {
// Arrange
var services = new ServiceCollection();
// Act
services.AddOpenIddict<OpenIddictDbContext, Guid>();
// Assert
Assert.Contains(services, service => service.ImplementationType == type);
}
[Theory]
[InlineData(typeof(OpenIddictApplicationManager<OpenIddictApplication>))]
[InlineData(typeof(OpenIddictAuthorizationManager<OpenIddictAuthorization>))]
[InlineData(typeof(OpenIddictScopeManager<OpenIddictScope>))]
[InlineData(typeof(OpenIddictTokenManager<OpenIddictToken>))]
[InlineData(typeof(OpenIddictApplicationStore<OpenIddictApplication, OpenIddictToken, OpenIddictDbContext, string>))]
[InlineData(typeof(OpenIddictAuthorizationStore<OpenIddictAuthorization, OpenIddictToken, OpenIddictDbContext, string>))]
[InlineData(typeof(OpenIddictScopeStore<OpenIddictScope, OpenIddictDbContext, string>))]
[InlineData(typeof(OpenIddictTokenStore<OpenIddictToken, OpenIddictAuthorization, OpenIddictDbContext, string>))]
public void AddOpenIddict_KeyTypeDefaultsToString(Type type) {
// Arrange
var services = new ServiceCollection();
// Act
services.AddOpenIddict<OpenIddictDbContext>();
// Assert
Assert.Contains(services, service => service.ImplementationType == type);
}
}
}

3
test/OpenIddict.Tests/Placeholder.cs

@ -1,3 +0,0 @@
namespace OpenIddict.Tests {
public class Placeholder { }
}
Loading…
Cancel
Save