Skip to content

Commit

Permalink
Add CredentialsListener (#5192)
Browse files Browse the repository at this point in the history
Fixes #5191
  • Loading branch information
georgii-borovinskikh-sonarsource authored Jan 25, 2024
1 parent b58e5a2 commit fae67d4
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/SLCore.UnitTests/Common/TokenDtoTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>();
}
}
50 changes: 50 additions & 0 deletions src/SLCore.UnitTests/Common/UserAndPasswordDtoTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>();
actPwd.Should().ThrowExactly<ArgumentNullException>();
}
}
Original file line number Diff line number Diff line change
@@ -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<CredentialsListener, ISLCoreListener>();
}

[TestMethod]
public void MefCtor_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<CredentialsListener>();
}

[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();
}
}
Original file line number Diff line number Diff line change
@@ -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<GetCredentialsParams>(str);

result.connectionId.Should().Be("id123");
}
}
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>();
}

[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<ArgumentNullException>();
}

[TestMethod]
public void Ctor_NoCredentials_HasNullCredentialsProperty()
{
GetCredentialsResponse.NoCredentials.credentials.Should().BeNull();
}

[TestMethod]
public void ResponseWithNoCredentials_SerializedCorrectly()
{
JsonConvert.SerializeObject(GetCredentialsResponse.NoCredentials).Should().Be("""{"credentials":null}""");
}
}
34 changes: 34 additions & 0 deletions src/SLCore/Common/Models/TokenDto.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
36 changes: 36 additions & 0 deletions src/SLCore/Common/Models/UsernamePasswordDto.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Loading

0 comments on commit fae67d4

Please sign in to comment.