Skip to content

Commit

Permalink
SLVS-1465 Use shared binding: show message when credentials do not ex…
Browse files Browse the repository at this point in the history
…ist (#5704)
  • Loading branch information
gabriela-trutan-sonarsource committed Sep 27, 2024
1 parent 2918f18 commit b920961
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,43 @@ await bindingController.DidNotReceive()
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
}

[TestMethod]
public async Task UseSharedBindingAsync_SharedBindingSonarCloudConnectionWithMissingCredentials_ReturnsFalseAndLogsAndInformsUser()
{
testSubject.SharedBindingConfigModel = sonarCloudSharedBindingConfigModel;
var expectedServerConnection = new ServerConnection.SonarCloud(testSubject.SharedBindingConfigModel.Organization);
SetupBoundProject(expectedServerConnection);
expectedServerConnection.Credentials = null;

var response = await testSubject.UseSharedBindingAsync();

response.Success.Should().BeFalse();
logger.WriteLine(Resources.UseSharedBinding_CredentiasNotFound, testSubject.SharedBindingConfigModel.Organization);
messageBox.Received(1).Show(UiResources.NotFoundCredentialsForSharedBindingMessageBoxText, UiResources.NotFoundCredentialsForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
await bindingController.DidNotReceive()
.BindAsync(Arg.Is<BoundServerProject>(proj =>
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
}


[TestMethod]
public async Task UseSharedBindingAsync_SharedBindingSonarQubeConnectionWithMissingCredentials_ReturnsFalseAndLogsAndInformsUser()
{
testSubject.SharedBindingConfigModel = sonarQubeSharedBindingConfigModel;
var expectedServerConnection = new ServerConnection.SonarQube(testSubject.SharedBindingConfigModel.Uri);
SetupBoundProject(expectedServerConnection);
expectedServerConnection.Credentials = null;

var response = await testSubject.UseSharedBindingAsync();

response.Success.Should().BeFalse();
logger.WriteLine(Resources.UseSharedBinding_CredentiasNotFound, testSubject.SharedBindingConfigModel.Uri);
messageBox.Received(1).Show(UiResources.NotFoundCredentialsForSharedBindingMessageBoxText, UiResources.NotFoundCredentialsForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
await bindingController.DidNotReceive()
.BindAsync(Arg.Is<BoundServerProject>(proj =>
proj.ServerProjectKey == testSubject.SharedBindingConfigModel.ProjectKey), Arg.Any<CancellationToken>());
}

[TestMethod]
public async Task UseSharedBindingAsync_BindingFails_ReturnsFalse()
{
Expand All @@ -866,6 +903,7 @@ public async Task UseSharedBindingAsync_BindingFails_ReturnsFalse()
response.Success.Should().BeFalse();
}


private void MockServices()
{
serverConnectionsRepositoryAdapter = Substitute.For<IServerConnectionsRepositoryAdapter>();
Expand Down Expand Up @@ -908,6 +946,7 @@ private void SetupBoundProject(ServerConnection serverConnection, ServerProject
{
expectedServerProject ??= serverProject;

serverConnection.Credentials = validCredentials;
var boundServerProject = new BoundServerProject(ALocalProjectKey, expectedServerProject.Key, serverConnection);
var configurationProvider = Substitute.For<IConfigurationProvider>();
configurationProvider.GetConfiguration().Returns(new BindingConfiguration(boundServerProject, SonarLintMode.Connected, "binding-dir"));
Expand Down
10 changes: 10 additions & 0 deletions src/ConnectedMode/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/ConnectedMode/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,7 @@
<data name="UnexpectedConnectionType" xml:space="preserve">
<value>Unexpected server connection type</value>
</data>
<data name="UseSharedBinding_CredentiasNotFound" xml:space="preserve">
<value>[ConnectedMode/UseSharedBinding] The credentials for the connection {0} could not be found</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSharedBindingConfigurationDetected}" Value="False">
<DataTrigger Binding="{Binding Path=IsUseSharedBindingButtonVisible}" Value="False">
<Setter Property="ToolTip" Value="{x:Static res:UiResources.SharedBindingConfigurationTooltip}"/>
</DataTrigger>
</Style.Triggers>
Expand Down
51 changes: 39 additions & 12 deletions src/ConnectedMode/UI/ManageBinding/ManageBindingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,25 @@ public async Task ExportBindingConfigurationAsync()
}
}

public void Dispose()
{
cancellationTokenSource?.Dispose();
}

internal void DetectSharedBinding()
{
if (IsCurrentProjectBound)
{
return;
}
SharedBindingConfigModel = connectedModeBindingServices.SharedBindingConfigProvider.GetSharedBinding();
}

internal async Task<AdapterResponse> UseSharedBindingAsync()
{
var connection = SharedBindingConfigModel.IsSonarCloud()
? new ConnectionInfo(SharedBindingConfigModel.Organization, ConnectionServerType.SonarCloud)
: new ConnectionInfo(SharedBindingConfigModel.Uri.ToString(), ConnectionServerType.SonarQube);
if (!connectedModeServices.ServerConnectionsRepositoryAdapter.TryGet(connection, out var serverConnection))
var connectionInfo = CreteConnectionInfoFromSharedBinding();
if (!ConnectionExists(connectionInfo, out var serverConnection) || !CredentialsExists(connectionInfo, serverConnection))
{
connectedModeServices.Logger.WriteLine(ConnectedMode.Resources.UseSharedBinding_ConnectionNotFound, connection.Id);
connectedModeServices.MessageBox.Show(UiResources.NotFoundConnectionForSharedBindingMessageBoxText, UiResources.NotFoundConnectionForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
return new AdapterResponse(false);
}

Expand Down Expand Up @@ -286,17 +296,34 @@ private async Task<AdapterResponse> BindAsync(ServerConnection serverConnection,
}
}

internal void DetectSharedBinding()
private bool ConnectionExists(ConnectionInfo connectionInfo, out ServerConnection serverConnection)
{
if (IsCurrentProjectBound)
if (connectedModeServices.ServerConnectionsRepositoryAdapter.TryGet(connectionInfo, out serverConnection))
{
return;
return true;
}
SharedBindingConfigModel = connectedModeBindingServices.SharedBindingConfigProvider.GetSharedBinding();

connectedModeServices.Logger.WriteLine(ConnectedMode.Resources.UseSharedBinding_ConnectionNotFound, connectionInfo.Id);
connectedModeServices.MessageBox.Show(UiResources.NotFoundConnectionForSharedBindingMessageBoxText, UiResources.NotFoundConnectionForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
return false;
}

public void Dispose()
private bool CredentialsExists(ConnectionInfo connectionInfo, ServerConnection serverConnection)
{
cancellationTokenSource?.Dispose();
if (serverConnection.Credentials != null)
{
return true;
}
connectedModeServices.Logger.WriteLine(ConnectedMode.Resources.UseSharedBinding_CredentiasNotFound, connectionInfo.Id);
connectedModeServices.MessageBox.Show(UiResources.NotFoundCredentialsForSharedBindingMessageBoxText, UiResources.NotFoundCredentialsForSharedBindingMessageBoxCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
return false;

}

private ConnectionInfo CreteConnectionInfoFromSharedBinding()
{
return SharedBindingConfigModel.IsSonarCloud()
? new ConnectionInfo(SharedBindingConfigModel.Organization, ConnectionServerType.SonarCloud)
: new ConnectionInfo(SharedBindingConfigModel.Uri.ToString(), ConnectionServerType.SonarQube);
}
}
20 changes: 20 additions & 0 deletions src/ConnectedMode/UI/Resources/UiResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/ConnectedMode/UI/Resources/UiResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,11 @@ Please manually add the connection through "Manage Connections" and then try aga
<data name="CalculatingConnectionReferencesText" xml:space="preserve">
<value>Calculating connection references...</value>
</data>
<data name="CalculatingConnectionReferencesFailedText" xml:space="preserve">
<value>Calculating the connection references failed</value>
<data name="NotFoundCredentialsForSharedBindingMessageBoxCaption" xml:space="preserve">
<value>Credentials not found</value>
</data>
<data name="CalculatingConnectionReferencesText" xml:space="preserve">
<value>Calculating connection references...</value>
<data name="NotFoundCredentialsForSharedBindingMessageBoxText" xml:space="preserve">
<value>The shared binding could not be executed, because the credentials for the connection to the server could not be found.
Please manually add the credentials for the connection and then try again.</value>
</data>
</root>

0 comments on commit b920961

Please sign in to comment.