-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SLCore credentials provider implementation
- Loading branch information
1 parent
ee90d47
commit 3fe9eba
Showing
9 changed files
with
334 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
src/SLCore.Listeners.UnitTests/CredentialsListenerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Alm.Authentication; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using SonarLint.VisualStudio.ConnectedMode.Binding; | ||
using SonarLint.VisualStudio.SLCore.Common; | ||
using SonarLint.VisualStudio.SLCore.Common.Models; | ||
using SonarLint.VisualStudio.SLCore.Core; | ||
using SonarLint.VisualStudio.SLCore.Listener.Credentials; | ||
using SonarLint.VisualStudio.SLCore.Listeners.Implementation; | ||
using SonarLint.VisualStudio.TestInfrastructure; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.Listeners.UnitTests; | ||
|
||
[TestClass] | ||
public class CredentialsListenerTests | ||
{ | ||
private static readonly Uri SonarQubeUri = new("https://next.sonarqube.com"); | ||
private static readonly Uri SonarCloudUri = new("https://sonarcloud.io"); | ||
private static readonly string SonarQubeConnectionId = ConnectionIdPrefix.SonarQubePrefix + SonarQubeUri; | ||
private const string SonarCloudConnectionId = ConnectionIdPrefix.SonarCloudPrefix + "myorganization"; | ||
|
||
[TestMethod] | ||
public void MefCtor_CheckIsExported() | ||
{ | ||
MefTestHelpers.CheckTypeCanBeImported<CredentialsListener, ISLCoreListener>( | ||
MefTestHelpers.CreateExport<ICredentialStoreService>()); | ||
} | ||
|
||
[TestMethod] | ||
public void MefCtor_CheckIsSingleton() | ||
{ | ||
MefTestHelpers.CheckIsSingletonMefComponent<CredentialsListener>(); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_NullConnectionId_ReturnsNoCredentials() | ||
{ | ||
var testSubject = CreateTestSubject(out _); | ||
|
||
var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(null)); | ||
|
||
response.Should().BeSameAs(GetCredentialsResponse.NoCredentials); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_NullParams_ReturnsNoCredentials() | ||
{ | ||
var testSubject = CreateTestSubject(out _); | ||
|
||
var response = await testSubject.GetCredentialsAsync(null); | ||
|
||
response.Should().BeSameAs(GetCredentialsResponse.NoCredentials); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_SonarQubeCredentialsNotFound_ReturnsNoCredentials() | ||
{ | ||
var testSubject = CreateTestSubject(out var credentialStoreMock); | ||
credentialStoreMock.Setup(x => x.ReadCredentials(It.Is((TargetUri uri) => UriEquals(uri, SonarQubeUri)))).Returns((Credential)null); | ||
|
||
var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(SonarQubeConnectionId)); | ||
|
||
response.Should().BeSameAs(GetCredentialsResponse.NoCredentials); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_SonarCloudCredentialsNotFound_ReturnsNoCredentials() | ||
{ | ||
var testSubject = CreateTestSubject(out var credentialStoreMock); | ||
credentialStoreMock.Setup(x => x.ReadCredentials(It.Is((TargetUri uri) => UriEquals(uri, SonarCloudUri)))).Returns((Credential)null); | ||
|
||
var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(SonarQubeConnectionId)); | ||
|
||
response.Should().BeSameAs(GetCredentialsResponse.NoCredentials); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_SonarQubeUsernameAndPasswordFound_ReturnsUsernameAndPassword() | ||
{ | ||
const string username = "user1"; | ||
const string password = "password123"; | ||
|
||
var testSubject = CreateTestSubject(out var credentialStoreMock); | ||
credentialStoreMock.Setup(x => x.ReadCredentials(It.Is((TargetUri uri) => UriEquals(uri, SonarQubeUri)))).Returns(new Credential(username, password)); | ||
|
||
var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(SonarQubeConnectionId)); | ||
|
||
response.Should().BeEquivalentTo(new GetCredentialsResponse(new UsernamePasswordDto(username, password))); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_SonarQubeTokenFound_ReturnsToken() | ||
{ | ||
const string token = "token123"; | ||
|
||
var testSubject = CreateTestSubject(out var credentialStoreMock); | ||
credentialStoreMock.Setup(x => x.ReadCredentials(It.Is((TargetUri uri) => UriEquals(uri, SonarQubeUri)))).Returns(new Credential(token)); | ||
|
||
var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(SonarQubeConnectionId)); | ||
|
||
response.Should().BeEquivalentTo(new GetCredentialsResponse(new TokenDto(token))); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_SonarCloudUsernameAndPasswordFound_ReturnsUsernameAndPassword() | ||
{ | ||
const string username = "user1"; | ||
const string password = "password123"; | ||
|
||
var testSubject = CreateTestSubject(out var credentialStoreMock); | ||
credentialStoreMock.Setup(x => x.ReadCredentials(It.Is((TargetUri uri) => UriEquals(uri, SonarCloudUri)))).Returns(new Credential(username, password)); | ||
|
||
var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(SonarCloudConnectionId)); | ||
|
||
response.Should().BeEquivalentTo(new GetCredentialsResponse(new UsernamePasswordDto(username, password))); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetCredentialsAsync_SonarCloudTokenFound_ReturnsToken() | ||
{ | ||
const string token = "token123"; | ||
|
||
var testSubject = CreateTestSubject(out var credentialStoreMock); | ||
credentialStoreMock.Setup(x => x.ReadCredentials(It.Is((TargetUri uri) => UriEquals(uri, SonarCloudUri)))).Returns(new Credential(token)); | ||
|
||
var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams(SonarCloudConnectionId)); | ||
|
||
response.Should().BeEquivalentTo(new GetCredentialsResponse(new TokenDto(token))); | ||
} | ||
|
||
private CredentialsListener CreateTestSubject(out Mock<ICredentialStoreService> credentialStoreMock) | ||
{ | ||
credentialStoreMock = new Mock<ICredentialStoreService>(); | ||
|
||
return new CredentialsListener(credentialStoreMock.Object); | ||
} | ||
|
||
private bool UriEquals(TargetUri uri, Uri serverUri) => serverUri.Equals((Uri)uri); | ||
} |
92 changes: 92 additions & 0 deletions
92
src/SLCore.Listeners/Implementation/ICredentialsListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System; | ||
using System.ComponentModel.Composition; | ||
using System.Threading.Tasks; | ||
using SonarLint.VisualStudio.ConnectedMode.Binding; | ||
using SonarLint.VisualStudio.SLCore.Common; | ||
using SonarLint.VisualStudio.SLCore.Common.Models; | ||
using SonarLint.VisualStudio.SLCore.Core; | ||
using SonarLint.VisualStudio.SLCore.Listener.Credentials; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.Listeners.Implementation | ||
{ | ||
/// <summary> | ||
/// Credentials provider for SLCore | ||
/// </summary> | ||
[Export(typeof(ISLCoreListener))] | ||
[PartCreationPolicy(CreationPolicy.Shared)] | ||
internal class CredentialsListener : ICredentialsListener | ||
{ | ||
private static readonly Uri SonarCloudUri = new Uri("https://sonarcloud.io"); | ||
private readonly ICredentialStoreService credentialStore; | ||
|
||
[ImportingConstructor] | ||
public CredentialsListener(ICredentialStoreService credentialStore) | ||
{ | ||
this.credentialStore = credentialStore; | ||
} | ||
|
||
public Task<GetCredentialsResponse> GetCredentialsAsync(GetCredentialsParams parameters) | ||
{ | ||
var serverUri = GetServerUriFromConnectionId(parameters?.connectionId); | ||
|
||
if (serverUri == null) | ||
{ | ||
return Task.FromResult(GetCredentialsResponse.NoCredentials); | ||
} | ||
|
||
var credentials = credentialStore.ReadCredentials(serverUri); | ||
|
||
if (credentials == null) | ||
{ | ||
return Task.FromResult(GetCredentialsResponse.NoCredentials); | ||
} | ||
|
||
return Task.FromResult(string.IsNullOrEmpty(credentials.Password) | ||
? new GetCredentialsResponse(new TokenDto(credentials.Username)) | ||
: new GetCredentialsResponse(new UsernamePasswordDto(credentials.Username, credentials.Password))); | ||
} | ||
|
||
private static Uri GetServerUriFromConnectionId(string connectionId) | ||
{ | ||
if (connectionId == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (connectionId.StartsWith(ConnectionIdPrefix.SonarCloudPrefix)) | ||
{ | ||
return SonarCloudUri; | ||
} | ||
|
||
if (connectionId.StartsWith(ConnectionIdPrefix.SonarQubePrefix)) | ||
{ | ||
return Uri.TryCreate(connectionId.Substring(ConnectionIdPrefix.SonarQubePrefix.Length), UriKind.Absolute, | ||
out var uri) | ||
? uri | ||
: null; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Runtime.CompilerServices; | ||
|
||
#if SignAssembly | ||
[assembly: InternalsVisibleTo("SonarLint.VisualStudio.SLCore.UnitTests,PublicKey=002400000480000094000000060200000024000052534131000400000100010081b4345a022cc0f4b42bdc795a5a7a1623c1e58dc2246645d751ad41ba98f2749dc5c4e0da3a9e09febcb2cd5b088a0f041f8ac24b20e736d8ae523061733782f9c4cd75b44f17a63714aced0b29a59cd1ce58d8e10ccdb6012c7098c39871043b7241ac4ab9f6b34f183db716082cd57c1ff648135bece256357ba735e67dc6")] | ||
[assembly: InternalsVisibleTo("SonarLint.VisualStudio.SLCore.Listeners.UnitTests,PublicKey=002400000480000094000000060200000024000052534131000400000100010081b4345a022cc0f4b42bdc795a5a7a1623c1e58dc2246645d751ad41ba98f2749dc5c4e0da3a9e09febcb2cd5b088a0f041f8ac24b20e736d8ae523061733782f9c4cd75b44f17a63714aced0b29a59cd1ce58d8e10ccdb6012c7098c39871043b7241ac4ab9f6b34f183db716082cd57c1ff648135bece256357ba735e67dc6")] | ||
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] | ||
#else | ||
|
||
[assembly: InternalsVisibleTo("SonarLint.VisualStudio.SLCore.UnitTests")] | ||
[assembly: InternalsVisibleTo("SonarLint.VisualStudio.SLCore.Listeners.UnitTests")] | ||
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] | ||
#endif |
58 changes: 0 additions & 58 deletions
58
src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.