-
Notifications
You must be signed in to change notification settings - Fork 19
/
HiredWaitersHubTests.cs
123 lines (101 loc) · 4.05 KB
/
HiredWaitersHubTests.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Cafe.Core.AuthContext.Commands;
using Cafe.Core.ManagerContext.Commands;
using Cafe.Core.WaiterContext.Commands;
using Cafe.Domain.Events;
using Cafe.Tests.Business.AuthContext;
using Cafe.Tests.Customizations;
using Moq;
using Shouldly;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
namespace Cafe.Tests.Api.Hubs
{
public class HiredWaitersHubTests : ResetDatabaseLifetime
{
private readonly AppFixture _fixture;
private readonly AuthTestsHelper _authTestsHelper;
private readonly string _hubUrl;
public HiredWaitersHubTests()
{
_fixture = new AppFixture();
_authTestsHelper = new AuthTestsHelper(_fixture);
_hubUrl = _fixture.GetCompleteServerUrl("/hiredWaiters");
}
[Theory]
[CustomizedAutoData]
public async Task AuthenticatedSubscribersAreNotifiedAboutHiredWaiters(HireWaiter hireWaiterCommand)
{
// Arrange
var adminAccessToken = await _authTestsHelper.GetAdminToken();
var testConnection = BuildTestConnection(adminAccessToken);
await testConnection.OpenAsync();
// Act
await _fixture.SendAsync(hireWaiterCommand);
// Assert
await testConnection
.VerifyMessageReceived<WaiterHired>(
e => e.Waiter.Id == hireWaiterCommand.Id &&
e.Waiter.ShortName == hireWaiterCommand.ShortName,
Times.Once());
}
[Theory]
[CustomizedAutoData]
public async Task CannotConnectIfNotAManager(Register registerCommand)
{
// Arrange
// The newly registered user is not going to have any roles assigned
var accessToken = (await _authTestsHelper.RegisterAndLogin(registerCommand)).TokenString;
var exception = await Should.ThrowAsync<HttpRequestException>(async () =>
{
await BuildTestConnection(accessToken)
.OpenAsync();
});
exception.Message.ShouldContain("403");
}
[Theory]
[CustomizedAutoData]
public async Task CanConnectAsManager(Register registerCommand, HireManager hireManagerCommand, HireWaiter hireWaiterCommand)
{
// Arrange
await _authTestsHelper.Register(registerCommand);
await _fixture.SendAsync(hireManagerCommand);
var assignManagerToAccountCommand = new AssignManagerToAccount
{
AccountId = registerCommand.Id,
ManagerId = hireManagerCommand.Id
};
await _fixture.SendAsync(assignManagerToAccountCommand);
var accessToken = (await _authTestsHelper.Login(registerCommand.Email, registerCommand.Password)).TokenString;
var testConnection = BuildTestConnection(accessToken);
await testConnection.OpenAsync();
// Act
await _fixture.SendAsync(hireWaiterCommand);
// Assert
await testConnection
.VerifyMessageReceived<WaiterHired>(
e => e.Waiter.Id == hireWaiterCommand.Id &&
e.Waiter.ShortName == hireWaiterCommand.ShortName,
Times.Once());
}
[Fact]
public async Task CannotConnectWithAnInvalidToken()
{
// Arrange
var accessToken = "an obviously invalid access token";
// Act, Assert
var exception = await Should.ThrowAsync<HttpRequestException>(async () =>
{
await BuildTestConnection(accessToken)
.OpenAsync();
});
exception.Message.ShouldContain("401");
}
private TestHubConnection BuildTestConnection(string accessToken) =>
new TestHubConnectionBuilder()
.OnHub(_hubUrl)
.WithExpectedEvent<WaiterHired>(nameof(WaiterHired))
.WithAccessToken(accessToken)
.Build();
}
}