Exemple #1
0
        public void PerformDeleteAd_AdDoesNotExists_ReturnMessage()
        {
            // Given
            DeleteAdModel model = new DeleteAdModel
            {
                AdId = 56
            };

            var adRepoMock = new Moq.Mock <IAdRepository>();

            adRepoMock.Setup(r => r.GetAdById <BaseAd>(model.AdId)).Returns((BaseAd)null);

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, null, null);

            // When
            DeleteAdModel result = service.DeleteAd(model);

            // Then
            Assert.AreEqual(model.AdId, result.AdId);
            Assert.IsNull(result.Password);
            Assert.IsNull(result.SelectedDeletionReasonId);
            Assert.AreEqual(0, result.NbTry);
            Assert.AreEqual("Cette annonce n'existe pas ou plus.", result.InfoMessage);
            Assert.IsFalse(result.CanDeleteAd);
            Assert.IsFalse(result.IsDeleted);
        }
Exemple #2
0
        public void PerformDeleteAd_AdExists_PasswordMatchesNoReasonAndNoSearchAdCache_MarkAdAsDeleted()
        {
            // Given
            DeleteAdModel model = new DeleteAdModel
            {
                AdId     = 56,
                Password = "******",
                NbTry    = 1,
                SelectedDeletionReasonId = null
            };
            SearchAdCache adc = new SearchAdCache()
            {
                AdId = 56
            };
            User creator = new User()
            {
                Password = "******"
            };
            BaseAd ad = new Ad()
            {
                CreatedBy = creator
            };

            var adRepoMock = new Moq.Mock <IAdRepository>();

            adRepoMock.Setup(r => r.GetAdById <BaseAd>(model.AdId)).Returns(ad);

            var repoMock = new Moq.Mock <IRepository>();

            repoMock.Setup(x => x.Get <SearchAdCache>(model.AdId)).Returns(adc);
            repoMock.Setup(x => x.Save <BaseAd>(ad));
            repoMock.Setup(x => x.Delete <SearchAdCache>(adc));

            var helperMock = new Moq.Mock <IHelperService>();

            helperMock.Setup(x => x.GetCurrentDateTime()).Returns(new DateTime(2013, 01, 26));

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, repoMock.Object, helperMock.Object);

            // When
            DeleteAdModel result = service.DeleteAd(model);

            // Then
            Assert.AreEqual(model.AdId, result.AdId);
            Assert.IsNull(result.Password);
            Assert.IsNull(result.SelectedDeletionReasonId);
            Assert.AreEqual(0, result.NbTry);
            Assert.AreEqual("Votre annonce a correctement été supprimée. Elle n'est plus disponible à la recherche.", result.InfoMessage);
            Assert.IsFalse(result.CanDeleteAd);
            Assert.IsTrue(result.IsDeleted);

            Assert.AreEqual(new DateTime(2013, 01, 26), ad.DeletionDate);
            Assert.IsTrue(ad.IsDeleted);
            Assert.IsNull(ad.DeletedReason);

            repoMock.Verify(x => x.Save <BaseAd>(ad), Times.Once());
            repoMock.Verify(x => x.Delete <SearchAdCache>(adc), Times.Never());
        }
        public ActionResult Delete(DeleteAdModel model)
        {
            var result = _adDeletionServices.DeleteAd(model);

            if (result.NbTry > 0)
            {
                ModelState.AddModelError("Password", "Mot de passe incorrect");
            }

            return(View(result));
        }
        public DeleteAdModel DeleteAd(long adId)
        {
            DeleteAdModel result = new DeleteAdModel();

            result.AdId        = adId;
            result.CanDeleteAd = _adRepository.CanDeleteAd(adId);

            if (!result.CanDeleteAd)
            {
                result.InfoMessage = "Cette annonce n'existe pas ou plus.";
            }

            return(result);
        }
        public DeleteAdModel DeleteAd(DeleteAdModel model)
        {
            DeleteAdModel result = new DeleteAdModel();

            result.AdId = model.AdId;

            BaseAd ad = _adRepository.GetAdById <BaseAd>(model.AdId);

            if (ad == null || ad.IsDeleted)
            {
                result.InfoMessage = "Cette annonce n'existe pas ou plus.";
                result.CanDeleteAd = false;
                return(result);
            }

            if (model.Password != ad.CreatedBy.Password)
            {
                result.NbTry = ++model.NbTry;
                result.SelectedDeletionReasonId = model.SelectedDeletionReasonId;
                result.CanDeleteAd = true;
                return(result);
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
            {
                ad.IsDeleted    = true;
                ad.DeletionDate = _helper.GetCurrentDateTime();
                if (model.SelectedDeletionReasonId.HasValue)
                {
                    ad.DeletedReason = _repository.Get <DeletionReason>(model.SelectedDeletionReasonId);
                }
                _repository.Save <BaseAd>(ad);

                SearchAdCache adCache = _repository.Get <SearchAdCache>(ad.Id);
                if (adCache != null)
                {
                    _repository.Delete <SearchAdCache>(adCache);
                }

                result.IsDeleted   = true;
                result.InfoMessage = "Votre annonce a correctement été supprimée. Elle n'est plus disponible à la recherche.";

                scope.Complete();
            }

            return(result);
        }
Exemple #6
0
        public void DeleteAd_AdExists_ReturnFilledInModelForDeletion()
        {
            // Given
            long adId = 56;

            var adRepoMock = new Moq.Mock <IAdRepository>();

            adRepoMock.Setup(r => r.CanDeleteAd(adId)).Returns(true);

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, null, null);

            // When
            DeleteAdModel model = service.DeleteAd(adId);

            // Then
            Assert.AreEqual(adId, model.AdId);
            Assert.IsNull(model.Password);
            Assert.IsNull(model.SelectedDeletionReasonId);
            Assert.AreEqual(0, model.NbTry);
            Assert.IsNull(model.InfoMessage);
            Assert.IsTrue(model.CanDeleteAd);
            Assert.IsFalse(model.IsDeleted);
        }
