public async Task GetPhoto_WhenCalled_RetrievesPhotoFromDb()
        {
            var id = 1;
            await _controller.GetPhoto(id);

            _repo.Verify(r => r.GetPhoto(id));
        }
Пример #2
0
        public void IntegrationTest_Given_PhotoId_When_GetPhoto_Then_Return_PhotoForReturnDtoWithOkStatus(int photoId)
        {
            //Arrange
            var expected = Data.PhotoForReturnDto();

            //Act
            var result = _sut.GetPhoto(photoId).Result;

            //Assert
            result.Should().BeOfType(typeof(OkObjectResult));
            result.As <OkObjectResult>().Value.As <PhotoForReturnDto>()
            .Should()
            .BeEquivalentTo(expected.First(p => p.Id == photoId));
        }
Пример #3
0
        public async Task NotGetPhoto_NotFound()
        {
            // Arrange
            var seedIds = new List <int> {
                new Random().Next(1, 50), new Random().Next(51, 100)
            };
            var notFoundId          = GetRandomIntExcept(seedIds);
            var mockPhotoRepository = new Mock <IPhotoRepository>();

            mockPhotoRepository.Setup(m => m.GetAsync(notFoundId, true)).ReturnsAsync((Photo)null);
            var mockCommentRepository  = new Mock <ICommentRepository>();
            var mockCategoryRepository = new Mock <ICategoryRepository>();
            var mockUserRepository     = new Mock <IUserRepository>();
            var mockAlbumRepository    = new Mock <IAlbumRepository>();
            var mockUnitOfWork         = new Mock <IUnitOfWork>();
            var mockPhotoUploadService = new Mock <IPhotoUploadService>();
            var mockHost = new Mock <IWebHostEnvironment>();

            mockHost.SetupGet(m => m.WebRootPath).Returns(string.Empty);
            var mockObjectDetectionService = new Mock <IObjectDetectionService>();
            PhotosController controller    = new PhotosController(this._mapper, mockPhotoRepository.Object,
                                                                  mockCategoryRepository.Object, mockUserRepository.Object, mockCommentRepository.Object,
                                                                  mockAlbumRepository.Object, mockUnitOfWork.Object,
                                                                  mockPhotoUploadService.Object, mockHost.Object, mockObjectDetectionService.Object);
            // Act
            var result = await controller.GetPhoto(notFoundId);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }
        public async Task GetPhoto_InvalidIdPassed_ReturnsOkResult()
        {
            var photo = new Photo()
            {
                Id = 1
            };
            var photoToReturn = new PhotoForReturnDto()
            {
                Id = 1
            };
            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(m => m.Map <PhotoForReturnDto>(photo)).Returns(photoToReturn);

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

            var repositoryMock = new Mock <IRepositoryWrapper>();

            repositoryMock.Setup(r => r.PhotoRepository.GetPhotoAsync(It.IsAny <int>())).ReturnsAsync(photo);

            var controllerMock = new PhotosController(repositoryMock.Object, mapperMock.Object, cloudinaryConfigMock);

            var result = await controllerMock.GetPhoto(2);

            Assert.IsType <OkObjectResult>(result);
        }
Пример #5
0
        public void Given_PhotoId_When_GetPhoto_ThenReturn_OkStatusWithPhotoForReturnDto()
        {
            //Arrange
            var photoId = 1;

            _mockedUnitOfWork.Setup(s => s.Photo.GetAsync(photoId)).ReturnsAsync(new Photo());

            //Act
            var result = _cut.GetPhoto(photoId).Result;

            //Assert
            result.Should().BeOfType <OkObjectResult>();
            result.As <OkObjectResult>().Value.Should().NotBeNull();
            result.As <OkObjectResult>().Value.Should().BeOfType <PhotoForReturnDto>();

            _mockedUnitOfWork.Verify(v => v.Photo.GetAsync(photoId), Times.Once);
        }
        public async Task GetPhoto_Success()
        {
            //Arrange------------------------------------------------------------------------------------------------------------------------------
            _mockRepo.Setup(x => x.PhotoRepository.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(UnitTestsDataInput.Users.First().Photos.First());

            _mockMapper.Setup(x => x.Map <PhotoForReturnProfileDto>(It.IsAny <Photo>()))
            .Returns(UnitTestsDataInput.PhotoForReturnProfileDto);


            var rout = new RouteData();

            rout.Values.Add("userId", UnitTestsDataInput.Users.First().Id);

            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, UnitTestsDataInput.userLogedInId),
            };
            var identity        = new ClaimsIdentity(claims);
            var claimsPrincipal = new ClaimsPrincipal(identity);
            var mockContext     = new Mock <HttpContext>();

            mockContext.SetupGet(x => x.User).Returns(claimsPrincipal);

            _controller.ControllerContext = new ControllerContext
            {
                HttpContext = mockContext.Object,
                RouteData   = rout
            };


            //Act----------------------------------------------------------------------------------------------------------------------------------
            var result = await _controller.GetPhoto(It.IsAny <string>());

            var okResult = result as OkObjectResult;

            //Assert-------------------------------------------------------------------------------------------------------------------------------
            Assert.NotNull(okResult);
            Assert.IsType <PhotoForReturnProfileDto>(okResult.Value);
            Assert.Equal(200, okResult.StatusCode);
        }
