diff --git a/src/SLCore.UnitTests/Common/TokenDtoTests.cs b/src/SLCore.UnitTests/Common/TokenDtoTests.cs new file mode 100644 index 0000000000..ca97a3e483 --- /dev/null +++ b/src/SLCore.UnitTests/Common/TokenDtoTests.cs @@ -0,0 +1,46 @@ +/* + * 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 SonarLint.VisualStudio.SLCore.Common.Models; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Common; + +[TestClass] +public class TokenDtoTests +{ + [TestMethod] + public void Ctor_SetsPropertiesCorrectly() + { + var token = "token123"; + + var testSubject = new TokenDto(token); + + testSubject.token.Should().BeSameAs(token); + } + + [TestMethod] + public void Ctor_NullParameter_Throws() + { + Action act = () => new TokenDto(null); + + act.Should().ThrowExactly(); + } +} diff --git a/src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs b/src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs new file mode 100644 index 0000000000..24092271f7 --- /dev/null +++ b/src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs @@ -0,0 +1,50 @@ +/* + * 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 SonarLint.VisualStudio.SLCore.Common.Models; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Common; + +[TestClass] +public class UserAndPasswordDtoTests +{ + [TestMethod] + public void Ctor_SetsPropertiesCorrectly() + { + var username = "user123"; + var password = "password123"; + + var testSubject = new UsernamePasswordDto(username, password); + + testSubject.username.Should().BeSameAs(username); + testSubject.password.Should().BeSameAs(password); + } + + [TestMethod] + public void Ctor_NullParameter_Throws() + { + Action actUsr = () => new UsernamePasswordDto(null, "password123"); + Action actPwd = () => new UsernamePasswordDto("user123", null); + + actUsr.Should().ThrowExactly(); + actPwd.Should().ThrowExactly(); + } +} diff --git a/src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs b/src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs new file mode 100644 index 0000000000..47c4622344 --- /dev/null +++ b/src/SLCore.UnitTests/Listener/Credentials/CredentialsListenerTests.cs @@ -0,0 +1,58 @@ +/* + * 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 SonarLint.VisualStudio.SLCore.Core; +using SonarLint.VisualStudio.SLCore.Listener.Credentials; +using SonarLint.VisualStudio.TestInfrastructure; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener.Credentials; + +[TestClass] +public class CredentialsListenerTests +{ + [TestMethod] + public void MefCtor_CheckIsExported() + { + MefTestHelpers.CheckTypeCanBeImported(); + } + + [TestMethod] + public void MefCtor_CheckIsSingleton() + { + MefTestHelpers.CheckIsSingletonMefComponent(); + } + + [TestMethod] + public async Task GetCredentialsAsync_StubImpl_ReturnsNoCredentials() + { + var testSubject = CreateTestSubject(); + + var response = await testSubject.GetCredentialsAsync(new GetCredentialsParams($"randomstring-{Guid.NewGuid()}")); + + response.Should().BeSameAs(GetCredentialsResponse.NoCredentials); + } + + private CredentialsListener CreateTestSubject() + { + return new CredentialsListener(); + } +} diff --git a/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsParamsTests.cs b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsParamsTests.cs new file mode 100644 index 0000000000..163a1e376e --- /dev/null +++ b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsParamsTests.cs @@ -0,0 +1,38 @@ +/* + * 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 Newtonsoft.Json; +using SonarLint.VisualStudio.SLCore.Listener.Credentials; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener.Credentials; + +[TestClass] +public class GetCredentialsParamsTests +{ + [TestMethod] + public void DeserializeObject_DeserializesCorrectly() + { + var str = """{"connectionId":"id123"}"""; + + var result = JsonConvert.DeserializeObject(str); + + result.connectionId.Should().Be("id123"); + } +} diff --git a/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsResponseTests.cs b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsResponseTests.cs new file mode 100644 index 0000000000..74c5054251 --- /dev/null +++ b/src/SLCore.UnitTests/Listener/Credentials/GetCredentialsResponseTests.cs @@ -0,0 +1,96 @@ +/* + * 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 Newtonsoft.Json; +using SonarLint.VisualStudio.SLCore.Common.Models; +using SonarLint.VisualStudio.SLCore.Listener.Credentials; + +namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener.Credentials; + +[TestClass] +public class GetCredentialsResponseTests +{ + [TestMethod] + public void SerializeObject_ResponseWithToken_SerializedCorrectly() + { + var testSubject = new GetCredentialsResponse(new TokenDto("token123")); + + var serializeObject = JsonConvert.SerializeObject(testSubject); + serializeObject.Should().Be("""{"credentials":{"token":"token123"}}"""); + } + + [TestMethod] + public void Ctor_ResponseWithToken_CredentialsSetCorrectly() + { + var tokenDto = new TokenDto("token123"); + var testSubject = new GetCredentialsResponse(tokenDto); + + testSubject.credentials.Left.Should().BeSameAs(tokenDto); + testSubject.credentials.Right.Should().BeNull(); + } + + [TestMethod] + public void Ctor_NullTokenDto_Throws() + { + Action act = () => new GetCredentialsResponse(token: null); + + act.Should().ThrowExactly(); + } + + [TestMethod] + public void SerializeObject_ResponseWithUserAndPassword_SerializedCorrectly() + { + var testSubject = new GetCredentialsResponse(new UsernamePasswordDto("user123", "password123")); + + var serializeObject = JsonConvert.SerializeObject(testSubject); + serializeObject.Should().Be("""{"credentials":{"username":"user123","password":"password123"}}"""); + } + + [TestMethod] + public void Ctor_ResponseWithUserAndPassword_CredentialsSetCorrectly() + { + var usernamePasswordDto = new UsernamePasswordDto("user123", "password123"); + var testSubject = new GetCredentialsResponse(usernamePasswordDto); + + testSubject.credentials.Right.Should().BeSameAs(usernamePasswordDto); + testSubject.credentials.Left.Should().BeNull(); + } + + [TestMethod] + public void Ctor_NullUserAndPasswordDto_Throws() + { + Action act = () => new GetCredentialsResponse(usernamePassword: null); + + act.Should().ThrowExactly(); + } + + [TestMethod] + public void Ctor_NoCredentials_HasNullCredentialsProperty() + { + GetCredentialsResponse.NoCredentials.credentials.Should().BeNull(); + } + + [TestMethod] + public void ResponseWithNoCredentials_SerializedCorrectly() + { + JsonConvert.SerializeObject(GetCredentialsResponse.NoCredentials).Should().Be("""{"credentials":null}"""); + } +} diff --git a/src/SLCore/Common/Models/TokenDto.cs b/src/SLCore/Common/Models/TokenDto.cs new file mode 100644 index 0000000000..b60a89c363 --- /dev/null +++ b/src/SLCore/Common/Models/TokenDto.cs @@ -0,0 +1,34 @@ +/* + * 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; + +namespace SonarLint.VisualStudio.SLCore.Common.Models +{ + public class TokenDto + { + public TokenDto(string token) + { + this.token = token ?? throw new ArgumentNullException(nameof(token)); + } + + public string token { get; } + } +} diff --git a/src/SLCore/Common/Models/UsernamePasswordDto.cs b/src/SLCore/Common/Models/UsernamePasswordDto.cs new file mode 100644 index 0000000000..4834c8aaaf --- /dev/null +++ b/src/SLCore/Common/Models/UsernamePasswordDto.cs @@ -0,0 +1,36 @@ +/* + * 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; + +namespace SonarLint.VisualStudio.SLCore.Common.Models +{ + public class UsernamePasswordDto + { + public UsernamePasswordDto(string username, string password) + { + this.username = username ?? throw new ArgumentNullException(nameof(username)); + this.password = password ?? throw new ArgumentNullException(nameof(password)); + } + + public string username { get; } + public string password { get; } + } +} diff --git a/src/SLCore/Listener/Credentials/CredentialsListener.cs b/src/SLCore/Listener/Credentials/CredentialsListener.cs new file mode 100644 index 0000000000..8233a4cba9 --- /dev/null +++ b/src/SLCore/Listener/Credentials/CredentialsListener.cs @@ -0,0 +1,40 @@ +/* + * 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.ComponentModel.Composition; +using System.Threading.Tasks; +using SonarLint.VisualStudio.SLCore.Core; + +namespace SonarLint.VisualStudio.SLCore.Listener.Credentials +{ + /// + /// Credentials provider for SLCore + /// + [Export(typeof(ISLCoreListener))] + [PartCreationPolicy(CreationPolicy.Shared)] + internal class CredentialsListener : ISLCoreListener + { + public Task GetCredentialsAsync(GetCredentialsParams parameters) + { + // stub implementation + return Task.FromResult(GetCredentialsResponse.NoCredentials); + } + } +} diff --git a/src/SLCore/Listener/Credentials/GetCredentialsParams.cs b/src/SLCore/Listener/Credentials/GetCredentialsParams.cs new file mode 100644 index 0000000000..526d70a87b --- /dev/null +++ b/src/SLCore/Listener/Credentials/GetCredentialsParams.cs @@ -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. + */ + +namespace SonarLint.VisualStudio.SLCore.Listener.Credentials +{ + internal class GetCredentialsParams + { + public GetCredentialsParams(string connectionId) + { + this.connectionId = connectionId; + } + + public string connectionId { get; } + } +} diff --git a/src/SLCore/Listener/Credentials/GetCredentialsResponse.cs b/src/SLCore/Listener/Credentials/GetCredentialsResponse.cs new file mode 100644 index 0000000000..6c06263239 --- /dev/null +++ b/src/SLCore/Listener/Credentials/GetCredentialsResponse.cs @@ -0,0 +1,48 @@ +/* + * 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 Newtonsoft.Json; +using SonarLint.VisualStudio.SLCore.Common.Models; +using SonarLint.VisualStudio.SLCore.Protocol; + +namespace SonarLint.VisualStudio.SLCore.Listener.Credentials +{ + internal class GetCredentialsResponse + { + // credentials property is nullable on the SLCore side + public static readonly GetCredentialsResponse NoCredentials = new GetCredentialsResponse(); + + private GetCredentialsResponse(){} + + public GetCredentialsResponse(TokenDto token) + { + credentials = Either.CreateLeft(token ?? throw new ArgumentNullException(nameof(token))); + } + + public GetCredentialsResponse(UsernamePasswordDto usernamePassword) + { + credentials = Either.CreateRight(usernamePassword ?? throw new ArgumentNullException(nameof(usernamePassword))); + } + + [JsonConverter(typeof(EitherJsonConverter))] + public Either credentials { get; } + } +}