Exemple #7
0
        public void DeleteAd_AdIsAlreadyDeleted_ReturnMessage()
        {
            // Given
            long adId = 56;

            var adRepoMock = new Moq.Mock <IAdRepository>();

            adRepoMock.Setup(r => r.CanDeleteAd(adId)).Returns(false);

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, null, null);

            // When
            DeleteAdModel model = service.DeleteAd(adId);

            // Then
            Assert.AreEqual(adId, model.AdId);
            Assert.IsNull(model.Password);
            Assert.IsNull(model.SelectedDeletionReasonId);
            Assert.AreEqual(0, model.NbTry);
            Assert.AreEqual("Cette annonce n'existe pas ou plus.", model.InfoMessage);
            Assert.IsFalse(model.CanDeleteAd);
            Assert.IsFalse(model.IsDeleted);
        }
Exemple #8
0
        public void PerformDeleteAd_AdExists_PasswordDoesNotMatch()
        {
            // Given
            DeleteAdModel model = new DeleteAdModel
            {
                AdId     = 56,
                Password = "******",
                NbTry    = 1,
                SelectedDeletionReasonId = 7
            };
            User creator = new User()
            {
                Password = "******"
            };
            BaseAd ad = new Ad()
            {
                CreatedBy = creator
            };

            var adRepoMock = new Moq.Mock <IAdRepository>();

            adRepoMock.Setup(r => r.GetAdById <BaseAd>(model.AdId)).Returns(ad);

            AdDeletionServices service = new AdDeletionServices(adRepoMock.Object, null, null);

            // When
            DeleteAdModel result = service.DeleteAd(model);

            // Then
            Assert.AreEqual(model.AdId, result.AdId);
            Assert.IsNull(result.Password);
            Assert.AreEqual(7, result.SelectedDeletionReasonId);
            Assert.AreEqual(2, result.NbTry);
            Assert.IsNull(result.InfoMessage);
            Assert.IsTrue(result.CanDeleteAd);
            Assert.IsFalse(result.IsDeleted);
        }