public async Task <IResponse> AddAsync(CreateRequestAchievementModel model, Guid userId, CancellationToken cancellationToken) { var user = await _userRepository.GetByIdAsync(userId, cancellationToken); var achievement = await _achievementRepository.GetByIdAsync(model.AchievementId, cancellationToken); var requestAchievementPage = _stringLocalizer["RequestAchievementPage"].ToString(); var pageWithParams = requestAchievementPage.Replace("{userLastName}", user.LastName); pageWithParams = pageWithParams.Replace("{userFirstName}", user.FirstName); pageWithParams = pageWithParams.Replace("{message}", model.Message); pageWithParams = pageWithParams.Replace("{achievementName}", achievement.Name); var emails = await _userRepository.GetAdminsEmailsAsync(cancellationToken); await _emailService.SendEmailsAsync("Request achievement", pageWithParams, cancellationToken, emails.ToArray()); var entity = _mapper.Map <RequestAchievement>(model); entity.UserId = userId; await _requestAchievementRepository.AddAsync(entity, cancellationToken); await _unitOfWork.SaveChangesAsync(cancellationToken); return(new OkResponse()); }
public async Task AddAsync_ValidRequestAchievementModel_ReturnsIResponse(CreateRequestAchievementModel model) { //Arrange var user = UserDumbData.GetRandomEntity(); var achievement = AchievementDumbData.GetRandomEntity(); var cancellationToken = new CancellationToken(); _userRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(user)); _userRepository.Setup(x => x.GetAdminsEmailsAsync(cancellationToken)).Returns(Task.FromResult(new List <string>() as ICollection <string>)); _achievementRepository.Setup(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken)).Returns(Task.FromResult(achievement)); _unitOfWork.Setup(x => x.SaveChangesAsync(cancellationToken)).Returns(Task.CompletedTask); _emailService.Setup(x => x.SendEmailsAsync(It.IsAny <string>(), It.IsAny <string>(), cancellationToken, It.IsAny <string[]>())).Returns(Task.CompletedTask); _requestAchievementRepository.Setup(x => x.AddAsync(It.IsAny <RequestAchievement>(), cancellationToken)).Returns(Task.FromResult(_mapper.Map <RequestAchievement>(model))); string key = "RequestAchievementPage"; var localizedString = new LocalizedString(key, key); _stringLocalizer.Setup(_ => _[key]).Returns(localizedString); // Act var response = await _requestAchievementService.AddAsync(model, user.Id, cancellationToken); // Assert _userRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once); _userRepository.Verify(x => x.GetAdminsEmailsAsync(cancellationToken), Times.Once); _unitOfWork.Verify(x => x.SaveChangesAsync(cancellationToken), Times.Once); _emailService.Verify(x => x.SendEmailsAsync(It.IsAny <string>(), It.IsAny <string>(), cancellationToken, It.IsAny <string[]>()), Times.Once); _stringLocalizer.Verify(_ => _[key], Times.Once); _achievementRepository.Verify(x => x.GetByIdAsync(It.IsAny <Guid>(), cancellationToken), Times.Once); _requestAchievementRepository.Verify(x => x.AddAsync(It.IsAny <RequestAchievement>(), cancellationToken), Times.Once); response.Should().BeEquivalentTo(new OkResponse()); }