< Summary

Class:ApplicationCore.PostCommentService
Assembly:ApplicationCore
File(s):C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Services\PostComment\PostCommentService.cs
Covered lines:44
Uncovered lines:0
Coverable lines:44
Total lines:82
Line coverage:100% (44 of 44)
Covered branches:17
Total branches:20
Branch coverage:85% (17 of 20)

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)10100%100%
PostCommentAsync()160100%81.25%
AreSameSites(...)40100%100%

File(s)

C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Services\PostComment\PostCommentService.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.Net;
 4using System.Threading.Tasks;
 5using ApplicationCore.Model;
 6using Microsoft.AspNetCore.Http;
 7
 8namespace ApplicationCore
 9{
 10    public class PostCommentService : IPostCommentService
 11    {
 12        private readonly CommentConfig _config;
 13        private readonly ICommentFactory _commentFactory;
 14        private readonly IPullRequestService _pullRequestService;
 15
 1016        public PostCommentService(
 1017            CommentConfig config,
 1018            ICommentFactory commentFactory,
 1019            IPullRequestService pullRequestService)
 1020        {
 1021            _config = config;
 1022            _commentFactory = commentFactory;
 1023            _pullRequestService = pullRequestService;
 1024        }
 25
 26        public async Task<PostCommentResult> PostCommentAsync(IFormCollection form)
 827        {
 828            if (form is null)
 129            {
 130                throw new ArgumentNullException(nameof(form));
 31            }
 32
 33            //Make sure the site posting the comment is the correct site.
 734            var allowedSite = _config.WebsiteUrl.AbsoluteUri;
 735            var postedSite = form["CommentSite"]; // TODO: fix magic string
 736            if (!string.IsNullOrWhiteSpace(allowedSite) && !AreSameSites(allowedSite, postedSite))
 337            {
 338                return new PostCommentResult(
 339                    HttpStatusCode.BadRequest,
 340                    string.Format(
 341                        CultureInfo.InvariantCulture,
 342                        CommentResources.AreNotSameSitesErrorMessage,
 343                        postedSite.ToString() ?? "null"));
 44            }
 45
 446            var commentResult = await _commentFactory.CreateFromFormAsync(form).ConfigureAwait(false);
 47
 448            if (!commentResult.HasErrors)
 349            {
 350                var pullRequestResult = await _pullRequestService.TryCreatePullRequestAsync(commentResult.Comment).Confi
 51
 352                if (!pullRequestResult.HasError)
 253                {
 254                    if (!Uri.TryCreate(form["Redirect"], UriKind.Absolute, out var redirectUri))
 155                    {
 156                        return new PostCommentResult(HttpStatusCode.OK);
 57                    }
 58                    else
 159                    {
 160                        return new PostCommentResult(HttpStatusCode.Redirect, redirectUri);
 61                    }
 62                }
 63                else
 164                {
 165                    return new PostCommentResult(HttpStatusCode.BadRequest, $"Creating pull request failed: {pullRequest
 66                }
 67            }
 68            else
 169            {
 70                // TODO: Include possible exception
 171                return new PostCommentResult(HttpStatusCode.BadRequest, string.Join("\n", commentResult.Errors));
 72            }
 773        }
 74
 75        private static bool AreSameSites(string commentSite, string postedCommentSite)
 776        {
 777            return Uri.TryCreate(commentSite, UriKind.Absolute, out var commentSiteUri)
 778                && Uri.TryCreate(postedCommentSite, UriKind.Absolute, out var postedCommentSiteUri)
 779                && commentSiteUri.Host.Equals(postedCommentSiteUri.Host, StringComparison.OrdinalIgnoreCase);
 780        }
 81    }
 82}