public async Task SetMainPhoto_PhotoIsNotFromBook_ReturnsUnathorizedResponse()
        {
            var newPhotoId = 3;

            var result = await _controller.SetMainPhoto(_book.Id, newPhotoId);

            Assert.That(result, Is.TypeOf <UnauthorizedResult>());
        }
Example #2
0
        public void IntegrationTest_Given_ProductIdAndPhotoIdThatAreNotRelated_When_SetMainPhoto_Then_Return_Unauthorized(int productId, int photoId)
        {
            //Act
            var result = _sut.SetMainPhoto(productId, photoId).Result;


            //Assert
            result.Should().BeOfType(typeof(BadRequestObjectResult));
            result.As <BadRequestObjectResult>().Value.Should().Be("Trying to change photo that do not exists for that product");
        }
Example #3
0
        public void Given_ProductIdAndPhotoId_When_SetMainPhoto_CaseWhenPhotoIsNotMain_ThenReturn_NoContent()
        {
            //Arrange
            int productId = 1;
            int photoId   = 2;

            int photoIdOfCurrentMainPhoto = 1;

            _mockedUnitOfWork.Setup(s => s.Product.GetWithPhotosOnly(productId))
            .ReturnsAsync(new Product()
            {
                Photos = new List <Photo>()
                {
                    new Photo()
                    {
                        Id = photoIdOfCurrentMainPhoto, isMain = true
                    },
                    new Photo()
                    {
                        Id = photoId
                    }
                }
            });

            _mockedUnitOfWork.Setup(s => s.Photo.GetAsync(photoId))
            .ReturnsAsync(new Photo()
            {
                Id = photoId, isMain = false
            });

            _mockedUnitOfWork.Setup(s => s.Photo.FindAsync(p => p.ProductId == productId && p.isMain == true))
            .ReturnsAsync(new Photo()
            {
                Id = photoIdOfCurrentMainPhoto, isMain = true
            });

            _mockedUnitOfWork.Setup(s => s.SaveAsync()).ReturnsAsync(true);


            //Act
            var result = _cut.SetMainPhoto(productId, photoId).Result;

            //Assert
            result.Should().BeOfType <NoContentResult>();

            _mockedUnitOfWork.Verify(v => v.Product.GetWithPhotosOnly(productId), Times.Once);

            _mockedUnitOfWork.Verify(v => v.Photo.GetAsync(photoId), Times.Once);

            _mockedUnitOfWork.Verify(v => v.Photo.FindAsync(p => p.ProductId == productId && p.isMain == true), Times.Once);

            _mockedUnitOfWork.Verify(v => v.SaveAsync(), Times.Once);
        }
        public async Task SetMainPhoto_NotMainPhotoPassed_ReturnsBadRequest()
        {
            var photo = new Photo()
            {
                Id     = 1,
                IsMain = true,
            };
            var photoToMain = new Photo()
            {
                Id     = 2,
                IsMain = false,
            };
            var user = new User()
            {
                Id     = 1,
                Photos = new List <Photo> {
                    photo, photoToMain
                }
            };
            var userClaims = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.NameIdentifier, "1"),
            }));

            var mapperMock           = new Mock <IMapper>();
            var cloudinaryConfigMock = Options.Create <CloudinarySettings>(new CloudinarySettings
            {
                ApiKey    = "test",
                ApiSecret = "test",
                CloudName = "test"
            });

            var repositoryMock = new Mock <IRepositoryWrapper>();

            repositoryMock.Setup(r => r.UserRepository.GetUserAsync(It.IsAny <int>())).ReturnsAsync(() => user);
            repositoryMock.Setup(r => r.PhotoRepository.GetPhotoAsync(It.IsAny <int>())).ReturnsAsync(() => photoToMain);
            repositoryMock.Setup(r => r.PhotoRepository.GetMainPhotoForUserAsync(It.IsAny <int>())).ReturnsAsync(() => photo);
            repositoryMock.Setup(r => r.SaveAllAsync()).ReturnsAsync(true);
            var controllerMock = new PhotosController(repositoryMock.Object, mapperMock.Object, cloudinaryConfigMock);


            controllerMock.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = userClaims
                }
            };

            var result = await controllerMock.SetMainPhoto(user.Id, photoToMain.Id);

            Assert.IsType <NoContentResult>(result);
            Assert.Equal(true, photoToMain.IsMain);
            Assert.Equal(false, photo.IsMain);
        }