public async Task OnGet_StateUnderTest_ExpectedBehavior() { IReadOnlyList <PingbackEntity> pingback = new PingbackEntity[] { }; _mockMediator.Setup(p => p.Send(It.IsAny <GetPingbacksQuery>(), default)) .Returns(Task.FromResult(pingback)); var pingbackModel = CreatePingbackModel(); await pingbackModel.OnGet(); Assert.IsNotNull(pingbackModel.PingbackRecords); }
public async Task OnGet_StateUnderTest_ExpectedBehavior() { IReadOnlyList <PingbackEntity> pingback = new PingbackEntity[] { }; _mockPingbackService.Setup(p => p.GetPingbacksAsync()) .Returns(Task.FromResult(pingback)); var pingbackModel = CreatePingbackModel(); await pingbackModel.OnGet(); Assert.IsNotNull(pingbackModel.PingbackRecords); }
public async Task <PingbackResponse> ReceivePingAsync(string requestBody, string ip, Action <PingbackEntity> pingSuccessAction) { try { if (string.IsNullOrWhiteSpace(requestBody)) { _logger.LogError("Pingback requestBody is null"); return(PingbackResponse.GenericError); } var valid = ValidateRequest(requestBody); if (!valid) { return(PingbackResponse.InvalidPingRequest); } _logger.LogInformation($"Processing Pingback from: {_sourceUrl} ({ip}) to {_targetUrl}"); var pingRequest = await _pingSourceInspector.ExamineSourceAsync(_sourceUrl, _targetUrl); if (null == pingRequest) { return(PingbackResponse.InvalidPingRequest); } if (!pingRequest.SourceHasLink) { _logger.LogError("Pingback error: The source URI does not contain a link to the target URI."); return(PingbackResponse.Error17SourceNotContainTargetUri); } if (pingRequest.ContainsHtml) { _logger.LogWarning("Spam detected on current Pingback..."); return(PingbackResponse.SpamDetectedFakeNotFound); } var(slug, pubDate) = GetSlugInfoFromUrl(pingRequest.TargetUrl); var spec = new PostSpec(pubDate, slug); var(id, title) = await _postRepo.SelectFirstOrDefaultAsync(spec, p => new Tuple <Guid, string>(p.Id, p.Title)); if (id == Guid.Empty) { _logger.LogError($"Can not get post id and title for url '{pingRequest.TargetUrl}'"); return(PingbackResponse.Error32TargetUriNotExist); } _logger.LogInformation($"Post '{id}:{title}' is found for ping."); var pinged = _pingbackRepo.Any(p => p.TargetPostId == id && p.SourceUrl == pingRequest.SourceUrl && p.SourceIp.Trim() == ip); if (pinged) { return(PingbackResponse.Error48PingbackAlreadyRegistered); } _logger.LogInformation("Adding received pingback..."); var uri = new Uri(_sourceUrl); var obj = new PingbackEntity { Id = Guid.NewGuid(), PingTimeUtc = DateTime.UtcNow, Domain = uri.Host, SourceUrl = _sourceUrl, SourceTitle = pingRequest.Title, TargetPostId = id, TargetPostTitle = title, SourceIp = ip }; await _pingbackRepo.AddAsync(obj); pingSuccessAction?.Invoke(obj); return(PingbackResponse.Success); } catch (Exception e) { _logger.LogError(e, nameof(ReceivePingAsync)); return(PingbackResponse.GenericError); } }