From 69ef19199964ed2dc7cd891f160d0a5c79521bd1 Mon Sep 17 00:00:00 2001 From: Iulia Dormenco <31412211+decembrya@users.noreply.github.com> Date: Tue, 5 Oct 2021 10:33:33 +0300 Subject: [PATCH] 106 create certification bug (#108) * fix empty photo bug * remove using --- .../CreateCertificationHandler.cs | 12 ++++-- .../CreateCertificationHandlerShould.cs | 37 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Src/DeUrgenta.Certifications.Api/CommandHandlers/CreateCertificationHandler.cs b/Src/DeUrgenta.Certifications.Api/CommandHandlers/CreateCertificationHandler.cs index e405aa0..8338737 100644 --- a/Src/DeUrgenta.Certifications.Api/CommandHandlers/CreateCertificationHandler.cs +++ b/Src/DeUrgenta.Certifications.Api/CommandHandlers/CreateCertificationHandler.cs @@ -46,10 +46,14 @@ public async Task> Handle(CreateCertification request await _context.Certifications.AddAsync(certification, cancellationToken); await _context.SaveChangesAsync(cancellationToken); - var photoUrl = await _storage.SaveAttachmentAsync(certification.Id, - user.Sub, - request.Photo.OpenReadStream(), - Path.GetExtension(request.Photo.FileName)); + string photoUrl = null; + if (request.Photo != null) + { + photoUrl = await _storage.SaveAttachmentAsync(certification.Id, + user.Sub, + request.Photo.OpenReadStream(), + Path.GetExtension(request.Photo.FileName)); + } return new CertificationModel { diff --git a/Src/Tests/DeUrgenta.Certifications.Api.Tests/CommandHandlers/CreateCertificationHandlerShould.cs b/Src/Tests/DeUrgenta.Certifications.Api.Tests/CommandHandlers/CreateCertificationHandlerShould.cs index e612a53..99d394f 100644 --- a/Src/Tests/DeUrgenta.Certifications.Api.Tests/CommandHandlers/CreateCertificationHandlerShould.cs +++ b/Src/Tests/DeUrgenta.Certifications.Api.Tests/CommandHandlers/CreateCertificationHandlerShould.cs @@ -4,8 +4,10 @@ using DeUrgenta.Certifications.Api.Commands; using DeUrgenta.Certifications.Api.Models; using DeUrgenta.Certifications.Api.Storage; +using DeUrgenta.Certifications.Api.Tests.Builders; using DeUrgenta.Common.Validation; using DeUrgenta.Domain; +using DeUrgenta.Domain.Entities; using DeUrgenta.Tests.Helpers; using NSubstitute; using Shouldly; @@ -41,5 +43,40 @@ public async Task Return_failed_result_when_validation_fails() // Assert result.IsFailure.ShouldBeTrue(); } + + [Fact] + public async Task Return_success_if_photo_is_not_provided_in_request() + { + //Arrange + var userSub = TestDataProviders.RandomString(); + var certificationRequest = new CertificationRequestBuilder() + .WithPhoto(null) + .Build(); + + var user = new User + { + Sub = userSub, + FirstName = TestDataProviders.RandomString(), + LastName = TestDataProviders.RandomString() + }; + await _dbContext.Users.AddAsync(user); + await _dbContext.SaveChangesAsync(); + + var createCertification = new CreateCertification(userSub, certificationRequest); + + var storage = Substitute.For(); + var validator = Substitute.For>(); + validator + .IsValidAsync(createCertification) + .Returns(Task.FromResult(true)); + + var sut = new CreateCertificationHandler(validator, _dbContext, storage); + + //Act + var result = await sut.Handle(createCertification, CancellationToken.None); + + //Assert + result.IsSuccess.ShouldBeTrue(); + } } }