Skip to content

Commit

Permalink
SLVS-1738 Rename interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriela-trutan-sonarsource committed Dec 24, 2024
1 parent 5e0bd1e commit ff1ab40
Show file tree
Hide file tree
Showing 23 changed files with 60 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Binding;
public class UnintrusiveBindingControllerTests
{
private static readonly CancellationToken ACancellationToken = CancellationToken.None;
private static readonly BasicAuthCredentials ValidToken = new("TOKEN", new SecureString());
private static readonly UsernameAndPasswordCredentials ValidToken = new("TOKEN", new SecureString());
private static readonly BoundServerProject AnyBoundProject = new("any", "any", new ServerConnection.SonarCloud("any", credentials: ValidToken));
private IActiveSolutionChangedHandler activeSolutionChangedHandler;
private IBindingProcess bindingProcess;
Expand Down Expand Up @@ -86,8 +86,8 @@ await sonarQubeService
.Received()
.ConnectAsync(
Arg.Is<ConnectionInformation>(x => x.ServerUri.Equals("https://sonarcloud.io/")
&& ((BasicAuthCredentials)x.Credentials).UserName.Equals(ValidToken.UserName)
&& string.IsNullOrEmpty(((BasicAuthCredentials)x.Credentials).Password.ToUnsecureString())),
&& ((UsernameAndPasswordCredentials)x.Credentials).UserName.Equals(ValidToken.UserName)
&& string.IsNullOrEmpty(((UsernameAndPasswordCredentials)x.Credentials).Password.ToUnsecureString())),
ACancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private void MockValidBinding(string bindingPath, BoundSonarQubeProject sonarQub

private static BoundSonarQubeProject CreateBoundProject(string url, string projectKey)
{
return new BoundSonarQubeProject(new Uri(url), projectKey, "projectName", credentials: new BasicAuthCredentials("admin", "admin".ToSecureString()));
return new BoundSonarQubeProject(new Uri(url), projectKey, "projectName", credentials: new UsernameAndPasswordCredentials("admin", "admin".ToSecureString()));
}

private static bool IsExpectedServerConnection(ServerConnection serverConnection, BoundSonarQubeProject boundProject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void BoundSonarQubeProject_CreateConnectionInformation_NoCredentials()
public void BoundSonarQubeProject_CreateConnectionInformation_BasicAuthCredentials()
{
// Arrange
var creds = new BasicAuthCredentials("UserName", "password".ToSecureString());
var creds = new UsernameAndPasswordCredentials("UserName", "password".ToSecureString());
var input = new BoundSonarQubeProject(new Uri("http://server"), "ProjectKey", "projectName", creds,
new SonarQubeOrganization("org_key", "org_name"));

Expand All @@ -66,7 +66,7 @@ public void BoundSonarQubeProject_CreateConnectionInformation_BasicAuthCredentia

// Assert
conn.ServerUri.Should().Be(input.ServerUri);
var basicAuth = conn.Credentials as BasicAuthCredentials;
var basicAuth = conn.Credentials as UsernameAndPasswordCredentials;
basicAuth.Should().NotBeNull();
basicAuth.UserName.Should().Be(creds.UserName);
basicAuth.Password.ToUnsecureString().Should().Be(creds.Password.ToUnsecureString());
Expand Down Expand Up @@ -114,15 +114,15 @@ public void BoundServerProject_CreateConnectionInformation_NoCredentials()
public void BoundServerProject_CreateConnectionInformation_BasicAuthCredentials()
{
// Arrange
var creds = new BasicAuthCredentials("UserName", "password".ToSecureString());
var creds = new UsernameAndPasswordCredentials("UserName", "password".ToSecureString());
var input = new BoundServerProject("solution", "ProjectKey", new ServerConnection.SonarCloud("org_key", credentials: creds));

// Act
ConnectionInformation conn = input.CreateConnectionInformation();

// Assert
conn.ServerUri.Should().Be(input.ServerConnection.ServerUri);
var basicAuth = conn.Credentials as BasicAuthCredentials;
var basicAuth = conn.Credentials as UsernameAndPasswordCredentials;
basicAuth.Should().NotBeNull();
basicAuth.UserName.Should().Be(creds.UserName);
basicAuth.Password.ToUnsecureString().Should().Be(creds.Password.ToUnsecureString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void BoundProject_Serialization()
// Arrange
var serverUri = new Uri("https://finding-nemo.org");
var projectKey = "MyProject Key";
var testSubject = new BoundSonarQubeProject(serverUri, projectKey, "projectName", new BasicAuthCredentials("used", "pwd".ToSecureString()));
var testSubject = new BoundSonarQubeProject(serverUri, projectKey, "projectName", new UsernameAndPasswordCredentials("used", "pwd".ToSecureString()));

// Act (serialize + de-serialize)
string data = JsonHelper.Serialize(testSubject);
Expand All @@ -55,7 +55,7 @@ public void BoundProject_BindingJsonModel_Serialization()
// Arrange
var serverUri = new Uri("https://finding-nemo.org");
var projectKey = "MyProject Key";
var testSubject = new BoundSonarQubeProject(serverUri, projectKey, "projectName", new BasicAuthCredentials("used", "pwd".ToSecureString()));
var testSubject = new BoundSonarQubeProject(serverUri, projectKey, "projectName", new UsernameAndPasswordCredentials("used", "pwd".ToSecureString()));

// Act (serialize + de-serialize)
string data = JsonHelper.Serialize(testSubject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public void Load_CredentialsExist_CredentialsWithSecuredString()
.Returns(credentials);

var actual = testSubject.Load(mockUri);
actual.Should().BeEquivalentTo(new BasicAuthCredentials("user", "password".ToSecureString()));
actual.Should().BeEquivalentTo(new UsernameAndPasswordCredentials("user", "password".ToSecureString()));
}

[TestMethod]
public void Save_ServerUriIsNull_CredentialsNotSaved()
{
var credentials = new BasicAuthCredentials("user", "password".ToSecureString());
var credentials = new UsernameAndPasswordCredentials("user", "password".ToSecureString());

testSubject.Save(credentials, null);

Expand All @@ -108,7 +108,7 @@ public void Save_CredentialsAreNotBasicAuth_CredentialsNotSaved()
[TestMethod]
public void Save_CredentialsAreBasicAuth_CredentialsSavedWithUnsecuredString()
{
var credentials = new BasicAuthCredentials("user", "password".ToSecureString());
var credentials = new UsernameAndPasswordCredentials("user", "password".ToSecureString());
testSubject.Save(credentials, mockUri);

store.Received(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SolutionBindingRepositoryTests
private ISolutionBindingCredentialsLoader credentialsLoader;
private TestLogger logger;

private BasicAuthCredentials mockCredentials;
private UsernameAndPasswordCredentials mockCredentials;
private ServerConnection serverConnection;
private IServerConnectionsRepository serverConnectionsRepository;
private ISolutionBindingFileLoader solutionBindingFileLoader;
Expand All @@ -59,7 +59,7 @@ public void TestInitialize()

testSubject = new SolutionBindingRepository(unintrusiveBindingPathProvider, bindingJsonModelConverter, serverConnectionsRepository, solutionBindingFileLoader, credentialsLoader, logger);

mockCredentials = new BasicAuthCredentials("user", "pwd".ToSecureString());
mockCredentials = new UsernameAndPasswordCredentials("user", "pwd".ToSecureString());

serverConnection = new ServerConnection.SonarCloud("org");
boundServerProject = new BoundServerProject("solution.123", "project_123", serverConnection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@
namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence;

[TestClass]
public class BasicAuthCredentialsTests
public class UsernameAndPasswordCredentialsTests
{
private const string Username = "username";
private const string Password = "pwd";

[TestMethod]
public void Ctor_WhenUsernameIsNull_ThrowsArgumentNullException()
{
Action act = () => new BasicAuthCredentials(null, Password.ToSecureString());
Action act = () => new UsernameAndPasswordCredentials(null, Password.ToSecureString());

act.Should().Throw<ArgumentNullException>();
}

[TestMethod]
public void Ctor_WhenPasswordIsNull_ThrowsArgumentNullException()
{
Action act = () => new BasicAuthCredentials(Username, null);
Action act = () => new UsernameAndPasswordCredentials(Username, null);

act.Should().Throw<ArgumentNullException>();
}

[TestMethod]
public void Dispose_DisposesPassword()
{
var testSubject = new BasicAuthCredentials(Username, Password.ToSecureString());
var testSubject = new UsernameAndPasswordCredentials(Username, Password.ToSecureString());

testSubject.Dispose();

Expand All @@ -60,9 +60,9 @@ public void Dispose_DisposesPassword()
public void Clone_ClonesPassword()
{
var password = "pwd";
var testSubject = new BasicAuthCredentials(Username, password.ToSecureString());
var testSubject = new UsernameAndPasswordCredentials(Username, password.ToSecureString());

var clone = (BasicAuthCredentials)testSubject.Clone();
var clone = (UsernameAndPasswordCredentials)testSubject.Clone();

clone.Should().NotBeSameAs(testSubject);
clone.Password.Should().NotBeSameAs(testSubject.Password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ private static Connection CreateSonarQubeConnection(bool enableSmartNotification

private static bool IsExpectedCredentials(IConnectionCredentials credentials, string expectedUsername, string expectedPassword)
{
return credentials is BasicAuthCredentials basicAuthCredentials && basicAuthCredentials.UserName == expectedUsername && basicAuthCredentials.Password?.ToUnsecureString() == expectedPassword;
return credentials is UsernameAndPasswordCredentials basicAuthCredentials && basicAuthCredentials.UserName == expectedUsername && basicAuthCredentials.Password?.ToUnsecureString() == expectedPassword;
}

private void MockTryGet(string connectionId, bool expectedResponse, ServerConnection expectedServerConnection)
Expand Down
6 changes: 3 additions & 3 deletions src/ConnectedMode.UnitTests/SlCoreConnectionAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests;
[TestClass]
public class SlCoreConnectionAdapterTests
{
private static readonly BasicAuthCredentials ValidToken = new ("I_AM_JUST_A_TOKEN", new SecureString());
private static readonly UsernameAndPasswordCredentials ValidToken = new ("I_AM_JUST_A_TOKEN", new SecureString());
private readonly ServerConnection.SonarQube sonarQubeConnection = new(new Uri("http://localhost:9000/"), new ServerConnectionSettings(true), ValidToken);
private readonly ServerConnection.SonarCloud sonarCloudConnection = new("myOrg", new ServerConnectionSettings(true), ValidToken);

Expand Down Expand Up @@ -288,7 +288,7 @@ public async Task GetAllProjectsAsync_ConnectionToSonarQubeWithCredentials_Calls
{
const string username = "username";
const string password = "password";
sonarQubeConnection.Credentials = new BasicAuthCredentials(username, password.CreateSecureString());
sonarQubeConnection.Credentials = new UsernameAndPasswordCredentials(username, password.CreateSecureString());

await testSubject.GetAllProjectsAsync(sonarQubeConnection);

Expand All @@ -310,7 +310,7 @@ public async Task GetAllProjectsAsync_ConnectionToSonarCloudWithCredentials_Call
{
const string username = "username";
const string password = "password";
sonarCloudConnection.Credentials = new BasicAuthCredentials(username, password.CreateSecureString());
sonarCloudConnection.Credentials = new UsernameAndPasswordCredentials(username, password.CreateSecureString());

await testSubject.GetAllProjectsAsync(sonarCloudConnection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ManageBindingViewModelTests
private readonly ServerProject serverProject = new("a-project", "A Project");
private readonly ConnectionInfo sonarQubeConnectionInfo = new("http://localhost:9000", ConnectionServerType.SonarQube);
private readonly ConnectionInfo sonarCloudConnectionInfo = new("organization", ConnectionServerType.SonarCloud);
private readonly BasicAuthCredentials validCredentials = new("TOKEN", new SecureString());
private readonly UsernameAndPasswordCredentials validCredentials = new("TOKEN", new SecureString());
private readonly SharedBindingConfigModel sonarQubeSharedBindingConfigModel = new() { Uri = new Uri("http://localhost:9000"), ProjectKey = "myProj" };
private readonly SharedBindingConfigModel sonarCloudSharedBindingConfigModel = new() { Organization = "myOrg", ProjectKey = "myProj" };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public IConnectionCredentials Load(Uri boundServerUri)

return credentials == null
? null
: new BasicAuthCredentials(credentials.Username, credentials.Password.ToSecureString());
: new UsernameAndPasswordCredentials(credentials.Username, credentials.Password.ToSecureString());
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability",
Expand All @@ -63,7 +63,7 @@ public IConnectionCredentials Load(Uri boundServerUri)
Target = "~M:SonarLint.VisualStudio.Integration.Persistence.FileBindingSerializer.WriteBindingInformation(System.String,SonarLint.VisualStudio.Integration.Persistence.BoundProject)~System.Boolean")]
public void Save(IConnectionCredentials credentials, Uri boundServerUri)
{
if (boundServerUri == null || !(credentials is BasicAuthCredentials basicCredentials))
if (boundServerUri == null || !(credentials is UsernameAndPasswordCredentials basicCredentials))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

namespace SonarLint.VisualStudio.ConnectedMode.Persistence;

internal sealed class BasicAuthCredentials(string userName, SecureString password) : IBasicAuthCredentials
internal sealed class UsernameAndPasswordCredentials(string userName, SecureString password) : IUsernameAndPasswordCredentials
{
public string UserName { get; } = userName ?? throw new ArgumentNullException(nameof(userName));

public SecureString Password { get; } = password ?? throw new ArgumentNullException(nameof(password));

public void Dispose() => Password?.Dispose();

public object Clone() => new BasicAuthCredentials(UserName, Password.CopyAsReadOnly());
public object Clone() => new UsernameAndPasswordCredentials(UserName, Password.CopyAsReadOnly());
}
4 changes: 2 additions & 2 deletions src/ConnectedMode/ServerConnectionsRepositoryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ private static IConnectionCredentials MapCredentials(ICredentialsModel credentia
switch (credentialsModel)
{
case TokenCredentialsModel tokenCredentialsModel:
return new BasicAuthCredentials(tokenCredentialsModel.Token.ToUnsecureString(), new SecureString());
return new UsernameAndPasswordCredentials(tokenCredentialsModel.Token.ToUnsecureString(), new SecureString());
case UsernamePasswordModel usernameCredentialsModel:
return new BasicAuthCredentials(usernameCredentialsModel.Username, usernameCredentialsModel.Password);
return new UsernameAndPasswordCredentials(usernameCredentialsModel.Username, usernameCredentialsModel.Password);
default:
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConnectedMode/SlCoreConnectionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private static Either<TokenDto, UsernamePasswordDto> MapCredentials(IConnectionC
throw new ArgumentException($"Unexpected {nameof(ICredentialsModel)} argument");
}

var basicAuthCredentials = (BasicAuthCredentials) credentials;
var basicAuthCredentials = (UsernameAndPasswordCredentials) credentials;
return basicAuthCredentials.Password?.Length > 0
? GetEitherForUsernamePassword(basicAuthCredentials.UserName, basicAuthCredentials.Password.ToUnsecureString())
: GetEitherForToken(basicAuthCredentials.UserName);
Expand Down
4 changes: 2 additions & 2 deletions src/ConnectedMode/UI/Credentials/ICredentialsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class TokenCredentialsModel(SecureString token) : ICredentialsModel

public IConnectionCredentials ToICredentials()
{
return new BasicAuthCredentials(Token.ToUnsecureString(), new SecureString());
return new UsernameAndPasswordCredentials(Token.ToUnsecureString(), new SecureString());
}
}

Expand All @@ -47,6 +47,6 @@ public class UsernamePasswordModel(string username, SecureString password) : ICr

public IConnectionCredentials ToICredentials()
{
return new BasicAuthCredentials(Username, Password);
return new UsernameAndPasswordCredentials(Username, Password);
}
}
14 changes: 7 additions & 7 deletions src/Integration.UnitTests/Service/ConnectionInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public void ConnectionInformation_WithLoginInformation()
var passwordUnsecure = "admin";
var password = passwordUnsecure.ToSecureString();
var serverUri = new Uri("http://localhost/");
var credentials = new BasicAuthCredentials(userName, password);
var credentials = new UsernameAndPasswordCredentials(userName, password);
var testSubject = new ConnectionInformation(serverUri, credentials);

// Act
password.Dispose(); // Connection information should maintain it's own copy of the password

// Assert
((BasicAuthCredentials)testSubject.Credentials).Password.ToUnsecureString().Should().Be(passwordUnsecure, "Password doesn't match");
((BasicAuthCredentials)testSubject.Credentials).UserName.Should().Be(userName, "UserName doesn't match");
((UsernameAndPasswordCredentials)testSubject.Credentials).Password.ToUnsecureString().Should().Be(passwordUnsecure, "Password doesn't match");
((UsernameAndPasswordCredentials)testSubject.Credentials).UserName.Should().Be(userName, "UserName doesn't match");
testSubject.ServerUri.Should().Be(serverUri, "ServerUri doesn't match");

// Act clone
Expand All @@ -57,11 +57,11 @@ public void ConnectionInformation_WithLoginInformation()
testSubject.Dispose();

// Assert testSubject
Exceptions.Expect<ObjectDisposedException>(() => ((BasicAuthCredentials)testSubject.Credentials).Password.ToUnsecureString());
Exceptions.Expect<ObjectDisposedException>(() => ((UsernameAndPasswordCredentials)testSubject.Credentials).Password.ToUnsecureString());

// Assert testSubject2
((BasicAuthCredentials)testSubject2.Credentials).Password.ToUnsecureString().Should().Be(passwordUnsecure, "Password doesn't match");
((BasicAuthCredentials)testSubject.Credentials).UserName.Should().Be(userName, "UserName doesn't match");
((UsernameAndPasswordCredentials)testSubject2.Credentials).Password.ToUnsecureString().Should().Be(passwordUnsecure, "Password doesn't match");
((UsernameAndPasswordCredentials)testSubject.Credentials).UserName.Should().Be(userName, "UserName doesn't match");
testSubject2.ServerUri.Should().Be(serverUri, "ServerUri doesn't match");
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public void ConnectionInformation_Ctor_FixesSonarCloudUri()
public void ConnectionInformation_Ctor_ArgChecks()
{
Exceptions.Expect<ArgumentNullException>(() => new ConnectionInformation(null));
Exceptions.Expect<ArgumentNullException>(() => new ConnectionInformation(null, new BasicAuthCredentials("user", "pwd".ToSecureString())));
Exceptions.Expect<ArgumentNullException>(() => new ConnectionInformation(null, new UsernameAndPasswordCredentials("user", "pwd".ToSecureString())));
}
}
}
Loading

0 comments on commit ff1ab40

Please sign in to comment.