Exemple #1
0
        public void AuthorizationAttribute_Query_Custom_Attribute()
        {
            IPrincipal user = this.CreateIPrincipal("user1");

            // Instantiate a new DomainService to use for a Query
            using (AuthorizationTestDomainService testDomainService = new AuthorizationTestDomainService())
            {
                testDomainService.Initialize(new DomainServiceContext(new MockDataService(user), DomainOperationType.Query));

                // Get a DomainServiceDescription for that same domain service
                DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(AuthorizationTestDomainService));

                // Locate the QueryAllow query
                DomainOperationEntry entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "QueryAllow");
                Assert.IsNotNull(entry, "Did not find QueryAllow entry");
                Assert.AreEqual("Query", entry.OperationType, "Query operation type expected for query operation");

                // Ask the domain service to perform authorization.
                // The principal will be located via the mock data service created above.
                AuthorizationResult result = testDomainService.IsAuthorized(entry, entity: null);

                if (result != AuthorizationResult.Allowed)
                {
                    Assert.Fail("Expected QueryAllow to be approved: " + result.ErrorMessage);
                }

                // Try again with a different query that will be denied
                entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "QueryDeny");
                Assert.IsNotNull(entry, "Did not find QueryDeny entry");

                result = testDomainService.IsAuthorized(entry, entity: null);

                Assert.AreNotSame(AuthorizationResult.Allowed, result, "Expected QueryDeny to be denied");
            }
        }
Exemple #2
0
        public void AuthorizationAttribute_Invoke_Allowed_No_Attributes()
        {
            IPrincipal user = this.CreateIPrincipal("user1");

            // Instantiate a new DomainService to use for an Invoke
            using (AuthorizationTestDomainService testDomainService = new AuthorizationTestDomainService())
            {
                testDomainService.Initialize(new DomainServiceContext(new MockDataService(user), DomainOperationType.Invoke));

                // Get a DomainServiceDescription for that same domain service
                DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(AuthorizationTestDomainService));

                // Locate the invoke method
                DomainOperationEntry invokeEntry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "InvokeNoAuth");
                Assert.IsNotNull(invokeEntry, "Could not locate InvokeNoAuth invoke");
                Assert.AreEqual("Invoke", invokeEntry.OperationType, "Invoke operation entry should show Invoke operation type");

                // Ask the domain service to perform authorization.
                // The principal will be located via the mock data service created above.
                // Invokes do not expect an entity instance.
                AuthorizationResult result = testDomainService.IsAuthorized(invokeEntry, entity: null);

                Assert.AreSame(AuthorizationResult.Allowed, result, "Expected invoke with no auth attributes to be allowed.");
            }
        }
 private static void Generate <T>()
 {
     new AuthenticationCodeProcessor(new CSharpCodeProvider()).ProcessGeneratedCode(
         DomainServiceDescription.GetDescription(typeof(T)),
         null /* unused in negative tests */,
         null /* unused in negative tests */);
 }
