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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
31 changed files
with
702 additions
and
42 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Conduit/Conduit.Migrations/006_CreateCollectionForFavorites.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,27 @@ | ||
using NoSqlMigrator.Infrastructure; | ||
|
||
namespace Conduit.Migrations; | ||
|
||
// Manual alternative: create a Favorites collection in _default scope | ||
[Migration(6)] | ||
public class CreateCollectionForFavorites : MigrateBase | ||
{ | ||
private readonly string? _scopeName; | ||
|
||
public CreateCollectionForFavorites() | ||
{ | ||
_scopeName = _config["Couchbase:ScopeName"]; | ||
} | ||
|
||
public override void Up() | ||
{ | ||
Create.Collection("Favorites") | ||
.InScope(_scopeName); | ||
} | ||
|
||
public override void Down() | ||
{ | ||
Delete.Collection("Favorites") | ||
.FromScope(_scopeName); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
Conduit/Conduit.Tests/Integration/Articles/Handlers/FavoriteArticleHandlerTests.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,64 @@ | ||
using Conduit.Tests.TestHelpers.Data; | ||
using Conduit.Web.Articles.Handlers; | ||
using Conduit.Web.Articles.Services; | ||
using Conduit.Web.DataAccess.Providers; | ||
using Couchbase.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Conduit.Tests.Integration.Articles.Handlers; | ||
|
||
public class FavoriteArticleHandlerTests : CouchbaseIntegrationTest | ||
{ | ||
private IConduitArticlesCollectionProvider _articleCollectionProvider; | ||
private FavoriteArticleHandler _handler; | ||
private IConduitFavoritesCollectionProvider _favoritesCollectionProvider; | ||
private ArticlesDataService _articleDataService; | ||
private IConduitUsersCollectionProvider _usersCollectionProvider; | ||
|
||
public override async Task Setup() | ||
{ | ||
await base.Setup(); | ||
|
||
ServiceCollection.AddCouchbaseBucket<IConduitBucketProvider>("ConduitIntegrationTests", b => | ||
{ | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitArticlesCollectionProvider>("Articles"); | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitFavoritesCollectionProvider>("Favorites"); | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitUsersCollectionProvider>("Users"); | ||
}); | ||
|
||
_articleCollectionProvider = ServiceProvider.GetRequiredService<IConduitArticlesCollectionProvider>(); | ||
_favoritesCollectionProvider = ServiceProvider.GetRequiredService<IConduitFavoritesCollectionProvider>(); | ||
_usersCollectionProvider = ServiceProvider.GetRequiredService<IConduitUsersCollectionProvider>(); | ||
_articleDataService = new ArticlesDataService(_articleCollectionProvider, _favoritesCollectionProvider); | ||
|
||
// setup handler and dependencies | ||
_handler = new FavoriteArticleHandler(_articleDataService); | ||
} | ||
|
||
[Test] | ||
public async Task FavoriteArticleHandler_adds_article_to_a_users_favorites() | ||
{ | ||
// arrange | ||
var user = await _usersCollectionProvider.CreateUserInDatabase(); | ||
var article = await _articleCollectionProvider.CreateArticleInDatabase(); | ||
var request = new FavoriteArticleRequest(); | ||
request.Slug = article.Slug; | ||
request.Username = user.Username; | ||
|
||
// act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// assert | ||
Assert.That(result.ValidationErrors == null || !result.ValidationErrors.Any(), Is.True); | ||
await _favoritesCollectionProvider.AssertExists(user.Username, x => | ||
{ | ||
Assert.That(x.Contains(request.Slug), Is.True); | ||
}); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
Conduit/Conduit.Tests/Integration/Articles/Handlers/GetArticleHandlerTests.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; | ||
using Conduit.Tests.TestHelpers.Data; | ||
using Conduit.Web.Articles.Handlers; | ||
using Conduit.Web.Articles.Services; | ||
using Conduit.Web.DataAccess.Providers; | ||
using Conduit.Web.Extensions; | ||
using Conduit.Web.Follows.Services; | ||
using Conduit.Web.Users.Services; | ||
using Couchbase.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Conduit.Tests.Integration.Articles.Handlers; | ||
|
||
public class GetArticleHandlerTests : CouchbaseIntegrationTest | ||
{ | ||
private IConduitArticlesCollectionProvider _articleCollectionProvider; | ||
private IConduitUsersCollectionProvider _usersCollectionProvider; | ||
private IConduitFollowsCollectionProvider _followsCollectionProvider; | ||
private ArticlesDataService _articleDataService; | ||
private IConduitFavoritesCollectionProvider _favoriteCollectionProvider; | ||
private GetArticleHandler _handler; | ||
private AuthService _authService; | ||
private UserDataService _userDataService; | ||
private FollowsDataService _followDataService; | ||
private Random _random; | ||
|
||
public override async Task Setup() | ||
{ | ||
await base.Setup(); | ||
|
||
ServiceCollection.AddCouchbaseBucket<IConduitBucketProvider>("ConduitIntegrationTests", b => | ||
{ | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitUsersCollectionProvider>("Users"); | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitArticlesCollectionProvider>("Articles"); | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitFollowsCollectionProvider>("Follows"); | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitFavoritesCollectionProvider>("Favorites"); | ||
}); | ||
|
||
_usersCollectionProvider = ServiceProvider.GetRequiredService<IConduitUsersCollectionProvider>(); | ||
_articleCollectionProvider = ServiceProvider.GetRequiredService<IConduitArticlesCollectionProvider>(); | ||
_followsCollectionProvider = ServiceProvider.GetRequiredService<IConduitFollowsCollectionProvider>(); | ||
_favoriteCollectionProvider = ServiceProvider.GetRequiredService<IConduitFavoritesCollectionProvider>(); | ||
|
||
// setup handler and dependencies | ||
var jwtSecrets = new JwtSecrets | ||
{ | ||
Audience = "dummy-audience", | ||
Issuer = "dummy-issuer", | ||
SecurityKey = "dummy-securitykey" | ||
}; | ||
_authService = new AuthService(new OptionsWrapper<JwtSecrets>(jwtSecrets)); | ||
_articleDataService = new ArticlesDataService(_articleCollectionProvider, _favoriteCollectionProvider); | ||
_followDataService = new FollowsDataService(_followsCollectionProvider, _authService); | ||
_userDataService = new UserDataService(_usersCollectionProvider, _authService); | ||
_handler = new GetArticleHandler(_articleDataService, _userDataService, _followDataService); | ||
_random = new Random(); | ||
} | ||
|
||
[Test] | ||
public async Task GetArticleHandler_Returns_article() | ||
{ | ||
// arrange | ||
var currentUser = await _usersCollectionProvider.CreateUserInDatabase(); | ||
var authorUser = await _usersCollectionProvider.CreateUserInDatabase(); | ||
var article = await _articleCollectionProvider.CreateArticleInDatabase(authorUsername: authorUser.Username); | ||
var request = new GetArticleRequest(article.Slug, currentUser.Username); | ||
|
||
// act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// assert | ||
Assert.That(result.ArticleView.Slug, Is.EqualTo(article.Slug)); | ||
Assert.That(result.ArticleView.Title, Is.EqualTo(article.Title)); | ||
Assert.That(result.ArticleView.Author.Username, Is.EqualTo(authorUser.Username)); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
Conduit/Conduit.Tests/Integration/Articles/Services/ArticlesDataService/FavoriteTests.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,59 @@ | ||
using Conduit.Tests.TestHelpers.Data; | ||
using Conduit.Web.DataAccess.Providers; | ||
using Couchbase.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Conduit.Tests.Integration.Articles.Services.ArticlesDataService; | ||
|
||
[TestFixture] | ||
public class FavoriteTests : CouchbaseIntegrationTest | ||
{ | ||
private IConduitArticlesCollectionProvider _articleCollectionProvider; | ||
private IConduitFavoritesCollectionProvider _favoriteCollectionProvider; | ||
private Web.Articles.Services.ArticlesDataService _articleDataService; | ||
|
||
[SetUp] | ||
public override async Task Setup() | ||
{ | ||
await base.Setup(); | ||
|
||
ServiceCollection.AddCouchbaseBucket<IConduitBucketProvider>("ConduitIntegrationTests", b => | ||
{ | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitArticlesCollectionProvider>("Articles"); | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitFavoritesCollectionProvider>("Favorites"); | ||
}); | ||
|
||
_articleCollectionProvider = ServiceProvider.GetRequiredService<IConduitArticlesCollectionProvider>(); | ||
_favoriteCollectionProvider = ServiceProvider.GetRequiredService<IConduitFavoritesCollectionProvider>(); | ||
|
||
_articleDataService = new Web.Articles.Services.ArticlesDataService( | ||
_articleCollectionProvider, | ||
_favoriteCollectionProvider); | ||
} | ||
|
||
[TestCase(0,1)] | ||
[TestCase(73,74)] | ||
public async Task Favoriting_works_and_increases_count(int initialCount, int expectedCount) | ||
{ | ||
// arrange | ||
var article = await _articleCollectionProvider.CreateArticleInDatabase(favoritesCount: initialCount); | ||
var user = UserHelper.CreateUser(); | ||
|
||
// act | ||
await _articleDataService.Favorite(article.Slug, user.Username); | ||
|
||
// assert | ||
await _favoriteCollectionProvider.AssertExists(user.Username, x => | ||
{ | ||
Assert.That(x.Contains(article.Slug), Is.True); | ||
}); | ||
await _articleCollectionProvider.AssertExists(article.Slug, x => | ||
{ | ||
Assert.That(x.FavoritesCount, Is.EqualTo(expectedCount)); | ||
}); | ||
} | ||
} |
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
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
1 change: 1 addition & 0 deletions
1
Conduit/Conduit.Tests/TestHelpers/Dto/CreateArticleRequestHelper.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
Oops, something went wrong.