154 lines
4.1 KiB
C#
154 lines
4.1 KiB
C#
|
|
using Service.Message;
|
||
|
|
|
||
|
|
namespace WebApi.Test.Application;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// NotificationApplication 单元测试
|
||
|
|
/// </summary>
|
||
|
|
public class NotificationApplicationTest : BaseApplicationTest
|
||
|
|
{
|
||
|
|
private readonly INotificationService _notificationService;
|
||
|
|
private readonly ILogger<NotificationApplication> _logger;
|
||
|
|
private readonly NotificationApplication _application;
|
||
|
|
|
||
|
|
public NotificationApplicationTest()
|
||
|
|
{
|
||
|
|
_notificationService = Substitute.For<INotificationService>();
|
||
|
|
_logger = CreateMockLogger<NotificationApplication>();
|
||
|
|
_application = new NotificationApplication(_notificationService, _logger);
|
||
|
|
}
|
||
|
|
|
||
|
|
#region GetVapidPublicKeyAsync Tests
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task GetVapidPublicKeyAsync_应返回公钥()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var expectedKey = "BM5wX9Y8Z_test_vapid_public_key";
|
||
|
|
_notificationService.GetVapidPublicKeyAsync().Returns(expectedKey);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = await _application.GetVapidPublicKeyAsync();
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
result.Should().Be(expectedKey);
|
||
|
|
await _notificationService.Received(1).GetVapidPublicKeyAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task GetVapidPublicKeyAsync_返回空字符串_应正常处理()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
_notificationService.GetVapidPublicKeyAsync().Returns(string.Empty);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
var result = await _application.GetVapidPublicKeyAsync();
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
result.Should().BeEmpty();
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region SubscribeAsync Tests
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SubscribeAsync_有效订阅信息_应成功订阅()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var subscription = new PushSubscription
|
||
|
|
{
|
||
|
|
Endpoint = "https://fcm.googleapis.com/fcm/send/test",
|
||
|
|
P256DH = "test_p256dh_key",
|
||
|
|
Auth = "test_auth_key"
|
||
|
|
};
|
||
|
|
|
||
|
|
// Act
|
||
|
|
await _application.SubscribeAsync(subscription);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
await _notificationService.Received(1).SubscribeAsync(Arg.Is<PushSubscription>(
|
||
|
|
s => s.Endpoint == subscription.Endpoint &&
|
||
|
|
s.P256DH == subscription.P256DH &&
|
||
|
|
s.Auth == subscription.Auth
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SubscribeAsync_多次订阅同一端点_应正常处理()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var subscription = new PushSubscription
|
||
|
|
{
|
||
|
|
Endpoint = "https://fcm.googleapis.com/fcm/send/test",
|
||
|
|
P256DH = "test_key",
|
||
|
|
Auth = "test_auth"
|
||
|
|
};
|
||
|
|
|
||
|
|
// Act
|
||
|
|
await _application.SubscribeAsync(subscription);
|
||
|
|
await _application.SubscribeAsync(subscription);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
await _notificationService.Received(2).SubscribeAsync(Arg.Any<PushSubscription>());
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
|
||
|
|
#region SendNotificationAsync Tests
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SendNotificationAsync_有效消息_应成功发送()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var message = "测试通知消息";
|
||
|
|
|
||
|
|
// Act
|
||
|
|
await _application.SendNotificationAsync(message);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
await _notificationService.Received(1).SendNotificationAsync(message);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SendNotificationAsync_空消息_应调用服务()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var message = string.Empty;
|
||
|
|
|
||
|
|
// Act
|
||
|
|
await _application.SendNotificationAsync(message);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
await _notificationService.Received(1).SendNotificationAsync(message);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SendNotificationAsync_长消息_应成功发送()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var longMessage = new string('测', 500);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
await _application.SendNotificationAsync(longMessage);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
await _notificationService.Received(1).SendNotificationAsync(longMessage);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SendNotificationAsync_特殊字符消息_应成功发送()
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
var specialMessage = "测试<html> 特殊\"字符\"消息";
|
||
|
|
|
||
|
|
// Act
|
||
|
|
await _application.SendNotificationAsync(specialMessage);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
await _notificationService.Received(1).SendNotificationAsync(specialMessage);
|
||
|
|
}
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
}
|