diff --git a/src/ConnectedMode.UnitTests/Persistence/SolutionBindingCredentialsLoaderTests.cs b/src/ConnectedMode.UnitTests/Persistence/SolutionBindingCredentialsLoaderTests.cs index f7f8a9de1e..d92a4bd945 100644 --- a/src/ConnectedMode.UnitTests/Persistence/SolutionBindingCredentialsLoaderTests.cs +++ b/src/ConnectedMode.UnitTests/Persistence/SolutionBindingCredentialsLoaderTests.cs @@ -18,10 +18,8 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -using System; using Microsoft.Alm.Authentication; using SonarLint.VisualStudio.ConnectedMode.Binding; -using Moq; using SonarLint.VisualStudio.ConnectedMode.Persistence; using SonarLint.VisualStudio.Core.Binding; using SonarQube.Client.Helpers; @@ -31,16 +29,16 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence [TestClass] public class SolutionBindingCredentialsLoaderTests { - private Mock store; + private ICredentialStoreService store; private Uri mockUri; private SolutionBindingCredentialsLoader testSubject; [TestInitialize] public void Setup() { - store = new Mock(); + store = Substitute.For(); mockUri = new Uri("http://sonarsource.com"); - testSubject = new SolutionBindingCredentialsLoader(store.Object); + testSubject = new SolutionBindingCredentialsLoader(store); } [TestMethod] @@ -61,7 +59,7 @@ public void Load_ServerUriIsNull_Null() [TestMethod] public void Load_NoCredentials_Null() { - store.Setup(x => x.ReadCredentials(mockUri)).Returns(null as Credential); + store.ReadCredentials(mockUri).Returns(null as Credential); var actual = testSubject.Load(mockUri); actual.Should().Be(null); @@ -72,7 +70,7 @@ public void Load_CredentialsExist_CredentialsWithSecuredString() { var credentials = new Credential("user", "password"); store - .Setup(x => x.ReadCredentials(It.Is(t => t.ActualUri == mockUri))) + .ReadCredentials(Arg.Is(t => t.ActualUri == mockUri)) .Returns(credentials); var actual = testSubject.Load(mockUri); @@ -86,7 +84,7 @@ public void Save_ServerUriIsNull_CredentialsNotSaved() testSubject.Save(credentials, null); - store.Verify(x=> x.WriteCredentials(It.IsAny(), It.IsAny()), Times.Never); + store.DidNotReceive().WriteCredentials(Arg.Any(), Arg.Any()); } [TestMethod] @@ -94,7 +92,7 @@ public void Save_CredentialsAreNull_CredentialsNotSaved() { testSubject.Save(null, mockUri); - store.Verify(x => x.WriteCredentials(It.IsAny(), It.IsAny()), Times.Never); + store.DidNotReceive().WriteCredentials(Arg.Any(), Arg.Any()); } [TestMethod] @@ -103,7 +101,7 @@ public void Save_CredentialsAreNotBasicAuth_CredentialsNotSaved() var mockCredentials = new Mock(); testSubject.Save(mockCredentials.Object, mockUri); - store.Verify(x => x.WriteCredentials(It.IsAny(), It.IsAny()), Times.Never); + store.DidNotReceive().WriteCredentials(Arg.Any(), Arg.Any()); } [TestMethod] @@ -112,11 +110,26 @@ public void Save_CredentialsAreBasicAuth_CredentialsSavedWithUnsecuredString() var credentials = new BasicAuthCredentials("user", "password".ToSecureString()); testSubject.Save(credentials, mockUri); - store.Verify(x => - x.WriteCredentials( - It.Is(t => t.ActualUri == mockUri), - It.Is(c=> c.Username == "user" && c.Password == "password")), - Times.Once); + store.Received(1) + .WriteCredentials( + Arg.Is(t => t.ActualUri == mockUri), + Arg.Is(c=> c.Username == "user" && c.Password == "password")); + } + + [TestMethod] + public void DeleteCredentials_UriNull_DoesNotCallStoreDeleteCredentials() + { + testSubject.DeleteCredentials(null); + + store.DidNotReceive().DeleteCredentials(Arg.Any()); + } + + [TestMethod] + public void DeleteCredentials_UriProvided_CallsStoreDeleteCredentials() + { + testSubject.DeleteCredentials(mockUri); + + store.Received(1).DeleteCredentials(Arg.Any()); } } } diff --git a/src/ConnectedMode/Persistence/ISolutionBindingCredentialsLoader.cs b/src/ConnectedMode/Persistence/ISolutionBindingCredentialsLoader.cs index bbbdc79d0c..d3bfb0c31b 100644 --- a/src/ConnectedMode/Persistence/ISolutionBindingCredentialsLoader.cs +++ b/src/ConnectedMode/Persistence/ISolutionBindingCredentialsLoader.cs @@ -25,6 +25,7 @@ namespace SonarLint.VisualStudio.ConnectedMode.Persistence { interface ISolutionBindingCredentialsLoader { + void DeleteCredentials(Uri boundServerUri); ICredentials Load(Uri boundServerUri); void Save(ICredentials credentials, Uri boundServerUri); } diff --git a/src/ConnectedMode/Persistence/SolutionBindingCredentialsLoader.cs b/src/ConnectedMode/Persistence/SolutionBindingCredentialsLoader.cs index 74899b32f6..6232873efd 100644 --- a/src/ConnectedMode/Persistence/SolutionBindingCredentialsLoader.cs +++ b/src/ConnectedMode/Persistence/SolutionBindingCredentialsLoader.cs @@ -18,8 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -using System; -using System.Diagnostics; using Microsoft.Alm.Authentication; using SonarLint.VisualStudio.ConnectedMode.Binding; using SonarLint.VisualStudio.Core.Binding; @@ -36,6 +34,15 @@ public SolutionBindingCredentialsLoader(ICredentialStoreService store) this.store = store ?? throw new ArgumentNullException(nameof(store)); } + public void DeleteCredentials(Uri boundServerUri) + { + if(boundServerUri == null) + { + return; + } + store.DeleteCredentials(boundServerUri); + } + public ICredentials Load(Uri boundServerUri) { if (boundServerUri == null)