Пример #7
0
        public async void GetAlbumsReturn_NotFoundResponse()
        {
            //Mocking Setup
            var mockUserRequest = new Mock <IPhotoRequestHandler>();

            mockUserRequest.Setup(req => req.GetPhoto(1)).ReturnsAsync(GetPhoto());

            var mockIMapper = new Mock <IMapper>();

            mockIMapper.Setup(op => op.Map <PhotoDto>(It.IsAny <Photo>())).Returns(GetPhotoDto());
            //Act
            var controller = new PhotosController(mockUserRequest.Object, mockIMapper.Object);
            var album      = await controller.GetPhoto(3);

            //Assert
            Assert.IsType <Microsoft.AspNetCore.Mvc.NotFoundResult>(album.Result);
        }
Пример #8
0
        public async Task GetPhoto()
        {
            // Arrange
            var seedIds = new List <int> {
                new Random().Next(1, 50), new Random().Next(51, 100)
            };
            var seedPhotos             = SeedPhotos(seedIds);
            var mockPhotoRepository    = new Mock <IPhotoRepository>();
            var mockCommentRepository  = new Mock <ICommentRepository>();
            var mockCategoryRepository = new Mock <ICategoryRepository>();
            var mockUserRepository     = new Mock <IUserRepository>();
            var mockAlbumRepository    = new Mock <IAlbumRepository>();
            var mockUnitOfWork         = new Mock <IUnitOfWork>();
            var mockPhotoUploadService = new Mock <IPhotoUploadService>();
            var mockHost = new Mock <IWebHostEnvironment>();

            mockHost.SetupGet(m => m.WebRootPath).Returns(string.Empty);
            var mockObjectDetectionService = new Mock <IObjectDetectionService>();

            PhotosController controller = new PhotosController(this._mapper, mockPhotoRepository.Object,
                                                               mockCategoryRepository.Object, mockUserRepository.Object, mockCommentRepository.Object,
                                                               mockAlbumRepository.Object, mockUnitOfWork.Object,
                                                               mockPhotoUploadService.Object, mockHost.Object, mockObjectDetectionService.Object);

            foreach (var seedPhoto in seedPhotos)
            {
                var id = seedPhoto.Id;
                var seedPhotoResource = this._mapper.Map <Photo, PhotoResource>(seedPhoto);
                mockPhotoRepository.Setup(m => m.GetAsync(id, true)).ReturnsAsync(seedPhoto);

                seedPhotoResource.BoundingBoxFilePath = string.Format("{0}/{1}", controller.OutputFolderUrl, seedPhotoResource.FilePath);
                seedPhotoResource.FilePath            = string.Format("{0}/{1}", controller.UploadFolderUrl, seedPhotoResource.FilePath);
                // Act
                var result = await controller.GetPhoto(id);

                // Assert
                Assert.IsType <OkObjectResult>(result);
                Assert.IsType <PhotoResource>(((OkObjectResult)result).Value);
                PhotoResource returnedPhotoResource = (PhotoResource)((OkObjectResult)result).Value;
                Assert.True(returnedPhotoResource.Equals(seedPhotoResource));
            }
        }
        public void GetPhoto_InvalidPhotoId_ThrowInvalidOperationException()
        {
            var authServiceMock = new Mock <IAuthService>();
            var dataServiceMock = new Mock <IDataService>();

            Guid photoId = Guid.NewGuid();

            dataServiceMock.Setup(u => u.GetPhoto(photoId)).Throws(new InvalidOperationException("User hasn`t this photo."));

            var controller = new PhotosController(dataServiceMock.Object, authServiceMock.Object);

            var result = controller.GetPhoto(photoId);

            var statusCode = ((BadRequestObjectResult)result).StatusCode;
            var jsonValue  = JsonConvert.SerializeObject(((BadRequestObjectResult)result).Value);
            var dictionary = JsonConvert.DeserializeObject <Dictionary <object, object> >(jsonValue);

            Assert.True(statusCode == 400);
            Assert.Equal("User hasn`t this photo.", dictionary["Message"]);

            dataServiceMock.VerifyAll();
        }