public void Given_CategoryExists_When_RemoveEntry_Then_CorrectEntryRemoved()
        {
            // Arrange
            const string categoryToRemove = "Family Guy";
            const string entryToRemove    = "brian";
            var          dat = new EnvCryptDat()
            {
                Categories = new[]
                {
                    new Category()
                    {
                        Name    = categoryToRemove,
                        Entries = new List <Entry>()
                        {
                            new Entry()
                            {
                                Name    = "bart",
                                KeyName = "bart"
                            },
                            new Entry()
                            {
                                Name    = entryToRemove,
                                KeyName = entryToRemove
                            }
                        }
                    },
                    new Category()
                    {
                        Name    = "some other entry",
                        Entries = new List <Entry>()
                        {
                            new Entry()
                            {
                                Name    = entryToRemove,
                                KeyName = entryToRemove
                            }
                        }
                    }
                }
            };

            // Act
            var hasBeenFound = dat.RemoveEntry(categoryToRemove, entryToRemove);

            // Assert
            hasBeenFound.Should().BeTrue("entry exists in the DAT POCO");
            dat.Categories.Should().HaveCount(2);
            dat.Categories[0].Entries.Should().HaveCount(1);
            dat.Categories[1].Entries.Should().HaveCount(1);
            dat.Categories[0].Name.Should().Be(categoryToRemove, "there is still an entry in the category so the category should remain");
            dat.Categories[0].Entries[0].Name.Should().NotBe(entryToRemove);
        }
        public void Given_EntryExistsAndIsTheOnlyEntryInCategory_When_RemoveEntry_Then_CategoryRemoved()
        {
            // Arrange
            const string categoryToRemove = "Family Guy";
            const string entryToRemove    = "brian";
            var          dat = new EnvCryptDat()
            {
                Categories = new List <Category>()
                {
                    new Category()
                    {
                        Name    = categoryToRemove,
                        Entries = new List <Entry>()
                        {
                            new Entry()
                            {
                                Name    = entryToRemove,
                                KeyName = entryToRemove
                            }
                        }
                    },
                    new Category()
                    {
                        Name    = "some other entry",
                        Entries = new List <Entry>()
                        {
                            new Entry()
                            {
                                Name    = entryToRemove,
                                KeyName = entryToRemove
                            }
                        }
                    }
                }
            };

            // Act
            var hasBeenFound = dat.RemoveEntry(categoryToRemove, entryToRemove);

            // Assert
            hasBeenFound.Should().BeTrue("entry exists in the DAT POCO");
            dat.Categories.Should().HaveCount(1);
            dat.Categories[0].Name.Should()
            .NotBe(categoryToRemove, "category was empty after removal of {0} entry so should be removed",
                   entryToRemove);
        }
Esempio n. 3
0
        public static void AddEntry(this EnvCryptDat toDatPoco,
                                    string categoryName, string entryName,
                                    KeyBase key, IList <byte[]> segments, bool overwriteIfEntryExists = false)
        {
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(categoryName), "categoryName");
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(entryName), "entryName");
            Contract.Requires <ArgumentNullException>(key != null, "key");
            Contract.Requires <ArgumentNullException>(segments != null, "segments");
            //
            var isNewEntry = true;
            var entryToAdd = new Entry();

            var isNewCategory   = true;
            var categoryToAddTo = new Category()
            {
                Name    = categoryName,
                Entries = new List <Entry>()
            };


            // Search for entries with the same name in the desired category
            {
                Category foundCategory;
                if (toDatPoco.SearchForCategory(categoryName, out foundCategory))
                {
                    isNewCategory   = false;
                    categoryToAddTo = foundCategory;
                }
            }

            if (!isNewCategory)
            {
                Entry foundEntry;
                if (categoryToAddTo.SearchForEntry(entryName, out foundEntry))
                {
                    if (overwriteIfEntryExists)
                    {
                        isNewEntry = false;
                        entryToAdd = foundEntry;
                        toDatPoco.RemoveEntry(categoryName, entryName);
                    }
                    else
                    {
                        throw new EnvCryptException(
                                  "the entry '{0}' already exists in the category '{1}' and the option to overwrite was not chosen",
                                  entryName, categoryName);
                    }
                }
            }

            entryToAdd.Name                = entryName;
            entryToAdd.KeyName             = key.Name;
            entryToAdd.KeyHash             = key.GetHashCode();
            entryToAdd.EncryptionAlgorithm = key.Algorithm;
            entryToAdd.EncryptedValue      = segments;

            categoryToAddTo.Entries.Add(entryToAdd);

            if (isNewCategory || !isNewEntry)
            {
                toDatPoco.Categories.Add(categoryToAddTo);
            }
        }