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); }
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); }
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.UseAuthentication()' is called before 'app.UseMvc()' " + "and that the action route corresponds to the endpoint path registered via " + "'services.AddOpenIddict().Enable[...]Endpoint(...)'.", exception.Message); }
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); }