Exemple #1
0
        [Test]///Checked
        public void SaveShouldNotChangeCompFieldsIfTheyAreNull()
        {
            UserS.SeedPeshoAndGosho(context);
            var comps    = CompS.SeedTwoCompsToUser(context, UserS.GoshoId);
            var usedComp = comps[0];

            string initialDescription    = usedComp.Description;
            string initialName           = usedComp.Name;
            string initialTargetLanguage = usedComp.TargetLanguage;
            string initialSourceLanguage = usedComp.SourceLanguage;

            var compSave = new ComparisonSave
            {
                Id             = usedComp.Id,
                Description    = null,
                Name           = null,
                TargetLanguage = null,
                SourceLanguage = null,
            };

            ChangeTrackerOperations.DetachAll(this.context);
            Action action = () => this.comparisonService.Save(compSave, UserS.GoshoUsername);

            action.Invoke();

            usedComp = context.Comparisons.SingleOrDefault(x => x.Id == usedComp.Id);

            usedComp.Description.Should().Be(initialDescription);
            usedComp.Name.Should().Be(initialName);
            usedComp.TargetLanguage.Should().Be(initialTargetLanguage);
            usedComp.SourceLanguage.Should().Be(initialSourceLanguage);
        }
Exemple #2
0
        [Test]///Checked
        public void SaveShouldChangeCompFieldsIfTheyAreNotNull()
        {
            const string newDescription    = "New Description";
            const string newName           = "NewName";
            const string newTargetLanguage = "New Target Language";
            const string newSourceLanguage = "New Source Language";

            UserS.SeedPeshoAndGosho(context);
            var comps    = CompS.SeedTwoCompsToUser(context, UserS.GoshoId);
            var usedComp = comps[0];
            var compSave = new ComparisonSave
            {
                Id             = usedComp.Id,
                Description    = newDescription,
                Name           = newName,
                TargetLanguage = newTargetLanguage,
                SourceLanguage = newSourceLanguage,
            };

            ChangeTrackerOperations.DetachAll(this.context);
            Action action = () => this.comparisonService.Save(compSave, UserS.GoshoUsername);

            action.Invoke();

            ///reataching the entity we want to monitor
            usedComp = context.Comparisons.SingleOrDefault(x => x.Id == usedComp.Id);

            usedComp.Description.Should().Be(newDescription);
            usedComp.Name.Should().Be(newName);
            usedComp.TargetLanguage.Should().Be(newTargetLanguage);
            usedComp.SourceLanguage.Should().Be(newSourceLanguage);
        }
