Exemplo n.º 1
0
        public async Task ReturnViewWithoutModelWhenSongIsUploadedSuccessfully()
        {
            string expectedMessage = "successfully";

            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength - 1);

            var uploadSongStub = new Mock <ICommandService <UploadSong> >();

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                uploadSong: uploadSongStub.Object);

            // Act
            var result = await sut.Upload(model) as WithMessageResult;

            var realAction = result.Action as ViewResult;

            // Assert
            Assert.IsNull(realAction.Model);
        }
Exemplo n.º 2
0
        public async Task ReturnViewWithSuccessMessageWhenSongIsEditedSuccessfully()
        {
            string expectedMessage = "edited successfully";

            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength - 1);

            var editSongMock = new Mock <ICommandService <EditSong> >();

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: editSongMock.Object);

            // Act
            var result = await sut.Edit(It.IsAny <string>(), model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("success", result.Type);
            StringAssert.Contains(expectedMessage, result.Message);
        }
Exemplo n.º 3
0
        public async Task ReturnViewWithErrorMessageWhenCommandServiceThrowNotFoundException()
        {
            string expectedMessage = "Fake message";

            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength - 1);

            var editSongStub = new Mock <ICommandService <EditSong> >();

            editSongStub.Setup(u => u.ExecuteAsync(It.IsAny <EditSong>()))
            .Throws(new NotFoundException(expectedMessage));

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: editSongStub.Object);

            // Act
            var result = await sut.Edit(It.IsAny <string>(), model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("danger", result.Type);
            Assert.AreEqual(expectedMessage, result.Message);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(string id, SongFormModel model)
        {
            if (model.File != null &&
                (!model.File.IsSong() || model.File.Length > SongMaxLength))
            {
                return(View(model)
                       .WithErrorMessage($"Your submission should be an audio file and no more than {SongMaxMBs} MBs in size!"));
            }

            string fileExtension = model.File
                                   .GetFileExtension();

            EditSong command = new EditSong()
            {
                Title         = model.Title,
                FileExtension = fileExtension,
                ReleasedYear  = model.ReleasedYear,
                Singer        = model.Singer,
                SongFile      = model.File?.ToByteArray(),
                SongId        = id
            };

            string message = await this.CallServiceAsync(
                async() => await this.editSong.ExecuteAsync(command));

            if (message != null)
            {
                return(View()
                       .WithErrorMessage(message));
            }

            return(View()
                   .WithSuccessMessage("Song edited successfully."));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Upload(SongFormModel model)
        {
            model.Categories = await this.GetCategoriesListItemsAsync();

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (model.Song is null)
            {
                TempData.AddErrorMessage("Please, choose a file!");

                return(View(model));
            }

            if (!model.Song.FileName.EndsWith($".{SongExtension}") ||
                model.Song.Length > SongFileMaxLength)
            {
                TempData.AddErrorMessage($"Your submission should be a .{SongExtension} file and no more than 10 MBs in size!");

                return(View(model));
            }

            if (await this.songService.ExistsAsync(model.Name))
            {
                TempData.AddErrorMessage($"Song {model.Name} already exists!");

                return(View(model));
            }

            string userId = this.userManager.GetUserId(User);

            byte[] fileContent = await model.Song.ToByteArrayAsync();

            bool success = await this.songService.UploadAsync(
                userId,
                model.Name,
                model.Singer,
                model.CategoryId,
                fileContent);

            if (!success)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage($"Song {model.Name}.{SongExtension} uploaded successfully.");

            return(View(model));
        }
Exemplo n.º 6
0
        public async Task ReturnViewWithErrorMessageWhenSongFileIsNull()
        {
            SongFormModel model = new SongFormModel();

            // Arrange
            SongsController sut = CreateSongsController(Mock.Of <ICommandService <UploadSong> >());

            // Act
            var result = await sut.Upload(model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("danger", result.Type);
            StringAssert.Contains("choose a file", result.Message);
        }
Exemplo n.º 7
0
        public async Task ReturnViewWithTheSameModelWhenSongFileIsNull()
        {
            SongFormModel model = new SongFormModel();

            // Arrange
            SongsController sut = CreateSongsController(Mock.Of <ICommandService <UploadSong> >());

            // Act
            var result = await sut.Upload(model) as WithMessageResult;

            var realAction = result.Action as ViewResult;

            // Assert
            Assert.IsNotNull(realAction.Model);
            Assert.AreSame(model, realAction.Model);
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Edit(string id)
        {
            Song song = null;
            GetSongForEditById query = new GetSongForEditById()
            {
                SongId = id
            };

            string message = await this.CallServiceAsync(
                async() => song = await this.getSongForEdit.ExecuteAsync(query));

            if (message != null)
            {
                return(View()
                       .WithErrorMessage(message));
            }

            SongFormModel model = Mapper.Map <SongFormModel>(song);

            return(View(model));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Upload(SongFormModel model)
        {
            if (model.File is null)
            {
                return(View(model)
                       .WithErrorMessage("Please, choose a file!"));
            }

            // Business validation?
            if (!model.File.IsSong() || model.File.Length > SongMaxLength)
            {
                return(View(model)
                       .WithErrorMessage($"Your submission should be an audio file and no more than {SongMaxMBs} MBs in size!"));
            }

            string fileExtension = model.File
                                   .GetFileExtension();

            UploadSong command = new UploadSong()
            {
                UploaderId    = this.User?.FindFirst(ClaimTypes.NameIdentifier).Value,
                Title         = model.Title,
                FileExtension = fileExtension,
                ReleasedYear  = model.ReleasedYear,
                Singer        = model.Singer,
                SongFile      = model.File.ToByteArray()
            };

            string message = await this.CallServiceAsync(
                async() => await this.uploadSong.ExecuteAsync(command));

            if (message != null)
            {
                return(View(model)
                       .WithErrorMessage(message));
            }

            return(View()
                   .WithSuccessMessage("Song uploaded successfully."));
        }
Exemplo n.º 10
0
        public async Task ReturnViewWithErrorMessageWhenPassedFileTypeIsNotSong()
        {
            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "InvalidSongFile");

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(Mock.Of <ICommandService <UploadSong> >());

            // Act
            var result = await sut.Upload(model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("danger", result.Type);
            StringAssert.Contains("should be an audio", result.Message);
        }
Exemplo n.º 11
0
        public async Task ReturnViewWithErrorMessageWhenPassedFileIsTooLarge()
        {
            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength + 1);

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: Mock.Of <ICommandService <EditSong> >());

            // Act
            var result = await sut.Edit(It.IsAny <string>(), model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("danger", result.Type);
            StringAssert.Contains("no more than", result.Message);
        }
Exemplo n.º 12
0
        public async Task TheEditSongCommandServiceOnce()
        {
            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength - 1);

            var editSongMock = new Mock <ICommandService <EditSong> >();

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: editSongMock.Object);

            // Act
            await sut.Edit(It.IsAny <string>(), model);

            // Assert
            editSongMock.Verify(x => x.ExecuteAsync(It.IsAny <EditSong>()), Times.Once());
        }