Exemple #4
0
        public void AuthorizationAttribute_Invoke_Custom_Attribute()
        {
            IPrincipal user = this.CreateIPrincipal("user1");

            // Instantiate a new DomainService to use for an Invoke
            using (AuthorizationTestDomainService testDomainService = new AuthorizationTestDomainService())
            {
                testDomainService.Initialize(new DomainServiceContext(new MockDataService(user), DomainOperationType.Invoke));

                // Get a DomainServiceDescription for that same domain service
                DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(AuthorizationTestDomainService));

                // Locate the invoke method
                DomainOperationEntry invokeEntry = description.DomainOperationEntries.Single(p => p.Name == "InvokeAllow");

                // Ask the domain service to perform authorization.
                // The principal will be located via the mock data service created above.
                // Invokes do not expect an entity instance.
                AuthorizationResult result = testDomainService.IsAuthorized(invokeEntry, entity: null);

                Assert.AreSame(AuthorizationResult.Allowed, result, "Expected invoke with custom auth attributes to be allowed.");

                // Do that again but using an Invoke that will deny based on its own name
                invokeEntry = description.DomainOperationEntries.Single(p => p.Name == "InvokeDeny");
                result      = testDomainService.IsAuthorized(invokeEntry, entity: null);
                Assert.AreNotSame(AuthorizationResult.Allowed, result, "Expected invoke with denying custom attributes to be denied.");
            }
        }
        /// <summary>
        /// Helper method performs a query operation against a given proxy instance.
        /// </summary>
        /// <param name="domainService">The type of <see cref="DomainService"/> to perform this query operation against.</param>
        /// <param name="context">The current context.</param>
        /// <param name="domainServiceInstances">The list of tracked <see cref="DomainService"/> instances that any newly created
        /// <see cref="DomainServices"/> will be added to.</param>
        /// <param name="queryName">The name of the query to invoke.</param>
        /// <param name="parameters">The query parameters.</param>
        /// <returns>The query results. May be null if there are no query results.</returns>
        /// <exception cref="ArgumentNullException">if <paramref name="context"/> is null.</exception>
        /// <exception cref="ArgumentNullException">if <paramref name="queryName"/> is null or an empty string.</exception>
        /// <exception cref="InvalidOperationException">if no match query operation exists on the <paramref name="context"/>.</exception>
        /// <exception cref="OperationException">if operation errors are thrown during execution of the query operation.</exception>
        public static IEnumerable Query(Type domainService, DomainServiceContext context, IList <DomainService> domainServiceInstances, string queryName, object[] parameters)
        {
            context = new DomainServiceContext(context, DomainOperationType.Query);
            DomainService            service            = CreateDomainServiceInstance(domainService, context, domainServiceInstances);
            DomainServiceDescription serviceDescription = DomainServiceDescription.GetDescription(service.GetType());
            DomainOperationEntry     queryOperation     = serviceDescription.GetQueryMethod(queryName);

            if (queryOperation == null)
            {
                string errorMessage =
                    string.Format(
                        CultureInfo.InvariantCulture,
                        Resource.DomainServiceProxy_QueryOperationNotFound,
                        queryName,
                        domainService);

                throw new InvalidOperationException(errorMessage);
            }

            int totalCount;
            IEnumerable <ValidationResult> validationErrors;

            object[]         parameterValues  = parameters ?? new object[0];
            QueryDescription queryDescription = new QueryDescription(queryOperation, parameterValues);

            IEnumerable result = service.Query(queryDescription, out validationErrors, out totalCount);

            if (validationErrors != null && validationErrors.Any())
            {
                IEnumerable <ValidationResultInfo> operationErrors = validationErrors.Select(ve => new ValidationResultInfo(ve.ErrorMessage, ve.MemberNames));
                throw new OperationException(Resource.DomainServiceProxy_OperationError, operationErrors);
            }

            return(result);
        }
 public void QueryAttribute_DomainService_Illegal_Default_With_Params()
 {
     ExceptionHelper.ExpectInvalidOperationException(delegate
     {
         DomainServiceDescription.GetDescription(typeof(QueryAttribute_DomainService_Default_Query_Has_Params));
     }, string.Format(CultureInfo.CurrentCulture, Resource.DomainServiceDescription_DefaultQuery_Cannot_Have_Params, "GetEntity1"));
 }
        public void ETagPropertyUpdatesEntity()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity1));

            PropertyDescriptor descriptor = properties["ETag"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for ETag.");

            TMPT_MockEntity1 entity = new TMPT_MockEntity1();

            Assert.AreEqual(entity.GetETag(), descriptor.GetValue(entity),
                            "The value returned should match the entity.");

            string etag = "etag";

            entity.SetETag(etag);
            Assert.AreEqual(entity.GetETag(), descriptor.GetValue(entity),
                            "The value returned should match the updated entity.");

            etag = "etag2";
            descriptor.SetValue(entity, etag);
            Assert.AreEqual(etag, entity.GetETag(),
                            "The entity value should match the updated etag.");
            Assert.AreEqual(etag, descriptor.GetValue(entity),
                            "The descriptor value should match the updated etag.");

            descriptor.ResetValue(entity);
            Assert.AreEqual(entity.GetETag(), descriptor.GetValue(entity),
                            "The reset value returned should match the entity.");
        }
        public void DefaultRowKeyMetadata()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity1));

            PropertyDescriptor descriptor = properties["RowKey"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for RowKey.");
            Assert.AreEqual(typeof(string), descriptor.PropertyType,
                            "The RowKey type should be a string.");

            KeyAttribute keyAttribute = (KeyAttribute)descriptor.Attributes[typeof(KeyAttribute)];

            Assert.IsNotNull(keyAttribute,
                             "The RowKey should have a KeyAttribute.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The RowKey should have an EditableAttribute.");
            Assert.IsFalse(editableAttribute.AllowEdit,
                           "The RowKey should not allow editing.");
            Assert.IsTrue(editableAttribute.AllowInitialValue,
                          "The RowKey should allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The RowKey should have an DisplayAttribute.");
            Assert.IsFalse(displayAttribute.AutoGenerateField,
                           "The RowKey should not be included in autogeneration.");
        }
        public void ETagProperty()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity1));

            PropertyDescriptor descriptor = properties["ETag"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for ETag.");
            Assert.AreEqual(typeof(string), descriptor.PropertyType,
                            "The ETag type should be a string.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The ETag should have an EditableAttribute.");
            Assert.IsFalse(editableAttribute.AllowEdit,
                           "The ETag should not allow editing.");
            Assert.IsFalse(editableAttribute.AllowInitialValue,
                           "The ETag should not allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The ETag should have an DisplayAttribute.");
            Assert.IsFalse(displayAttribute.AutoGenerateField,
                           "The ETag should not be included in autogeneration.");

            ConcurrencyCheckAttribute concurrencyCheckAttribute = (ConcurrencyCheckAttribute)descriptor.Attributes[typeof(ConcurrencyCheckAttribute)];

            Assert.IsNotNull(concurrencyCheckAttribute,
                             "The ETag should have an ConcurrencyCheckAttribute.");
        }
        public void OverwrittenTimestampMetadata()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity2));

            PropertyDescriptor descriptor = properties["Timestamp"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for Timestamp.");
            Assert.AreEqual(typeof(DateTime), descriptor.PropertyType,
                            "The Timestamp type should be a DateTime.");

            TimestampAttribute timestampAttribute = (TimestampAttribute)descriptor.Attributes[typeof(TimestampAttribute)];

            Assert.IsNotNull(timestampAttribute,
                             "The Timestamp should have a TimestampAttribute.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The Timestamp should have an EditableAttribute.");
            Assert.IsTrue(editableAttribute.AllowEdit,
                          "The Timestamp should allow editing.");
            Assert.IsTrue(editableAttribute.AllowInitialValue,
                          "The Timestamp should allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The Timestamp should have an DisplayAttribute.");
            Assert.AreEqual("T", displayAttribute.Name,
                            "The Timestamp names should be equal.");
        }
        public void OverwrittenRowKeyMetadata()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity2));

            PropertyDescriptor descriptor = properties["RowKey"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for RowKey.");
            Assert.AreEqual(typeof(string), descriptor.PropertyType,
                            "The RowKey type should be a string.");

            KeyAttribute keyAttribute = (KeyAttribute)descriptor.Attributes[typeof(KeyAttribute)];

            Assert.IsNotNull(keyAttribute,
                             "The RowKey should have a KeyAttribute.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The RowKey should have an EditableAttribute.");
            Assert.IsTrue(editableAttribute.AllowEdit,
                          "The RowKey should allow editing.");
            Assert.IsTrue(editableAttribute.AllowInitialValue,
                          "The RowKey should allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The RowKey should have an DisplayAttribute.");
            Assert.AreEqual("RK", displayAttribute.Name,
                            "The RowKey names shoulds be equal.");
        }
        public void DomainOperationEntry_All_Types()
        {
            DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(DomainOperationEntryTestDomainService));

            // Invoke
            DomainOperationEntry entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "InvokeMethod");

            Assert.IsNotNull(entry, "Could not locate InvokeMethod");
            Assert.IsNull(entry.AssociatedType, "Invoke should have no associated type");
            Assert.AreEqual(typeof(DomainOperationEntryTestDomainService), entry.DomainServiceType, "Wrong domain service type");
            Assert.AreEqual(DomainOperation.Invoke, entry.Operation, "Wrong domain operation");
            Assert.AreEqual("Invoke", entry.OperationType, "Wrong operation type for this DomainOperationEntry");

            // Invoke with entity
            entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "InvokeMethodEntity");
            Assert.IsNotNull(entry, "Could not locate InvokeMethodEntity");
            Assert.AreEqual(typeof(DomainOperationEntryTestEntity), entry.AssociatedType, "Wrong associated type");
            Assert.AreEqual(typeof(DomainOperationEntryTestDomainService), entry.DomainServiceType, "Wrong domain service type");
            Assert.AreEqual(DomainOperation.Invoke, entry.Operation, "Wrong domain operation");
            Assert.AreEqual("Invoke", entry.OperationType, "Wrong operation type for this DomainOperationEntry");

            // Query
            entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "GetEntities");
            Assert.IsNotNull(entry, "Could not locate GetEntities");
            Assert.AreEqual(typeof(DomainOperationEntryTestEntity), entry.AssociatedType, "Wrong associated type");
            Assert.AreEqual(typeof(DomainOperationEntryTestDomainService), entry.DomainServiceType, "Wrong domain service type");
            Assert.AreEqual(DomainOperation.Query, entry.Operation, "Wrong domain operation");
            Assert.AreEqual("Query", entry.OperationType, "Wrong operation type for this DomainOperationEntry");

            // Insert
            entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "InsertMethod");
            Assert.IsNotNull(entry, "Could not locate InsertMethod");
            Assert.AreEqual(typeof(DomainOperationEntryTestEntity), entry.AssociatedType, "Wrong associated type");
            Assert.AreEqual(typeof(DomainOperationEntryTestDomainService), entry.DomainServiceType, "Wrong domain service type");
            Assert.AreEqual(DomainOperation.Insert, entry.Operation, "Wrong domain operation"); Assert.AreEqual("Insert", entry.OperationType, "Wrong operation type for this DomainOperationEntry");

            // Update
            entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "UpdateMethod");
            Assert.IsNotNull(entry, "Could not locate InvokeMethod");
            Assert.AreEqual(typeof(DomainOperationEntryTestEntity), entry.AssociatedType, "Wrong associated type");
            Assert.AreEqual(typeof(DomainOperationEntryTestDomainService), entry.DomainServiceType, "Wrong domain service type");
            Assert.AreEqual(DomainOperation.Update, entry.Operation, "Wrong domain operation");
            Assert.AreEqual("Update", entry.OperationType, "Wrong operation type for this DomainOperationEntry");

            // Custom Update
            entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "CustomMethod");
            Assert.IsNotNull(entry, "Could not locate CustomMethod");
            Assert.AreEqual(typeof(DomainOperationEntryTestEntity), entry.AssociatedType, "Wrong associated type");
            Assert.AreEqual(typeof(DomainOperationEntryTestDomainService), entry.DomainServiceType, "Wrong domain service type");
            Assert.AreEqual(DomainOperation.Custom, entry.Operation, "Wrong domain operation");
            Assert.AreEqual("Update", entry.OperationType, "Wrong operation type for this DomainOperationEntry");

            // Delete
            entry = description.DomainOperationEntries.SingleOrDefault(p => p.Name == "DeleteMethod");
            Assert.IsNotNull(entry, "Could not locate DeleteMethod");
            Assert.AreEqual(typeof(DomainOperationEntryTestEntity), entry.AssociatedType, "Wrong associated type");
            Assert.AreEqual(typeof(DomainOperationEntryTestDomainService), entry.DomainServiceType, "Wrong domain service type");
            Assert.AreEqual(DomainOperation.Delete, entry.Operation, "Wrong domain operation");
            Assert.AreEqual("Delete", entry.OperationType, "Wrong operation type for this DomainOperationEntry");
        }
        public void GetDomainMethods_Sanity()
        {
            DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(DomainMethod_ValidProvider_MultipleMethods));

            // verify that GetDomainMethods with City type only returns methods that are associated with City
            IEnumerable <DomainOperationEntry> domainMethods = description.GetCustomMethods(typeof(City));

            Assert.IsNotNull(domainMethods);
            Assert.AreEqual(4, domainMethods.Count());
            Assert.IsNotNull(domainMethods.Single(m => m.Name == "ProcessCity"));
            Assert.IsNotNull(domainMethods.Single(m => m.Name == "AssignCityZone"));
            Assert.IsNotNull(domainMethods.Single(m => m.Name == "AssignCityZoneIfAuthorized"));
            Assert.IsNotNull(domainMethods.Single(m => m.Name == "AutoAssignCityZone"));
            Assert.IsNull(domainMethods.FirstOrDefault(m => m.Name == "ProcessCounty"));

            // verify that GetDomainMethods with Zip type returns one method
            domainMethods = description.GetCustomMethods(typeof(Zip));
            Assert.AreEqual(2, domainMethods.Count());
            Assert.IsNotNull(domainMethods.Single(m => m.Name == "ReassignZipCode"));

            // verify that GetDomainMethods with County type returns one method
            domainMethods = description.GetCustomMethods(typeof(County));
            Assert.AreEqual(1, domainMethods.Count());
            Assert.IsNotNull(domainMethods.Single(m => m.Name == "ProcessCounty"));

            // verify that GetDomainMethods return empty collection when passing in type that is not associated with any methods on the provider
            domainMethods = description.GetCustomMethods(typeof(State));
            Assert.IsNotNull(domainMethods);
            Assert.AreEqual(0, domainMethods.Count());
        }
