|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using FlowCtl.Commands.Plugins.Install; |
| 6 | +using FlowCtl.Core.Services.Authentication; |
| 7 | +using FlowCtl.Core.Services.Logger; |
| 8 | +using FlowSynx.Client; |
| 9 | +using FlowSynx.Client.Authentication; |
| 10 | +using FlowSynx.Client.Messages.Requests.Plugins; |
| 11 | +using FlowSynx.Client.Messages.Responses; |
| 12 | +using FlowSynx.Client.Services; |
| 13 | +using Moq; |
| 14 | + |
| 15 | +namespace FlowCtl.UnitTests.Commands.Plugins; |
| 16 | + |
| 17 | +public class InstallPluginCommandOptionsHandlerTests |
| 18 | +{ |
| 19 | + [Fact] |
| 20 | + public async Task HandleAsync_DefaultsToLatestVersion_WhenVersionIsMissing() |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + var loggerMock = new Mock<IFlowCtlLogger>(); |
| 24 | + var pluginsServiceMock = new Mock<IPluginsService>(); |
| 25 | + var flowSynxClientMock = new Mock<IFlowSynxClient>(); |
| 26 | + var authenticationManagerMock = CreateAuthenticationManagerMock(); |
| 27 | + |
| 28 | + flowSynxClientMock.Setup(client => client.Plugins) |
| 29 | + .Returns(pluginsServiceMock.Object); |
| 30 | + flowSynxClientMock.Setup(client => client.SetAuthenticationStrategy(It.IsAny<IAuthenticationStrategy>())); |
| 31 | + |
| 32 | + pluginsServiceMock.Setup(service => service.InstallAsync( |
| 33 | + It.Is<InstallPluginRequest>(request => request.Version == "latest"), |
| 34 | + It.IsAny<CancellationToken>())) |
| 35 | + .ReturnsAsync(CreateSuccessfulInstallResult()); |
| 36 | + |
| 37 | + var handler = new InstallPluginCommandOptionsHandler( |
| 38 | + loggerMock.Object, |
| 39 | + flowSynxClientMock.Object, |
| 40 | + authenticationManagerMock.Object); |
| 41 | + |
| 42 | + var options = new InstallPluginCommandOptions |
| 43 | + { |
| 44 | + Type = "email-sender", |
| 45 | + Version = null |
| 46 | + }; |
| 47 | + |
| 48 | + // Act |
| 49 | + await handler.HandleAsync(options, CancellationToken.None); |
| 50 | + |
| 51 | + // Assert |
| 52 | + pluginsServiceMock.Verify(service => service.InstallAsync( |
| 53 | + It.Is<InstallPluginRequest>(request => request.Version == "latest"), |
| 54 | + It.IsAny<CancellationToken>()), Times.Once); |
| 55 | + |
| 56 | + loggerMock.Verify(logger => logger.Write( |
| 57 | + It.Is<string>(message => message.Contains("latest", StringComparison.OrdinalIgnoreCase))), |
| 58 | + Times.Once); |
| 59 | + } |
| 60 | + |
| 61 | + private static Mock<IAuthenticationManager> CreateAuthenticationManagerMock() |
| 62 | + { |
| 63 | + var mock = new Mock<IAuthenticationManager>(); |
| 64 | + mock.SetupGet(manager => manager.IsLoggedIn).Returns(true); |
| 65 | + mock.SetupGet(manager => manager.IsBasicAuthenticationUsed).Returns(false); |
| 66 | + mock.Setup(manager => manager.GetData()) |
| 67 | + .Returns(new AuthenticationData { AccessToken = "token" }); |
| 68 | + return mock; |
| 69 | + } |
| 70 | + |
| 71 | + private static HttpResult<Result<Unit>> CreateSuccessfulInstallResult() => |
| 72 | + new() |
| 73 | + { |
| 74 | + StatusCode = 200, |
| 75 | + Payload = new Result<Unit> |
| 76 | + { |
| 77 | + Succeeded = true, |
| 78 | + Data = new Unit(), |
| 79 | + Messages = new List<string>() |
| 80 | + } |
| 81 | + }; |
| 82 | +} |
0 commit comments