Example #1
0
        public async Task UserCreatedEventHandler_Consume_Creates_User()
        {
            StoryContext.OpenInMemoryConnection();
            try
            {
                var userCreatedEvent = new UserCreatedEvent
                {
                    UserKey      = Guid.NewGuid(),
                    ProfileImage = "image",
                    FirstName    = "John",
                    LastName     = "Doe"
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new UserCreatedEventHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    await handler.ConsumeAsync(userCreatedEvent);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.AreEqual(1, context.Users.Count());
                    Assert.AreEqual(userCreatedEvent.UserKey, context.Users.Single().UserKey);
                    Assert.AreEqual(userCreatedEvent.ProfileImage, context.Users.Single().ProfileImage);
                    Assert.AreEqual(Helpers.GetUserName(userCreatedEvent), context.Users.Single().Name);
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
Example #2
0
        public async Task GetCharitiesRequestHandler_Handle_Returns_Charities()
        {
            StoryContext.OpenInMemoryConnection();
            try
            {
                using (var context = StoryContext.GetInMemoryContext())
                {
                    for (var i = 0; i < 25; i++)
                    {
                        context.Charities.Add(new Charity
                        {
                            CharityKey = Guid.NewGuid(),
                            Name       = "charity"
                        });
                    }
                    context.SaveChanges();
                }

                GetCharitiesResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new GetCharitiesRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(new GetCharitiesRequest());
                }

                Assert.AreEqual(25, response.Charities.Count);
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
Example #3
0
        public async Task CreateStoryRequestHandler_Handle_Returns_InvalidUserKey()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var author = new User
                {
                    UserKey = Guid.NewGuid()
                };

                var charity = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Users.Add(author);
                    context.Charities.Add(charity);
                    context.SaveChanges();
                }

                var request = new CreateStoryRequest
                {
                    CharityKey    = charity.CharityKey,
                    StoryKey      = Guid.NewGuid(),
                    AuthorUserKey = Guid.NewGuid(),
                    Title         = "title",
                    CoverImage    = "cover",
                    Images        = new List <string>
                    {
                        "image1",
                        "image2"
                    }
                };

                CreateStoryResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new CreateStoryRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(request);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.IsFalse(response.IsSuccess);
                    Assert.IsFalse(context.Stories.Any());
                    Assert.IsFalse(context.Images.Any());
                    Assert.AreEqual(ErrorType.InvalidUserKey, response.ErrorType);
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
        public async Task CharityUpdatedEventHandler_Handles_Event()
        {
            StoryContext.OpenInMemoryConnection();
            try
            {
                var charityUpdatedEvent = new CharityUpdatedEvent
                {
                    CharityKey = Guid.NewGuid(),
                    Name       = "newName"
                };
                var otherKey = Guid.NewGuid();

                using (var context = StoryContext.GetInMemoryContext())
                {
                    var charity1 = new Charity
                    {
                        CharityKey = charityUpdatedEvent.CharityKey,
                        Name       = "oldName"
                    };
                    var charity2 = new Charity
                    {
                        CharityKey = otherKey,
                        Name       = "otherOldName"
                    };

                    context.Charities.Add(charity1);
                    context.Charities.Add(charity2);
                    context.SaveChanges();
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new CharityUpdatedEventHandler(context);
                    await handler.ConsumeAsync(charityUpdatedEvent);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.AreEqual(1, context.Charities.Count(a => a.CharityKey == charityUpdatedEvent.CharityKey && a.Name == charityUpdatedEvent.Name));
                    Assert.AreEqual(1, context.Charities.Count(a => a.CharityKey == otherKey && a.Name == "otherOldName"));
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
        public async Task GetStoryByUrlRequestHandler_Handle_Returns_Story()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var charity = new Charity
                {
                    CharityKey = Guid.NewGuid(),
                    Url        = "some"
                };

                var story = new Story
                {
                    StoryKey     = Guid.NewGuid(),
                    Title        = "title",
                    Charity      = charity,
                    UrlComponent = "none"
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Stories.Add(story);
                    context.SaveChanges();
                }

                GetStoryByUrlResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new GetStoryByUrlRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(new GetStoryByUrlRequest
                    {
                        CharityUrl        = "some",
                        StoryUrlComponent = "none"
                    });
                }

                Assert.IsTrue(response.IsSuccess);
                Assert.AreEqual(story.StoryKey, response.Story.StoryKey);
                Assert.AreEqual(story.Title, response.Story.Title);
                Assert.IsNull(response.Story.AuthorUserKey);
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
        public async Task ApproveStoryRequestHandler_Handle_Returns_AlreadyActive()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var charity = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };
                var story = new Story
                {
                    StoryKey   = Guid.NewGuid(),
                    IsApproved = true,
                    Charity    = charity
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Stories.Add(story);
                    context.SaveChanges();
                }

                ApproveStoryResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new ApproveStoryRequestHandler(context);
                    response = await handler.Handle(new ApproveStoryRequest
                    {
                        StoryKey = story.StoryKey
                    });
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.IsFalse(response.IsSuccess);
                    Assert.IsTrue(context.Stories.Single().IsApproved);
                    Assert.AreEqual(ErrorType.AlreadyActive, response.ErrorType);
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
Example #7
0
        public async Task UserUpdatedEventHandler_Consume_Updates_Name()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var userUpdatedEvent = new UserUpdatedEvent
                {
                    UserKey      = Guid.NewGuid(),
                    FirstName    = "Ellen",
                    LastName     = "Doe",
                    EmailAddress = "*****@*****.**",
                    ProfileImage = "new image"
                };

                var user = new User
                {
                    UserKey      = userUpdatedEvent.UserKey,
                    Name         = "John Doe",
                    ProfileImage = "old image"
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Users.Add(user);
                    context.SaveChanges();
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new UserUpdatedEventHandler(context);
                    await handler.ConsumeAsync(userUpdatedEvent);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.AreEqual("Ellen Doe", context.Users.Single().Name);
                    Assert.AreEqual("new image", context.Users.Single().ProfileImage);
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
Example #8
0
        public async Task UserUpdatedEventHandler_Consume_Event_Has_No_UserKey()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var userUpdatedEvent = new UserUpdatedEvent
                {
                    UserKey      = Guid.Empty,
                    FirstName    = "Ellen",
                    LastName     = "Doe",
                    ProfileImage = "new image"
                };

                var charityAction = new User
                {
                    UserKey      = Guid.NewGuid(),
                    Name         = "John Doe",
                    ProfileImage = "old image"
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Users.Add(charityAction);
                    context.SaveChanges();
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new UserUpdatedEventHandler(context);
                    await handler.ConsumeAsync(userUpdatedEvent);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.AreEqual("John Doe", context.Users.Single().Name);
                    Assert.AreEqual("old image", context.Users.Single().ProfileImage);
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
        public async Task GetStoriesRequestHandlerTests_Handle_Returns_Stories()
        {
            StoryContext.OpenInMemoryConnection();
            try
            {
                var charity = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };
                using (var context = StoryContext.GetInMemoryContext())
                {
                    for (var i = 0; i < 25; i++)
                    {
                        context.Stories.Add(new Story
                        {
                            StoryKey = Guid.NewGuid(),
                            Charity  = charity
                        });
                    }

                    context.SaveChanges();
                }

                GetStoriesResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new GetStoriesRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(new GetStoriesRequest
                    {
                        PageNumber = 2,
                        PageSize   = 20
                    });
                }

                Assert.AreEqual(25, response.TotalNumberOfResults);
                Assert.AreEqual(5, response.Results.Count);
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
        public async Task GetStoryByKeyRequestHandler_Handle_Returns_No_Story()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var charity = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };

                var story = new Story
                {
                    StoryKey = Guid.NewGuid(),
                    Title    = "title",
                    Charity  = charity
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Stories.Add(story);
                    context.SaveChanges();
                }

                GetStoryByKeyResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new GetStoryByKeyRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(new GetStoryByKeyRequest
                    {
                        StoryKey = Guid.NewGuid()
                    });
                }

                Assert.IsFalse(response.IsSuccess);
                Assert.IsNull(response.Story);
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
Example #11
0
        public async Task CharityCreatedEventHandler_Handle_Returns_Success()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var Event = new CharityCreatedEvent()
                {
                    Category      = Core.Enums.Category.EnvironmentAndNatureConservation,
                    CharityKey    = Guid.NewGuid(),
                    CoverImage    = "No image given",
                    Email         = "*****@*****.**",
                    IBAN          = "NotReallyAnIBAN",
                    KVKNumber     = "10",
                    Name          = "TestName",
                    Url           = "test",
                    OwnerUserName = "******",
                    Slogan        = "This is a very good testing slogan",
                    ThankYou      = "ThankYou"
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new CharityCreatedEventHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    await handler.ConsumeAsync(Event);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.AreEqual(1, context.Charities.Count());
                    Assert.AreEqual(Event.Name, context.Charities.Single().Name);
                    Assert.AreEqual(Event.Url, context.Charities.Single().Url);
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
Example #12
0
        public async Task CreateStoryRequestHandler_Handle_Returns_Success()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var author = new User
                {
                    UserKey = Guid.NewGuid()
                };

                var charity = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Users.Add(author);
                    context.Charities.Add(charity);
                    context.SaveChanges();
                }

                var request = new CreateStoryRequest
                {
                    CharityKey    = charity.CharityKey,
                    AuthorUserKey = author.UserKey,
                    StoryKey      = Guid.NewGuid(),
                    Title         = "title",
                    CoverImage    = "cover",
                    Images        = new List <string>
                    {
                        "image1",
                        "image2"
                    }
                };

                CreateStoryResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new CreateStoryRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(request);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.IsTrue(response.IsSuccess);
                    Assert.AreEqual(1, context.Stories.Count());
                    Assert.AreEqual(request.CoverImage, context.Stories.Single().CoverImage);
                    Assert.AreEqual(request.Title, context.Stories.Single().Title);
                    Assert.AreEqual(request.StoryKey, context.Stories.Single().StoryKey);
                    Assert.AreEqual(request.AuthorUserKey, context.Stories.IncludeAuthor().Single().Author.UserKey);
                    Assert.AreEqual(2, context.Images.Count());
                    Assert.AreEqual(2, context.Stories.IncludeImages().Single().Images.Count);
                    Assert.AreEqual(1, context.Images.Count(i => i.Base64 == "image1"));
                    Assert.AreEqual(1, context.Images.Count(i => i.Base64 == "image2"));
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
        public async Task GetStoriesRequestHandlerTests_Handle_Returns_Filtered_Stories()
        {
            StoryContext.OpenInMemoryConnection();
            try
            {
                var charity1 = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };
                var charity2 = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };
                var charity3 = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };
                var authorUserKey = Guid.NewGuid();

                using (var context = StoryContext.GetInMemoryContext())
                {
                    for (var i = 0; i < 25; i++)
                    {
                        context.Stories.Add(new Story
                        {
                            StoryKey = Guid.NewGuid(),
                            Charity  = charity3
                        });
                    }

                    for (var i = 0; i < 10; i++)
                    {
                        if (i % 2 == 0)
                        {
                            if (i % 4 == 0)
                            {
                                context.Stories.Add(new Story
                                {
                                    Charity    = charity1,
                                    StoryKey   = Guid.NewGuid(),
                                    IsApproved = true,
                                    Author     = new User
                                    {
                                        UserKey = authorUserKey,
                                        Name    = "John Doe"
                                    }
                                });
                            }
                            else
                            {
                                context.Stories.Add(new Story
                                {
                                    Charity    = charity2,
                                    StoryKey   = Guid.NewGuid(),
                                    IsApproved = true
                                });
                            }
                        }
                        else
                        {
                            context.Stories.Add(new Story
                            {
                                Charity    = charity2,
                                StoryKey   = Guid.NewGuid(),
                                IsApproved = true
                            });
                        }
                    }

                    context.SaveChanges();
                }

                GetStoriesResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new GetStoriesRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(new GetStoriesRequest
                    {
                        OnlyApproved  = true,
                        AuthorUserKey = authorUserKey,
                        PageNumber    = 2,
                        PageSize      = 2
                    });
                }

                Assert.AreEqual(3, response.TotalNumberOfResults);
                Assert.AreEqual(1, response.Results.Count);
                Assert.IsTrue(response.Results.All(r => r.AuthorUserKey == authorUserKey && r.AuthorName == "John Doe" && r.IsApproved && r.CharityKey == charity1.CharityKey));
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }
Example #14
0
        public async Task UpdateStoryRequestHandler_Handle_Returns_NotFound()
        {
            StoryContext.OpenInMemoryConnection();

            try
            {
                var charity = new Charity
                {
                    CharityKey = Guid.NewGuid()
                };
                var image1 = new Image
                {
                    Base64 = "image1"
                };

                var image2 = new Image
                {
                    Base64 = "image2"
                };
                var story = new Story
                {
                    StoryKey   = Guid.NewGuid(),
                    Title      = "title",
                    CoverImage = "cover",
                    Charity    = charity,
                    Images     = new List <Image>
                    {
                        image1,
                        image2
                    }
                };

                using (var context = StoryContext.GetInMemoryContext())
                {
                    context.Stories.Add(story);
                    context.SaveChanges();
                }

                var request = new UpdateStoryRequest
                {
                    StoryKey = Guid.NewGuid(),
                    Title    = "newTitle",
                    Images   = new List <string>
                    {
                        "image3"
                    },
                    StoryImage = "storyImage"
                };

                UpdateStoryResponse response;
                using (var context = StoryContext.GetInMemoryContext())
                {
                    var handler = new UpdateStoryRequestHandler(context);
                    response = await handler.Handle(request);
                }

                using (var context = StoryContext.GetInMemoryContext())
                {
                    Assert.IsFalse(response.IsSuccess);
                    Assert.AreEqual(ErrorType.NotFound, response.ErrorType);
                    Assert.AreEqual(story.StoryImage, context.Stories.Single().StoryImage);
                    Assert.AreEqual(story.Title, context.Stories.Single().Title);
                    Assert.AreEqual(story.CoverImage, story.CoverImage);
                    Assert.AreEqual(story.CoreText, story.CoreText);
                    Assert.AreEqual(2, context.Images.Count());
                    Assert.AreEqual(1, context.Images.Count(i => i.Base64 == image1.Base64));
                    Assert.AreEqual(1, context.Images.Count(i => i.Base64 == image2.Base64));
                }
            }
            finally
            {
                StoryContext.CloseInMemoryConnection();
            }
        }