Exemple #14
0
        public void TestDomainService_UpdateMemberToDefaultValue()
        {
            TestDomainServices.EF.Catalog service = ServerTestHelper.CreateInitializedDomainService <TestDomainServices.EF.Catalog>(DomainOperationType.Submit);

            DomainServiceDescription serviceDescription = DomainServiceDescription.GetDescription(service.GetType());

            // in the below, non RTO is simulated by leaving ReorderPoint as its default value in the
            // original instance
            AdventureWorksModel.Product currProduct = new AdventureWorksModel.Product {
                ProductID = 1, ReorderPoint = 0, Weight = 0
            };
            AdventureWorksModel.Product origProduct = new AdventureWorksModel.Product {
                ProductID = 1, Weight = 50.0M
            };

            // verify expected test state - this test relies on the below attribute values
            PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(AdventureWorksModel.Product))["ReorderPoint"];

            Assert.IsNull(pd.Attributes[typeof(RoundtripOriginalAttribute)]);
            pd = TypeDescriptor.GetProperties(typeof(AdventureWorksModel.Product))["Weight"];
            Assert.IsNotNull(pd.Attributes[typeof(RoundtripOriginalAttribute)]);
            pd = TypeDescriptor.GetProperties(typeof(AdventureWorksModel.Product))["SafetyStockLevel"];
            Assert.IsNotNull(pd.Attributes[typeof(ExcludeAttribute)]);

            ObjectContextExtensions.AttachAsModified(service.ObjectContext.Products, currProduct, origProduct);

            // verify the expected property modifications
            ObjectStateEntry stateEntry = service.ObjectContext.ObjectStateManager.GetObjectStateEntry(currProduct);

            string[] modifiedProperties = stateEntry.GetModifiedProperties().ToArray();
            Assert.IsTrue(modifiedProperties.Contains("ReorderPoint"));      // no RTO so this should be modified
            Assert.IsTrue(modifiedProperties.Contains("Weight"));            // RTO so this is picked up by normal value comparison
            Assert.IsFalse(modifiedProperties.Contains("SafetyStockLevel")); // excluded member, so shouldn't be marked modified
            Assert.IsFalse(modifiedProperties.Contains("ProductID"));        // key members shouldn't be marked modified
        }
        /// <summary>
        /// Helper method performs a query operation against a given proxy instance.
        /// </summary>
        /// <param name="domainService">The type of <see cref="DomainService"/> to perform this query operation against.</param>
        /// <param name="context">The current context.</param>
        /// <param name="domainServiceInstances">The list of tracked <see cref="DomainService"/> instances that any newly created
        /// <see cref="DomainServices"/> will be added to.</param>
        /// <param name="queryName">The name of the query to invoke.</param>
        /// <param name="parameters">The query parameters.</param>
        /// <returns>The query results. May be null if there are no query results.</returns>
        /// <exception cref="ArgumentNullException">if <paramref name="context"/> is null.</exception>
        /// <exception cref="ArgumentNullException">if <paramref name="queryName"/> is null or an empty string.</exception>
        /// <exception cref="InvalidOperationException">if no match query operation exists on the <paramref name="context"/>.</exception>
        /// <exception cref="OperationException">if operation errors are thrown during execution of the query operation.</exception>
        public static IEnumerable Query(Type domainService, DomainServiceContext context, IList <DomainService> domainServiceInstances, string queryName, object[] parameters)
        {
            context = new DomainServiceContext(context, DomainOperationType.Query);
            DomainService            service            = CreateDomainServiceInstance(domainService, context, domainServiceInstances);
            DomainServiceDescription serviceDescription = DomainServiceDescription.GetDescription(service.GetType());
            DomainOperationEntry     queryOperation     = serviceDescription.GetQueryMethod(queryName);

            if (queryOperation == null)
            {
                string errorMessage =
                    string.Format(
                        CultureInfo.InvariantCulture,
                        Resource.DomainServiceProxy_QueryOperationNotFound,
                        queryName,
                        domainService);

                throw new InvalidOperationException(errorMessage);
            }

            object[]         parameterValues  = parameters ?? Array.Empty <object>();
            QueryDescription queryDescription = new QueryDescription(queryOperation, parameterValues);

            var actualMethod = s_queryGeneric.MakeGenericMethod(queryDescription.Method.AssociatedType);

            try
            {
                return((IEnumerable)actualMethod.Invoke(null, new object[] { service, queryDescription }));
            }
            catch (TargetInvocationException tie) when(tie.InnerException is object)
            {
                throw tie.InnerException;
            }
        }
