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.
- Loading branch information
Showing
8 changed files
with
283 additions
and
7 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
.../Conduit.Tests/Functional/Articles/Controllers/ArticlesControllerTests/UnfavoriteTests.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,53 @@ | ||
using System.Net; | ||
using Conduit.Web.DataAccess.Providers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System.Net.Http.Headers; | ||
using Conduit.Tests.TestHelpers.Data; | ||
using Conduit.Web.DataAccess.Models; | ||
|
||
namespace Conduit.Tests.Functional.Articles.Controllers.ArticlesControllerTests; | ||
|
||
[TestFixture] | ||
public class UnfavoriteTests : FunctionalTestBase | ||
{ | ||
private IConduitUsersCollectionProvider _usersCollectionProvider; | ||
private User _user; | ||
private IConduitArticlesCollectionProvider _articleCollectionProvider; | ||
private IConduitFavoritesCollectionProvider _favoriteCollectionProvider; | ||
private Random _random; | ||
|
||
[SetUp] | ||
public override async Task Setup() | ||
{ | ||
await base.Setup(); | ||
|
||
// setup database objects for arranging | ||
var service = WebAppFactory.Services; | ||
_usersCollectionProvider = service.GetRequiredService<IConduitUsersCollectionProvider>(); | ||
|
||
// setup an authorized header | ||
_user = await _usersCollectionProvider.CreateUserInDatabase(); | ||
var jwtToken = AuthSvc.GenerateJwtToken(_user.Email, _user.Username); | ||
WebClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", jwtToken); | ||
|
||
// setup services | ||
_articleCollectionProvider = service.GetRequiredService<IConduitArticlesCollectionProvider>(); | ||
_favoriteCollectionProvider = service.GetRequiredService<IConduitFavoritesCollectionProvider>(); | ||
_random = new Random(); | ||
} | ||
|
||
[Test] | ||
public async Task Valid_unfavoriting_of_article() | ||
{ | ||
// arrange | ||
var author = await _usersCollectionProvider.CreateUserInDatabase(); | ||
var article = await _articleCollectionProvider.CreateArticleInDatabase(authorUsername: author.Username); | ||
await _favoriteCollectionProvider.AddFavoriteInDatabase(_user.Username, article.Slug); | ||
|
||
// act | ||
var response = await WebClient.DeleteAsync($"api/article/{article.Slug}/favorite"); | ||
|
||
// assert | ||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Conduit.Web.Articles.Services; | ||
using Conduit.Web.DataAccess.Providers; | ||
using Conduit.Web.Extensions; | ||
using Couchbase.KeyValue; | ||
|
||
namespace Conduit.Tests.TestHelpers.Data; | ||
|
||
public static class FavoritesHelper | ||
{ | ||
public static async Task AddFavoriteInDatabase(this IConduitFavoritesCollectionProvider @this, | ||
string? username = "", | ||
string? articleSlug = "") | ||
{ | ||
var random = new Random(); | ||
username ??= $"valid-username-{random.String(8)}"; | ||
articleSlug ??= $"valid-slug-{random.String(8)}::{random.String(10)}"; | ||
|
||
var collection = await @this.GetCollectionAsync(); | ||
|
||
var set = collection.Set<string>(ArticlesDataService.FavoriteDocId(username)); | ||
await set.AddAsync(articleSlug.GetArticleKey()); | ||
|
||
return; | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
Conduit/Conduit.Web/Articles/Handlers/UnfavoriteArticleHandler.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,31 @@ | ||
using Conduit.Web.Articles.Services; | ||
using FluentValidation.Results; | ||
using MediatR; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class UnfavoriteArticleHandler : IRequestHandler<UnfavoriteArticleRequest, UnfavoriteArticleResponse> | ||
{ | ||
private readonly IArticlesDataService _articlesDataService; | ||
|
||
public UnfavoriteArticleHandler(IArticlesDataService articlesDataService) | ||
{ | ||
_articlesDataService = articlesDataService; | ||
} | ||
|
||
public async Task<UnfavoriteArticleResponse> Handle(UnfavoriteArticleRequest request, CancellationToken cancellationToken) | ||
{ | ||
var articleExists = await _articlesDataService.Exists(request.Slug); | ||
if (!articleExists) | ||
{ | ||
return new UnfavoriteArticleResponse | ||
{ | ||
ValidationErrors = new List<ValidationFailure> { new ValidationFailure("", "Article not found.") } | ||
}; | ||
} | ||
|
||
await _articlesDataService.Unfavorite(request.Slug, request.Username); | ||
|
||
return new UnfavoriteArticleResponse(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Conduit/Conduit.Web/Articles/Handlers/UnfavoriteArticleRequest.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,9 @@ | ||
using MediatR; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class UnfavoriteArticleRequest : IRequest<UnfavoriteArticleResponse> | ||
{ | ||
public string Username { get; set; } | ||
public string Slug { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
Conduit/Conduit.Web/Articles/Handlers/UnfavoriteArticleResponse.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,8 @@ | ||
using FluentValidation.Results; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class UnfavoriteArticleResponse | ||
{ | ||
public List<ValidationFailure> ValidationErrors { 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