1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00
bitwarden/test/Core.Test/NotificationCenter/Queries/GetNotificationsForUserQueryTest.cs
Maciej Zieniuk f3f81deb98
[PM-11123] Service layer for Notification Center (#4741)
* PM-11123: Service layer

* PM-11123: Service layer for Notification Center

* PM-11123: Throw error on unsupported requirement

* PM-11123: Missing await

* PM-11123: Cleanup

* PM-11123: Unit Test coverage

* PM-11123: Flipping the authorization logic to be exact match of fail, formatting

* PM-11123: Async warning

* PM-11123: Using AuthorizeOrThrowAsync, removal of redundant set new id

* PM-11123: UT typo

* PM-11123: UT fix
2024-10-02 19:23:19 +02:00

56 lines
2.1 KiB
C#

#nullable enable
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.NotificationCenter.Models.Filter;
using Bit.Core.NotificationCenter.Queries;
using Bit.Core.NotificationCenter.Repositories;
using Bit.Core.Test.NotificationCenter.AutoFixture;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
namespace Bit.Core.Test.NotificationCenter.Queries;
using Bit.Core.NotificationCenter.Entities;
using NSubstitute;
using Xunit;
[SutProviderCustomize]
[NotificationCustomize]
public class GetNotificationsForUserQueryTest
{
private static void Setup(SutProvider<GetNotificationsForUserQuery> sutProvider,
List<Notification> notifications, NotificationStatusFilter statusFilter, Guid? userId)
{
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
sutProvider.GetDependency<INotificationRepository>().GetByUserIdAndStatusAsync(
userId.GetValueOrDefault(Guid.NewGuid()), Arg.Any<ClientType>(), statusFilter)
.Returns(notifications);
}
[Theory]
[BitAutoData]
public async Task GetByUserIdStatusFilterAsync_NotLoggedIn_NotFoundException(
SutProvider<GetNotificationsForUserQuery> sutProvider,
List<Notification> notifications, NotificationStatusFilter notificationStatusFilter)
{
Setup(sutProvider, notifications, notificationStatusFilter, userId: null);
await Assert.ThrowsAsync<NotFoundException>(() =>
sutProvider.Sut.GetByUserIdStatusFilterAsync(notificationStatusFilter));
}
[Theory]
[BitAutoData]
public async Task GetByUserIdStatusFilterAsync_NotificationsFound_Returned(
SutProvider<GetNotificationsForUserQuery> sutProvider,
List<Notification> notifications, NotificationStatusFilter notificationStatusFilter)
{
Setup(sutProvider, notifications, notificationStatusFilter, Guid.NewGuid());
var actualNotifications = await sutProvider.Sut.GetByUserIdStatusFilterAsync(notificationStatusFilter);
Assert.Equal(notifications, actualNotifications);
}
}