Exemple #16
0
 public void InvalidProvider_DupMethodName()
 {
     ExceptionHelper.ExpectException <InvalidOperationException>(delegate
     {
         DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(OnlineMethod_InvalidProvider_DupMethodName));
     }, string.Format(CultureInfo.CurrentCulture, Resource.DomainOperationEntryOverload_NotSupported, "TestMethod"));
 }
Exemple #17
0
        public async Task DomainService_DirectQuery()
        {
            DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(TestDomainServices.EF.Catalog));

            TestDomainServices.EF.Catalog service = new TestDomainServices.EF.Catalog();
            DomainServiceContext          dsc     = new DomainServiceContext(new MockDataService(new MockUser("mathew")
            {
                IsAuthenticated = true
            }), DomainOperationType.Query);

            service.Initialize(dsc);

            DomainOperationEntry queryOperation = description.GetQueryMethod("GetPurchaseOrders");

            ServiceQuery serviceQuery = new ServiceQuery();

            serviceQuery.QueryParts = new ServiceQueryPart[]
            {
                new ServiceQueryPart("where", "(it.Freight!=0)"),
                new ServiceQueryPart("take", "1")
            };

            QueryResult <AdventureWorksModel.PurchaseOrder> result = await QueryProcessor.ProcessAsync <AdventureWorksModel.PurchaseOrder>(service, queryOperation, Array.Empty <object>(), serviceQuery);

            Assert.AreEqual(1, result.RootResults.Count());
        }
