Beispiel #1
0
        public async Task GetCharityByKeyRequest_Handle_Returns_Charity()
        {
            CharityContext.OpenInMemoryConnection();
            try
            {
                //Arrange
                var key = Guid.NewGuid();
                using (var context = CharityContext.GetInMemoryContext())
                {
                    var charity = new Charity
                    {
                        CharityKey    = key,
                        Name          = "testNameCharity",
                        OwnerUserName = "******",
                        Email         = "*****@*****.**",
                        Category      = Core.Enums.Category.None,
                        KVKNumber     = "",
                        IBAN          = "0-IBAN",
                        CoverImage    = "",
                        Slogan        = "Test"
                    };

                    context.Add(charity);
                    context.SaveChanges();
                }
                var request = new GetCharityByKeyRequest()
                {
                    CharityKey = key
                };

                GetCharityByKeyResponse response;

                //Act
                using (var context = CharityContext.GetInMemoryContext())
                {
                    var handler = new GetCharityByKeyRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(request);
                }

                //Assert
                using (var context = CharityContext.GetInMemoryContext())
                {
                    Assert.AreEqual(response.Charity.CharityKey, request.CharityKey);
                    Assert.AreEqual(response.Charity.Name, context.Charities.Single().Name);
                    Assert.AreEqual(response.Charity.OwnerUserName, context.Charities.Single().OwnerUserName);
                    Assert.AreEqual(response.Charity.Email, context.Charities.Single().Email);
                    Assert.AreEqual(response.Charity.Category, context.Charities.Single().Category);
                    Assert.AreEqual(response.Charity.KVKNumber, context.Charities.Single().KVKNumber);
                    Assert.AreEqual(response.Charity.IBAN, context.Charities.Single().IBAN);
                    Assert.AreEqual(response.Charity.CoverImage, context.Charities.Single().CoverImage);
                    Assert.AreEqual(response.Charity.Slogan, context.Charities.Single().Slogan);
                    Assert.IsTrue(response.IsSuccess);
                }
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }
        public async Task ApproveCharityRequest_Handle_Returns_AlreadyActive()
        {
            CharityContext.OpenInMemoryConnection();

            try
            {
                var busMock = new Mock <IBus>();
                busMock.Setup(m => m.PublishAsync(It.IsAny <CharityCreatedEvent>())).Returns(Task.FromResult(true));

                ApproveCharityResponse response;
                var request = new ApproveCharityRequest
                {
                    CharityKey = Guid.NewGuid()
                };
                var charity = new Charity
                {
                    CharityKey    = request.CharityKey,
                    IsApproved    = true,
                    Name          = "0",
                    OwnerUserName = "******",
                    Email         = "testEmail",
                    Category      = Category.AnimalProtection,
                    KVKNumber     = "1",
                    IBAN          = "1111-1111",
                    CoverImage    = "x",
                    Slogan        = "Test2"
                };

                using (var context = CharityContext.GetInMemoryContext())
                {
                    context.Charities.Add(charity);
                    context.SaveChanges();
                }

                using (var context = CharityContext.GetInMemoryContext())
                {
                    var handler = new ApproveCharityRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()), busMock.Object);
                    response = await handler.Handle(request);
                }

                using (var context = CharityContext.GetInMemoryContext())
                {
                    Assert.IsTrue(context.Charities.Single().IsApproved);
                    Assert.IsFalse(response.IsSuccess);
                    Assert.AreEqual(ErrorType.AlreadyActive, response.ErrorType);
                }
                busMock.Verify(m => m.PublishAsync(It.Is <CharityCreatedEvent>(e => e.CharityKey == request.CharityKey)), Times.Never);
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }
Beispiel #3
0
        public async Task GetCharityByUrlRequest_Handle_Returns_Null()
        {
            CharityContext.OpenInMemoryConnection();
            try
            {
                //Arrange

                using (var context = CharityContext.GetInMemoryContext())
                {
                    var charity = new Charity
                    {
                        CharityKey    = Guid.NewGuid(),
                        Name          = "testNameCharity",
                        OwnerUserName = "******",
                        Email         = "*****@*****.**",
                        Category      = Core.Enums.Category.None,
                        KVKNumber     = "",
                        IBAN          = "0-IBAN",
                        CoverImage    = "",
                        Slogan        = "Test",
                        Url           = "test",
                        IsApproved    = true
                    };

                    context.Add(charity);
                    context.SaveChanges();
                }

                var request = new GetCharityByUrlRequest
                {
                    Url = "WeirdUrl"
                };

                GetCharityByUrlResponse response;

                //Act
                using (var context = CharityContext.GetInMemoryContext())
                {
                    var handler = new GetCharityByUrlRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(request);
                }

                //Assert
                Assert.IsFalse(response.IsSuccess);
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }
        public async Task CreateCharityRequest_Handle_Returns_Success()
        {
            CharityContext.OpenInMemoryConnection();

            try
            {
                CreateCharityResponse response;
                var request = new CreateCharityRequest
                {
                    CharityKey    = Guid.NewGuid(),
                    Name          = "testName",
                    OwnerUserName = "******",
                    Email         = "*****@*****.**",
                    Category      = Category.None,
                    KVKNumber     = "",
                    IBAN          = "0-IBAN",
                    CoverImage    = "",
                    Slogan        = "Test"
                };

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

                using (var context = CharityContext.GetInMemoryContext())
                {
                    Assert.AreEqual(request.CharityKey, context.Charities.Single().CharityKey);
                    Assert.AreEqual(request.Name, context.Charities.Single().Name);
                    Assert.AreEqual(request.OwnerUserName, context.Charities.Single().OwnerUserName);
                    Assert.AreEqual(request.Email, context.Charities.Single().Email);
                    Assert.AreEqual(request.Category, context.Charities.Single().Category);
                    Assert.AreEqual(request.KVKNumber, context.Charities.Single().KVKNumber);
                    Assert.AreEqual(request.IBAN, context.Charities.Single().IBAN);
                    Assert.AreEqual(request.CoverImage, context.Charities.Single().CoverImage);
                    Assert.AreEqual(request.Slogan, context.Charities.Single().Slogan);
                    Assert.IsFalse(context.Charities.Single().IsApproved);
                    Assert.IsTrue(response.IsSuccess);
                }
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }
Beispiel #5
0
        public async Task UserUpdatedEventHandler_Consume_Updates_Name()
        {
            CharityContext.OpenInMemoryConnection();

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

                var charity = new Charity
                {
                    OwnerUserKey  = userUpdatedEvent.UserKey,
                    OwnerUserName = "******"
                };

                using (var context = CharityContext.GetInMemoryContext())
                {
                    context.Charities.Add(charity);
                    context.SaveChanges();
                }

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

                using (var context = CharityContext.GetInMemoryContext())
                {
                    Assert.AreEqual("Ellen Doe", context.Charities.Single().OwnerUserName);
                }
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }
        public async Task CreateCharityRequest_Handle_Returns_InvalidKey()
        {
            CharityContext.OpenInMemoryConnection();

            try
            {
                CreateCharityResponse response;
                var request = new CreateCharityRequest
                {
                    CharityKey    = Guid.Empty,
                    Name          = "testName",
                    OwnerUserName = "******",
                    Email         = "*****@*****.**",
                    Category      = Category.None,
                    KVKNumber     = "",
                    IBAN          = "0-IBAN",
                    CoverImage    = "",
                    Slogan        = "Test"
                };

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

                using (var context = CharityContext.GetInMemoryContext())
                {
                    Assert.IsFalse(context.Charities.Any());
                    Assert.AreEqual(ErrorType.InvalidKey, response.ErrorType);
                    Assert.IsFalse(response.IsSuccess);
                }
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }
        public async Task GetCharitiesRequestHandlerTests_Handle_Returns_Charities()
        {
            CharityContext.OpenInMemoryConnection();
            try
            {
                using (var context = CharityContext.GetInMemoryContext())
                {
                    for (var i = 0; i < 25; i++)
                    {
                        context.Charities.Add(new Charity
                        {
                            CharityKey = Guid.NewGuid()
                        });
                    }

                    context.SaveChanges();
                }

                GetCharitiesResponse response;
                using (var context = CharityContext.GetInMemoryContext())
                {
                    var handler = new GetCharitiesRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(new GetCharitiesRequest
                    {
                        PageNumber = 2,
                        PageSize   = 20
                    });
                }

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

            try
            {
                var busMock = new Mock <IBus>();
                busMock.Setup(m => m.PublishAsync(It.IsAny <CharityUpdatedEvent>())).Returns(Task.FromResult(true));

                UpdateCharityResponse response;
                var newKey  = Guid.NewGuid();
                var request = new UpdateCharityRequest()
                {
                    CharityKey    = newKey,
                    Name          = "0",
                    OwnerUserName = "******",
                    Email         = "0",
                    Category      = Core.Enums.Category.None,
                    KVKNumber     = "",
                    IBAN          = "0-IBAN",
                    CoverImage    = "",
                    Slogan        = "0"
                };

                using (var context = CharityContext.GetInMemoryContext())
                {
                    context.Charities.Add(new Charity
                    {
                        CharityKey    = newKey,
                        Name          = "0",
                        OwnerUserName = "******",
                        Email         = "testEmail",
                        Category      = Category.AnimalProtection,
                        KVKNumber     = "1",
                        IBAN          = "1111-1111",
                        CoverImage    = "x",
                        Slogan        = "Test2"
                    });

                    context.SaveChanges();
                }

                using (var context = CharityContext.GetInMemoryContext())
                {
                    var handler = new UpdateCharityRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()), busMock.Object);
                    response = await handler.Handle(request);
                }

                using (var context = CharityContext.GetInMemoryContext())
                {
                    Assert.AreEqual(request.CharityKey, context.Charities.Single().CharityKey);
                    Assert.AreEqual(request.Name, context.Charities.Single().Name);
                    Assert.AreEqual(request.OwnerUserName, context.Charities.Single().OwnerUserName);
                    Assert.AreEqual(request.Email, context.Charities.Single().Email);
                    Assert.AreEqual(request.Category, context.Charities.Single().Category);
                    Assert.AreEqual(request.KVKNumber, context.Charities.Single().KVKNumber);
                    Assert.AreEqual(request.IBAN, context.Charities.Single().IBAN);
                    Assert.AreEqual(request.CoverImage, context.Charities.Single().CoverImage);
                    Assert.AreEqual(request.Slogan, context.Charities.Single().Slogan);
                    Assert.IsTrue(response.IsSuccess);
                }
                busMock.Verify(m => m.PublishAsync(It.Is <CharityUpdatedEvent>(e => e.CharityKey == request.CharityKey && e.Name == request.Name)), Times.Once);
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }
        public async Task GetCharityActionsRequestHandlerTests_Handle_Returns_Filtered_Charities()
        {
            CharityContext.OpenInMemoryConnection();
            try
            {
                var charityKey = Guid.NewGuid();
                using (var context = CharityContext.GetInMemoryContext())
                {
                    for (var i = 0; i < 25; i++)
                    {
                        context.Charities.Add(new Charity
                        {
                            CharityKey  = Guid.NewGuid(),
                            Name        = "this is a charity for john",
                            Description = "doe",
                            Category    = Category.EducationAndResearch | Category.Culture
                        });
                    }

                    for (var i = 0; i < 15; i++)
                    {
                        if (i % 2 == 0)
                        {
                            if (i % 4 == 0)
                            {
                                context.Charities.Add(new Charity
                                {
                                    CharityKey  = charityKey,
                                    Name        = "this is a charity for john",
                                    Description = "doe",
                                    Category    = Category.EducationAndResearch | Category.Culture
                                });
                            }
                            else
                            {
                                context.Charities.Add(new Charity
                                {
                                    CharityKey  = charityKey,
                                    Name        = "this is a charity for john",
                                    Description = "doe",
                                    Category    = Category.EducationAndResearch | Category.Health
                                });
                            }
                        }
                        else
                        {
                            context.Charities.Add(new Charity
                            {
                                CharityKey = charityKey
                            });
                        }
                    }

                    context.SaveChanges();
                }

                GetCharitiesResponse response;
                using (var context = CharityContext.GetInMemoryContext())
                {
                    var handler = new GetCharitiesRequestHandler(context, AutoMapperHelper.BuildMapper(new MappingProfile()));
                    response = await handler.Handle(new GetCharitiesRequest
                    {
                        Category = Category.EducationAndResearch | Category.Culture,
                        Keywords = new List <string> {
                            "john", "doe"
                        },
                        PageNumber = 3,
                        PageSize   = 10
                    });
                }

                Assert.AreEqual(29, response.TotalNumberOfResults);
                Assert.AreEqual(9, response.Results.Count);
                Assert.IsTrue(response.Results.All(r => r.Category == (Category.EducationAndResearch | Category.Culture)));
            }
            finally
            {
                CharityContext.CloseInMemoryConnection();
            }
        }