Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

#226 Участник команды может создать пост в ленте команды #239

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions common/server/Garnet/Garnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ProjectReference Include="..\..\..\features\team\server\Garnet.Teams\Garnet.Teams.csproj" />
<ProjectReference Include="..\..\..\features\user\server\Garnet.Users\Garnet.Users.csproj" />
<ProjectReference Include="..\..\..\features\notification\server\Garnet.Notifications\Garnet.Notifications.csproj" />
<ProjectReference Include="..\..\..\features\newsfeed\server\Garnet.NewsFeed\Garnet.NewsFeed.csproj" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions common/server/Garnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Garnet.Project;
using Garnet.Notifications;
using Microsoft.AspNetCore.Authorization;
using Garnet.NewsFeed;

namespace Garnet;

Expand Down Expand Up @@ -38,6 +39,7 @@ public static async Task Main(string[] args)
.AddGarnetUsers()
.AddGarnetTeams()
.AddGarnetNotifications()
.AddGarnetNewsFeed()
.AddGarnetProjects();

builder.Services.AddControllers();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using NUnit.Framework;

[assembly: Parallelizable(ParallelScope.Fixtures)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Garnet.Common.AcceptanceTests.Support;
using Garnet.NewsFeed.Infrastructure.Api;
using Garnet.NewsFeed.Infrastructure.MongoDB;

namespace Garnet.NewsFeed.AcceptanceTests
{
public class BaseSteps
{
protected GiveMe GiveMe { get; }
protected Db Db { get; }
public NewsFeedQuery Query { get; }
public NewsFeedMutation Mutation { get; }

public BaseSteps(StepsArgs args)
{
GiveMe = args.GiveMe;
Db = args.Db;
Query = args.Query;
Mutation = args.Mutation;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Функция:
Я, как пользователь
Хочу иметь возможность создавать посты в ленте своей команды
Чтобы уведомлять других пользователей о новостях от имени команды

Контекст:
Допустим существует команда 'DreamTeam'
И существует пользователь 'Вася'
И пользователь 'Вася' является участником команды 'DreamTeam'

Сценарий: Создание поста
Когда пользователь 'Вася' создает пост с содержанием 'Супер мега анонс' в ленте команды 'DreamTeam'
Тогда количество постов в ленте команды 'DreamTeam' равно '1'

Сценарий: Только участники могут создавать посты в ленте команды
Допустим существует пользователь 'Маша'
Когда пользователь 'Маша' создает пост с содержанием 'Супер мега анонс' в ленте команды 'DreamTeam'
Тогда пользователь получает ошибку 'NewsFeedOnlyTeamParticipantCanCreate'
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using FluentAssertions;
using Garnet.Common.AcceptanceTests.Contexts;
using Garnet.Common.AcceptanceTests.Fakes;
using Garnet.Common.Infrastructure.Support;
using Garnet.NewsFeed.AcceptanceTests.Support;
using Garnet.NewsFeed.Application.NewsFeedPost;
using Garnet.NewsFeed.Infrastructure.Api.NewsFeedPostCreate;
using HotChocolate.Execution;
using MongoDB.Driver;
using TechTalk.SpecFlow;

namespace Garnet.NewsFeed.AcceptanceTests.Features.NewsFeedPostCreate
{
[Binding]
public class NewsFeedPostCreateSteps : BaseSteps
{
private readonly QueryExceptionsContext _queryExceptionsContext;
private readonly CurrentUserProviderFake _currentUserProviderFake;
private readonly INewsFeedPostRepository _newsFeedPostRepository;

public NewsFeedPostCreateSteps(
CurrentUserProviderFake currentUserProviderFake,
QueryExceptionsContext queryExceptionsContext,
INewsFeedPostRepository newsFeedPostRepository,
StepsArgs args) : base(args)
{
_newsFeedPostRepository = newsFeedPostRepository;
_currentUserProviderFake = currentUserProviderFake;
_queryExceptionsContext = queryExceptionsContext;
}

[Given(@"существует команда '(.*)'")]
public async Task GivenСуществуетКоманда(string teamName)
{
var team = new NewsFeedTeamBuilder().WithTeamId(teamName);
await Db.NewsFeedTeam.InsertOneAsync(
team
);
}

[Given(@"существует пользователь '(.*)'")]
public Task GivenСуществуетПользователь(string username)
{
_currentUserProviderFake.RegisterUser(username, Uuid.NewGuid());
return Task.CompletedTask;
}

[Given(@"пользователь '(.*)' является участником команды '(.*)'")]
public async Task GivenПользовательЯвляетсяУчастникомКоманды(string username, string teamName)
{
var userId = _currentUserProviderFake.GetUserIdByUsername(username);
var participant = new NewsFeedTeamParticipantBuilder().WithUserId(userId).WithTeamId(teamName);
await Db.NewsFeedTeamParticipant.InsertOneAsync(participant);
}

[When(@"пользователь '(.*)' создает пост с содержанием '(.*)' в ленте команды '(.*)'")]
public async Task WhenПользовательСоздаетПостССодержаниемВЛентеКоманды(string username, string content, string teamName)
{
var input = new NewsFeedPostCreateInput(teamName, content);

_currentUserProviderFake.LoginAs(username);
try
{
await Mutation.NewsFeedPostCreate(input);
}
catch (QueryException ex)
{
_queryExceptionsContext.QueryExceptions.Add(ex);
}
}

[Then(@"количество постов в ленте команды '(.*)' равно '(.*)'")]
public async Task ThenКоличествоПостовВЛентеКомандыРавно(string teamName, int postCount)
{
var posts = await Db.NewsFeedPost.Find(x => x.TeamId == teamName).ToListAsync();
posts.Count().Should().Be(postCount);
}

[Then(@"пользователь получает ошибку '(.*)'")]
public Task ThenПользовательПолучаетОшибку(string errorCode)
{
var validError = _queryExceptionsContext.QueryExceptions.First().Errors.Any(x => x.Code == errorCode);
validError.Should().BeTrue();
return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\..\..\common\server\Garnet.Common.AcceptanceTests\Garnet.Common.AcceptanceTests.csproj" />
<ProjectReference Include="..\Garnet.NewsFeed.Infrastructure\Garnet.NewsFeed.Infrastructure.csproj" />
<ProjectReference Include="..\Garnet.NewsFeed\Garnet.NewsFeed.csproj" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Garnet.NewsFeed.AcceptanceTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="SpecFlow.NUnit" Version="3.9.74" />
<PackageReference Include="SpecFlow.Plus.LivingDocPlugin" Version="3.9.57" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Garnet.Common.Infrastructure.MongoDb.Migrations;
using Microsoft.Extensions.DependencyInjection;
using Mongo2Go;
using TechTalk.SpecFlow;

namespace Garnet.NewsFeed.AcceptanceTests
{
[Binding]
public class ScenarioDecorator
{
[BeforeScenario]
public async Task BeforeScenario(ScenarioContext context)
{
var services = context.ScenarioContainer.Resolve<IServiceProvider>();
await services.ExecuteRepeatableMigrations(CancellationToken.None);
}

[AfterScenario]
public void AfterScenario(ScenarioContext context)
{
var services = context.ScenarioContainer.Resolve<IServiceProvider>();
var mongo = services.GetRequiredService<MongoDbRunner>();
mongo.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Garnet.Common.AcceptanceTests.Fakes;
using Garnet.Common.AcceptanceTests.Support;
using Garnet.Common.Application;
using Garnet.Common.Infrastructure.Support;
using Garnet.NewsFeed.Infrastructure.Api;
using Garnet.NewsFeed.Infrastructure.MongoDB;
using Microsoft.Extensions.DependencyInjection;
using Mongo2Go;
using SolidToken.SpecFlow.DependencyInjection;
using Garnet.Common.AcceptanceTests.Contexts;
using Garnet.Common.Infrastructure.Api;

namespace Garnet.NewsFeed.AcceptanceTests
{
public static class Startup
{
[ScenarioDependencies]
public static IServiceCollection CreateServices()
{
var services = new ServiceCollection();

services.AddNewsFeedInternal();

services.AddScoped<NewsFeedQuery>();
services.AddScoped<NewsFeedMutation>();

services.AddScoped<CurrentUserProviderFake>();
services.AddScoped<ICurrentUserProvider>(o => o.GetRequiredService<CurrentUserProviderFake>());

services.AddScoped<QueryExceptionsContext>();

services.AddScoped<DateTimeServiceFake>();
services.AddScoped<IDateTimeService>(o => o.GetRequiredService<DateTimeServiceFake>());

services.AddScoped<GiveMe>();
services.AddScoped<StepsArgs>();

services.AddCancellationTokenProvider();

AddMongoDb(services);
AddMessageBus(services);

return services;
}

private static void AddMongoDb(IServiceCollection services)
{
services.AddScoped<MongoDbRunner>(_ => MongoDbRunner.Start());
services.AddScoped<DbFactory>(o =>
{
var mongo = o.GetRequiredService<MongoDbRunner>();
return new DbFactory(mongo.ConnectionString);
});
services.AddScoped<Db>(o => o.GetRequiredService<DbFactory>().Create());
}

private static void AddMessageBus(IServiceCollection services)
{
services.AddGarnetNewsFeedMessageBus(Uuid.NewGuid());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Garnet.Common.AcceptanceTests.Support;
using Garnet.NewsFeed.Infrastructure.Api;
using Garnet.NewsFeed.Infrastructure.MongoDB;

namespace Garnet.NewsFeed.AcceptanceTests
{
public class StepsArgs
{
public GiveMe GiveMe { get; }
public Db Db { get; }
public NewsFeedQuery Query { get; }
public NewsFeedMutation Mutation { get; }

public StepsArgs(GiveMe giveMe, Db db, NewsFeedQuery query, NewsFeedMutation mutation)
{
GiveMe = giveMe;
Db = db;
Query = query;
Mutation = mutation;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Garnet.Common.Infrastructure.Support;
using Garnet.NewsFeed.Infrastructure.MongoDB.NewsFeedTeam;

namespace Garnet.NewsFeed.AcceptanceTests.Support
{
public class NewsFeedTeamBuilder
{
private string _id = Uuid.NewMongo();
private string _ownerUserId = "system";

public NewsFeedTeamBuilder WithOwner(string ownerUserId)
{
_ownerUserId = ownerUserId;
return this;
}

public NewsFeedTeamBuilder WithTeamId(string teamId)
{
_id = teamId;
return this;
}

public NewsFeedTeamDocument Build()
{
return NewsFeedTeamDocument.Create(_id, _ownerUserId);
}

public static implicit operator NewsFeedTeamDocument(NewsFeedTeamBuilder builder)
{
return builder.Build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Garnet.Common.Infrastructure.Support;
using Garnet.NewsFeed.Infrastructure.MongoDB.NewsFeedTeamParticipant;

namespace Garnet.NewsFeed.AcceptanceTests.Support
{
public class NewsFeedTeamParticipantBuilder
{
private string _id = Uuid.NewMongo();
private string _userId = "system";
private string _teamId = "TeamId";

public NewsFeedTeamParticipantBuilder WithUserId(string userId)
{
_userId = userId;
return this;
}

public NewsFeedTeamParticipantBuilder WithTeamId(string teamId)
{
_teamId = teamId;
return this;
}

public NewsFeedTeamParticipantDocument Build()
{
return NewsFeedTeamParticipantDocument.Create(_id, _teamId, _userId);
}

public static implicit operator NewsFeedTeamParticipantDocument(NewsFeedTeamParticipantBuilder builder)
{
return builder.Build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://specflow.org/specflow-config.json",
"language": {
"feature": "ru-RU"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\Garnet.NewsFeed.Events\Garnet.NewsFeed.Events.csproj" />
<ProjectReference Include="..\..\..\..\common\server\Garnet.Common.Application\Garnet.Common.Application.csproj" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Garnet.NewsFeed.Application</RootNamespace>
</PropertyGroup>

</Project>
Loading