Exemple #18
0
        public void ClientCodeGenerationDispatcher_Generate_Using_T4_Custom()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, null);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.AreEqual(generator.GetType(), typeof(T4DomainServiceClientCodeGenerator), "dispatcher found " + generator.GetType() + " but should have found T4DomainServiceClientProxyGenerator");

                DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(DispatcherDomainService));
                string generatedCode         = generator.GenerateCode(host, new DomainServiceDescription[] { dsd }, options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected T4 generator to generate code");
                TestHelper.AssertGeneratedCodeContains(generatedCode, T4DomainServiceClientCodeGenerator.GeneratedBoilerPlate);
                TestHelper.AssertGeneratedCodeContains(generatedCode, "public class DispatcherEntity : Entity");
                TestHelper.AssertNoErrorsOrWarnings(logger);
            }
        }
        public void RequiresRoleAttribute_Authorize_MultipleAttributes_Denied()
        {
            // Create user in only role1, which should be denied because we require (1 or 2) AND (3 or 4)
            IPrincipal user = this.CreateIPrincipal("user1", "role1");

            // Instantiate a new DomainService to use for an Invoke
            using (RequiresRoleTestService testDomainService = new RequiresRoleTestService())
            {
                testDomainService.Initialize(new DomainServiceContext(new MockDataService(user), DomainOperationType.Invoke));

                // Get a DomainServiceDescription for that same domain service
                DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(RequiresRoleTestService));

                // Locate the invoke method
                DomainOperationEntry invokeEntry = description.DomainOperationEntries.Single(p => p.Name == "Method1");

                // Ask the domain service to perform authorization.
                // The principal will be located via the mock data service created above.
                // Invokes do not expect an entity instance.
                AuthorizationResult result = testDomainService.IsAuthorized(invokeEntry, entity: null);

                Assert.AreNotSame(AuthorizationResult.Allowed, result, "Expected user in role1 to be denied against invoke requiring roles (1 or 2) && (3 or 4) in multiple attributes");

                // Validate the formatted denial message includes the invoke we attempted
                string expectedMessage = String.Format(CultureInfo.CurrentCulture, Resource.AuthorizationAttribute_Default_Message, "Method1");
                Assert.AreEqual(expectedMessage, result.ErrorMessage, "Expected default denial message plus name of the invoke method");
            }
        }
