public async Task GetGallery_not_mine_and_it_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            GalleryEntity newGallery = new GalleryEntity()
            {
                Id       = Guid.NewGuid(),
                fk_owner = UserEntities[1].Id,
                Name     = "TestName",
                owner    = UserEntities[1]
            };

            GalleryEntities.Add(newGallery);

            // Act
            ActionResult <GalleryDTO> response = await controller.GetGallery(newGallery.Id);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(UnauthorizedResult));
            var result = response.Result as UnauthorizedResult;

            Assert.AreEqual(401, result.StatusCode);
            GalleryEntities.Remove(newGallery);
        }
Exemplo n.º 2
0
        public async Task DeleteImage_mine_and_it_exist()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            Guid        imageId     = Guid.NewGuid();
            ImageEntity imageEntity = new ImageEntity()
            {
                Id = imageId, fk_gallery = GalleryEntities[0].Id, gallery = GalleryEntities[0], Name = "Test1", Extension = ".jpg", SizeInBytes = 100
            };

            ImageEntities.Add(imageEntity);

            // Act
            ActionResult response = await controller.DeleteImage(GalleryEntities[0].Id, imageId);

            // Assert
            Assert.IsInstanceOfType(response, typeof(NoContentResult));
            var result = response as NoContentResult;

            Assert.AreEqual(204, result.StatusCode);
            ImageService.Verify(repo => repo.DeleteImageAsync(It.IsAny <Guid>()), Times.Once());
            ImageEntities.Remove(imageEntity);
        }
        public async Task DeleteGallery_mine_and_it_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            GalleryEntity newGallery = new GalleryEntity()
            {
                Id       = Guid.NewGuid(),
                fk_owner = UserEntities[0].Id,
                Name     = "TestName",
                owner    = UserEntities[0]
            };

            GalleryEntities.Add(newGallery);

            // Act
            ActionResult response = await controller.DeleteGallery(newGallery.Id);

            // Assert
            Assert.IsInstanceOfType(response, typeof(NoContentResult));
            var result = response as NoContentResult;

            Assert.AreEqual(204, result.StatusCode);
            GalleryService.Verify(repo => repo.DeleteGalleryAsync(It.IsAny <Guid>()), Times.Once());
            GalleryEntities.Remove(newGallery);
        }
        public async Task PutGallery_mine_and_it_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            GalleryEntity newGalleryEntity = new GalleryEntity()
            {
                Id       = Guid.NewGuid(),
                Name     = "TestGalleryName",
                fk_owner = UserEntities[0].Id,
                owner    = UserEntities[0]
            };

            GalleryEntities.Add(newGalleryEntity);

            // Act
            GalleryPutDTO             galleryPutDto = new GalleryPutDTO("UpdatedTestGalleryName");
            ActionResult <GalleryDTO> response      = await controller.PutGallery(newGalleryEntity.Id, galleryPutDto);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(CreatedAtActionResult));
            var result = response.Result as CreatedAtActionResult;

            Assert.AreEqual(201, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(GalleryDTO));
            GalleryDTO retrievedItem = result.Value as GalleryDTO;

            Assert.AreEqual(retrievedItem.Id, newGalleryEntity.Id);
            Assert.AreEqual(retrievedItem.Name, "UpdatedTestGalleryName");
            GalleryEntities.Remove(newGalleryEntity);
        }
        public async Task DeleteGallery_not_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            // Act
            ActionResult response = await controller.DeleteGallery(Guid.NewGuid());

            // Assert
            Assert.IsInstanceOfType(response, typeof(NotFoundResult));
            var result = response as NotFoundResult;

            Assert.AreEqual(404, result.StatusCode);
        }
