public void Create_ShouldNotCreateWhenExtensionIsNotCorrect()
        {
            var user  = UserCreator.Create("test");
            var file  = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");
            var model = new CreatePhotoInputModel()
            {
                Title            = "Test",
                Description      = "Test",
                IsCommentAllowed = false,
                IsPrivate        = false,
                LicenseId        = "Test",
                File             = file,
            };
            var list = new List <Photo>();

            var photosRepo      = DeletableEntityRepositoryMock.Get <Photo>(list);
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var       service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);
            Exception ex      = Assert.Throws <AggregateException>(() => service.CreatePhotoAsync(user.Id, "path", model).Wait());

            Assert.Contains("Invalid image extension", ex.Message);
            Assert.Empty(list);
        }
        public void Create_ShouldCreateSuccessfully()
        {
            var user  = UserCreator.Create("test");
            var file  = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.png");
            var model = new CreatePhotoInputModel()
            {
                Title            = "Test",
                Description      = "Test",
                IsCommentAllowed = false,
                IsPrivate        = false,
                LicenseId        = "Test",
                File             = file,
            };
            var list = new List <Photo>();

            var photosRepo      = DeletableEntityRepositoryMock.Get <Photo>(list);
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);

            service.CreatePhotoAsync(user.Id, "path", model).Wait();

            Assert.Single(list);
        }
        public void Delete_ShouldThrowExceptionWhenPhotoDoesNotExists()
        {
            var user  = UserCreator.Create("test");
            var photo = PhotoCreator.Create(user, false, false);

            var list            = new List <Photo>();
            var photosRepo      = DeletableEntityRepositoryMock.Get <Photo>(list);
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var       service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);
            Exception ex      = Assert.Throws <AggregateException>(() => service.DeletePhotoAsync(photo.Id, user.Id).Wait());

            Assert.Contains("Such photo does not exists", ex.Message);
        }
        public void GetAllByUserId_ShouldReturnAllPhotosWhenUserIsOwner()
        {
            var user         = UserCreator.Create("test");
            var privatePhoto = PhotoCreator.Create(user, true, false);
            var publicPhoto  = PhotoCreator.Create(user, false, false);

            var photosRepo = DeletableEntityRepositoryMock.Get <Photo>(new List <Photo>()
            {
                privatePhoto, publicPhoto
            });
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);
            var result  = service.GetAllByUserId <PhotoViewModel>(user.Id, user.Id);

            Assert.Equal(2, result.Count);
        }
        public void Delete_ShouldDeletePhotoSuccessfully()
        {
            var user  = UserCreator.Create("test");
            var photo = PhotoCreator.Create(user, false, false);

            var list = new List <Photo>()
            {
                photo
            };
            var photosRepo      = DeletableEntityRepositoryMock.Get <Photo>(list);
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);

            service.DeletePhotoAsync(photo.Id, user.Id).Wait();

            Assert.Empty(list);
        }
        public void GetLatestPublic_ShouldReturnOnlyPublicPhotos()
        {
            var user   = UserCreator.Create("test");
            var photos = new List <Photo>()
            {
                PhotoCreator.Create(user, false, false),
                PhotoCreator.Create(user, true, false),
                PhotoCreator.Create(user, false, false),
                PhotoCreator.Create(user, true, false),
            };

            var photosRepo      = DeletableEntityRepositoryMock.Get <Photo>(photos);
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var service = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);
            var result  = service.GetLatestPublic <PhotoViewModel>();

            Assert.Equal(2, result.Count);
        }
        public void GetById_ShouldNotReturnPrivatePhotoWhenUserIsNotOwner()
        {
            var user         = UserCreator.Create("test");
            var visitor      = UserCreator.Create("visitor");
            var privatePhoto = PhotoCreator.Create(user, true, false);
            var publicPhoto  = PhotoCreator.Create(user, false, false);

            var photosRepo = DeletableEntityRepositoryMock.Get <Photo>(new List <Photo>()
            {
                privatePhoto, publicPhoto
            });
            var photoAlbumsRepo = EfRepositoryMock.Get <PhotoAlbum>(new List <PhotoAlbum>());
            var storageService  = PhotoStorageServiceMock.Get();
            var metadataService = PhotoMetadataServiceMock.Get();

            var service            = new PhotoService(photosRepo.Object, storageService.Object, metadataService.Object, photoAlbumsRepo.Object);
            var privatePhotoResult = service.GetById <PhotoViewModel>(privatePhoto.Id, visitor.Id);
            var publicPhotoResult  = service.GetById <PhotoViewModel>(publicPhoto.Id, visitor.Id);

            Assert.Null(privatePhotoResult);
            Assert.NotNull(publicPhotoResult);
        }