Exemple #20
0
        public void ServerValidation_Query()
        {
            TestDomainServices.TestProvider_Scenarios service = ServerTestHelper.CreateInitializedDomainService <TestDomainServices.TestProvider_Scenarios>(DomainOperationType.Query);

            DomainServiceDescription serviceDescription = DomainServiceDescription.GetDescription(service.GetType());
            DomainOperationEntry     method             = serviceDescription.DomainOperationEntries.Single(p => p.Name == "QueryWithParamValidation");
            QueryDescription         qd = new QueryDescription(method, new object[] { -1, "ABC" });
            int totalCount;

            IEnumerable <ValidationResult> validationErrors;

            service.Query(qd, out validationErrors, out totalCount);

            Assert.IsNotNull(validationErrors);
            Assert.AreEqual(2, validationErrors.Count());

            ValidationResult error = validationErrors.ElementAt(0);

            Assert.AreEqual("The field a must be between 0 and 10.", error.ErrorMessage);
            Assert.AreEqual("a", error.MemberNames.Single());

            error = validationErrors.ElementAt(1);
            Assert.AreEqual("The field b must be a string with a maximum length of 2.", error.ErrorMessage);
            Assert.AreEqual("b", error.MemberNames.Single());
        }
Exemple #21
0
        public void ClientCodeGenerationDispatcher_Finds_Default_By_Name()
        {
            ConsoleLogger logger = new ConsoleLogger();
            ClientCodeGenerationOptions options = new ClientCodeGenerationOptions()
            {
                Language = "C#"
            };

            ICodeGenerationHost host = TestHelper.CreateMockCodeGenerationHost(logger, /*sharedTypeService*/ null);

            // Create a new dispatcher and call an internal extensibility point to add ourselves
            // into the MEF composition container
            using (ClientCodeGenerationDispatcher dispatcher = new ClientCodeGenerationDispatcher())
            {
                string[] compositionAssemblies = new string[] { Assembly.GetExecutingAssembly().Location, typeof(T4DomainServiceClientCodeGenerator).Assembly.Location };

                IDomainServiceClientCodeGenerator generator = dispatcher.FindCodeGenerator(host, options, compositionAssemblies, CodeDomClientCodeGenerator.GeneratorName);
                Assert.IsNotNull(generator, "the dispatcher did not find any code generator");
                Assert.IsTrue(typeof(CodeDomClientCodeGenerator).IsAssignableFrom(generator.GetType()), "dispatcher found " + generator.GetType() + " but should have found CodeDomClientCodeGenerator");

                DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(DispatcherDomainService));
                string generatedCode         = generator.GenerateCode(host, new DomainServiceDescription[] { dsd }, options);

                Assert.IsFalse(string.IsNullOrEmpty(generatedCode), "expected code to have been generated");
                Assert.IsTrue(generatedCode.Contains("public sealed partial class DispatcherDomainContext : DomainContext"), "Expected generated code to contain public sealed partial class DispatcherDomainContext : DomainContext");
            }
        }
        public async Task DomainService_CallSubmitDirectly()
        {
            DomainServiceDescription description      = DomainServiceDescription.GetDescription(typeof(DomainMethod_ValidProvider_MultipleMethods));
            List <ChangeSetEntry>    changeSetEntries = new List <ChangeSetEntry>();

            ChangeSetEntry processCityOperation = new ChangeSetEntry();

            processCityOperation.Entity = new City {
                Name = "Redmond", CountyName = "King", StateName = "WA"
            };
            processCityOperation.DomainOperationEntry = description.GetCustomMethod(typeof(City), "ProcessCity");
            processCityOperation.Operation            = DomainOperation.Update;
            processCityOperation.EntityActions        = new EntityActionCollection {
                { "ProcessCity", new object[] { new byte[] { byte.MaxValue, byte.MinValue, 123 } } }
            };
            changeSetEntries.Add(processCityOperation);

            ChangeSet changeset = new ChangeSet(changeSetEntries);
            DomainMethod_ValidProvider_MultipleMethods myTestProvider = ServerTestHelper.CreateInitializedDomainService <DomainMethod_ValidProvider_MultipleMethods>(DomainOperationType.Submit);
            await myTestProvider.SubmitAsync(changeset, CancellationToken.None);

            // check that the domain services have invoked the domain method correctly by checking the internal variables set
            Assert.AreEqual <string>("ProcessCity_", myTestProvider.Invoked);
            Assert.AreEqual <int>(3, myTestProvider.InputData.Length);
            Assert.AreEqual <byte>(123, myTestProvider.InputData[2]);
        }
