1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00
bitwarden-server/test/Core.Test/NotificationCenter/Queries/GetNotificationStatusForUserTest.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

86 lines
3.5 KiB
C#

#nullable enable
using Bit.Core.Context;
using Bit.Core.Exceptions;
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 System.Security.Claims;
using Bit.Core.NotificationCenter.Authorization;
using Bit.Core.NotificationCenter.Entities;
using Microsoft.AspNetCore.Authorization;
using NSubstitute;
using Xunit;
[SutProviderCustomize]
[NotificationStatusCustomize]
public class GetNotificationStatusForUserQueryTest
{
private static void Setup(SutProvider<GetNotificationStatusForUserQuery> sutProvider,
Guid notificationId, NotificationStatus? notificationStatus, Guid? userId, bool authorized = false)
{
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
sutProvider.GetDependency<INotificationStatusRepository>()
.GetByNotificationIdAndUserIdAsync(notificationId, userId.GetValueOrDefault(Guid.NewGuid()))
.Returns(notificationStatus);
sutProvider.GetDependency<IAuthorizationService>()
.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), notificationStatus ?? Arg.Any<NotificationStatus>(),
Arg.Is<IEnumerable<IAuthorizationRequirement>>(reqs =>
reqs.Contains(NotificationStatusOperations.Read)))
.Returns(authorized ? AuthorizationResult.Success() : AuthorizationResult.Failed());
}
[Theory]
[BitAutoData]
public async Task GetByUserIdStatusFilterAsync_UserNotLoggedIn_NotFoundException(
SutProvider<GetNotificationStatusForUserQuery> sutProvider,
Guid notificationId, NotificationStatus notificationStatus)
{
Setup(sutProvider, notificationId, notificationStatus, userId: null, true);
await Assert.ThrowsAsync<NotFoundException>(() =>
sutProvider.Sut.GetByNotificationIdAndUserIdAsync(notificationId));
}
[Theory]
[BitAutoData]
public async Task GetByUserIdStatusFilterAsync_NotificationStatusNotFound_NotFoundException(
SutProvider<GetNotificationStatusForUserQuery> sutProvider,
Guid notificationId)
{
Setup(sutProvider, notificationId, notificationStatus: null, Guid.NewGuid(), true);
await Assert.ThrowsAsync<NotFoundException>(() =>
sutProvider.Sut.GetByNotificationIdAndUserIdAsync(notificationId));
}
[Theory]
[BitAutoData]
public async Task GetByUserIdStatusFilterAsync_AuthorizationFailed_NotFoundException(
SutProvider<GetNotificationStatusForUserQuery> sutProvider,
Guid notificationId, NotificationStatus notificationStatus)
{
Setup(sutProvider, notificationId, notificationStatus, Guid.NewGuid(), authorized: false);
await Assert.ThrowsAsync<NotFoundException>(() =>
sutProvider.Sut.GetByNotificationIdAndUserIdAsync(notificationId));
}
[Theory]
[BitAutoData]
public async Task GetByUserIdStatusFilterAsync_NotificationFoundAuthorized_Returned(
SutProvider<GetNotificationStatusForUserQuery> sutProvider,
Guid notificationId, NotificationStatus notificationStatus)
{
Setup(sutProvider, notificationId, notificationStatus, Guid.NewGuid(), true);
var actualNotificationStatus = await sutProvider.Sut.GetByNotificationIdAndUserIdAsync(notificationId);
Assert.Equal(notificationStatus, actualNotificationStatus);
}
}