Example #1
0
        public IActionResult CreateDonationDeclaration(CreateDeclarationRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var validationResult = ValidateOperation(request.DonationAmount);

            if (validationResult.StatusCode != 200)
            {
                return(validationResult);
            }

            var donationDeclaration = new DonationDeclaration
            {
                Name           = request.Name,
                PostCode       = request.PostCode,
                DonationAmount = request.DonationAmount
            };

            var response = new CreateDeclarationResponse
            {
                DeclarationId = _donationDeclarationService.Insert(donationDeclaration).ToString(),
                GiftAidAmount = _giftAidCalculator.CalculateGiftAid(request.DonationAmount)
            };

            return(Ok(response));
        }
Example #2
0
        public async Task ShouldCreateDonationSuccessfully()
        {
            var testCreateDonationDeclarationRequest = new CreateDeclarationRequest
            {
                PostCode       = "testPostCode",
                DonationAmount = 800,
                Name           = "TestName"
            };

            _optionsMonitorMock.Setup(o => o.CurrentValue).Returns(new AppSettings
            {
                TaxRatePercentage     = "20",
                MinimumDonationAmount = "20",
                MaximumDonationAmount = "1000"
            });

            _donationDeclarationServiceMock.Setup(x => x.Insert(It.IsAny <DonationDeclaration>())).Returns("12345");

            var expectedResponse = new CreateDeclarationResponse
            {
                DeclarationId = "12345",
                GiftAidAmount = 200
            };

            var controller      = new GiftAidController(_optionsMonitorMock.Object, _giftAidCalculatorMock.Object, _donationDeclarationServiceMock.Object);
            var response        = controller.CreateDonationDeclaration(testCreateDonationDeclarationRequest);
            var responseContext = response as ObjectResult;

            Assert.AreEqual(expectedResponse.ToString(), responseContext.Value.ToString());
        }
Example #3
0
        public async Task CreateDonationShouldReturnBadRequestWhenAmountIsNotValid()
        {
            var testCreateDonationDeclarationRequest = new CreateDeclarationRequest
            {
                PostCode       = "testPostCode",
                DonationAmount = 2,
                Name           = "TestName"
            };

            _optionsMonitorMock.Setup(o => o.CurrentValue).Returns(new AppSettings
            {
                TaxRatePercentage     = "20",
                MinimumDonationAmount = "20",
                MaximumDonationAmount = "50"
            });

            var expectedResponse = new ObjectResult("Donation amount can not be smaller than 20 and can not be larger than 50");

            expectedResponse.StatusCode = 400;

            var controller      = new GiftAidController(_optionsMonitorMock.Object, _giftAidCalculatorMock.Object, _donationDeclarationServiceMock.Object);
            var response        = controller.CreateDonationDeclaration(testCreateDonationDeclarationRequest);
            var responseContext = response as ObjectResult;

            Assert.AreEqual(expectedResponse.StatusCode, responseContext.StatusCode);
            Assert.AreEqual(expectedResponse.Value.ToString(), responseContext.Value.ToString());
        }
Example #4
0
        public async Task CreateDonationShouldReturnBadRequestSinceRequestBodyIsIncomplete()
        {
            var testCreateDonationDeclarationRequest = new CreateDeclarationRequest
            {
                PostCode       = "testPostCode",
                DonationAmount = 2
            };

            _optionsMonitorMock.Setup(o => o.CurrentValue).Returns(new AppSettings
            {
                TaxRatePercentage     = "20",
                MinimumDonationAmount = "20",
                MaximumDonationAmount = "50"
            });

            var controller      = new GiftAidController(_optionsMonitorMock.Object, _giftAidCalculatorMock.Object, _donationDeclarationServiceMock.Object);
            var response        = controller.CreateDonationDeclaration(testCreateDonationDeclarationRequest);
            var responseContext = response as ObjectResult;

            Assert.AreEqual(400, responseContext.StatusCode);
        }
Example #5
0
        public async Task CreateDonationShouldReturnInternalServerErrorWhenConfigurationIsNotValid()
        {
            var testCreateDonationDeclarationRequest = new CreateDeclarationRequest
            {
                PostCode       = "testPostCode",
                DonationAmount = 2,
                Name           = "TestName"
            };

            _optionsMonitorMock.Setup(o => o.CurrentValue).Returns(new AppSettings());

            var expectedResponse = new ObjectResult("Internal Server Error: Definition of one or more configuration settings of the application are invalid.");

            expectedResponse.StatusCode = 500;

            var controller      = new GiftAidController(_optionsMonitorMock.Object, _giftAidCalculatorMock.Object, _donationDeclarationServiceMock.Object);
            var response        = controller.CreateDonationDeclaration(testCreateDonationDeclarationRequest);
            var responseContext = response as ObjectResult;

            Assert.AreEqual(expectedResponse.StatusCode, responseContext.StatusCode);
            Assert.AreEqual(expectedResponse.Value.ToString(), responseContext.Value.ToString());
        }