Exemple #23
0
        public void DomainService_DirectQuery()
        {
            DomainServiceDescription description = DomainServiceDescription.GetDescription(typeof(TestDomainServices.EF.Catalog));

            TestDomainServices.EF.Catalog service = new TestDomainServices.EF.Catalog();
            DomainServiceContext          dsc     = new DomainServiceContext(new MockDataService(new MockUser("mathew")
            {
                IsAuthenticated = true
            }), DomainOperationType.Query);

            service.Initialize(dsc);

            DomainOperationEntry queryOperation = description.GetQueryMethod("GetPurchaseOrders");

            ServiceQuery serviceQuery = new ServiceQuery();

            serviceQuery.QueryParts = new ServiceQueryPart[]
            {
                new ServiceQueryPart("where", "(it.Freight!=0)"),
                new ServiceQueryPart("take", "1")
            };

            IEnumerable <ValidationResult> validationErrors;
            int totalCount;
            QueryResult <AdventureWorksModel.PurchaseOrder> result = QueryProcessor.Process <AdventureWorksModel.PurchaseOrder>(service, queryOperation, new object[0], serviceQuery, out validationErrors, out totalCount);

            Assert.AreEqual(1, result.RootResults.Count());
        }
        public void OtherArgsMustBeOfPredefinedTypes()
        {
            ExceptionHelper.ExpectException <InvalidOperationException>(delegate
            {
                DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_MultipleEntities));
            }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainOperationEntry_ParamMustBeSimple, "ProcessCity", "county"));

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate
            {
                DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_ArgOfTypeObject));
            }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainOperationEntry_ParamMustBeSimple, "ProcessCity", "objArg"));

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate
            {
                DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_ArgOfTypeIntPtr));
            }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainOperationEntry_ParamMustBeSimple, "ProcessCity", "intPtrArg"));

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate
            {
                DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_ArgOfTypeUIntPtr));
            }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainOperationEntry_ParamMustBeSimple, "ProcessCity", "uintPtrArg"));

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate
            {
                DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_ArgOfComplexTypeIEnumerable));
            }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainOperationEntry_ParamMustBeSimple, "ProcessCity", "ienumerableArg"));

            ExceptionHelper.ExpectException <InvalidOperationException>(delegate
            {
                DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_ArgOfComplexTypeList));
            }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainOperationEntry_ParamMustBeSimple, "ProcessCity", "listArg"));
        }
 public void ParameterlessDomainMethodNotSupported()
 {
     ExceptionHelper.ExpectException <InvalidOperationException>(delegate
     {
         DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_Parameterless));
     }, Resource.InvalidCustomMethod_MethodCannotBeParameterless);
 }
 public void FirstArgMustBeEntity()
 {
     ExceptionHelper.ExpectException <InvalidOperationException>(delegate
     {
         DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_FirstArgNonEntity));
     }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainMethod_ParamMustBeEntity, "name", "ProcessCity"));
 }
 public void InvalidReturnType()
 {
     ExceptionHelper.ExpectException <InvalidOperationException>(delegate
     {
         DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_InvalidReturnType));
     }, string.Format(CultureInfo.CurrentCulture, Resource.InvalidDomainOperationEntry_NonQueryMustReturnVoid, "ProcessCity"));
 }
 public void OverloadsNotSupported()
 {
     ExceptionHelper.ExpectException <InvalidOperationException>(delegate
     {
         DomainServiceDescription.GetDescription(typeof(DomainMethod_InvalidProvider_MethodOverloads));
     }, string.Format(CultureInfo.CurrentCulture, Resource.DomainOperationEntryOverload_NotSupported, "ProcessCity"));
 }
