mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
6d1c53beeb
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
310 lines
10 KiB
C#
310 lines
10 KiB
C#
using AutoFixture;
|
|
using Govor.Domain.Models;
|
|
using Govor.Domain.Models.Users;
|
|
using Govor.Domain.Repositories;
|
|
using Govor.Domain.Repositories.Exceptions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Govor.Domain.Tests.Repositories;
|
|
|
|
[TestFixture]
|
|
public class UserSessionsRepositoryTests
|
|
{
|
|
private Fixture _fixture;
|
|
private DbContextOptions<GovorDbContext> _options;
|
|
private int _testIteration = 0;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_testIteration += 1;
|
|
|
|
_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(UserSessionsRepositoryTests)}_{_testIteration}")
|
|
.Options;
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetAllSessions_ReturnsAllSessions()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var sessions = _fixture.CreateMany<UserSession>(random.Next(2, 10)).ToList();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
context.UserSessions.AddRange(sessions);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await repository.GetAllAsync();
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.Count, Is.EqualTo(sessions.Count));
|
|
Assert.That(result, Is.EquivalentTo(sessions));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_EmptySetDb_When_GetAll_Should_Throw_NotFoundException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundException>(async () => await repository.GetAllAsync());
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidId_When_GetByIdAsync_ShouldReturnSession()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var sessions = _fixture.CreateMany<UserSession>(random.Next(2, 10)).ToList();
|
|
|
|
var id = sessions.First().Id;
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
context.UserSessions.AddRange(sessions);
|
|
await context.SaveChangesAsync();
|
|
// Act
|
|
var result = await repository.GetByIdAsync(id);
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.Id, Is.EqualTo(id));
|
|
Assert.That(result, Is.EqualTo(sessions.First()));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_InvalidId_When_GetByIdAsync_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<Guid>>(async () => await repository.GetByIdAsync(_fixture.Create<Guid>()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidCreatedAt_When_GetByCreatedAtAsync_ShouldReturnSession()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var sessions = _fixture.CreateMany<UserSession>(random.Next(2, 10)).ToList();
|
|
|
|
var created = sessions.First().CreatedAt;
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
context.UserSessions.AddRange(sessions);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var list = await repository.GetByCreatedAtAsync(created);
|
|
|
|
|
|
// Assert
|
|
var result = list.First();
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.CreatedAt, Is.EqualTo(created));
|
|
Assert.That(result, Is.EqualTo(sessions.First()));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_ValidCreatedAt_When_GetByCreatedAtAsync_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<DateTime>>(async () => await repository.GetByCreatedAtAsync(_fixture.Create<DateTime>()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidExpiresAt_When_GetByExpiresAtAsync_ShouldReturnSession()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var sessions = _fixture.CreateMany<UserSession>(random.Next(2, 10)).ToList();
|
|
|
|
var expiresAt = sessions.First().ExpiresAt;
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
context.UserSessions.AddRange(sessions);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var list = await repository.GetByExpiresAtAsync(expiresAt);
|
|
|
|
// Assert
|
|
var result = list.First();
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result.ExpiresAt, Is.EqualTo(expiresAt));
|
|
Assert.That(result, Is.EqualTo(sessions.First()));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_ValidExpiresAt_When_GetByExpiresAtAsync_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<DateTime>>(async () => await repository.GetByExpiresAtAsync(_fixture.Create<DateTime>()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_isRevoked_When_GetByRevokedAsync_ShouldReturnSession()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var sessions = _fixture.Build<UserSession>()
|
|
.With(f => f.IsRevoked, true)
|
|
.CreateMany(random.Next(2, 10)).ToList();
|
|
|
|
var isRevoke = sessions.First().IsRevoked;
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
context.UserSessions.AddRange(sessions);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await repository.GetByRevokedAsync(isRevoke);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.EquivalentTo(sessions));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_InvalidRevoked_When_GetByExpiresAtAsync_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<bool>>(async () => await repository.GetByRevokedAsync(false));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidRefreshToken_When_GetByRefreshTokenAsync_ShouldReturnSession()
|
|
{
|
|
// Arrange
|
|
var random = new Random();
|
|
var sessions = _fixture.Build<UserSession>()
|
|
.With(f => f.IsRevoked, true)
|
|
.CreateMany(random.Next(2, 10)).ToList();
|
|
|
|
var token = sessions.First().RefreshTokenHash;
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
context.UserSessions.AddRange(sessions);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result = await repository.GetByHashedRefreshTokenAsync(token);
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Not.Null);
|
|
Assert.That(result, Is.EqualTo(sessions.First()));
|
|
}
|
|
|
|
[Test]
|
|
public void Given_InvalidRefreshToken_When_GetByRefreshTokenAsync_Should_Throw_NotFoundByKeyException()
|
|
{
|
|
// Arrange
|
|
using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<NotFoundByKeyException<string>>(async () => await repository.GetByHashedRefreshTokenAsync(_fixture.Create<string>()));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ValidUserSessions_When_AddAsync_Then_UserSessionsAdded()
|
|
{
|
|
// Arrange
|
|
var session = _fixture.Create<UserSession>();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
// Act
|
|
await repository.AddAsync(session);
|
|
|
|
// Assert
|
|
Assert.That(context.UserSessions.Count, Is.EqualTo(1));
|
|
Assert.That(context.UserSessions.First(), Is.EqualTo(session));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_InvalidUserSessions_When_AddAsync_Should_Throw_AdditionException()
|
|
{
|
|
// Arrange
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
// Act & Assert
|
|
Assert.ThrowsAsync<AdditionException>(async () => await repository.AddAsync(default));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_ExistUserSessions_When_Exist_Then_ReturnTrue()
|
|
{
|
|
// Arrange
|
|
var session = _fixture.Create<UserSession>();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
context.UserSessions.Add(session);
|
|
await context.SaveChangesAsync();
|
|
|
|
// Act
|
|
var result1 = repository.Exist(session);
|
|
var result2 = repository.Exist(session.Id);
|
|
var result3 = repository.Exist(session.RefreshTokenHash);
|
|
|
|
// Assert
|
|
Assert.That(result1, Is.True);
|
|
Assert.That(result2, Is.True);
|
|
Assert.That(result3, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Given_NotExistUserSessions_When_Exist_Then_ReturnFalse()
|
|
{
|
|
// Arrange
|
|
var session = _fixture.Create<UserSession>();
|
|
|
|
await using var context = new GovorDbContext(_options);
|
|
var repository = new UserSessionsRepository(context);
|
|
|
|
// Act
|
|
var result1 = repository.Exist(session);
|
|
var result2 = repository.Exist(session.Id);
|
|
var result3 = repository.Exist(session.RefreshTokenHash);
|
|
// Assert
|
|
Assert.That(result1, Is.False);
|
|
Assert.That(result2, Is.False);
|
|
Assert.That(result3, Is.False);
|
|
}
|
|
} |