public void MapOfFieldWithEventName_RendersTwoItem() { using var restorePoint = new GraphQLProviderRestorePoint(); SchemaSubscriptionEventMap.ClearCache(); var schema = new TestServerBuilder <EventMapSchema>() .AddSubscriptionServer() .AddGraphController <OneFieldMapWithEventNameController>() .Build() .Schema; var map = SchemaSubscriptionEventMap.CreateEventMap(schema); var pathName = "[subscription]/OneFieldMapWithEventName/TestActionMethod"; var pathEventName = new SubscriptionEventName(typeof(EventMapSchema), pathName); var eventName = new SubscriptionEventName(typeof(EventMapSchema), "shortTestName"); Assert.AreEqual(2, map.Count); Assert.IsTrue(map.ContainsKey(eventName)); Assert.IsNotNull(map[eventName]); Assert.AreEqual(pathName, map[eventName].Path); Assert.IsTrue(map.ContainsKey(eventName)); Assert.IsNotNull(map[eventName]); Assert.AreEqual(pathName, map[eventName].Path); // ensure both items reference the same object Assert.AreSame(map[pathEventName], map[eventName]); }
public void ExecuteRule_EnsureCorrectErrorIsGenerated(string expectedRuleError, string queryText) { using var restorePoint = new GraphQLProviderRestorePoint(); var server = new TestServerBuilder() .AddGraphType <PeopleMoverController>() .AddGraphType <AllowDirective>() .AddGraphType <RestrictDirective>() .AddSubscriptionServer() .Build(); // parse the query var document = server.CreateDocument(queryText); // inspect for error codes if (document.Messages.Count != 1) { var errors = document.Messages.Where( x => x.MetaData != null && x.MetaData.ContainsKey("Rule")) .Select(x => x.MetaData["Rule"]); string errorMessage = $"Expected 1 error ({expectedRuleError}) but recieved {document.Messages.Count}: '{string.Join(", ", errors)}'"; Assert.Fail(errorMessage); } var message = document.Messages[0]; Assert.IsNotNull(message.MetaData); Assert.IsTrue(message.MetaData.ContainsKey("Rule")); var ruleBroke = message.MetaData["Rule"]; Assert.AreEqual(expectedRuleError, ruleBroke.ToString()); }
public void GeneralPropertyCheck() { using var restorePoint = new GraphQLProviderRestorePoint(); GraphQLProviders.TemplateProvider = null; var primaryOptions = new SchemaOptions <GraphSchema>(); var subscriptionOptions = new SubscriptionServerOptions <GraphSchema>(); var extension = new SubscriptionPublisherSchemaExtension <GraphSchema>(); extension.Configure(primaryOptions); Assert.IsTrue(primaryOptions.DeclarationOptions.AllowedOperations.Contains(GraphCollection.Subscription)); Assert.IsTrue(GraphQLProviders.TemplateProvider is SubscriptionEnabledTemplateProvider); }
public void DuplicateEventName_ThrowsException() { using var restorePoint = new GraphQLProviderRestorePoint(); SchemaSubscriptionEventMap.ClearCache(); var schema = new TestServerBuilder <EventMapSchema>() .AddSubscriptionServer() .AddGraphController <DuplicateEventNameController>() .Build() .Schema; Assert.Throws <DuplicateNameException>(() => { var map = SchemaSubscriptionEventMap.CreateEventMap(schema); }); }
public void AddSubscriptionPublishing_RegistrationChecks() { using var restorePoint = new GraphQLProviderRestorePoint(); // ensure the runtime is in a default state (just in case the statics got messed up) GraphQLProviders.TemplateProvider = new DefaultTypeTemplateProvider(); GraphQLProviders.GraphTypeMakerProvider = new DefaultGraphTypeMakerProvider(); var serviceCollection = new ServiceCollection(); var returned = serviceCollection.AddGraphQL(options => { options.AddGraphType <FanController>(); }) .AddSubscriptionPublishing(); this.EnsureSubscriptionPublishingRegistrations(serviceCollection.BuildServiceProvider()); }
public void AddASubscriptionAction_WithoutUpdatingTheConfiguration_ThrowsDeclarationException() { using var restorePoint = new GraphQLProviderRestorePoint(); GraphQLProviders.TemplateProvider = new SubscriptionEnabledTemplateProvider(); var schema = new GraphSchema() as ISchema; schema.SetNoAlterationConfiguration(); // do not tell the schema to allow the subscription operation type // schema.SetSubscriptionAllowances(); // attempt to add a controller with a subscription Assert.Throws <ArgumentOutOfRangeException>(() => { var manager = new GraphSchemaManager(schema); manager.EnsureGraphType <SimpleMethodController>(); }); }
public void RetrieveFieldPathByName_YieldsCorrectPath() { using var restorePoint = new GraphQLProviderRestorePoint(); SchemaSubscriptionEventMap.ClearCache(); var schema = new TestServerBuilder <EventMapSchema>() .AddSubscriptionServer() .AddGraphController <OneFieldMapController>() .Build() .Schema; var map = SchemaSubscriptionEventMap.CreateEventMap(schema); var pathName = "[subscription]/OneFieldMap/TestActionMethod"; var eventName = new SubscriptionEventName(typeof(EventMapSchema), pathName); var fieldPath = schema.RetrieveSubscriptionFieldPath(eventName); Assert.IsNotNull(fieldPath); Assert.AreEqual(pathName, fieldPath.Path); }
public void MapOfFieldWithNoEventName_RendersOneItem() { using var restorePoint = new GraphQLProviderRestorePoint(); SchemaSubscriptionEventMap.ClearCache(); var schema = new TestServerBuilder <EventMapSchema>() .AddSubscriptionServer() .AddGraphController <OneFieldMapController>() .Build() .Schema; var map = SchemaSubscriptionEventMap.CreateEventMap(schema); var pathName = "[subscription]/OneFieldMap/TestActionMethod"; var eventName = new SubscriptionEventName(typeof(EventMapSchema), pathName); Assert.AreEqual(1, map.Count); Assert.IsTrue(map.ContainsKey(eventName)); Assert.IsNotNull(map[eventName]); Assert.AreEqual(pathName, map[eventName].Path); }
public void ExplicitDeclarationOfPerRequestAuthorizationAddsServerSuccessfully() { // setup the server with a hard declaration of nothing using var restorePoint = new GraphQLProviderRestorePoint(); // ensure the runtime is in a default state (just in case the statics got messed up) GraphQLProviders.TemplateProvider = new DefaultTypeTemplateProvider(); GraphQLProviders.GraphTypeMakerProvider = new DefaultGraphTypeMakerProvider(); GraphQLMvcSchemaBuilderExtensions.Clear(); var serviceCollection = new ServiceCollection(); var returned = serviceCollection.AddGraphQL(options => { options.AddGraphType <FanController>(); options.AuthorizationOptions.Method = AuthorizationMethod.PerRequest; }) .AddSubscriptionServer(); this.EnsureSubscriptionServerRegistrations(serviceCollection.BuildServiceProvider()); }
public void AddSubscriptions_RegistrationChecks() { using var restorePoint = new GraphQLProviderRestorePoint(); // ensure the runtime is in a default state (just in case the statics got messed up) GraphQLProviders.TemplateProvider = new DefaultTypeTemplateProvider(); GraphQLProviders.GraphTypeMakerProvider = new DefaultGraphTypeMakerProvider(); GraphQLMvcSchemaBuilderExtensions.Clear(); var serviceCollection = new ServiceCollection(); var returned = serviceCollection.AddGraphQL(options => { options.AddGraphType <FanController>(); }) .AddSubscriptions(); var sp = serviceCollection.BuildServiceProvider(); // ensure both publishing and server stuff has been registered this.EnsureSubscriptionServerRegistrations(sp); this.EnsureSubscriptionPublishingRegistrations(sp); }
public void AddSingleSubscriptionAction_AllDefaults_EnsureFieldStructure() { using var restorePoint = new GraphQLProviderRestorePoint(); GraphQLProviders.TemplateProvider = new SubscriptionEnabledTemplateProvider(); var schema = new GraphSchema() as ISchema; schema.SetNoAlterationConfiguration(); schema.SetSubscriptionAllowances(); var manager = new GraphSchemaManager(schema); manager.EnsureGraphType <SimpleMethodController>(); // query always exists // subscription root was found via the method parsed // mutation was not provided Assert.IsTrue(schema.OperationTypes.ContainsKey(GraphCollection.Query)); Assert.IsFalse(schema.OperationTypes.ContainsKey(GraphCollection.Mutation)); Assert.IsTrue(schema.OperationTypes.ContainsKey(GraphCollection.Subscription)); // field for the controller exists var topFieldName = nameof(SimpleMethodController).Replace(Constants.CommonSuffix.CONTROLLER_SUFFIX, string.Empty); Assert.IsTrue(schema.OperationTypes[GraphCollection.Subscription].Fields.ContainsKey(topFieldName)); // ensure the field on the subscription operation is the right name (i.e. the controller name) var topField = schema.OperationTypes[GraphCollection.Subscription][topFieldName]; Assert.IsNotNull(topField); var type = schema.KnownTypes.FindGraphType(topField) as IObjectGraphType; var action = TemplateHelper.CreateFieldTemplate <SimpleMethodController>(nameof(SimpleMethodController.TestActionMethod)); // ensure the action was put into the field collection of the controller operation Assert.IsTrue(type.Fields.ContainsKey(action.Route.Name)); }
public void ExplicitDeclarationOfPerFieldAuthorizationFailsServerCreation() { // setup the server with a hard declaration of nothing using var restorePoint = new GraphQLProviderRestorePoint(); // ensure the runtime is in a default state (just in case the statics got messed up) GraphQLProviders.TemplateProvider = new DefaultTypeTemplateProvider(); GraphQLProviders.GraphTypeMakerProvider = new DefaultGraphTypeMakerProvider(); GraphQLMvcSchemaBuilderExtensions.Clear(); var serviceCollection = new ServiceCollection(); var schemaBuilder = serviceCollection.AddGraphQL(options => { options.AddGraphType <FanController>(); options.AuthorizationOptions.Method = AuthorizationMethod.PerField; }); // server should value to generate Assert.Throws <ApolloSubscriptionServerException>( () => { schemaBuilder.AddSubscriptionServer(); }); }
public void GeneralPropertyCheck() { using var restorePoint = new GraphQLProviderRestorePoint(); GraphQLProviders.TemplateProvider = null; var primaryOptions = new SchemaOptions <GraphSchema>(); var subscriptionOptions = new SubscriptionServerOptions <GraphSchema>(); (var builder, var queryPipeline, var fieldPipeline) = CreateSchemaBuilderMock(); var extension = new ApolloSubscriptionServerSchemaExtension <GraphSchema>(builder.Object, subscriptionOptions); extension.Configure(primaryOptions); Assert.IsTrue(primaryOptions.DeclarationOptions.AllowedOperations.Contains(GraphCollection.Subscription)); Assert.AreEqual(2, extension.RequiredServices.Count); Assert.IsNotNull(extension.RequiredServices.SingleOrDefault(x => x.ServiceType == typeof(SubscriptionServerOptions <GraphSchema>))); Assert.IsNotNull(extension.RequiredServices.SingleOrDefault(x => x.ServiceType == typeof(ApolloMessageConverterFactory))); Assert.AreEqual(1, extension.OptionalServices.Count); Assert.IsNotNull(extension.OptionalServices.SingleOrDefault(x => x.ServiceType == typeof(ISubscriptionServer <GraphSchema>))); Assert.IsTrue(GraphQLProviders.TemplateProvider is SubscriptionEnabledTemplateProvider); // 9 middleware components in the subscription-swapped primary query pipeline registered by type // 1 middleware component registered by instance queryPipeline.Verify(x => x.Clear()); queryPipeline.Verify( x => x.AddMiddleware <IGraphMiddlewareComponent <GraphQueryExecutionContext> >( It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(9)); queryPipeline.Verify( x => x.AddMiddleware( It.IsAny <IGraphMiddlewareComponent <GraphQueryExecutionContext> >(), It.IsAny <string>()), Times.Exactly(1)); // ensur query level authorzation component was added queryPipeline.Verify( x => x.AddMiddleware <AuthorizeQueryOperationMiddleware <GraphSchema> >( It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(1)); // four components in the sub swaped field pipeline fieldPipeline.Verify(x => x.Clear()); fieldPipeline.Verify( x => x.AddMiddleware <IGraphMiddlewareComponent <GraphFieldExecutionContext> >(It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(4)); // ensure field authroization component was NOT added // to the field pipeline fieldPipeline.Verify( x => x.AddMiddleware <AuthorizeFieldMiddleware <GraphSchema> >(It.IsAny <ServiceLifetime>(), It.IsAny <string>()), Times.Exactly(0)); }