示例#1
0
        public void StoreElement_PersistsData()
        {
            var element      = XElement.Parse("<Element1/>");
            var friendlyName = "Element1";
            var key          = new DataProtectionKey()
            {
                FriendlyName = friendlyName, Xml = element.ToString()
            };

            var services = GetServices(nameof(StoreElement_PersistsData));
            var service  = new EntityFrameworkCoreXmlRepository <DataProtectionKeyContext>(services, NullLoggerFactory.Instance);

            service.StoreElement(element, friendlyName);

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = services.CreateScope().ServiceProvider.GetRequiredService <DataProtectionKeyContext>())
            {
                Assert.Equal(1, context.DataProtectionKeys.Count());
                Assert.Equal(key.FriendlyName, context.DataProtectionKeys.Single()?.FriendlyName);
                Assert.Equal(key.Xml, context.DataProtectionKeys.Single()?.Xml);
            }
        }
        public void GetAllElements_should_return_deserialization_error_as_null()
        {
            var dbName   = Guid.NewGuid().ToString();
            var provider = new ServiceCollection()
                           .AddDbContext <OperationalDbContext>(o => o.UseInMemoryDatabase(dbName))
                           .BuildServiceProvider(validateScopes: true);

            using (var scope = provider.CreateScope())
            {
                using var context = scope.ServiceProvider.GetRequiredService <OperationalDbContext>();
                context.KeyRotationKeys.Add(new KeyRotationKey
                {
                    FriendlyName = "test",
                    Xml          = "non desialiazable"
                });
                context.SaveChanges();
            }

            var sut = new EntityFrameworkCoreXmlRepository <OperationalDbContext>(provider, NullLoggerFactory.Instance);

            var result = sut.GetAllElements();

            Assert.Null(result.First());
        }