Exemplo n.º 6
0
        public async Task GetUser_get_self_not_exist()
        {
            // Arrange
            var controller = new UserController(UserService.Object);
            var randomGuid = Guid.NewGuid();

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(randomGuid.ToString());

            // Act
            ActionResult <UserDTO> response = await controller.GetUser(randomGuid);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(NotFoundResult));
            var result = response.Result as NotFoundResult;

            Assert.AreEqual(404, result.StatusCode);
        }
        public async Task PutGallery_not_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            // Act
            GalleryPutDTO             galleryPutDto = new GalleryPutDTO("UpdatedTestGalleryName");
            ActionResult <GalleryDTO> response      = await controller.PutGallery(Guid.NewGuid(), galleryPutDto);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(NotFoundResult));
            var result = response.Result as NotFoundResult;

            Assert.AreEqual(404, result.StatusCode);
        }
Exemplo n.º 8
0
        public async Task GetImage_mine_and_image_not_exist()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            Guid imageId = Guid.NewGuid();

            // Act
            ActionResult response = await controller.GetImage(GalleryEntities[0].Id, imageId, false, null, null, null);

            // Assert
            Assert.IsInstanceOfType(response, typeof(NotFoundResult));
            var result = response as NotFoundResult;

            Assert.AreEqual(404, result.StatusCode);
        }
Exemplo n.º 9
0
        public async Task GetImages_not_own_gallery()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[1].Id.ToString());

            Guid       galleryId  = GalleryEntities[0].Id;
            Pagination pagination = new Pagination();

            // Act
            ActionResult <IEnumerable <byte[]> > response = await controller.GetImages(galleryId, pagination, false, null, null, null);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(UnauthorizedResult));
            var result = response.Result as UnauthorizedResult;

            Assert.AreEqual(401, result.StatusCode);
        }
Exemplo n.º 10
0
        public async Task CreateImage_not_own_gallery()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[1].Id.ToString());

            Guid             galleryId = GalleryEntities[0].Id;
            ImageCreationDTO newItem   = new ImageCreationDTO("CreatedTestName", TestFormFile());

            // Act
            ActionResult <ImageDTO> response = await controller.CreateImage(galleryId, newItem);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(UnauthorizedResult));
            var result = response.Result as UnauthorizedResult;

            Assert.AreEqual(401, result.StatusCode);
        }
Exemplo n.º 11
0
        public async Task DeleteImage_not_own_gallery()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[1].Id.ToString());

            Guid galleryId = GalleryEntities[0].Id;
            Guid imageId   = ImageEntities[0].Id;

            // Act
            ActionResult response = await controller.DeleteImage(galleryId, imageId);

            // Assert
            Assert.IsInstanceOfType(response, typeof(UnauthorizedResult));
            var result = response as UnauthorizedResult;

            Assert.AreEqual(401, result.StatusCode);
        }
Exemplo n.º 12
0
        public async Task GetUser_not_allow_get_another_users_data()
        {
            /**
             * Return 401 Unauthorized if trying to retrieve another users data
             * This unittest tries to access users[0] by setting users[1] as claim
             */

            // Arrange
            var controller = new UserController(UserService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[1].Id.ToString());

            // Act
            ActionResult <UserDTO> response = await controller.GetUser(UserEntities[0].Id);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(UnauthorizedResult));
            var result = response.Result as UnauthorizedResult;

            Assert.AreEqual(401, result.StatusCode);
        }
Exemplo n.º 13
0
        public async Task GetGalleries()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            var pagination = new Pagination();

            // Act
            ActionResult <IEnumerable <GalleryDTO> > response = await controller.GetGalleries(pagination);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(OkObjectResult));
            var result = response.Result as OkObjectResult;

            Assert.AreEqual(200, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(IEnumerable <GalleryDTO>));
            IEnumerable <GalleryDTO> retrievedItems = result.Value as IEnumerable <GalleryDTO>;

            Assert.AreEqual(2, retrievedItems.Count());
        }
Exemplo n.º 14
0
        public async Task GetGallery_mine_and_it_exist()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);
            var retrieveThisGalleryItem = GalleryEntities[0];

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            // Act
            ActionResult <GalleryDTO> response = await controller.GetGallery(retrieveThisGalleryItem.Id);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(OkObjectResult));
            var result = response.Result as OkObjectResult;

            Assert.AreEqual(200, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(GalleryDTO));
            GalleryDTO retrievedItem = result.Value as GalleryDTO;

            Assert.AreEqual(retrieveThisGalleryItem.Id, retrievedItem.Id);
            Assert.AreEqual(retrieveThisGalleryItem.Name, retrievedItem.Name);
        }
