1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

PM-10600: Unit test coverage for NotificationsApiPushNotificationService constructor

This commit is contained in:
Maciej Zieniuk 2024-11-08 15:30:42 +00:00
parent 676f10b832
commit 8b3f1318a2
No known key found for this signature in database
GPG Key ID: 9CACE59F1272ACD9
2 changed files with 54 additions and 0 deletions

View File

@ -209,6 +209,11 @@ public class NotificationsApiPushNotificationService : BaseIdentityClientService
await SendAsync(HttpMethod.Post, "send", request);
}
internal virtual async Task SendAsync(HttpMethod method, string path, object payload)
{
await base.SendAsync(method, path, payload);
}
private string GetContextIdentifier(bool excludeCurrentContext)
{
if (!excludeCurrentContext)

View File

@ -6,6 +6,7 @@ using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.NotificationCenter.Entities;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Test.AutoFixture;
using Bit.Core.Test.AutoFixture.CurrentContextFixtures;
using Bit.Core.Test.NotificationCenter.AutoFixture;
@ -13,6 +14,7 @@ using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using Bit.Test.Common.MockedHttpClient;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using NSubstitute;
using Xunit;
@ -22,6 +24,53 @@ namespace Bit.Core.Test.Services;
[HttpClientCustomize]
public class NotificationsApiPushNotificationServiceTests
{
[Theory]
[BitAutoData]
[GlobalSettingsCustomize]
public async void Constructor_DefaultGlobalSettings_CorrectHttpRequests(
SutProvider<NotificationsApiPushNotificationService> sutProvider, GlobalSettings globalSettings,
MockedHttpMessageHandler mockedHttpMessageHandler)
{
globalSettings.SelfHosted = true;
globalSettings.BaseServiceUri = new GlobalSettings.BaseServiceUriSettings(globalSettings);
globalSettings.ProjectName = "Notifications";
globalSettings.InternalIdentityKey = "internal-identity-key";
var tokenResponse = mockedHttpMessageHandler
.When(request =>
{
if (request.Method != HttpMethod.Post ||
!request.RequestUri!.Equals(new Uri("http://identity:5000/connect/token")) ||
request.Content == null ||
request.Content.Headers.ContentType?.MediaType != "application/x-www-form-urlencoded")
{
return false;
}
var formReader = new FormReader(request.Content.ReadAsStream()).ReadForm();
return formReader["scope"] == "internal" && formReader["client_id"] == "internal.Notifications" &&
formReader["client_secret"] == "internal-identity-key";
})
.RespondWith(HttpStatusCode.OK, new StringContent("{\"access_token\":\"token\"}"));
var sendResponse = mockedHttpMessageHandler
.When(request => request.Method == HttpMethod.Post &&
(request.RequestUri?.Equals(new Uri("http://notifications:5000/send")) ?? false) &&
request.Headers.Authorization?.ToString() == "Bearer token")
.RespondWith(HttpStatusCode.OK);
sutProvider.Reset();
sutProvider.SetDependency(typeof(GlobalSettings), globalSettings)
.Create();
// var sut = new NotificationsApiPushNotificationService(httpFactory, globalSettings, httpContextAccessor, logger);
await sutProvider.Sut.SendAsync(HttpMethod.Post, "send", "payload");
Assert.Equal(1, tokenResponse.NumberOfResponses);
Assert.Equal(1, sendResponse.NumberOfResponses);
}
[Theory]
[BitAutoData]
[NotificationCustomize]