Skip to content

Commit

Permalink
SLVS-1432 Support removing credentials from store (#5659)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriela-trutan-sonarsource authored and vnaskos-sonar committed Sep 19, 2024
1 parent dfb3413 commit 0fc33ac
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,16 +29,16 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence
[TestClass]
public class SolutionBindingCredentialsLoaderTests
{
private Mock<ICredentialStoreService> store;
private ICredentialStoreService store;
private Uri mockUri;
private SolutionBindingCredentialsLoader testSubject;

[TestInitialize]
public void Setup()
{
store = new Mock<ICredentialStoreService>();
store = Substitute.For<ICredentialStoreService>();
mockUri = new Uri("http://sonarsource.com");
testSubject = new SolutionBindingCredentialsLoader(store.Object);
testSubject = new SolutionBindingCredentialsLoader(store);
}

[TestMethod]
Expand All @@ -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);
Expand All @@ -72,7 +70,7 @@ public void Load_CredentialsExist_CredentialsWithSecuredString()
{
var credentials = new Credential("user", "password");
store
.Setup(x => x.ReadCredentials(It.Is<TargetUri>(t => t.ActualUri == mockUri)))
.ReadCredentials(Arg.Is<TargetUri>(t => t.ActualUri == mockUri))
.Returns(credentials);

var actual = testSubject.Load(mockUri);
Expand All @@ -86,15 +84,15 @@ public void Save_ServerUriIsNull_CredentialsNotSaved()

testSubject.Save(credentials, null);

store.Verify(x=> x.WriteCredentials(It.IsAny<TargetUri>(), It.IsAny<Credential>()), Times.Never);
store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>());
}

[TestMethod]
public void Save_CredentialsAreNull_CredentialsNotSaved()
{
testSubject.Save(null, mockUri);

store.Verify(x => x.WriteCredentials(It.IsAny<TargetUri>(), It.IsAny<Credential>()), Times.Never);
store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>());
}

[TestMethod]
Expand All @@ -103,7 +101,7 @@ public void Save_CredentialsAreNotBasicAuth_CredentialsNotSaved()
var mockCredentials = new Mock<ICredentials>();
testSubject.Save(mockCredentials.Object, mockUri);

store.Verify(x => x.WriteCredentials(It.IsAny<TargetUri>(), It.IsAny<Credential>()), Times.Never);
store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>());
}

[TestMethod]
Expand All @@ -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<TargetUri>(t => t.ActualUri == mockUri),
It.Is<Credential>(c=> c.Username == "user" && c.Password == "password")),
Times.Once);
store.Received(1)
.WriteCredentials(
Arg.Is<TargetUri>(t => t.ActualUri == mockUri),
Arg.Is<Credential>(c=> c.Username == "user" && c.Password == "password"));
}

[TestMethod]
public void DeleteCredentials_UriNull_DoesNotCallStoreDeleteCredentials()
{
testSubject.DeleteCredentials(null);

store.DidNotReceive().DeleteCredentials(Arg.Any<TargetUri>());
}

[TestMethod]
public void DeleteCredentials_UriProvided_CallsStoreDeleteCredentials()
{
testSubject.DeleteCredentials(mockUri);

store.Received(1).DeleteCredentials(Arg.Any<TargetUri>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 0fc33ac

Please sign in to comment.