Exemple #3
0
        private void AlterExistingItems(ComparisonSave saveData, Comparison comp)
        {
            var validItemIdsForAltering = context.ComparisonItems
                                          .Where(x => x.ComparisonId == comp.Id)
                                          .Select(x => x.Id)
                                          .ToArray();

            var idsToAlterDistict = saveData.AlteredItems.Select(x => x.Id).Distinct().ToArray();

            var dbItemsToAlter = context.ComparisonItems
                                 .Where(x => x.ComparisonId == comp.Id && idsToAlterDistict.Contains(x.Id))
                                 .ToArray();

            if (idsToAlterDistict.Any(x => !validItemIdsForAltering.Contains(x)))
            {
                throw new AccessDenied("The comparison items you are trying to alter does not belong the comparison you are altering!");
            }

            ///DELETION
            var idsToDelete = saveData.AlteredItems.Where(x => x.PropertyChanged == "deleted")
                              .Select(x => x.Id)
                              .ToArray();

            if (idsToDelete.Length != idsToDelete.Distinct().ToArray().Length)
            {
                throw new Exception("deletion should be send once per item");
            }

            var now = DateTime.UtcNow;

            foreach (var id in idsToDelete)
            {
                var currentItem = dbItemsToAlter.Single(x => x.Id == id);
                currentItem.DeletedOn = now;
                currentItem.IsDeleted = true;
            }

            ///Removing the deleted items from other changes that might have happened to them;
            saveData.AlteredItems = saveData.AlteredItems.Where(x => !idsToDelete.Contains(x.Id)).ToHashSet();

            foreach (var alteredItem in saveData.AlteredItems)
            {
                var propToAlter = typeof(ComparisonItem)
                                  .GetProperty(alteredItem.PropertyChanged, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

                var propType = propToAlter.PropertyType;

                var typeConverter = TypeDescriptor.GetConverter(propType);
                var newValue      = typeConverter.ConvertFromString(alteredItem.NewValue);

                var dbItemToAlter = dbItemsToAlter.SingleOrDefault(x => x.Id == alteredItem.Id);

                propToAlter.SetValue(dbItemToAlter, newValue);
            }
        }
Exemple #4
0
        ///validated
        public void Save(ComparisonSave saveData, string username)
        {
            User       user = null;
            Comparison comp = null;

            this.ValidateSave(saveData, username, ref user, ref comp);
            this.SetCompatisonFields(saveData, comp);
            this.AlterExistingItems(saveData, comp);
            this.SaveNewItems(saveData, comp);
            context.SaveChanges();
        }
Exemple #5
0
 public bool SaveApi(ComparisonSave saveData, string username)
 {
     try
     {
         this.Save(saveData, username);
         return(true);
     }
     catch (Exception e)
     {
         return(false);
     }
 }
Exemple #6
0
        private void SaveNewItems(ComparisonSave saveData, Comparison comp)
        {
            var newItems = saveData.NewItems.Select(x => Mapper.Map <ComparisonItem>(x)).ToArray();

            ///TODO: Maybe some validation here sql injection or script attack;
            foreach (var newItem in newItems)
            {
                newItem.ComparisonId = comp.Id;
            }

            this.context.ComparisonItems.AddRange(newItems);
        }
Exemple #7
0
        [Test]///Checked
        public void SaveShouldThrowIfCompDoesNotBelongToUser()
        {
            UserS.SeedPeshoAndGosho(context);
            var comps    = CompS.SeedTwoCompsToUser(context, UserS.GoshoId);
            var compSave = new ComparisonSave
            {
                Id = comps[0].Id,
            };

            ChangeTrackerOperations.DetachAll(this.context);
            Action action = () => this.comparisonService.Save(compSave, UserS.PeshoUsername);

            action.Should().Throw <AccessDenied>().WithMessage("The comparison does not belong to you!");
        }
Exemple #8
0
        [Test]///Checked
        public void SaveShouldThrowIfNonexistantComparison()
        {
            UserS.SeedPeshoAndGosho(context);
            CompS.SeedTwoCompsToUser(context, UserS.GoshoId);
            var invalidCompId = 42;
            var compSave      = new ComparisonSave
            {
                Id = invalidCompId,
            };

            ChangeTrackerOperations.DetachAll(this.context);
            Action action = () => this.comparisonService.Save(compSave, UserS.GoshoUsername);

            action.Should().Throw <ItemNotFound>().WithMessage("The comparison you are trying to modify does not exist!");
        }
Exemple #9
0
        [Test]///Checked
        public void SaveShouldThrow_IfSomeOfToBeAlteredItems_HaveIdsNotInTheComparisonsItems()
        {
            UserS.SeedPeshoAndGosho(context);
            var comps    = CompS.SeedTwoCompsToUser(context, UserS.GoshoId);
            var usedComp = comps[0];
            var items    = CompS.SeedThreeItemsToComp(this.context, usedComp);

            var compSave = new ComparisonSave
            {
                Id             = usedComp.Id,
                Description    = null,
                Name           = null,
                TargetLanguage = null,
                SourceLanguage = null,
                AlteredItems   = new HashSet <ComparisonItemChange>
                {
                    new ComparisonItemChange
                    {
                        Id              = 1,
                        NewValue        = "new test value",
                        PropertyChanged = "Comment"
                    },
                    new ComparisonItemChange
                    {
                        Id              = 2,
                        NewValue        = "new test value",
                        PropertyChanged = "Order"
                    },
                    new ComparisonItemChange
                    {
                        Id              = 3,
                        NewValue        = "new test value",
                        PropertyChanged = "Source"
                    },
                    new ComparisonItemChange
                    {
                        Id              = 4,
                        NewValue        = "new test value",
                        PropertyChanged = "Target"
                    }
                }
            };

            ChangeTrackerOperations.DetachAll(this.context);
            Action action = () => this.comparisonService.Save(compSave, UserS.GoshoUsername);

            action.Should().Throw <AccessDenied>().WithMessage("The comparison items you are trying to alter does not belong the comparison you are altering!");
        }
Exemple #10
0
        private void ValidateSave(ComparisonSave saveData, string username, ref User user, ref Comparison comp)
        {
            user = context.Users.SingleOrDefault(x => x.UserName == username);
            if (user == null)
            {
                throw new UserNotFound(username);
            }

            comp = context.Comparisons.SingleOrDefault(x => x.Id == saveData.Id);
            if (comp == null)
            {
                throw new ItemNotFound("The comparison you are trying to modify does not exist!");
            }

            if (comp.UserId != user.Id)
            {
                throw new AccessDenied("The comparison does not belong to you!");
            }
        }
Exemple #11
0
        ///Sets the new value if the recieved argument is not null
        private void SetCompatisonFields(ComparisonSave saveData, Comparison comp)
        {
            var comparisonPropertis = saveData
                                      .GetType()
                                      .GetProperties(BindingFlags.Public | BindingFlags.Instance);

            comparisonPropertis = comparisonPropertis
                                  .Where(x => x.GetCustomAttributes <ComparisonPropertyAttribute>().Count() > 0)
                                  .ToArray();

            comparisonPropertis = comparisonPropertis
                                  .Where(x => x.GetValue(saveData) != null)
                                  .ToArray();

            foreach (var compProp in comparisonPropertis)
            {
                var matchingPropInDbModel = comp
                                            .GetType()
                                            .GetProperty(compProp.Name, BindingFlags.Public | BindingFlags.Instance);
                var newValue = compProp.GetValue(saveData);
                matchingPropInDbModel.SetValue(comp, newValue);
            }
        }
Exemple #12
0
        public ActionResult <bool> Save([FromBody] ComparisonSave data)
        {
            var result = this.comparisonService.SaveApi(data, this.User.Identity.Name);

            return(result);
        }
Exemple #13
0
        [Test]///Checked
        public void SaveShouldSaveNewItems()
        {
            const string item1Comment = "Comment1";
            const int    item1Order   = 4;
            const string item1Source  = "Source1";
            const string item1Target  = "Target1";

            const string item2Comment = "Comment2";
            const int    item2Order   = 3;
            const string item2Source  = "Source2";
            const string item2Target  = "Target2";

            UserS.SeedPeshoAndGosho(context);
            var comps    = CompS.SeedTwoCompsToUser(context, UserS.GoshoId);
            var usedComp = comps[0];

            var compSave = new ComparisonSave
            {
                Id             = usedComp.Id,
                Description    = null,
                Name           = null,
                TargetLanguage = null,
                SourceLanguage = null,
                NewItems       = new HashSet <ComparisonItemEdit>
                {
                    new ComparisonItemEdit
                    {
                        Id      = 33,
                        Comment = item1Comment,
                        Order   = item1Order,
                        Source  = item1Source,
                        Target  = item1Target,
                    },
                    new ComparisonItemEdit
                    {
                        Id      = 44,
                        Comment = item2Comment,
                        Order   = item2Order,
                        Source  = item2Source,
                        Target  = item2Target,
                    },
                }
            };

            ChangeTrackerOperations.DetachAll(this.context);
            Action action = () => this.comparisonService.Save(compSave, UserS.GoshoUsername);

            ///The mapping should set preSaved db items ids to 0; It Does
            action.Invoke();

            ///The items are actually already tracked from the execution
            ///of the action but I am including them for consistancy
            usedComp = context.Comparisons
                       .Include(x => x.Items)
                       .SingleOrDefault(x => x.Id == usedComp.Id);

            var newCompItems = usedComp.Items;

            ///TODO: find better way to identify the items, maybe seed them with id
            var item1 = newCompItems.SingleOrDefault(x => x.Comment == item1Comment);

            item1.Should().NotBe(null);
            item1.Source.Should().Be(item1Source);
            item1.Target.Should().Be(item1Target);
            item1.Order.Should().Be(item1Order);

            var item2 = newCompItems.SingleOrDefault(x => x.Comment == item2Comment);

            item2.Should().NotBe(null);
            item2.Source.Should().Be(item2Source);
            item2.Target.Should().Be(item2Target);
            item2.Order.Should().Be(item2Order);
        }
Exemple #14
0
        [Test]///Checked
        public void SaveShouldAlterExistingItems_ForAValidRequest()
        {
            const string item1NewSource  = "itemOneNewSource";
            const string item1NewTarget  = "itemOneNewTarget";
            const string item1NewComment = "itemOneNewComment";
            const int    item1NewOrder   = 4;

            const string item2NewSource  = "itemTwoNewSource";
            const string item2NewTarget  = "itemTwoNewTarget";
            const string item2NewComment = "itemTwoNewComment";
            const int    item2NewOrder   = 3;


            UserS.SeedPeshoAndGosho(context);
            var comps    = CompS.SeedTwoCompsToUser(context, UserS.GoshoId);
            var usedComp = comps[0];
            var items    = CompS.SeedThreeItemsToComp(this.context, usedComp);

            var compSave = new ComparisonSave
            {
                Id             = usedComp.Id,
                Description    = null,
                Name           = null,
                TargetLanguage = null,
                SourceLanguage = null,
                AlteredItems   = new HashSet <ComparisonItemChange>
                {
                    new ComparisonItemChange
                    {
                        Id              = CompS.Item1Id,
                        NewValue        = item1NewComment,
                        PropertyChanged = "Comment"
                    },
                    new ComparisonItemChange
                    {
                        Id              = CompS.Item1Id,
                        NewValue        = item1NewOrder.ToString(),
                        PropertyChanged = "Order"
                    },
                    new ComparisonItemChange
                    {
                        Id              = CompS.Item1Id,
                        NewValue        = item1NewSource,
                        PropertyChanged = "Source"
                    },
                    new ComparisonItemChange
                    {
                        Id              = CompS.Item1Id,
                        NewValue        = item1NewTarget,
                        PropertyChanged = "Target"
                    },


                    new ComparisonItemChange
                    {
                        Id              = CompS.Item2Id,
                        NewValue        = item2NewComment,
                        PropertyChanged = "Comment"
                    },
                    new ComparisonItemChange
                    {
                        Id              = CompS.Item2Id,
                        NewValue        = item2NewOrder.ToString(),
                        PropertyChanged = "Order"
                    },
                    new ComparisonItemChange
                    {
                        Id              = CompS.Item2Id,
                        NewValue        = item2NewSource,
                        PropertyChanged = "Source"
                    },
                    new ComparisonItemChange
                    {
                        Id              = CompS.Item2Id,
                        NewValue        = item2NewTarget,
                        PropertyChanged = "Target"
                    },
                }
            };

            ChangeTrackerOperations.DetachAll(this.context);
            Action action = () => this.comparisonService.Save(compSave, UserS.GoshoUsername);

            action.Invoke();

            var item1 = context.ComparisonItems.SingleOrDefault(x => x.Id == CompS.Item1Id);
            var item2 = context.ComparisonItems.SingleOrDefault(x => x.Id == CompS.Item2Id);

            item1.Comment.Should().Be(item1NewComment);
            item1.Order.Should().Be(item1NewOrder);
            item1.Source.Should().Be(item1NewSource);
            item1.Target.Should().Be(item1NewTarget);

            item2.Comment.Should().Be(item2NewComment);
            item2.Order.Should().Be(item2NewOrder);
            item2.Source.Should().Be(item2NewSource);
            item2.Target.Should().Be(item2NewTarget);
        }