Esempio n. 1
0
        public void Clone()
        {
            var emily = new Ogre()
            {
                Name          = "Emily",
                Key           = new PersistonKey("Ogre", "3", false),
                Money         = 2,
                OgreId        = 3,
                PaymentMethod = new List <Ogre.PaymentMethodRow>
                {
                    new Ogre.PaymentMethodRow {
                        Method = "THUMP", Notes = "taking stuff without money"
                    }
                }
            };

            var ddict = new DataDictionary();

            ddict.AddDatonUsingClassAnnotation(typeof(Ogre));
            var ogredef = ddict.DatonDefs["Ogre"];

            var clone = emily.Clone(ogredef) as Ogre;

            Assert.AreEqual("Emily", clone.Name);
            Assert.AreEqual(1, clone.PaymentMethod.Count);
            Assert.AreEqual("THUMP", clone.PaymentMethod[0].Method);
        }
Esempio n. 2
0
        public void View()
        {
            var xorg = new Ogre
            {
                Key           = new PersistonKey("Ogre", DatonKey.NEWPK, false),
                Name          = "Xorg",
                Money         = 4,
                PaymentMethod = new List <Ogre.PaymentMethodRow>
                {
                    new Ogre.PaymentMethodRow {
                        Method = "credit", Notes = "usually declined"
                    }
                }
            };

            //bill can't view money
            var bill = new User();

            bill.Roles = new[]
            {
                new RetroRole
                {
                    BaseLevel      = PermissionLevel.View,
                    TableOverrides = new List <TablePermission>
                    {
                        new TablePermission
                        {
                            TableName       = "Ogre",
                            BaseLevel       = PermissionLevel.View | PermissionLevel.Modify,
                            ColumnOverrides = new List <ColumnPermission>
                            {
                                new ColumnPermission {
                                    ColumnName = "Money", BaseLevel = PermissionLevel.None
                                }
                            }
                        },
                        new TablePermission
                        {
                            TableName = "PaymentMethod",
                            Level     = (usr, daton, tabname) => PermissionLevel.None
                        }
                    }
                }
            };

            var ddict = new DataDictionary();

            ddict.AddDatonUsingClassAnnotation(typeof(Ogre));
            var guard = new SecurityGuard(ddict, bill);

            guard.HidePrivateParts(xorg);

            Assert.IsNotNull(xorg.Name);
            Assert.IsNull(xorg.Money);
            Assert.AreEqual(1, xorg.PaymentMethod.Count);
            Assert.IsNull(xorg.PaymentMethod[0].Method);
            Assert.IsNull(xorg.PaymentMethod[0].Notes);
        }
Esempio n. 3
0
        public void FromDiff()
        {
            var emily = new Ogre()
            {
                Name          = "Emily",
                Key           = new PersistonKey("Ogre", "3", false),
                Version       = "v2",
                Money         = 2,
                OgreId        = 3,
                PaymentMethod = new List <Ogre.PaymentMethodRow>
                {
                    new Ogre.PaymentMethodRow {
                        PaymentMethodId = 91, Method = "THUMP", Notes = "n1"
                    },
                    new Ogre.PaymentMethodRow {
                        PaymentMethodId = 92, Method = "BOP", Notes = null
                    }
                }
            };

            var ddict = new DataDictionary();

            ddict.AddDatonUsingClassAnnotation(typeof(Ogre));

            string json         = "{'key':'Ogre|=9','version':'v2','ogre':[{'ogreId':9,'money':2.50,'paymentMethod-deleted':[{'paymentMethodId':92}],'paymentMethod-new':[{'method':'STOMP','notes':'n2'}]}]}".Replace('\'', '"');
            var    jobj         = JsonConvert.DeserializeObject <JObject>(json);
            var    emilyChanges = Retrovert.FromDiff(ddict, jobj);

            Assert.AreEqual(new PersistonKey("Ogre", "9", false), emilyChanges.Key);
            Assert.AreEqual("v2", emilyChanges.BasedOnVersion);
            Assert.AreEqual(9, emilyChanges.MainTable[0].Columns["OgreId"]);
            Assert.AreEqual((decimal)2.5, emilyChanges.MainTable[0].Columns["Money"]);

            emilyChanges.ApplyTo(ddict.DatonDefs["Ogre"], emily);
            Assert.AreEqual((decimal)2.5, emily.Money);
            Assert.IsFalse(emily.PaymentMethod.Any(m => m.Method == "BOP"));  //deleted BOP
            Assert.IsTrue(emily.PaymentMethod.Any(m => m.Method == "STOMP")); //added STOMP
            Assert.IsTrue(emily.PaymentMethod.Any(m => m.Method == "THUMP")); //no change
        }
