|
| 1 | +using System.Net; |
| 2 | +using System.Text; |
| 3 | +using ByteSync.Common.Business.Lobbies.Connections; |
| 4 | +using ByteSync.Common.Business.Misc; |
| 5 | +using ByteSync.Common.Business.Profiles; |
| 6 | +using ByteSync.Common.Controls.Json; |
| 7 | +using ByteSync.Functions.Http; |
| 8 | +using ByteSync.Functions.UnitTests.TestHelpers; |
| 9 | +using ByteSync.ServerCommon.Business.Auth; |
| 10 | +using ByteSync.ServerCommon.Interfaces.Services; |
| 11 | +using FluentAssertions; |
| 12 | +using Microsoft.Azure.Functions.Worker; |
| 13 | +using Moq; |
| 14 | + |
| 15 | +namespace ByteSync.Functions.UnitTests.Http; |
| 16 | + |
| 17 | +[TestFixture] |
| 18 | +public class CloudSessionProfileFunctionTests |
| 19 | +{ |
| 20 | + private static FunctionContext BuildFunctionContextWithClient() |
| 21 | + { |
| 22 | + var mockContext = new Mock<FunctionContext>(); |
| 23 | + var items = new Dictionary<object, object>(); |
| 24 | + mockContext.SetupGet(c => c.Items).Returns(items); |
| 25 | + |
| 26 | + var client = new Client("cli", "cliInst", "1.0.0", OSPlatforms.Windows, "127.0.0.1"); |
| 27 | + items[AuthConstants.FUNCTION_CONTEXT_CLIENT] = client; |
| 28 | + |
| 29 | + return mockContext.Object; |
| 30 | + } |
| 31 | + |
| 32 | + private static async Task WriteBodyAsync<T>(FakeHttpRequestData request, T body) |
| 33 | + { |
| 34 | + var json = JsonHelper.Serialize(body); |
| 35 | + var bytes = Encoding.UTF8.GetBytes(json); |
| 36 | + request.Body.SetLength(0); |
| 37 | + await request.Body.WriteAsync(bytes, 0, bytes.Length); |
| 38 | + request.Body.Position = 0; |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public async Task CreateCloudSessionProfile_ForwardsRequest_AndReturnsOk() |
| 43 | + { |
| 44 | + var serviceMock = new Mock<ICloudSessionProfileService>(); |
| 45 | + |
| 46 | + serviceMock |
| 47 | + .Setup(m => m.CreateCloudSessionProfile(It.IsAny<string>(), It.IsAny<Client>())) |
| 48 | + .ReturnsAsync(new CreateCloudSessionProfileResult()); |
| 49 | + |
| 50 | + var function = new CloudSessionProfileFunction(serviceMock.Object); |
| 51 | + var context = BuildFunctionContextWithClient(); |
| 52 | + var request = new FakeHttpRequestData(context); |
| 53 | + |
| 54 | + var sessionId = "S1"; |
| 55 | + await WriteBodyAsync(request, sessionId); |
| 56 | + |
| 57 | + var response = await function.CreateCloudSessionProfile(request, context); |
| 58 | + |
| 59 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 60 | + serviceMock.Verify(m => m.CreateCloudSessionProfile("S1", It.IsAny<Client>()), Times.Once); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public async Task GetCloudSessionProfileData_ForwardsRequest_AndReturnsOk() |
| 65 | + { |
| 66 | + var serviceMock = new Mock<ICloudSessionProfileService>(); |
| 67 | + |
| 68 | + GetCloudSessionProfileDataParameters? captured = null; |
| 69 | + serviceMock |
| 70 | + .Setup(m => m.GetCloudSessionProfileData(It.IsAny<GetCloudSessionProfileDataParameters>(), It.IsAny<Client>())) |
| 71 | + .Callback<GetCloudSessionProfileDataParameters, Client>((p, _) => captured = p) |
| 72 | + .ReturnsAsync(new CloudSessionProfileData()); |
| 73 | + |
| 74 | + var function = new CloudSessionProfileFunction(serviceMock.Object); |
| 75 | + var context = BuildFunctionContextWithClient(); |
| 76 | + var request = new FakeHttpRequestData(context); |
| 77 | + |
| 78 | + var parameters = new GetCloudSessionProfileDataParameters { SessionId = "P1", CloudSessionProfileId = "PC1" }; |
| 79 | + await WriteBodyAsync(request, parameters); |
| 80 | + |
| 81 | + var response = await function.GetCloudSessionProfileData(request, context, "P1"); |
| 82 | + |
| 83 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 84 | + captured.Should().NotBeNull(); |
| 85 | + captured!.CloudSessionProfileId.Should().Be("PC1"); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public async Task GetProfileDetailsPassword_ForwardsRequest_AndReturnsOk() |
| 90 | + { |
| 91 | + var serviceMock = new Mock<ICloudSessionProfileService>(); |
| 92 | + |
| 93 | + GetProfileDetailsPasswordParameters? captured = null; |
| 94 | + serviceMock |
| 95 | + .Setup(m => m.GetProfileDetailsPassword(It.IsAny<GetProfileDetailsPasswordParameters>(), It.IsAny<Client>())) |
| 96 | + .Callback<GetProfileDetailsPasswordParameters, Client>((p, _) => captured = p) |
| 97 | + .ReturnsAsync("password123"); |
| 98 | + |
| 99 | + var function = new CloudSessionProfileFunction(serviceMock.Object); |
| 100 | + var context = BuildFunctionContextWithClient(); |
| 101 | + var request = new FakeHttpRequestData(context); |
| 102 | + |
| 103 | + var parameters = new GetProfileDetailsPasswordParameters { ProfileClientId = "PC1" }; |
| 104 | + await WriteBodyAsync(request, parameters); |
| 105 | + |
| 106 | + var response = await function.GetProfileDetailsPassword(request, context, "P1"); |
| 107 | + |
| 108 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 109 | + captured.Should().NotBeNull(); |
| 110 | + captured!.ProfileClientId.Should().Be("PC1"); |
| 111 | + } |
| 112 | + |
| 113 | + [Test] |
| 114 | + public async Task DeleteCloudSessionProfile_ForwardsRequest_AndReturnsOk() |
| 115 | + { |
| 116 | + var serviceMock = new Mock<ICloudSessionProfileService>(); |
| 117 | + |
| 118 | + DeleteCloudSessionProfileParameters? captured = null; |
| 119 | + serviceMock |
| 120 | + .Setup(m => m.DeleteCloudSessionProfile(It.IsAny<DeleteCloudSessionProfileParameters>(), It.IsAny<Client>())) |
| 121 | + .Callback<DeleteCloudSessionProfileParameters, Client>((p, _) => captured = p) |
| 122 | + .ReturnsAsync(true); |
| 123 | + |
| 124 | + var function = new CloudSessionProfileFunction(serviceMock.Object); |
| 125 | + var context = BuildFunctionContextWithClient(); |
| 126 | + var request = new FakeHttpRequestData(context); |
| 127 | + |
| 128 | + var parameters = new DeleteCloudSessionProfileParameters { ProfileClientId = "PC1" }; |
| 129 | + await WriteBodyAsync(request, parameters); |
| 130 | + |
| 131 | + var response = await function.DeleteCloudSessionProfile(request, context, "P1"); |
| 132 | + |
| 133 | + response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 134 | + captured.Should().NotBeNull(); |
| 135 | + captured!.ProfileClientId.Should().Be("PC1"); |
| 136 | + } |
| 137 | +} |
0 commit comments