public void GetChildren_WithAddress1HighStreet_IncludesAddressKey() { var applicationContext = _testDIContext.CreateTestApplicationContext(); // Arrange SerializationInfo serializationInfo = new SerializationInfo(); bool expected = true; bool actual; IMobileObject mobileObject; PersonPOCO person = new PersonPOCO(); person.Address = new AddressPOCO() { AddressLine1 = "1 High Street" }; MobileFormatter formatter = new MobileFormatter(applicationContext); // Act mobileObject = (IMobileObject)person; mobileObject.GetChildren(serializationInfo, formatter); actual = serializationInfo.Children.ContainsKey("Address"); // Assert Assert.AreEqual(expected, actual); }
public void SerializeDataPortalException() { var obj = new Csla.Server.DataPortalException("test message", new Exception("inner message"), null); var applicationContext = _testDIContext.CreateTestApplicationContext(); var cloner = new Core.ObjectCloner(applicationContext); var obj2 = (Csla.Server.DataPortalException)cloner.Clone(obj); Assert.IsFalse(ReferenceEquals(obj, obj2)); Assert.AreEqual(obj.Message, obj2.Message); }
public async Task DataPortal_Create_Calls_IAuthorizeDataPortal_Authorize_WithCorrectParameters() { var applicationContext = _testDIContext.CreateTestApplicationContext(); var dp = _testDIContext.ServiceProvider.GetRequiredService <TestableDataPortal>(); await dp.Create(typeof(TestBO), null, new DataPortalContext(applicationContext, applicationContext.Principal, false), true); var result = (AuthorizeDataPortalStub)dp.AuthProvider; Assert.IsNotNull(result, "AuthorizeDataPortalStub not accessible"); Assert.AreEqual(typeof(TestBO), result.ClientRequest?.ObjectType); Assert.AreEqual(DataPortalOperations.Create, result.ClientRequest.Operation); }
public void TestUnAuthExecute() { IDataPortal <PermissionsRoot> dataPortal = _anonymousDIContext.CreateDataPortal <PermissionsRoot>(); ApplicationContext applicationContext = _anonymousDIContext.CreateTestApplicationContext(); TestResults.Reinitialise(); Assert.AreEqual(false, applicationContext.User.IsInRole("Admin")); PermissionsRoot pr = dataPortal.Create(); //should fail, because we're not an admin pr.DoWork(); }
public void If_FieldBackedString_Is_Changed_On_GrandChild_List_Item_After_Root_Is_Deserialized_Then_Root_ChildChanged_Event_Fires() { IDataPortal <EditableGetSet> dataPortal = _testDIContext.CreateDataPortal <EditableGetSet>(); IChildDataPortal <EditableGetSet> childDataPortal = _testDIContext.CreateChildDataPortal <EditableGetSet>(); var root = EditableGetSet.GetObject(dataPortal); var child = EditableGetSet.NewChildObject(childDataPortal); var grandChild = EditableGetSet.NewChildObject(childDataPortal); root.ManagedChildList.Add(child); child.ManagedChildList.Add(grandChild); var applicationContext = _testDIContext.CreateTestApplicationContext(); MemoryStream stream = new MemoryStream(); MobileFormatter formatter = new MobileFormatter(applicationContext); formatter.Serialize(stream, root); stream.Seek(0, SeekOrigin.Begin); root = (EditableGetSet)formatter.Deserialize(stream); int changed = 0; root.ChildChanged += (o, e) => { changed++; }; root.ManagedChildList[0].ManagedChildList[0].FieldBackedString = "changed"; Assert.AreEqual(1, changed, "after MobileFormatter"); changed = 0; root = root.Clone(); root.ChildChanged += (o, e) => { changed++; }; root.ManagedChildList[0].ManagedChildList[0].FieldBackedString = "changed again"; Assert.AreEqual(1, changed, "after clone"); }
public void TwoRules() { var context = GetContext(); var root = new TwoPropertyRules(); var rule = new TwoProps(TwoPropertyRules.Value1Property, TwoPropertyRules.Value2Property); var applicationContext = _testDIContext.CreateTestApplicationContext(); var ctx = new Csla.Rules.RuleContext(applicationContext, null, rule, root, new Dictionary <Core.IPropertyInfo, object> { { TwoPropertyRules.Value1Property, "a" }, { TwoPropertyRules.Value2Property, "b" } }); ((Csla.Rules.IBusinessRule)rule).Execute(ctx); context.Assert.AreEqual(0, ctx.Results.Count); ctx = new Csla.Rules.RuleContext(applicationContext, null, rule, root, new Dictionary <Core.IPropertyInfo, object> { { TwoPropertyRules.Value1Property, "" }, { TwoPropertyRules.Value2Property, "a" } }); ((Csla.Rules.IBusinessRule)rule).Execute(ctx); context.Assert.AreEqual(1, ctx.Results.Count); context.Assert.Success(); context.Complete(); }
public void ClientContext() { IDataPortal <Basic.Root> dataPortal = _testDIContext.CreateDataPortal <Basic.Root>(); ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext(); TestResults.Reinitialise(); applicationContext.ClientContext.Add("clientcontext", "client context data"); Assert.AreEqual("client context data", applicationContext.ClientContext["clientcontext"], "Matching data not retrieved"); Basic.Root root = dataPortal.Create(new Basic.Root.Criteria()); root.Data = "saved"; Assert.AreEqual("saved", root.Data, "Root data should be 'saved'"); Assert.AreEqual(true, root.IsDirty, "Object should be dirty"); Assert.AreEqual(true, root.IsValid, "Object should be valid"); TestResults.Reinitialise(); root = root.Save(); Assert.IsNotNull(root, "Root object should not be null"); Assert.AreEqual("Inserted", TestResults.GetResult("Root"), "Object not inserted"); Assert.AreEqual("saved", root.Data, "Root data should be 'saved'"); Assert.AreEqual(false, root.IsNew, "Object should not be new"); Assert.AreEqual(false, root.IsDeleted, "Object should not be deleted"); Assert.AreEqual(false, root.IsDirty, "Object should not be dirty"); //TODO: Is there a modern equivalent of this? //Assert.AreEqual("client context data", Csla.ApplicationContext.ClientContext["clientcontext"], "Client context data lost"); Assert.AreEqual("client context data", TestResults.GetResult("clientcontext"), "Global context data lost"); Assert.AreEqual("new global value", TestResults.GetResult("globalcontext"), "New global value lost"); }
public void Initialize_PrimitiveCriteria_NoExceptionRaised() { // Arrange bool executed = false; PrimitiveCriteria criteria = new PrimitiveCriteria(1); ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext(); RevalidatingInterceptor sut = new RevalidatingInterceptor(applicationContext); InterceptArgs args = new InterceptArgs() { ObjectType = typeof(Root), Operation = DataPortalOperations.Update, Parameter = criteria, IsSync = true }; applicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server); applicationContext.LocalContext["__logicalExecutionLocation"] = ApplicationContext.LogicalExecutionLocations.Server; // Act sut.Initialize(args); executed = true; // Assert Assert.IsTrue(executed); }
public void PerTypeAuthEditObjectViaInterface() { TestDIContext customDIContext = TestDIContextFactory.CreateContext( options => options.DataPortal( dp => dp.AddServerSideDataPortal(cfg => cfg.RegisterActivator <PerTypeAuthDPActivator>()) )); ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext(); Assert.IsFalse(BusinessRules.HasPermission(applicationContext, AuthorizationActions.EditObject, typeof(IPerTypeAuthRoot))); }
public void UseCustomSerializationFormatter() { TestDIContext customDIContext = TestDIContextFactory.CreateContext(options => options .Serialization(cfg => cfg .SerializationFormatter(typeof(NetDataContractSerializerWrapper)))); ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext(); var formatter = SerializationFormatterFactory.GetFormatter(applicationContext); Assert.IsInstanceOfType(formatter, typeof(NetDataContractSerializerWrapper)); }
public void SerializeCslaClaimsPrincipal() { var identity = new System.Security.Principal.GenericIdentity("rocky", "custom"); var principal = new Csla.Security.CslaClaimsPrincipal(identity); var applicationContext = _testDIContext.CreateTestApplicationContext(); var cloner = new Core.ObjectCloner(applicationContext); var clone = (Csla.Security.CslaClaimsPrincipal)cloner.Clone(principal); Assert.AreEqual(principal.Identity.Name, clone.Identity.Name); Assert.AreEqual(principal.Identity.AuthenticationType, clone.Identity.AuthenticationType); }
public void UseMobileFormatter() { ApplicationContext applicationContext; applicationContext = _testDIContext.CreateTestApplicationContext(); var serializer = ApplicationContext.SerializationFormatter; Assert.AreEqual(typeof(Csla.Serialization.Mobile.MobileFormatter), serializer); var s = Csla.Serialization.SerializationFormatterFactory.GetFormatter(applicationContext); Assert.IsInstanceOfType(s, typeof(Csla.Serialization.Mobile.MobileFormatter)); }
/// <summary> /// Create an Http Proxy instance for use in testing /// </summary> /// <returns>An instance of Csla.Channels.Http.HttpProxy for use in testing</returns> private Csla.Channels.Http.HttpProxy CreateTestHttpProxy() { Csla.Channels.Http.HttpProxy proxy; Csla.Channels.Http.HttpProxyOptions proxyOptions; System.Net.Http.HttpClient httpClient; var applicationContext = _testDIContext.CreateTestApplicationContext(); httpClient = new System.Net.Http.HttpClient(); proxyOptions = new Csla.Channels.Http.HttpProxyOptions(); proxy = new Csla.Channels.Http.HttpProxy(applicationContext, httpClient, proxyOptions); return(proxy); }
public void TestLogicalExecutionLocation() { IDataPortal <LocationBusinessBase> dataPortal = _testDIContext.CreateDataPortal <LocationBusinessBase>(); var applicationContext = _testDIContext.CreateTestApplicationContext(); Assert.AreEqual(ApplicationContext.LogicalExecutionLocations.Client, applicationContext.LogicalExecutionLocation, "Should be client"); #pragma warning disable CS0436 // Type conflicts with imported type LocationBusinessBase item = LocationBusinessBase.GetLocationBusinessBase(dataPortal); #pragma warning restore CS0436 // Type conflicts with imported type Assert.AreEqual(Csla.ApplicationContext.LogicalExecutionLocations.Server.ToString(), item.Data, "Should be server"); Assert.AreEqual(Csla.ApplicationContext.LogicalExecutionLocations.Server.ToString(), item.NestedData, "Nested should be server"); Assert.AreEqual(Csla.ApplicationContext.LogicalExecutionLocations.Client, applicationContext.LogicalExecutionLocation, "Should be client"); }
public void CloneClaimsPrincipal() { ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext(); var i = new ClaimsIdentity(); i.AddClaim(new Claim("name", "Franklin")); var p = new ClaimsPrincipal(i); var p1 = (ClaimsPrincipal)Core.ObjectCloner.GetInstance(applicationContext).Clone(p); Assert.AreNotSame(p, p1, "Should be different instances"); Assert.AreEqual(p.Claims.Count(), p1.Claims.Count(), "Should have same number of claims"); var c = p1.Claims.Where(r => r.Type == "name").First(); Assert.AreEqual("Franklin", c.Value, "Claim value should match"); }
public void AddErrorResultThrowsErrorWhenMessageIsEmpty() { var root = new TwoPropertyRules(); var rule = new TwoProps(TwoPropertyRules.Value1Property, TwoPropertyRules.Value2Property); var applicationContext = _testDIContext.CreateTestApplicationContext(); var ctx = new Csla.Rules.RuleContext(applicationContext, null, rule, root, new Dictionary <Core.IPropertyInfo, object> { { TwoPropertyRules.Value1Property, "a" }, { TwoPropertyRules.Value2Property, "b" } }); ctx.AddErrorResult(string.Empty, false); Assert.Fail("Must throw exception."); }
private NestingPOCO SerializeThenDeserialiseNestingPOCO(NestingPOCO valueToSerialize) { var applicationContext = _testDIContext.CreateTestApplicationContext(); System.IO.MemoryStream serializationStream; NestingPOCO deserializedValue; MobileFormatter formatter = new MobileFormatter(applicationContext); // Act using (serializationStream = new System.IO.MemoryStream()) { formatter.Serialize(serializationStream, valueToSerialize); serializationStream.Seek(0, System.IO.SeekOrigin.Begin); deserializedValue = formatter.Deserialize(serializationStream) as NestingPOCO; } return(deserializedValue); }
public void SerializationTest() { Csla.SmartDate d2; Csla.SmartDate clone; MemoryStream memoryStream; MobileFormatter mobileFormatter; ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext(); d2 = new Csla.SmartDate(); memoryStream = new MemoryStream(); mobileFormatter = new MobileFormatter(applicationContext); mobileFormatter.Serialize(memoryStream, d2); memoryStream.Seek(0, SeekOrigin.Begin); clone = (Csla.SmartDate)mobileFormatter.Deserialize(memoryStream); Assert.AreEqual(d2, clone, "Dates should have ben the same"); d2 = new Csla.SmartDate(DateTime.Now, false); memoryStream = new MemoryStream(); mobileFormatter = new MobileFormatter(applicationContext); mobileFormatter.Serialize(memoryStream, d2); memoryStream.Seek(0, SeekOrigin.Begin); clone = (Csla.SmartDate)mobileFormatter.Deserialize(memoryStream); Assert.AreEqual(d2, clone, "Dates should have ben the same"); d2 = new Csla.SmartDate(DateTime.Now.AddDays(10), false); d2.FormatString = "YYYY/DD/MM"; memoryStream = new MemoryStream(); mobileFormatter = new MobileFormatter(applicationContext); mobileFormatter.Serialize(memoryStream, d2); memoryStream.Seek(0, SeekOrigin.Begin); clone = (Csla.SmartDate)mobileFormatter.Deserialize(memoryStream); Assert.AreEqual(d2, clone, "Dates should have ben the same"); //cslalighttest.Serialization.PersonWIthSmartDateField person; //person = cslalighttest.Serialization.PersonWIthSmartDateField.GetPersonWIthSmartDateField("Sergey", 2000); //Assert.AreEqual(person.Birthdate, person.Clone().Birthdate, "Dates should have ben the same"); // //Csla.SmartDate expected = person.Birthdate; //person.BeginEdit(); //person.Birthdate = new Csla.SmartDate(expected.Date.AddDays(10)); // to guarantee it's a different value //person.CancelEdit(); //Csla.SmartDate actual = person.Birthdate; //Assert.AreEqual(expected, actual); }
public void TestAuthExecute() { TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin")); IDataPortal <PermissionsRoot> dataPortal = customDIContext.CreateDataPortal <PermissionsRoot>(); ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext(); TestResults.Reinitialise(); PermissionsRoot pr = dataPortal.Create(); //should work, because we are now logged in as an admin pr.DoWork(); Assert.AreEqual(true, applicationContext.Principal.IsInRole("Admin")); //set to null so the other testmethods continue to throw exceptions applicationContext.User = new ClaimsPrincipal(); Assert.AreEqual(false, applicationContext.Principal.IsInRole("Admin")); }
public void TestAuthorizationAfterEditCycle() { TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin")); IDataPortal <PermissionsRoot> dataPortal = customDIContext.CreateDataPortal <PermissionsRoot>(); ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext(); TestResults.Reinitialise(); PermissionsRoot pr = dataPortal.Create(); pr.FirstName = "something"; pr.BeginEdit(); pr.FirstName = "ba"; pr.CancelEdit(); applicationContext.User = new ClaimsPrincipal(); PermissionsRoot prClone = pr.Clone(); applicationContext.User = GetPrincipal("Admin"); prClone.FirstName = "somethiansdfasdf"; }
public void TestAuthBeginEditRules() { Guid managerInstanceId; TestDIContext customDIContext = TestDIContextFactory.CreateContext(GetPrincipal("Admin")); IDataPortal <DataPortal.DpRoot> dataPortal = customDIContext.CreateDataPortal <DataPortal.DpRoot>(); ApplicationContext applicationContext = customDIContext.CreateTestApplicationContext(); TestResults.Reinitialise(); DataPortal.DpRoot root = dataPortal.Create(new DataPortal.DpRoot.Criteria()); Assert.AreEqual(true, applicationContext.Principal.IsInRole("Admin")); root.Data = "Something new"; root.BeginEdit(); #region "Pre-Testing" root.Data = "Something new 1"; //Is it denying read properly? managerInstanceId = ((ApplicationContextManagerUnitTests)applicationContext.ContextManager).InstanceId; Debug.WriteLine(managerInstanceId); string result = root.DenyReadOnProperty; //Assert.AreEqual(managerInstanceId.ToString(), result); Assert.AreEqual("[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty, "Read should have been denied"); //Is it denying write properly? root.DenyWriteOnProperty = "DenyWriteOnProperty"; Assert.AreEqual("[DenyWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied"); //Is it denying both read and write properly? Assert.AreEqual("[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty, "Read should have been denied"); root.DenyReadWriteOnProperty = "DenyReadWriteONproperty"; Assert.AreEqual("[DenyReadWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied"); //Is it allowing both read and write properly? Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth, "Read should have been allowed"); root.AllowReadWriteOnProperty = "No value"; Assert.AreEqual("No value", root.Auth, "Write should have been allowed"); #endregion #region "Cancel Edit" //Cancel the edit and see if the authorization rules still work root.CancelEdit(); //Is it denying read properly? Assert.AreEqual("[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty, "Read should have been denied"); //Is it denying write properly? root.DenyWriteOnProperty = "DenyWriteOnProperty"; Assert.AreEqual("[DenyWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied"); //Is it denying both read and write properly? Assert.AreEqual("[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty, "Read should have been denied"); root.DenyReadWriteOnProperty = "DenyReadWriteONproperty"; Assert.AreEqual("[DenyReadWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied"); //Is it allowing both read and write properly? Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth, "Read should have been allowed"); root.AllowReadWriteOnProperty = "No value"; Assert.AreEqual("No value", root.Auth, "Write should have been allowed"); #endregion #region "Apply Edit" //Apply this edit and see if the authorization rules still work //Is it denying read properly? Assert.AreEqual("[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty, "Read should have been denied"); //Is it denying write properly? root.DenyWriteOnProperty = "DenyWriteOnProperty"; Assert.AreEqual("[DenyWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied"); //Is it denying both read and write properly? Assert.AreEqual("[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty, "Read should have been denied"); root.DenyReadWriteOnProperty = "DenyReadWriteONproperty"; Assert.AreEqual("[DenyReadWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied"); //Is it allowing both read and write properly? Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth, "Read should have been allowed"); root.AllowReadWriteOnProperty = "No value"; Assert.AreEqual("No value", root.Auth, "Write should have been allowed"); #endregion }
public void TestAuthCloneRules() { IDataPortal <DataPortal.DpRoot> dataPortal = _adminDIContext.CreateDataPortal <DataPortal.DpRoot>(); ApplicationContext applicationContext = _adminDIContext.CreateTestApplicationContext(); TestResults.Reinitialise(); DataPortal.DpRoot root = dataPortal.Fetch(new DataPortal.DpRoot.Criteria()); Assert.AreEqual(true, applicationContext.User.IsInRole("Admin")); #region "Pre Cloning Tests" //Is it denying read properly? Assert.AreEqual("[DenyReadOnProperty] Can't read property", root.DenyReadOnProperty, "Read should have been denied 1"); //Is it denying write properly? root.DenyWriteOnProperty = "DenyWriteOnProperty"; Assert.AreEqual("[DenyWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied 2"); //Is it denying both read and write properly? Assert.AreEqual("[DenyReadWriteOnProperty] Can't read property", root.DenyReadWriteOnProperty, "Read should have been denied 3"); root.DenyReadWriteOnProperty = "DenyReadWriteONproperty"; Assert.AreEqual("[DenyReadWriteOnProperty] Can't write variable", root.Auth, "Write should have been denied 4"); //Is it allowing both read and write properly? Assert.AreEqual(root.AllowReadWriteOnProperty, root.Auth, "Read should have been allowed 5"); root.AllowReadWriteOnProperty = "No value"; Assert.AreEqual("No value", root.Auth, "Write should have been allowed 6"); #endregion #region "After Cloning Tests" //Do they work under cloning as well? DataPortal.DpRoot newRoot = root.Clone(); TestResults.Reinitialise(); //Is it denying read properly? Assert.AreEqual("[DenyReadOnProperty] Can't read property", newRoot.DenyReadOnProperty, "Read should have been denied 7"); //Is it denying write properly? newRoot.DenyWriteOnProperty = "DenyWriteOnProperty"; Assert.AreEqual("[DenyWriteOnProperty] Can't write variable", newRoot.Auth, "Write should have been denied 8"); //Is it denying both read and write properly? Assert.AreEqual("[DenyReadWriteOnProperty] Can't read property", newRoot.DenyReadWriteOnProperty, "Read should have been denied 9"); newRoot.DenyReadWriteOnProperty = "DenyReadWriteONproperty"; Assert.AreEqual("[DenyReadWriteOnProperty] Can't write variable", newRoot.Auth, "Write should have been denied 10"); //Is it allowing both read and write properly? Assert.AreEqual(newRoot.AllowReadWriteOnProperty, newRoot.Auth, "Read should have been allowed 11"); newRoot.AllowReadWriteOnProperty = "AllowReadWriteOnProperty"; Assert.AreEqual("AllowReadWriteOnProperty", newRoot.Auth, "Write should have been allowed 12"); #endregion }
public void MergeInsert() { ApplicationContext applicationContext = _testDIContext.CreateTestApplicationContext(); IDataPortal <Foo> dataPortal = _testDIContext.CreateDataPortal <Foo>(); var obj = dataPortal.Create(); obj.Name = "1"; var cloned = obj.Clone(); cloned.Name = "2"; cloned.MockUpdated(); var changed = false; obj.PropertyChanged += (o, e) => { changed = true; }; var merger = new GraphMerger(applicationContext); merger.MergeGraph(obj, cloned); Assert.AreEqual(cloned.Name, obj.Name); Assert.AreEqual(cloned.IsDirty, obj.IsDirty); Assert.AreEqual(cloned.IsNew, obj.IsNew); Assert.IsFalse(obj.IsNew); Assert.IsFalse(obj.IsDirty); Assert.IsFalse(obj.IsValid); Assert.IsTrue(changed); }