blob: 9b28a64811740b0445de80b979b8a2bf0d88462a (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 | using FluentAssertions;
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Threading;
using System.Threading.Tasks;
using Timeline.SignalRHub;
using Xunit;
using Xunit.Abstractions;
namespace Timeline.Tests.IntegratedTests
{
    public class TimelineHubTest : BaseTimelineTest
    {
        public TimelineHubTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
        {
        }
        private HubConnection CreateConnection(string? token)
        {
            return new HubConnectionBuilder().WithUrl("ws://localhost/api/hub/timeline",
              options =>
              {
                  options.HttpMessageHandlerFactory = _ => TestApp.Server.CreateHandler();
                  options.AccessTokenProvider = token is null ? null : () => Task.FromResult(token);
              }).Build();
        }
        [Theory]
        [MemberData(nameof(TimelineNameGeneratorTestData))]
        public async Task TimelinePostUpdate_Should_Work(TimelineNameGenerator generator)
        {
            var token = await CreateTokenAsync(1);
            await using var connection = CreateConnection(token);
            await connection.StartAsync();
            connection.State.Should().Be(HubConnectionState.Connected);
            using SemaphoreSlim semaphore = new SemaphoreSlim(0);
            var changed = false;
            connection.On<string>(nameof(ITimelineClient.OnTimelinePostChanged), (timelineName) =>
            {
                timelineName.Should().Be(generator(1));
                changed = true;
                semaphore.Release();
            });
            await Task.Run(async () =>
            {
                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"));
                await semaphore.WaitAsync();
                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>();
        }
    }
}
 |