Пример #1
0
        public ResponseResultVm GetTheResponseUrl(string shortenUrl, int numberOfMonthsValid)
        {
            var responseResult = new ResponseResultVm();


            var originalUrl = _unitOfWork.ShortUrlRepository.GetByShortKey(shortenUrl);

            if (originalUrl != null)
            {
                if (_utilities.MonthsDifferenceBetweenTwoDates(DateTime.Now.Date, originalUrl.InsertDate.Date) >
                    numberOfMonthsValid)
                {
                    responseResult.ErrorMessage = "Sorry the URL you entered is not valid anymore";
                    return(responseResult);
                }



                var responseUrl = _utilities.GetTheResponseFromTheRequestedUrl(originalUrl.OriginalUrl).Result;


                if (responseUrl == null)
                {
                    responseResult.ErrorMessage = $"Sorry the URL {originalUrl} is not live";
                    return(responseResult);
                }

                responseResult.ResponseUrl = responseUrl;
                return(responseResult);
            }

            responseResult.ErrorMessage = "The Url you entered is not exist";
            return(responseResult);
        }
        public void NotFound_IfThereIsErrorMessages_ReturnBadRequest()
        {
            var responseResultVm = new ResponseResultVm {
                ErrorMessage = "Errors"
            };


            _mockServices.Setup(x => x.ShortUrlServices.GetTheResponseUrl("", 3)).Returns(responseResultVm);
            _mockConfiguration.Setup(x => x.GetSection("UrlValidMonths").Value).Returns("3");
            var homeController = new HomeController(_mockConfiguration.Object, _mockServices.Object);
            var result         = homeController.NotFound("");

            Assert.That(result, Is.TypeOf <BadRequestObjectResult>());
        }
        public void NotFound_IfThereIsNonErrorMesages_ReturnRedirect()
        {
            //Arrange

            var responseResultVm = new ResponseResultVm {
                ErrorMessage = null, ResponseUrl = "redirectionUrl"
            };


            //Act
            _mockServices.Setup(x => x.ShortUrlServices.GetTheResponseUrl("", 3)).Returns(responseResultVm);
            _mockConfiguration.Setup(x => x.GetSection("UrlValidMonths").Value).Returns("3");
            var homeController = new HomeController(_mockConfiguration.Object, _mockServices.Object);
            var result         = homeController.NotFound("");


            //Assert
            Assert.That(result, Is.TypeOf <RedirectResult>());
        }
Пример #4
0
        public void GetTheResponseUrl_WhenTheShortLinkNotExists_ReturnTheUrlYouEnterEDIsNotExist()
        {
            //Arrange
            var responseResult = new ResponseResultVm
            {
                ErrorMessage = "The Url you entered is not exist",
                ResponseUrl  = null
            };

            //Act
            _uow.Setup(x => x.ShortUrlRepository.GetByShortKey("")).Returns((ShortUrl)null);
            var shortUrlServices = new ShortUrlServices(_uow.Object, _utilities.Object);
            var result           = shortUrlServices.GetTheResponseUrl("", 3);

            _utilities.Verify(x => x.GetTheResponseFromTheRequestedUrl(""), Times.Never);

            //Assert
            Assert.That(result.ErrorMessage, Is.EqualTo(responseResult.ErrorMessage));
            Assert.That(result.ResponseUrl, Is.EqualTo(responseResult.ResponseUrl));
        }
Пример #5
0
        public void GetTheResponseUrl_WhenTheLinkIsNotValidAnyMore_ReturnTheUrlYouEnterEDNotValidAnyMore()
        {
            //Arrange
            var responseResult = new ResponseResultVm
            {
                ErrorMessage = "The Url you entered is not exist",
                ResponseUrl  = null
            };



            //Act
            _uow.Setup(x => x.ShortUrlRepository.GetByShortKey("")).Returns(new ShortUrl());
            _utilities.Setup(x => x.MonthsDifferenceBetweenTwoDates(new DateTime(), new DateTime())).Returns(4);
            var shortUrlServices = new ShortUrlServices(_uow.Object, _utilities.Object);
            var result           = shortUrlServices.GetTheResponseUrl("", 3);

            _utilities.Verify(x => x.GetTheResponseFromTheRequestedUrl(""), Times.Never);


            //Assert
            Assert.That(result.ErrorMessage, Does.Contain("is not live"));
            Assert.That(result.ResponseUrl, Is.EqualTo(responseResult.ResponseUrl));
        }