Exemplo n.º 15
0
        public async Task GetUser_get_self()
        {
            // Arrange
            var        controller           = new UserController(UserService.Object);
            UserEntity retrieveThisUserItem = UserEntities[0];

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            // Act
            ActionResult <UserDTO> response = await controller.GetUser(retrieveThisUserItem.Id);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(OkObjectResult));
            var result = response.Result as OkObjectResult;

            Assert.AreEqual(200, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(UserDTO));
            UserDTO retrievedItem = result.Value as UserDTO;

            Assert.AreEqual(retrieveThisUserItem.Id, retrievedItem.Id);
            Assert.AreEqual(retrieveThisUserItem.Username, retrievedItem.Username);
        }
Exemplo n.º 16
0
        public async Task CreateGallery()
        {
            // Arrange
            var controller = new GalleryController(GalleryService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[1].Id.ToString());

            GalleryCreationDTO newGalleryItem = new GalleryCreationDTO("CreatedTestName");

            // Act
            ActionResult <GalleryDTO> response = await controller.CreateGallery(newGalleryItem);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(CreatedAtActionResult));
            var result = response.Result as CreatedAtActionResult;

            Assert.AreEqual(201, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(GalleryDTO));
            GalleryDTO createdItem = result.Value as GalleryDTO;

            Assert.AreEqual(createdItem.Name, "CreatedTestName");
        }
Exemplo n.º 17
0
        public async Task GetImages()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            Guid       galleryId  = GalleryEntities[0].Id;
            Pagination pagination = new Pagination();

            // Act
            ActionResult <IEnumerable <byte[]> > response = await controller.GetImages(galleryId, pagination, false, null, null, null);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(OkObjectResult));
            var result = response.Result as OkObjectResult;

            Assert.AreEqual(200, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(IEnumerable <byte[]>));
            IEnumerable <byte[]> retrievedItems = result.Value as IEnumerable <byte[]>;

            Assert.AreEqual(1, retrievedItems.Count());
        }
Exemplo n.º 18
0
        public async Task DeleteImage_image_not_owned_by_gallery()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());
            Guid        imageId     = Guid.NewGuid();
            ImageEntity imageEntity = new ImageEntity()
            {
                Id = imageId, fk_gallery = GalleryEntities[0].Id, gallery = GalleryEntities[0], Name = "Test1", Extension = ".jpg", SizeInBytes = 100
            };

            ImageEntities.Add(imageEntity);

            // Act
            ActionResult response = await controller.DeleteImage(GalleryEntities[1].Id, imageId);

            // Assert
            Assert.IsInstanceOfType(response, typeof(NotFoundResult));
            var result = response as NotFoundResult;

            Assert.AreEqual(404, result.StatusCode);
            ImageEntities.Remove(imageEntity);
        }
Exemplo n.º 19
0
        public async Task CreateImage()
        {
            // Arrange
            var controller = new ImageController(GalleryService.Object, ImageService.Object);

            controller.ControllerContext = APIControllerUtils.CreateApiControllerContext(UserEntities[0].Id.ToString());

            Guid             galleryId = GalleryEntities[0].Id;
            ImageCreationDTO newItem   = new ImageCreationDTO("CreatedTestName", TestFormFile());

            // Act
            ActionResult <ImageDTO> response = await controller.CreateImage(galleryId, newItem);

            // Assert
            Assert.IsInstanceOfType(response.Result, typeof(CreatedAtActionResult));
            var result = response.Result as CreatedAtActionResult;

            Assert.AreEqual(201, result.StatusCode);
            Assert.IsNotNull(result.Value);
            Assert.IsInstanceOfType(result.Value, typeof(ImageDTO));
            ImageDTO createdItem = result.Value as ImageDTO;

            Assert.AreEqual(createdItem.Name, "CreatedTestName");
        }