Esempio n. 4
0
        public void ComputedCol()
        {
            var thor = new Ogre()
            {
                Name          = "Thor",
                Money         = 2,
                PaymentMethod = new List <Ogre.PaymentMethodRow>
                {
                    new Ogre.PaymentMethodRow {
                        Method = "credit"
                    }
                }
            };

            var ddict = new DataDictionary();

            ddict.AddDatonUsingClassAnnotation(typeof(Ogre));
            var ogredef = ddict.DatonDefs["Ogre"];

            thor.Recompute(ogredef);

            Assert.AreEqual("2 gold pieces", thor.FormattedMoney);
            Assert.AreEqual("Arrg, I'll pay with credit", thor.PaymentMethod[0].AngryMethod);
        }
Esempio n. 5
0
        public void Update()
        {
            var xorg = new Ogre
            {
                Key           = new PersistonKey("Ogre", DatonKey.NEWPK, false),
                Name          = "Xorg",
                Money         = 4,
                PaymentMethod = new List <Ogre.PaymentMethodRow>
                {
                    new Ogre.PaymentMethodRow {
                        Method = "credit", Notes = "usually declined"
                    }
                }
            };

            //bill can't update money
            var bill = new User();

            bill.Roles = new[]
            {
                new RetroRole
                {
                    BaseLevel      = PermissionLevel.All,
                    TableOverrides = new List <TablePermission>
                    {
                        new TablePermission
                        {
                            TableName       = "Ogre",
                            BaseLevel       = PermissionLevel.View | PermissionLevel.Modify,
                            ColumnOverrides = new List <ColumnPermission>
                            {
                                new ColumnPermission {
                                    ColumnName = "Money", BaseLevel = PermissionLevel.None
                                }
                            }
                        },
                        new TablePermission
                        {
                            TableName = "PaymentMethod",
                            Level     = (usr, daton, tabname) => PermissionLevel.None
                        }
                    }
                }
            };

            var ddict = new DataDictionary();

            ddict.AddDatonUsingClassAnnotation(typeof(Ogre));
            var ogredef    = ddict.DatonDefs["Ogre"];
            var paymentdef = ogredef.MainTableDef.Children[0];

            var diff = new PersistonDiff(ogredef, xorg.Key, xorg.Version)
            {
                MainTable = new List <PersistonDiff.DiffRow>
                {
                    new PersistonDiff.DiffRow
                    {
                        Kind    = DiffKind.Other,
                        Columns = new Dictionary <string, object>
                        {
                            { "Name", "Priscilla" }, //allowed
                            { "Money", (decimal)5.49 } //disallowed
                        },
                        ChildTables = new Dictionary <TableDef, List <PersistonDiff.DiffRow> >
                        {
                            {
                                paymentdef,
                                new List <PersistonDiff.DiffRow>
                                {
                                    new PersistonDiff.DiffRow
                                    {
                                        Kind    = DiffKind.Other,
                                        Columns = new Dictionary <string, object>
                                        {
                                            { "Method", "cash" }, //disallowed by function
                                            { "Notes", "cash is best" }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var guard  = new SecurityGuard(ddict, bill);
            var errors = guard.GetDisallowedWrites(xorg, ogredef, diff).ToArray();

            Assert.AreEqual(3, errors.Length);
            Assert.IsTrue(errors[0].Contains("Ogre.Money"));
            Assert.IsTrue(errors[1].Contains("PaymentMethod.Method"));
            Assert.IsTrue(errors[2].Contains("PaymentMethod.Notes"));
        }
Esempio n. 6
0
        public void Create()
        {
            var xorg = new Ogre
            {
                Key           = new PersistonKey("Ogre", DatonKey.NEWPK, false),
                Name          = "Xorg",
                Money         = 4,
                PaymentMethod = new List <Ogre.PaymentMethodRow>
                {
                    new Ogre.PaymentMethodRow {
                        Method = "credit", Notes = "usually declined"
                    }
                }
            };

            //nancy can only create new payment rows, not update existing
            var nancy = new User();

            nancy.Roles = new[]
            {
                new RetroRole
                {
                    BaseLevel      = PermissionLevel.None,
                    TableOverrides = new List <TablePermission>
                    {
                        new TablePermission
                        {
                            TableName = "PaymentMethod",
                            BaseLevel = PermissionLevel.Create
                        }
                    }
                }
            };

            var ddict = new DataDictionary();

            ddict.AddDatonUsingClassAnnotation(typeof(Ogre));
            var ogredef    = ddict.DatonDefs["Ogre"];
            var paymentdef = ogredef.MainTableDef.Children[0];

            var diff = new PersistonDiff(ogredef, xorg.Key, xorg.Version)
            {
                MainTable = new List <PersistonDiff.DiffRow>
                {
                    new PersistonDiff.DiffRow
                    {
                        Kind    = DiffKind.Other,
                        Columns = new Dictionary <string, object>
                        {
                            { "Name", "Priscilla" } //disallowed
                        },
                        ChildTables = new Dictionary <TableDef, List <PersistonDiff.DiffRow> >
                        {
                            {
                                paymentdef,
                                new List <PersistonDiff.DiffRow>
                                {
                                    new PersistonDiff.DiffRow
                                    {
                                        Kind    = DiffKind.Other,
                                        Columns = new Dictionary <string, object>
                                        {
                                            { "Notes", "disallowed" } //disallowed update
                                        }
                                    },
                                    new PersistonDiff.DiffRow
                                    {
                                        Kind    = DiffKind.NewRow,
                                        Columns = new Dictionary <string, object>
                                        {
                                            { "Method", "barter" } //allowed create row
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var guard  = new SecurityGuard(ddict, nancy);
            var errors = guard.GetDisallowedWrites(xorg, ogredef, diff).ToArray();

            Assert.AreEqual(2, errors.Length);
            Assert.IsTrue(errors[0].Contains("Ogre.Name"));
            Assert.IsTrue(errors[1].Contains("PaymentMethod.Notes"));
        }
Esempio n. 7
0
        public void ToDenseWire()
        {
            var jill = new Employee
            {
                EmpId     = 9,
                FirstName = "Jill",
                Key       = new PersistonKey("Employee", "9", false),
                Version   = "v2"
            };
            var elist = new EmployeeList
            {
                Key      = new ViewonKey("EmployeeList"),
                Version  = "v2",
                Employee = new List <EmployeeList.TopRow>
                {
                    new EmployeeList.TopRow
                    {
                        EmpId     = 9,
                        FirstName = "Jill"
                                    //last name missing
                    }
                }
            };
            var emily = new Ogre()
            {
                Name          = "Emily",
                Key           = new PersistonKey("Ogre", "3", false),
                Version       = "v2",
                Money         = 2,
                OgreId        = 3,
                PaymentMethod = new List <Ogre.PaymentMethodRow>
                {
                    new Ogre.PaymentMethodRow {
                        Method = "THUMP", Notes = "n1"
                    },
                    new Ogre.PaymentMethodRow {
                        Method = "BOP", Notes = null
                    }
                }
            };

            var ddict = new DataDictionary();

            ddict.AddDatonUsingClassAnnotation(typeof(Employee));
            ddict.AddDatonUsingClassAnnotation(typeof(EmployeeList));
            ddict.AddDatonUsingClassAnnotation(typeof(Ogre));

            string json = Retrovert.ToWire(ddict, jill, false);
            //expected has single quotes so the source code is more readable
            string expected = "{'key':'Employee|=9','version':'v2','content':[[9,'Jill']]}";

            Assert.AreEqual(expected.Replace('\'', '"'), json);

            json = Retrovert.ToWire(ddict, elist, false);
            //expected has single quotes so the source code is more readable
            expected = "{'key':'EmployeeList','version':'v2','content':[[9,'Jill',null,0,null]]}";
            Assert.AreEqual(expected.Replace('\'', '"'), json);

            json = Retrovert.ToWire(ddict, emily, false);
            //expected has single quotes so the source code is more readable
            expected = "{'key':'Ogre|=3','version':'v2','content':[[3,'Emily',2,null,[[0,'THUMP','n1',null],[0,'BOP',null,null]]]]}";
            Assert.AreEqual(expected.Replace('\'', '"'), json);
        }