forked from gothinkster/realworld-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#21 delete comment endpoint and tests
- Loading branch information
Showing
10 changed files
with
244 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Conduit/Conduit.Tests/Integration/Articles/Services/CommentsDataService/DeleteTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using Conduit.Tests.TestHelpers.Data; | ||
using Conduit.Web.DataAccess; | ||
using Conduit.Web.DataAccess.Dto; | ||
using Conduit.Web.DataAccess.Providers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Conduit.Tests.Integration.Articles.Services.CommentsDataService; | ||
|
||
[TestFixture] | ||
public class DeleteTests : CouchbaseIntegrationTest | ||
{ | ||
private IConduitCommentsCollectionProvider _commentsCollectionProvider; | ||
private IConduitArticlesCollectionProvider _articlesCollectionProvider; | ||
private Web.Articles.Services.CommentsDataService _commentsDataService; | ||
private IConduitUsersCollectionProvider _userCollectionProvider; | ||
|
||
[SetUp] | ||
public override async Task Setup() | ||
{ | ||
await base.Setup(); | ||
|
||
_commentsCollectionProvider = ServiceProvider.GetRequiredService<IConduitCommentsCollectionProvider>(); | ||
_articlesCollectionProvider = ServiceProvider.GetRequiredService<IConduitArticlesCollectionProvider>(); | ||
_userCollectionProvider = ServiceProvider.GetRequiredService<IConduitUsersCollectionProvider>(); | ||
|
||
var couchbaseOptions = new OptionsWrapper<CouchbaseOptions>(new CouchbaseOptions()); | ||
_commentsDataService = new Web.Articles.Services.CommentsDataService(_commentsCollectionProvider, couchbaseOptions); | ||
} | ||
|
||
[Test] | ||
public async Task Can_delete_a_comment_by_id() | ||
{ | ||
// arrange an article | ||
var author = await _userCollectionProvider.CreateUserInDatabase(); | ||
var article = await _articlesCollectionProvider.CreateArticleInDatabase(authorUsername: author.Username); | ||
var commenter = await _userCollectionProvider.CreateUserInDatabase(); | ||
// arrange some comments on the article | ||
var comment1 = await _commentsCollectionProvider.CreateCommentInDatabase(article.Slug, commenter.Username); | ||
var comment2 = await _commentsCollectionProvider.CreateCommentInDatabase(article.Slug, commenter.Username); | ||
|
||
// act | ||
var result = await _commentsDataService.Delete(comment1.Id, article.Slug, commenter.Username); | ||
|
||
// assert | ||
Assert.That(result, Is.EqualTo(DataResultStatus.Ok)); | ||
await _commentsCollectionProvider.AssertExists(article.Slug, x => | ||
{ | ||
Assert.That(x.Count, Is.EqualTo(1)); | ||
Assert.That(x[0].Id, Is.EqualTo(comment2.Id)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public async Task Id_must_exist() | ||
{ | ||
// arrange an article | ||
var author = await _userCollectionProvider.CreateUserInDatabase(); | ||
var article = await _articlesCollectionProvider.CreateArticleInDatabase(authorUsername: author.Username); | ||
var comment1 = await _commentsCollectionProvider.CreateCommentInDatabase(article.Slug, author.Username); | ||
var idDoesntExist = comment1.Id + 1; | ||
|
||
// act | ||
var result = await _commentsDataService.Delete(idDoesntExist, article.Slug, author.Username); | ||
|
||
// assert | ||
Assert.That(result, Is.EqualTo(DataResultStatus.NotFound)); | ||
} | ||
|
||
[Test] | ||
public async Task Cant_delete_comments_that_you_didnt_author() | ||
{ | ||
// arrange an article | ||
var author = await _userCollectionProvider.CreateUserInDatabase(); | ||
var article = await _articlesCollectionProvider.CreateArticleInDatabase(authorUsername: author.Username); | ||
var comment1 = await _commentsCollectionProvider.CreateCommentInDatabase(article.Slug, author.Username); | ||
var someOtherUser = await _userCollectionProvider.CreateUserInDatabase(); | ||
|
||
// act | ||
var result = await _commentsDataService.Delete(comment1.Id, article.Slug, someOtherUser.Username); | ||
|
||
// assert | ||
Assert.That(result, Is.EqualTo(DataResultStatus.Unauthorized)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Conduit/Conduit.Web/Articles/Handlers/DeleteCommentHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Conduit.Web.Articles.Services; | ||
using Conduit.Web.DataAccess.Dto; | ||
using FluentValidation; | ||
using MediatR; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class DeleteCommentHandler : IRequestHandler<DeleteCommentRequest, DeleteCommentResponse> | ||
{ | ||
private readonly IValidator<DeleteCommentRequest> _validator; | ||
private readonly IArticlesDataService _articlesDataService; | ||
private readonly ICommentsDataService _commentsDataService; | ||
|
||
public DeleteCommentHandler(IValidator<DeleteCommentRequest> validator, IArticlesDataService articlesDataService, ICommentsDataService commentsDataService) | ||
{ | ||
_validator = validator; | ||
_articlesDataService = articlesDataService; | ||
_commentsDataService = commentsDataService; | ||
} | ||
|
||
public async Task<DeleteCommentResponse> Handle(DeleteCommentRequest request, CancellationToken cancellationToken) | ||
{ | ||
// validation | ||
var validationResult = await _validator.ValidateAsync(request, cancellationToken); | ||
if (!validationResult.IsValid) | ||
return new DeleteCommentResponse { ValidationErrors = validationResult.Errors }; | ||
|
||
// return error if article doesn't exist | ||
var doesArticleExist = await _articlesDataService.Exists(request.Slug); | ||
if (!doesArticleExist) | ||
return new DeleteCommentResponse { IsArticleNotFound = true }; | ||
|
||
var dataResponse = await _commentsDataService.Delete(request.CommentId, request.Slug, request.Username); | ||
|
||
if (dataResponse == DataResultStatus.NotFound) | ||
return new DeleteCommentResponse { IsCommentNotFound = true }; | ||
if (dataResponse == DataResultStatus.Unauthorized) | ||
return new DeleteCommentResponse { IsNotAuthorized = true }; | ||
if (dataResponse == DataResultStatus.Error) | ||
return new DeleteCommentResponse { IsFailed = true }; | ||
|
||
return new DeleteCommentResponse(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Conduit/Conduit.Web/Articles/Handlers/DeleteCommentRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using MediatR; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class DeleteCommentRequest : IRequest<DeleteCommentResponse> | ||
{ | ||
public string Slug { get; init; } | ||
public ulong CommentId { get; init; } | ||
public string Username { get; init; } | ||
} |
16 changes: 16 additions & 0 deletions
16
Conduit/Conduit.Web/Articles/Handlers/DeleteCommentRequestValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using FluentValidation; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class DeleteCommentRequestValidator : AbstractValidator<DeleteCommentRequest> | ||
{ | ||
public DeleteCommentRequestValidator() | ||
{ | ||
RuleFor(x => x.Slug) | ||
.Cascade(CascadeMode.Stop) | ||
.NotEmpty().WithMessage("Slug is required."); | ||
|
||
RuleFor(x => x.CommentId) | ||
.Must(v => v > 0).WithMessage("Must be a valid CommentID"); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Conduit/Conduit.Web/Articles/Handlers/DeleteCommentResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using FluentValidation.Results; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class DeleteCommentResponse | ||
{ | ||
public List<ValidationFailure> ValidationErrors { get; set; } | ||
public bool IsArticleNotFound { get; set; } | ||
public bool IsNotAuthorized { get; set; } | ||
public bool IsFailed { get; set; } | ||
public bool IsCommentNotFound { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ public enum DataResultStatus | |
NotFound = 0, | ||
Ok = 1, | ||
FailedToInsert = 2, | ||
Error = 3 | ||
Error = 3, | ||
Unauthorized = 4 | ||
} |