Exemple #29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DomainServiceHost"/> class with
        /// the type of service and its base addresses specified.
        /// </summary>
        /// <param name="domainServiceType">The type of the <see cref="DomainService"/> to host.</param>
        /// <param name="baseAddresses">
        /// An array of type <see cref="System.Uri"/> that contains the base addresses for
        /// the hosted service.
        /// </param>
        public DomainServiceHost(Type domainServiceType, params Uri[] baseAddresses)
        {
            if (domainServiceType == null)
            {
                throw new ArgumentNullException("domainServiceType");
            }

            if (baseAddresses == null)
            {
                throw new ArgumentNullException("baseAddresses");
            }

            EnableClientAccessAttribute att = (EnableClientAccessAttribute)TypeDescriptor.GetAttributes(domainServiceType)[typeof(EnableClientAccessAttribute)];

            // Filter out all non-HTTP addresses.
            HashSet <string> allowedSchemes = DomainServiceHost._allowedSchemes;

            // Additionally filter out HTTP addresses if this DomainService requires a secure endpoint.
            if (att != null && att.RequiresSecureEndpoint)
            {
                allowedSchemes = DomainServiceHost._allowedSecureSchemes;
            }

            // Apply the filter.
            baseAddresses = baseAddresses.Where(addr => allowedSchemes.Contains(addr.Scheme)).ToArray();

            this._domainServiceDescription = DomainServiceDescription.GetDescription(domainServiceType);
            this.InitializeDescription(domainServiceType, new UriSchemeKeyedCollection(baseAddresses));
        }
        public async Task Authorization_Custom_Authorization_On_Query()
        {
            CityDomainService        cities             = new CityDomainService();
            DomainServiceDescription serviceDescription = DomainServiceDescription.GetDescription(typeof(CityDomainService));
            DomainOperationEntry     getZipsIfUser      = serviceDescription.GetQueryMethod("GetZipsIfUser");

            // The attribute permits only a user named mathew to access the query
            MockUser        user        = new MockUser("NotZipGuy");
            MockDataService dataService = new MockDataService(user);

            cities.Initialize(new WcfDomainServiceContext(dataService, DomainOperationType.Query));

            // not authenticated should be denied cleanly because there is no user name
            Exception expectedException = null;

            System.Collections.IEnumerable result;
            try
            {
                user.IsAuthenticated = false;
                result = (await cities.QueryAsync <Zip>(new QueryDescription(getZipsIfUser), CancellationToken.None)).Result;
            }
            catch (UnauthorizedAccessException e)
            {
                expectedException = e;
            }
            Assert.IsNotNull(expectedException);
            Assert.AreEqual("Only one user is authorized for this query, and it isn't you.", expectedException.Message, "Expected this custom authorization deny message for non-authenticated user.");

            // Authenticated, but still not the right user name -- should be denied
            cities            = new CityDomainService();
            expectedException = null;
            user = new MockUser("NotZipGuy", new string[] { "clerk" });
            user.IsAuthenticated = true;
            dataService          = new MockDataService(user);
            cities.Initialize(new WcfDomainServiceContext(dataService, DomainOperationType.Query));
            try
            {
                result = (await cities.QueryAsync <Zip>(new QueryDescription(getZipsIfUser), CancellationToken.None))
                         .Result;
            }
            catch (UnauthorizedAccessException e)
            {
                expectedException = e;
            }
            Assert.IsNotNull(expectedException);
            Assert.AreEqual("Only one user is authorized for this query, and it isn't you.", expectedException.Message, "Expected this custom authorization deny message for authenticated user with wrong name.");

            // authenticated and in with the right name -- should be allowed
            cities = new CityDomainService();
            user   = new MockUser("ZipGuy");
            user.IsAuthenticated = true;
            dataService          = new MockDataService(user);
            cities.Initialize(new WcfDomainServiceContext(dataService, DomainOperationType.Query));
            var queryResult = await cities.QueryAsync <Zip>(new QueryDescription(getZipsIfUser), CancellationToken.None);

            Assert.IsNotNull(queryResult.Result);
            Assert.IsNull(queryResult.ValidationErrors);
            Assert.IsTrue(queryResult.Result.OfType <Zip>().Any(), "Expected non-zero number of zip codes returned");
        }