aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline.Tests/IntegratedTests/TimelineHubTest.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-05-15 16:01:21 +0800
committercrupest <crupest@outlook.com>2021-05-15 16:01:21 +0800
commit49b0cb7b465561447b8d4693ba988b13e0e1b57a (patch)
tree4c533d940af0b2691619a960f439f5f33129a9ff /BackEnd/Timeline.Tests/IntegratedTests/TimelineHubTest.cs
parent741230818e8c8093f7ead04c7df1c21a17cceae4 (diff)
downloadtimeline-49b0cb7b465561447b8d4693ba988b13e0e1b57a.tar.gz
timeline-49b0cb7b465561447b8d4693ba988b13e0e1b57a.tar.bz2
timeline-49b0cb7b465561447b8d4693ba988b13e0e1b57a.zip
feat: Timeline post change notification with signalr.
Diffstat (limited to 'BackEnd/Timeline.Tests/IntegratedTests/TimelineHubTest.cs')
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/TimelineHubTest.cs82
1 files changed, 82 insertions, 0 deletions
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelineHubTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelineHubTest.cs
new file mode 100644
index 00000000..66df74d7
--- /dev/null
+++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelineHubTest.cs
@@ -0,0 +1,82 @@
+using FluentAssertions;
+using Microsoft.AspNetCore.SignalR.Client;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Timeline.SignalRHub;
+using Xunit;
+
+namespace Timeline.Tests.IntegratedTests
+{
+ public class TimelineHubTest : BaseTimelineTest
+ {
+ private HubConnection CreateConnection(string? token)
+ {
+ return new HubConnectionBuilder().WithUrl($"http://localhost/api/hub/timeline{(token is null ? "" : "?token=" + token)}",
+ options => options.HttpMessageHandlerFactory = _ => TestApp.Server.CreateHandler()).Build();
+ }
+
+ [Theory]
+ [MemberData(nameof(TimelineNameGeneratorTestData))]
+ public async Task TimelinePostUpdate_Should_Work(TimelineNameGenerator generator)
+ {
+ var token = await CreateTokenAsync(1);
+
+ await using var connection = CreateConnection(token);
+
+ var changed = false;
+
+ connection.On<string>(nameof(ITimelineClient.OnTimelinePostChanged), (timelineName) =>
+ {
+ timelineName.Should().Be(generator(1));
+ changed = true;
+ });
+
+ await connection.StartAsync();
+ connection.State.Should().Be(HubConnectionState.Connected);
+
+ using var client = await CreateClientAsUser();
+
+ await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelinePostTest.CreateTextPostRequest("aaa"));
+ changed.Should().BeFalse();
+
+ await connection.InvokeAsync(nameof(TimelineHub.SubscribeTimelinePostChange), generator(1));
+
+ await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelinePostTest.CreateTextPostRequest("bbb"));
+ changed.Should().BeTrue();
+
+ changed = false;
+
+ await connection.InvokeAsync(nameof(TimelineHub.UnsubscribeTimelinePostChange), generator(1));
+
+ await client.TestPostAsync($"timelines/{generator(1)}/posts", TimelinePostTest.CreateTextPostRequest("ccc"));
+ changed.Should().BeFalse();
+ }
+
+ [Fact]
+ public async Task TimelinePostUpdate_InvalidName()
+ {
+ await using var connection = CreateConnection(null);
+ await connection.StartAsync();
+ await connection.Awaiting(c => c.InvokeAsync(nameof(TimelineHub.SubscribeTimelinePostChange), "!!!")).Should().ThrowAsync<Exception>();
+ }
+
+ [Fact]
+ public async Task TimelinePostUpdate_NotExist()
+ {
+ await using var connection = CreateConnection(null);
+ await connection.StartAsync();
+ await connection.Awaiting(c => c.InvokeAsync(nameof(TimelineHub.SubscribeTimelinePostChange), "timelinenotexist")).Should().ThrowAsync<Exception>();
+ }
+
+ [Fact]
+ public async Task TimelinePostUpdate_Forbid()
+ {
+ await using var connection = CreateConnection(null);
+ await connection.StartAsync();
+ await connection.Awaiting(c => c.InvokeAsync(nameof(TimelineHub.SubscribeTimelinePostChange), "t1")).Should().ThrowAsync<Exception>();
+ }
+ }
+}
+