Add friendship feature with models, repository, and tests

Introduces Friendship and PrivateChat models, repository interfaces, and implementations for managing friendships. Adds validators and comprehensive unit and integration tests for friendship logic. Updates InviteUserController with new endpoints and restricts access to admin role. Removes unnecessary project references from Govor.API.csproj and registers new DbSets in GovorDbContext.
This commit is contained in:
Artemy
2025-06-27 15:09:28 +07:00
parent 4f3f4ec066
commit 9b9cd73a96
15 changed files with 565 additions and 20 deletions
@@ -96,7 +96,7 @@ public class AdminsRepositoryTests
var repository = new AdminsRepository(context, _validator);
// Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.GetByIdAsync(Guid.Empty));
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.GetByIdAsync(_fixture.Create<Guid>()));
}
[Test]
@@ -0,0 +1,191 @@
using AutoFixture;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models;
using Govor.Data;
using Govor.Data.Repositories;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace Govor.API.Tests.IntegrationTests.EF.Repositories;
[TestFixture]
public class FriendshipsRepositoryTests
{
private Fixture _fixture;
private DbContextOptions<GovorDbContext> _options;
private IObjectValidator<Friendship> _validator = new FriendshipValidator();
private int _testIteration = 0;
[SetUp]
public void Setup()
{
_fixture = new Fixture();
_fixture.Behaviors
.OfType<ThrowingRecursionBehavior>()
.ToList()
.ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
_options = new DbContextOptionsBuilder<GovorDbContext>()
.UseInMemoryDatabase(databaseName: $"DbGovor_{nameof(FriendshipsRepositoryTests)}_{_testIteration}")
.Options;
_testIteration += 1;
}
[Test]
public async Task Given_NotEmptyDbSet_When_GetAllAsync_Then_Friendships_Are_Returned()
{
// Arrange
var random = new Random();
var friendships = _fixture.CreateMany<Friendship>(random.Next(3, 10)).ToList();
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
context.AddRange(friendships);
await context.SaveChangesAsync();
// Act
var result = await repository.GetAllAsync();
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Count(), Is.EqualTo(friendships.Count()));
Assert.That(result.Select(r => r.Id), Is.EquivalentTo(friendships.Select(r => r.Id)));
}
[Test]
public async Task Given_EmptyDbSet_When_GetAllAsync_Should_Throw_NotFoundException()
{
// Arrange
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
// Act & Assert
Assert.ThrowsAsync<NotFoundException>(async () => await repository.GetAllAsync());
}
[Test]
public async Task Given_ValidFriendshipId_When_GetByIdAsync_Then_Friendships_Are_Returned()
{
// Arrange
var random = new Random();
var friendships = _fixture.CreateMany<Friendship>(random.Next(3, 10)).ToList();
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
context.AddRange(friendships);
await context.SaveChangesAsync();
// Act
var result = await repository.GetByIdAsync(friendships.First().Id);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Id, Is.EqualTo(friendships.First().Id));
}
[Test]
public async Task Given_InvalidFriendshipId_When_GetByIdAsync_Should_Throw_NotFoundByKeyException()
{
// Arrange
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
// Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.GetByIdAsync(_fixture.Create<Guid>()));
}
[Test]
public async Task Given_ValidUserId_When_GetByIdAsync_Then_Friendships_Are_Returned()
{
// Arrange
var random = new Random();
var friendships = _fixture.CreateMany<Friendship>(random.Next(3, 10)).ToList();
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
context.AddRange(friendships);
await context.SaveChangesAsync();
// Act
var result = await repository.FindByUserIdAsync(friendships.First().RequesterId);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.First().Id, Is.EqualTo(friendships.First().Id));
}
[Test]
public async Task Given_InvalidUserId_When_GetByIdAsync_Should_Throw_NotFoundByKeyException()
{
// Arrange
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
// Act & Assert
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.FindByUserIdAsync(_fixture.Create<Guid>()));
}
[Test]
public async Task Given_ValidFriendship_When_AddAsync_Then_Added()
{
// Arrange
var friendship = _fixture.Create<Friendship>();
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
// Act
await repository.AddAsync(friendship);
// Assert
Assert.That(context.Friendships.Count, Is.EqualTo(1));
Assert.That(context.Friendships.First().Id, Is.EqualTo(friendship.Id));
}
[Test]
public async Task Given_InvalidFriendship_When_AddAsync_Should_Throw_AdditionException()
{
// Arrange
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
// Act & Assert
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
}
[Test]
public async Task Given_ExistFriendship_When_Exist_Then_True()
{
// Arrange
var friendship = _fixture.Create<Friendship>();
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
context.Friendships.Add(friendship);
await context.SaveChangesAsync();
// Act
var result = repository.Exist(friendship.RequesterId, friendship.AddresseeId);
// Assert
Assert.That(result, Is.True);
}
[Test]
public async Task Given_NotExistFriendship_When_Exist_Then_False()
{
// Arrange
await using var context = new GovorDbContext(_options);
var repository = new FriendshipsRepository(context, _validator);
// Act & Assert
Assert.That(repository.Exist(_fixture.Create<Guid>(), _fixture.Create<Guid>()), Is.False);
}
}
@@ -0,0 +1,70 @@
using AutoFixture;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models;
namespace Govor.API.Tests.UnitTests.Infrastructure.Validators;
[TestFixture]
public class FriendshipValidatorTests
{
private IObjectValidator<Friendship> _friendshipValidator;
private Fixture _fixture;
public FriendshipValidatorTests()
{
_friendshipValidator = new FriendshipValidator();
_fixture = new Fixture();
_fixture.Behaviors
.OfType<ThrowingRecursionBehavior>()
.ToList()
.ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());
}
[Test]
public void Given_ValidFriendship_When_Validate_Then_Returns_True()
{
var friendship = _fixture.Create<Friendship>();
// Act & Assert
Assert.DoesNotThrow( () => _friendshipValidator.Validate(friendship));
Assert.That(_friendshipValidator.TryValidate(friendship), Is.True);
}
[Test]
public void Given_EmptyFriendshipId_When_Validate_Then_Returns_False()
{
var friendship = _fixture.Create<Friendship>();
friendship.Id = Guid.Empty;
// Act & Assert
Assert.Throws<InvalidObjectException<Friendship>>( () => _friendshipValidator.Validate(friendship));
Assert.That(_friendshipValidator.TryValidate(friendship), Is.False);
}
[Test]
public void Given_EmptyRequesterId_When_Validate_Then_Returns_False()
{
var friendship = _fixture.Create<Friendship>();
friendship.RequesterId = Guid.Empty;
// Act & Assert
Assert.Throws<InvalidObjectException<Friendship>>( () => _friendshipValidator.Validate(friendship));
Assert.That(_friendshipValidator.TryValidate(friendship), Is.False);
}
[Test]
public void Given_AddresseeId_When_Validate_Then_Returns_False()
{
var friendship = _fixture.Create<Friendship>();
friendship.AddresseeId = Guid.Empty;
// Act & Assert
Assert.Throws<InvalidObjectException<Friendship>>( () => _friendshipValidator.Validate(friendship));
Assert.That(_friendshipValidator.TryValidate(friendship), Is.False);
}
}
@@ -25,7 +25,7 @@ public class InvitationValidatorTests
}
[Test]
public void Given_ValidInvitation_When_Exist_Then_Returns_True()
public void Given_ValidInvitation_When_Validate_Then_Returns_True()
{
var invitation = _fixture.Create<Invitation>();
@@ -35,7 +35,7 @@ public class InvitationValidatorTests
}
[Test]
public void Given_NullInvitation_When_Exist_Then_Returns_False()
public void Given_NullInvitation_When_Validate_Then_Returns_False()
{
// Act & Assert
Assert.Throws<InvalidObjectException<Invitation>>( () => _messageValidator.Validate(default));
@@ -43,7 +43,7 @@ public class InvitationValidatorTests
}
[Test]
public void Given_EmptyInvitationId_When_Exist_Then_Returns_False()
public void Given_EmptyInvitationId_When_Validate_Then_Returns_False()
{
// Arrange
var invitation = _fixture.Create<Invitation>();
@@ -55,7 +55,7 @@ public class InvitationValidatorTests
}
[Test]
public void Given_EmptyInvitationCreationDate_When_Exist_Then_Returns_False()
public void Given_EmptyInvitationCreationDate_When_Validate_Then_Returns_False()
{
// Arrange
var invitation = _fixture.Create<Invitation>();
@@ -67,7 +67,7 @@ public class InvitationValidatorTests
}
[Test]
public void Given_InvalidEndDatesAndIsActive_When_Exist_Then_Returns_False()
public void Given_InvalidEndDatesAndIsActive_When_Validate_Then_Returns_False()
{
// Arrange
var invitation = _fixture.Create<Invitation>();
@@ -79,7 +79,7 @@ public class InvitationValidatorTests
}
[Test]
public void Given_InvalidMaxParticipantsAndIsActive_When_Exist_Then_Returns_False()
public void Given_InvalidMaxParticipantsAndIsActive_When_Validate_Then_Returns_False()
{
// Arrange
var invitation = _fixture.Create<Invitation>();
@@ -92,7 +92,7 @@ public class InvitationValidatorTests
}
[Test]
public void Given_InvalidCode_When_Exist_Then_Returns_False()
public void Given_InvalidCode_When_Validate_Then_Returns_False()
{
// Arrange
var invitation = _fixture.Create<Invitation>();