-
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.
- Loading branch information
1 parent
1ee4dde
commit 4939006
Showing
16 changed files
with
501 additions
and
91 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
src/ConnectedMode.UnitTests/Persistence/CredentialsExtensionMethodsTests.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,106 @@ | ||
/* | ||
* 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 Microsoft.Alm.Authentication; | ||
using SonarLint.VisualStudio.ConnectedMode.Persistence; | ||
using SonarLint.VisualStudio.Core.Binding; | ||
using SonarQube.Client.Helpers; | ||
using SonarQube.Client.Models; | ||
|
||
namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence; | ||
|
||
[TestClass] | ||
public class CredentialsExtensionMethodsTests | ||
{ | ||
[TestMethod] | ||
public void ToCredential_NullCredentials_Throws() => Assert.ThrowsException<NotSupportedException>(() => ((IConnectionCredentials)null).ToCredential()); | ||
|
||
[TestMethod] | ||
public void ToCredential_BasicAuthCredentials_ReturnsExpected() | ||
{ | ||
var basicAuthCredentials = new UsernameAndPasswordCredentials("user", "pwd".ToSecureString()); | ||
|
||
var result = basicAuthCredentials.ToCredential(); | ||
|
||
result.Username.Should().Be(basicAuthCredentials.UserName); | ||
result.Password.Should().Be(basicAuthCredentials.Password.ToUnsecureString()); | ||
} | ||
|
||
[TestMethod] | ||
public void ToCredential_TokenAuthCredentials_ReturnsExpected() | ||
{ | ||
var tokenAuthCredentials = new TokenAuthCredentials("token".ToSecureString()); | ||
|
||
var result = tokenAuthCredentials.ToCredential(); | ||
|
||
result.Username.Should().Be(tokenAuthCredentials.Token.ToUnsecureString()); | ||
result.Password.Should().Be(string.Empty); | ||
} | ||
|
||
[TestMethod] | ||
public void ToConnectionCredentials_UsernameIsEmpty_ReturnsBasicAuthCredentialsWithPasswordAsToken() | ||
{ | ||
var credential = new Credential(string.Empty, "token"); | ||
|
||
var result = credential.ToConnectionCredentials(); | ||
|
||
var basicAuth = result as UsernameAndPasswordCredentials; | ||
basicAuth.Should().NotBeNull(); | ||
basicAuth.UserName.Should().Be(credential.Username); | ||
basicAuth.Password.ToUnsecureString().Should().Be(credential.Password); | ||
} | ||
|
||
/// <summary> | ||
/// For backward compatibility | ||
/// </summary> | ||
|
||
[TestMethod] | ||
public void ToConnectionCredentials_PasswordIsEmpty_ReturnsTokenAuthCredentialsWithUsernameAsToken() | ||
{ | ||
var credential = new Credential("token", string.Empty); | ||
|
||
var result = credential.ToConnectionCredentials(); | ||
|
||
var tokenAuth = result as TokenAuthCredentials; | ||
tokenAuth.Should().NotBeNull(); | ||
tokenAuth.Token.ToUnsecureString().Should().Be(credential.Username); | ||
} | ||
|
||
[TestMethod] | ||
public void ToConnectionCredentials_PasswordAndUsernameFilled_ReturnsBasicAuthCredentials() | ||
{ | ||
var credential = new Credential("username", "pwd"); | ||
|
||
var result = credential.ToConnectionCredentials(); | ||
|
||
var basicAuth = result as UsernameAndPasswordCredentials; | ||
basicAuth.Should().NotBeNull(); | ||
basicAuth.UserName.Should().Be(credential.Username); | ||
basicAuth.Password.ToUnsecureString().Should().Be(credential.Password); | ||
} | ||
|
||
[TestMethod] | ||
public void ToConnectionCredentials_Null_ReturnsNull() | ||
{ | ||
var result = ((Credential)null).ToConnectionCredentials(); | ||
|
||
result.Should().BeNull(); | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
src/ConnectedMode.UnitTests/Persistence/TokenAuthCredentialsTests.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,61 @@ | ||
/* | ||
* 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 SonarLint.VisualStudio.ConnectedMode.Persistence; | ||
using SonarLint.VisualStudio.TestInfrastructure; | ||
using SonarQube.Client.Helpers; | ||
|
||
namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence; | ||
|
||
[TestClass] | ||
public class TokenAuthCredentialsTests | ||
{ | ||
private const string Token = "token"; | ||
|
||
[TestMethod] | ||
public void Ctor_WhenTokenIsNull_ThrowsArgumentNullException() | ||
{ | ||
Action act = () => new TokenAuthCredentials(null); | ||
|
||
act.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[TestMethod] | ||
public void Dispose_DisposesPassword() | ||
{ | ||
var testSubject = new TokenAuthCredentials(Token.ToSecureString()); | ||
|
||
testSubject.Dispose(); | ||
|
||
Exceptions.Expect<ObjectDisposedException>(() => testSubject.Token.ToUnsecureString()); | ||
} | ||
|
||
[TestMethod] | ||
public void Clone_ClonesPassword() | ||
{ | ||
var testSubject = new TokenAuthCredentials(Token.ToSecureString()); | ||
|
||
var clone = (TokenAuthCredentials)testSubject.Clone(); | ||
|
||
clone.Should().NotBeSameAs(testSubject); | ||
clone.Token.Should().NotBeSameAs(testSubject.Token); | ||
clone.Token.ToUnsecureString().Should().Be(testSubject.Token.ToUnsecureString()); | ||
} | ||
} |
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
Oops, something went wrong.