-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCreateSessionServiceTests.cs
More file actions
290 lines (250 loc) · 12.7 KB
/
CreateSessionServiceTests.cs
File metadata and controls
290 lines (250 loc) · 12.7 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using ByteSync.Business.Profiles;
using ByteSync.Business.SessionMembers;
using ByteSync.Business.Sessions;
using ByteSync.Business.Sessions.Connecting;
using ByteSync.Business.Sessions.RunSessionInfos;
using ByteSync.Common.Business.EndPoints;
using ByteSync.Common.Business.Lobbies;
using ByteSync.Common.Business.Sessions;
using ByteSync.Common.Business.Sessions.Cloud.Connections;
using ByteSync.Interfaces.Controls.Applications;
using ByteSync.Interfaces.Controls.Communications;
using ByteSync.Interfaces.Controls.Communications.Http;
using ByteSync.Interfaces.Controls.Encryptions;
using ByteSync.Interfaces.Repositories;
using ByteSync.Interfaces.Services.Sessions.Connecting;
using ByteSync.Services.Sessions;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
namespace ByteSync.Tests.Controls.Sessions.Connecting;
[TestFixture]
public class CreateSessionServiceTests
{
private Mock<ICloudSessionConnectionRepository> _cloudSessionConnectionRepositoryMock;
private Mock<IDataEncrypter> _dataEncrypterMock;
private Mock<IEnvironmentService> _environmentServiceMock;
private Mock<ICloudSessionApiClient> _cloudSessionApiClientMock;
private Mock<IPublicKeysManager> _publicKeysManagerMock;
private Mock<ITrustProcessPublicKeysRepository> _trustProcessPublicKeysRepositoryMock;
private Mock<IDigitalSignaturesRepository> _digitalSignaturesRepositoryMock;
private Mock<IAfterJoinSessionService> _afterJoinSessionServiceMock;
private Mock<ICloudSessionConnectionService> _cloudSessionConnectionServiceMock;
private Mock<ILogger<CreateSessionService>> _loggerMock;
private CancellationTokenSource _cts;
private CreateSessionService _service;
[SetUp]
public void SetUp()
{
_cloudSessionConnectionRepositoryMock = new Mock<ICloudSessionConnectionRepository>();
_dataEncrypterMock = new Mock<IDataEncrypter>();
_environmentServiceMock = new Mock<IEnvironmentService>();
_cloudSessionApiClientMock = new Mock<ICloudSessionApiClient>();
_publicKeysManagerMock = new Mock<IPublicKeysManager>();
_trustProcessPublicKeysRepositoryMock = new Mock<ITrustProcessPublicKeysRepository>();
_digitalSignaturesRepositoryMock = new Mock<IDigitalSignaturesRepository>();
_afterJoinSessionServiceMock = new Mock<IAfterJoinSessionService>();
_cloudSessionConnectionServiceMock = new Mock<ICloudSessionConnectionService>();
_loggerMock = new Mock<ILogger<CreateSessionService>>();
_cts = new CancellationTokenSource();
_cloudSessionConnectionRepositoryMock.SetupGet(x => x.CancellationToken).Returns(_cts.Token);
_cloudSessionConnectionRepositoryMock.SetupGet(x => x.CancellationTokenSource).Returns(_cts);
_service = new CreateSessionService(
_cloudSessionConnectionRepositoryMock.Object,
_dataEncrypterMock.Object,
_environmentServiceMock.Object,
_cloudSessionApiClientMock.Object,
_publicKeysManagerMock.Object,
_trustProcessPublicKeysRepositoryMock.Object,
_digitalSignaturesRepositoryMock.Object,
_afterJoinSessionServiceMock.Object,
_cloudSessionConnectionServiceMock.Object,
_loggerMock.Object);
}
[Test]
public async Task CreateCloudSession_WithProfileInfo_ShouldReturnResultAndCallDependencies()
{
// Arrange
var runCloudSessionProfileInfo = new RunCloudSessionProfileInfo("Lobby1", new CloudSessionProfile(),
new CloudSessionProfileDetails(), LobbySessionModes.RunInventory);
var request = new CreateCloudSessionRequest(runCloudSessionProfileInfo);
var encryptedSettingsDefault = new EncryptedSessionSettings();
var encryptedPrivateDataDefault = new EncryptedSessionMemberPrivateData();
var publicKeyInfo = new PublicKeyInfo();
_dataEncrypterMock.Setup(x => x.EncryptSessionSettings(It.IsAny<SessionSettings>()))
.Returns(encryptedSettingsDefault);
_dataEncrypterMock.Setup(x => x.EncryptSessionMemberPrivateData(It.IsAny<SessionMemberPrivateData>()))
.Returns(encryptedPrivateDataDefault);
_environmentServiceMock.Setup(x => x.MachineName)
.Returns("TestMachine");
_publicKeysManagerMock.Setup(x => x.GetMyPublicKeyInfo())
.Returns(publicKeyInfo);
var dummyResult = new CloudSessionResult()
{
CloudSession = new()
{
SessionId = "TestSession"
}
};
CreateCloudSessionParameters capturedParameters = null;
_cloudSessionApiClientMock
.Setup(x => x.CreateCloudSession(It.IsAny<CreateCloudSessionParameters>(), It.IsAny<CancellationToken>()))
.Callback<CreateCloudSessionParameters, CancellationToken>((p, token) => capturedParameters = p)
.ReturnsAsync(dummyResult);
_cloudSessionConnectionServiceMock
.Setup(x => x.InitializeConnection(SessionConnectionStatus.CreatingSession))
.Returns(Task.CompletedTask);
_trustProcessPublicKeysRepositoryMock
.Setup(x => x.Start(It.IsAny<string>()))
.Returns(Task.CompletedTask);
_digitalSignaturesRepositoryMock
.Setup(x => x.Start(It.IsAny<string>()))
.Returns(Task.CompletedTask);
_afterJoinSessionServiceMock
.Setup(x => x.Process(It.IsAny<AfterJoinSessionRequest>()))
.Returns(Task.CompletedTask);
// Act
var result = await _service.CreateCloudSession(request);
// Assert
result.Should().BeEquivalentTo(dummyResult);
_cloudSessionConnectionRepositoryMock.Verify(x => x.SetConnectionStatus(SessionConnectionStatus.InSession), Times.Once);
_trustProcessPublicKeysRepositoryMock.Verify(x => x.Start("TestSession"), Times.Once);
_digitalSignaturesRepositoryMock.Verify(x => x.Start("TestSession"), Times.Once);
_afterJoinSessionServiceMock.Verify(x => x.Process(It.Is<AfterJoinSessionRequest>(req =>
req.CloudSessionResult == dummyResult &&
req.RunCloudSessionProfileInfo == request.RunCloudSessionProfileInfo &&
req.IsCreator == true)), Times.Once);
capturedParameters.Should().NotBeNull();
capturedParameters.LobbyId.Should().Be(request.RunCloudSessionProfileInfo.LobbyId);
capturedParameters.CreatorProfileClientId.Should().Be(request.RunCloudSessionProfileInfo.LocalProfileClientId);
capturedParameters.SessionSettings.Should().Be(encryptedSettingsDefault);
capturedParameters.CreatorPublicKeyInfo.Should().Be(publicKeyInfo);
capturedParameters.CreatorPrivateData.Should().Be(encryptedPrivateDataDefault);
// Vérification du log (utilisation de Verify sur la méthode Log générique)
_loggerMock.Verify(
x => x.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("Created Cloud Session") &&
v.ToString().Contains("TestSession")),
null,
It.IsAny<Func<It.IsAnyType, Exception, string>>()),
Times.Once);
}
[Test]
public async Task CreateCloudSession_WithNullProfileInfo_ShouldUseDefaultSessionSettings()
{
// Arrange
var request = new CreateCloudSessionRequest(null);
var encryptedSettingsDefault = new EncryptedSessionSettings();
var encryptedPrivateDataDefault = new EncryptedSessionMemberPrivateData();
var publicKeyInfo = new PublicKeyInfo();
_dataEncrypterMock.Setup(x => x.EncryptSessionSettings(It.IsAny<SessionSettings>()))
.Returns(encryptedSettingsDefault);
_dataEncrypterMock.Setup(x => x.EncryptSessionMemberPrivateData(It.IsAny<SessionMemberPrivateData>()))
.Returns(encryptedPrivateDataDefault);
_environmentServiceMock.Setup(x => x.MachineName)
.Returns("TestMachine");
_publicKeysManagerMock.Setup(x => x.GetMyPublicKeyInfo())
.Returns(publicKeyInfo);
var dummyResult = new CloudSessionResult()
{
CloudSession = new()
{
SessionId = "TestSession"
}
};
CreateCloudSessionParameters capturedParameters = null;
_cloudSessionApiClientMock
.Setup(x => x.CreateCloudSession(It.IsAny<CreateCloudSessionParameters>(), It.IsAny<CancellationToken>()))
.Callback<CreateCloudSessionParameters, CancellationToken>((p, token) => capturedParameters = p)
.ReturnsAsync(dummyResult);
_cloudSessionConnectionServiceMock
.Setup(x => x.InitializeConnection(SessionConnectionStatus.CreatingSession))
.Returns(Task.CompletedTask);
_trustProcessPublicKeysRepositoryMock
.Setup(x => x.Start(It.IsAny<string>()))
.Returns(Task.CompletedTask);
_digitalSignaturesRepositoryMock
.Setup(x => x.Start(It.IsAny<string>()))
.Returns(Task.CompletedTask);
_afterJoinSessionServiceMock
.Setup(x => x.Process(It.IsAny<AfterJoinSessionRequest>()))
.Returns(Task.CompletedTask);
// Act
var result = await _service.CreateCloudSession(request);
// Assert
result.Should().BeEquivalentTo(dummyResult);
capturedParameters.Should().NotBeNull();
capturedParameters.LobbyId.Should().BeNull();
capturedParameters.CreatorProfileClientId.Should().BeNull();
capturedParameters.SessionSettings.Should().Be(encryptedSettingsDefault);
capturedParameters.CreatorPublicKeyInfo.Should().Be(publicKeyInfo);
capturedParameters.CreatorPrivateData.Should().Be(encryptedPrivateDataDefault);
}
[Test]
public async Task CreateCloudSession_ShouldThrowTaskCanceledException_WhenCancellationRequested()
{
// Arrange
var runCloudSessionProfileInfo = new RunCloudSessionProfileInfo("Lobby1", new CloudSessionProfile(),
new CloudSessionProfileDetails(), LobbySessionModes.RunInventory);
var request = new CreateCloudSessionRequest(runCloudSessionProfileInfo);
// We simulate the cancellation by canceling the CancellationTokenSource
_cts.Cancel();
var dummyResult = new CloudSessionResult()
{
CloudSession = new()
{
SessionId = "TestSession"
}
};
_cloudSessionApiClientMock
.Setup(x => x.CreateCloudSession(It.IsAny<CreateCloudSessionParameters>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(dummyResult);
// Act
await _service.CreateCloudSession(request);
// Assert
_cloudSessionConnectionServiceMock.Verify(x => x.OnCreateSessionError(
It.Is<CreateSessionError>(err =>
err.Exception is TaskCanceledException &&
err.Status == CreateSessionStatus.CanceledByUser)),
Times.Once);
}
[Test]
public async Task CreateCloudSession_ShouldThrowException_WhenCloudSessionApiClientFails()
{
// Arrange
var runCloudSessionProfileInfo = new RunCloudSessionProfileInfo("Lobby1", new CloudSessionProfile(),
new CloudSessionProfileDetails(), LobbySessionModes.RunInventory);
var request = new CreateCloudSessionRequest(runCloudSessionProfileInfo);
var exception = new InvalidOperationException("Test exception");
_cloudSessionApiClientMock
.Setup(x => x.CreateCloudSession(It.IsAny<CreateCloudSessionParameters>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(exception);
// Act
await _service.CreateCloudSession(request);
// Assert
_cloudSessionConnectionServiceMock.Verify(x => x.OnCreateSessionError(
It.Is<CreateSessionError>(err =>
err.Exception == exception &&
err.Status == CreateSessionStatus.Error)),
Times.Once);
}
[Test]
public async Task CancelCreateCloudSession_ShouldCancelToken()
{
// Act
await _service.CancelCreateCloudSession();
// Assert: the CancellationTokenSource must be canceled
_cts.IsCancellationRequested.Should().BeTrue();
_loggerMock.Verify(
x => x.Log(
LogLevel.Information,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("User requested to cancel Cloud Session creation")),
null,
It.IsAny<Func<It.IsAnyType, Exception, string>>()),
Times.Once);
}
}