public async Task GetPluginAsync_ThrowsIfCancelled() { var resource = new PluginResource( Enumerable.Empty <PluginCreationResult>(), _packageSource, Mock.Of <ICredentialService>()); await Assert.ThrowsAsync <OperationCanceledException>( () => resource.GetPluginAsync(OperationClaim.DownloadPackage, new CancellationToken(canceled: true))); }
public async Task GetPluginAsync_ReturnsNullIfNoPlugins() { var resource = new PluginResource( Enumerable.Empty <PluginCreationResult>(), _packageSource, Mock.Of <ICredentialService>()); var result = await resource.GetPluginAsync(OperationClaim.DownloadPackage, CancellationToken.None); Assert.Null(result); }
public async Task GetPluginAsync_ReturnsNullIfNoPluginWithMatchingOperationClaim() { var resource = new PluginResource( new[] { new PluginCreationResult( Mock.Of <IPlugin>(), Mock.Of <IPluginMulticlientUtilities>(), new[] { OperationClaim.DownloadPackage }) }, _packageSource, Mock.Of <ICredentialService>()); var result = await resource.GetPluginAsync((OperationClaim)int.MaxValue, CancellationToken.None); Assert.Null(result); }
public async Task GetPluginAsync_ReturnsFirstPluginWithMatchingOperationClaim() { var plugin = new Mock <IPlugin>(MockBehavior.Strict); var utilities = new Mock <IPluginMulticlientUtilities>(MockBehavior.Strict); var connection = new Mock <IConnection>(MockBehavior.Strict); connection.Setup(x => x.SendRequestAndReceiveResponseAsync <SetCredentialsRequest, SetCredentialsResponse>( It.Is <MessageMethod>(m => m == MessageMethod.SetCredentials), It.Is <SetCredentialsRequest>(s => s.PackageSourceRepository == _packageSource.Source && s.ProxyUsername == null && s.ProxyPassword == null && s.Username == null && s.Password == null), It.IsAny <CancellationToken>())) .ReturnsAsync(new SetCredentialsResponse(MessageResponseCode.Success)); plugin.SetupGet(x => x.Connection) .Returns(connection.Object); utilities.Setup(x => x.DoOncePerPluginLifetimeAsync( It.IsNotNull <string>(), It.IsNotNull <Func <Task> >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(0)); var pluginCreationResults = new List <PluginCreationResult>() { new PluginCreationResult( plugin.Object, utilities.Object, new[] { OperationClaim.DownloadPackage }), new PluginCreationResult( Mock.Of <IPlugin>(), Mock.Of <IPluginMulticlientUtilities>(), new[] { OperationClaim.DownloadPackage }) }; var resource = new PluginResource( pluginCreationResults, _packageSource, credentialService: null); var result = await resource.GetPluginAsync(OperationClaim.DownloadPackage, CancellationToken.None); Assert.Same(pluginCreationResults[0].Plugin, result.Plugin); Assert.Same(pluginCreationResults[0].PluginMulticlientUtilities, result.PluginMulticlientUtilities); connection.Verify(); plugin.Verify(); utilities.Verify(); }
public async Task GetPluginAsync_ThrowsIfFirstPluginDiscoveryFailed() { var pluginCreationResults = new List <PluginCreationResult>() { new PluginCreationResult(message: "test"), new PluginCreationResult( Mock.Of <IPlugin>(), Mock.Of <IPluginMulticlientUtilities>(), new[] { OperationClaim.DownloadPackage }) }; var resource = new PluginResource( pluginCreationResults, _packageSource, Mock.Of <ICredentialService>()); var exception = await Assert.ThrowsAsync <PluginException>( () => resource.GetPluginAsync(OperationClaim.DownloadPackage, CancellationToken.None)); Assert.Equal("test", exception.Message); }
public async Task GetPluginAsync_SendsLastKnownGoodCredentialsFromCredentialsCache() { var plugin = new Mock <IPlugin>(MockBehavior.Strict); var utilities = new Mock <IPluginMulticlientUtilities>(MockBehavior.Strict); var connection = new Mock <IConnection>(MockBehavior.Strict); var credentialService = new Mock <ICredentialService>(MockBehavior.Strict); var proxyCredentials = new NetworkCredential(userName: "******", password: "******"); ICredentials proxyCredentialsOutResult = proxyCredentials; var packageSourceCredentials = new NetworkCredential(userName: "******", password: "******"); ICredentials packageSourceCredentialsOutResult = packageSourceCredentials; credentialService.Setup(x => x.TryGetLastKnownGoodCredentialsFromCache( It.Is <Uri>(u => u == _packageSource.SourceUri), It.Is <bool>(i => i), out proxyCredentialsOutResult)) .Returns(true); credentialService.Setup(x => x.TryGetLastKnownGoodCredentialsFromCache( It.Is <Uri>(u => u == _packageSource.SourceUri), It.Is <bool>(i => !i), out packageSourceCredentialsOutResult)) .Returns(true); connection.Setup(x => x.SendRequestAndReceiveResponseAsync <SetCredentialsRequest, SetCredentialsResponse>( It.Is <MessageMethod>(m => m == MessageMethod.SetCredentials), It.Is <SetCredentialsRequest>(s => s.PackageSourceRepository == _packageSource.Source && s.ProxyUsername == proxyCredentials.UserName && s.ProxyPassword == proxyCredentials.Password && s.Username == packageSourceCredentials.UserName && s.Password == packageSourceCredentials.Password), It.IsAny <CancellationToken>())) .ReturnsAsync(new SetCredentialsResponse(MessageResponseCode.Success)); plugin.SetupGet(x => x.Connection) .Returns(connection.Object); utilities.Setup(x => x.DoOncePerPluginLifetimeAsync( It.IsNotNull <string>(), It.IsNotNull <Func <Task> >(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(0)); var pluginCreationResults = new List <PluginCreationResult>() { new PluginCreationResult( plugin.Object, utilities.Object, new[] { OperationClaim.DownloadPackage }), }; var resource = new PluginResource( pluginCreationResults, _packageSource, credentialService.Object); var result = await resource.GetPluginAsync(OperationClaim.DownloadPackage, CancellationToken.None); Assert.Same(pluginCreationResults[0].Plugin, result.Plugin); Assert.Same(pluginCreationResults[0].PluginMulticlientUtilities, result.PluginMulticlientUtilities); connection.Verify(); plugin.Verify(); utilities.Verify(); credentialService.Verify(); }