|
1 | | -/* |
2 | | - * SonarLint for Visual Studio |
3 | | - * Copyright (C) 2016-2025 SonarSource SA |
4 | | - * mailto:info AT sonarsource DOT com |
5 | | - * |
6 | | - * This program is free software; you can redistribute it and/or |
7 | | - * modify it under the terms of the GNU Lesser General Public |
8 | | - * License as published by the Free Software Foundation; either |
9 | | - * version 3 of the License, or (at your option) any later version. |
10 | | - * |
11 | | - * This program is distributed in the hope that it will be useful, |
12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | - * Lesser General Public License for more details. |
15 | | - * |
16 | | - * You should have received a copy of the GNU Lesser General Public License |
17 | | - * along with this program; if not, write to the Free Software Foundation, |
18 | | - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | | - */ |
20 | | - |
21 | | -using Microsoft.Alm.Authentication; |
22 | | -using SonarLint.VisualStudio.ConnectedMode.Binding; |
23 | | -using SonarLint.VisualStudio.ConnectedMode.Persistence; |
24 | | -using SonarLint.VisualStudio.Core.Binding; |
25 | | -using SonarQube.Client.Helpers; |
26 | | - |
27 | | -namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence |
28 | | -{ |
29 | | - [TestClass] |
30 | | - public class SolutionBindingCredentialsLoaderTests |
31 | | - { |
32 | | - private ICredentialStoreService store; |
33 | | - private Uri mockUri; |
34 | | - private SolutionBindingCredentialsLoader testSubject; |
35 | | - |
36 | | - [TestInitialize] |
37 | | - public void Setup() |
38 | | - { |
39 | | - store = Substitute.For<ICredentialStoreService>(); |
40 | | - mockUri = new Uri("http://sonarsource.com"); |
41 | | - testSubject = new SolutionBindingCredentialsLoader(store); |
42 | | - } |
43 | | - |
44 | | - [TestMethod] |
45 | | - public void Ctor_NullStore_Exception() |
46 | | - { |
47 | | - Action act = () => new SolutionBindingCredentialsLoader(null); |
48 | | - |
49 | | - act.Should().ThrowExactly<ArgumentNullException>().And.ParamName.Should().Be("store"); |
50 | | - } |
51 | | - |
52 | | - [TestMethod] |
53 | | - public void Load_ServerUriIsNull_Null() |
54 | | - { |
55 | | - var actual = testSubject.Load(null); |
56 | | - |
57 | | - actual.Should().Be(null); |
58 | | - } |
59 | | - |
60 | | - [TestMethod] |
61 | | - public void Load_NoCredentials_Null() |
62 | | - { |
63 | | - MockReadCredentials(mockUri, null); |
64 | | - |
65 | | - var actual = testSubject.Load(mockUri); |
66 | | - |
67 | | - actual.Should().Be(null); |
68 | | - } |
69 | | - |
70 | | - [TestMethod] |
71 | | - public void Load_CredentialsExist_CredentialsWithSecuredString() |
72 | | - { |
73 | | - var credentials = new Credential("user", "password"); |
74 | | - MockReadCredentials(mockUri, credentials); |
75 | | - |
76 | | - var actual = testSubject.Load(mockUri); |
77 | | - |
78 | | - actual.Should().BeEquivalentTo(new UsernameAndPasswordCredentials("user", "password".ToSecureString())); |
79 | | - } |
80 | | - |
81 | | - [TestMethod] |
82 | | - public void Load_CredentialsExist_UsernameIsEmpty_BasicAuthCredentialsWithSecuredString() |
83 | | - { |
84 | | - var credentials = new Credential(string.Empty, "token"); |
85 | | - MockReadCredentials(mockUri, credentials); |
86 | | - |
87 | | - var actual = testSubject.Load(mockUri); |
88 | | - |
89 | | - actual.Should().BeEquivalentTo(new UsernameAndPasswordCredentials(string.Empty, "token".ToSecureString())); |
90 | | - } |
91 | | - |
92 | | - /// <summary> |
93 | | - /// For backward compatibility |
94 | | - /// </summary> |
95 | | - [TestMethod] |
96 | | - public void Load_CredentialsExist_PasswordIsEmpty_TokenCredentialsWithSecuredString() |
97 | | - { |
98 | | - var credentials = new Credential("token", string.Empty); |
99 | | - MockReadCredentials(mockUri, credentials); |
100 | | - |
101 | | - var actual = testSubject.Load(mockUri); |
102 | | - |
103 | | - actual.Should().BeEquivalentTo(new TokenAuthCredentials("token".ToSecureString())); |
104 | | - } |
105 | | - |
106 | | - [TestMethod] |
107 | | - public void Save_ServerUriIsNull_CredentialsNotSaved() |
108 | | - { |
109 | | - var credentials = new UsernameAndPasswordCredentials("user", "password".ToSecureString()); |
110 | | - |
111 | | - testSubject.Save(credentials, null); |
112 | | - |
113 | | - store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>()); |
114 | | - } |
115 | | - |
116 | | - [TestMethod] |
117 | | - public void Save_CredentialsAreNull_CredentialsNotSaved() |
118 | | - { |
119 | | - testSubject.Save(null, mockUri); |
120 | | - |
121 | | - store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>()); |
122 | | - } |
123 | | - |
124 | | - [TestMethod] |
125 | | - public void Save_CredentialsAreNotBasicAuth_CredentialsNotSaved() |
126 | | - { |
127 | | - try |
128 | | - { |
129 | | - testSubject.Save(new Mock<IConnectionCredentials>().Object, mockUri); |
130 | | - } |
131 | | - catch (Exception) |
132 | | - { |
133 | | - // ignored |
134 | | - } |
135 | | - |
136 | | - store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>()); |
137 | | - } |
138 | | - |
139 | | - [TestMethod] |
140 | | - public void Save_CredentialsAreBasicAuth_CredentialsSavedWithUnsecuredString() |
141 | | - { |
142 | | - var credentials = new UsernameAndPasswordCredentials("user", "password".ToSecureString()); |
143 | | - testSubject.Save(credentials, mockUri); |
144 | | - |
145 | | - store.Received(1) |
146 | | - .WriteCredentials( |
147 | | - Arg.Is<TargetUri>(t => t.ActualUri == mockUri), |
148 | | - Arg.Is<Credential>(c => c.Username == "user" && c.Password == "password")); |
149 | | - } |
150 | | - |
151 | | - [TestMethod] |
152 | | - public void Save_CredentialsAreTokenAuth_CredentialsSavedWithUnsecuredString() |
153 | | - { |
154 | | - var credentials = new TokenAuthCredentials("token".ToSecureString()); |
155 | | - |
156 | | - testSubject.Save(credentials, mockUri); |
157 | | - |
158 | | - store.Received(1) |
159 | | - .WriteCredentials( |
160 | | - Arg.Is<TargetUri>(t => t.ActualUri == mockUri), |
161 | | - Arg.Is<Credential>(c => c.Username == "token" && c.Password == string.Empty)); |
162 | | - } |
163 | | - |
164 | | - [TestMethod] |
165 | | - public void DeleteCredentials_UriNull_DoesNotCallStoreDeleteCredentials() |
166 | | - { |
167 | | - testSubject.DeleteCredentials(null); |
168 | | - |
169 | | - store.DidNotReceive().DeleteCredentials(Arg.Any<TargetUri>()); |
170 | | - } |
171 | | - |
172 | | - [TestMethod] |
173 | | - public void DeleteCredentials_UriProvided_CallsStoreDeleteCredentials() |
174 | | - { |
175 | | - testSubject.DeleteCredentials(mockUri); |
176 | | - |
177 | | - store.Received(1).DeleteCredentials(Arg.Any<TargetUri>()); |
178 | | - } |
179 | | - |
180 | | - private void MockReadCredentials(Uri uri, Credential credentials) => |
181 | | - store |
182 | | - .ReadCredentials(Arg.Is<TargetUri>(t => t.ActualUri == uri)) |
183 | | - .Returns(credentials); |
184 | | - } |
185 | | -} |
| 1 | +// /* |
| 2 | +// * SonarLint for Visual Studio |
| 3 | +// * Copyright (C) 2016-2025 SonarSource SA |
| 4 | +// * mailto:info AT sonarsource DOT com |
| 5 | +// * |
| 6 | +// * This program is free software; you can redistribute it and/or |
| 7 | +// * modify it under the terms of the GNU Lesser General Public |
| 8 | +// * License as published by the Free Software Foundation; either |
| 9 | +// * version 3 of the License, or (at your option) any later version. |
| 10 | +// * |
| 11 | +// * This program is distributed in the hope that it will be useful, |
| 12 | +// * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +// * Lesser General Public License for more details. |
| 15 | +// * |
| 16 | +// * You should have received a copy of the GNU Lesser General Public License |
| 17 | +// * along with this program; if not, write to the Free Software Foundation, |
| 18 | +// * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +// */ |
| 20 | +// |
| 21 | +// using Microsoft.Alm.Authentication; |
| 22 | +// using SonarLint.VisualStudio.ConnectedMode.Binding; |
| 23 | +// using SonarLint.VisualStudio.ConnectedMode.Persistence; |
| 24 | +// using SonarLint.VisualStudio.Core.Binding; |
| 25 | +// using SonarQube.Client.Helpers; |
| 26 | +// |
| 27 | +// namespace SonarLint.VisualStudio.ConnectedMode.UnitTests.Persistence |
| 28 | +// { |
| 29 | +// [TestClass] |
| 30 | +// public class DefaultBindingCredentialsLoaderTests |
| 31 | +// { |
| 32 | +// private ICredentialStoreService store; |
| 33 | +// private Uri mockUri; |
| 34 | +// private DefaultBindingCredentialsLoader testSubject; |
| 35 | +// |
| 36 | +// [TestInitialize] |
| 37 | +// public void Setup() |
| 38 | +// { |
| 39 | +// store = Substitute.For<ICredentialStoreService>(); |
| 40 | +// mockUri = new Uri("http://sonarsource.com"); |
| 41 | +// testSubject = new DefaultBindingCredentialsLoader(store); |
| 42 | +// } |
| 43 | +// |
| 44 | +// [TestMethod] |
| 45 | +// public void Ctor_NullStore_Exception() |
| 46 | +// { |
| 47 | +// Action act = () => new DefaultBindingCredentialsLoader(null); |
| 48 | +// |
| 49 | +// act.Should().ThrowExactly<ArgumentNullException>().And.ParamName.Should().Be("store"); |
| 50 | +// } |
| 51 | +// |
| 52 | +// [TestMethod] |
| 53 | +// public void Load_ServerUriIsNull_Null() |
| 54 | +// { |
| 55 | +// var actual = testSubject.Load(null); |
| 56 | +// |
| 57 | +// actual.Should().Be(null); |
| 58 | +// } |
| 59 | +// |
| 60 | +// [TestMethod] |
| 61 | +// public void Load_NoCredentials_Null() |
| 62 | +// { |
| 63 | +// MockReadCredentials(mockUri, null); |
| 64 | +// |
| 65 | +// var actual = testSubject.Load(mockUri); |
| 66 | +// |
| 67 | +// actual.Should().Be(null); |
| 68 | +// } |
| 69 | +// |
| 70 | +// [TestMethod] |
| 71 | +// public void Load_CredentialsExist_CredentialsWithSecuredString() |
| 72 | +// { |
| 73 | +// var credentials = new Credential("user", "password"); |
| 74 | +// MockReadCredentials(mockUri, credentials); |
| 75 | +// |
| 76 | +// var actual = testSubject.Load(mockUri); |
| 77 | +// |
| 78 | +// actual.Should().BeEquivalentTo(new UsernameAndPasswordCredentials("user", "password".ToSecureString())); |
| 79 | +// } |
| 80 | +// |
| 81 | +// [TestMethod] |
| 82 | +// public void Load_CredentialsExist_UsernameIsEmpty_BasicAuthCredentialsWithSecuredString() |
| 83 | +// { |
| 84 | +// var credentials = new Credential(string.Empty, "token"); |
| 85 | +// MockReadCredentials(mockUri, credentials); |
| 86 | +// |
| 87 | +// var actual = testSubject.Load(mockUri); |
| 88 | +// |
| 89 | +// actual.Should().BeEquivalentTo(new UsernameAndPasswordCredentials(string.Empty, "token".ToSecureString())); |
| 90 | +// } |
| 91 | +// |
| 92 | +// /// <summary> |
| 93 | +// /// For backward compatibility |
| 94 | +// /// </summary> |
| 95 | +// [TestMethod] |
| 96 | +// public void Load_CredentialsExist_PasswordIsEmpty_TokenCredentialsWithSecuredString() |
| 97 | +// { |
| 98 | +// var credentials = new Credential("token", string.Empty); |
| 99 | +// MockReadCredentials(mockUri, credentials); |
| 100 | +// |
| 101 | +// var actual = testSubject.Load(mockUri); |
| 102 | +// |
| 103 | +// actual.Should().BeEquivalentTo(new TokenAuthCredentials("token".ToSecureString())); |
| 104 | +// } |
| 105 | +// |
| 106 | +// [TestMethod] |
| 107 | +// public void Save_ServerUriIsNull_CredentialsNotSaved() |
| 108 | +// { |
| 109 | +// var credentials = new UsernameAndPasswordCredentials("user", "password".ToSecureString()); |
| 110 | +// |
| 111 | +// testSubject.Save(credentials, null); |
| 112 | +// |
| 113 | +// store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>()); |
| 114 | +// } |
| 115 | +// |
| 116 | +// [TestMethod] |
| 117 | +// public void Save_CredentialsAreNull_CredentialsNotSaved() |
| 118 | +// { |
| 119 | +// testSubject.Save(null, mockUri); |
| 120 | +// |
| 121 | +// store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>()); |
| 122 | +// } |
| 123 | +// |
| 124 | +// [TestMethod] |
| 125 | +// public void Save_CredentialsAreNotBasicAuth_CredentialsNotSaved() |
| 126 | +// { |
| 127 | +// try |
| 128 | +// { |
| 129 | +// testSubject.Save(new Mock<IConnectionCredentials>().Object, mockUri); |
| 130 | +// } |
| 131 | +// catch (Exception) |
| 132 | +// { |
| 133 | +// // ignored |
| 134 | +// } |
| 135 | +// |
| 136 | +// store.DidNotReceive().WriteCredentials(Arg.Any<TargetUri>(), Arg.Any<Credential>()); |
| 137 | +// } |
| 138 | +// |
| 139 | +// [TestMethod] |
| 140 | +// public void Save_CredentialsAreBasicAuth_CredentialsSavedWithUnsecuredString() |
| 141 | +// { |
| 142 | +// var credentials = new UsernameAndPasswordCredentials("user", "password".ToSecureString()); |
| 143 | +// testSubject.Save(credentials, mockUri); |
| 144 | +// |
| 145 | +// store.Received(1) |
| 146 | +// .WriteCredentials( |
| 147 | +// Arg.Is<TargetUri>(t => t.ActualUri == mockUri), |
| 148 | +// Arg.Is<Credential>(c => c.Username == "user" && c.Password == "password")); |
| 149 | +// } |
| 150 | +// |
| 151 | +// [TestMethod] |
| 152 | +// public void Save_CredentialsAreTokenAuth_CredentialsSavedWithUnsecuredString() |
| 153 | +// { |
| 154 | +// var credentials = new TokenAuthCredentials("token".ToSecureString()); |
| 155 | +// |
| 156 | +// testSubject.Save(credentials, mockUri); |
| 157 | +// |
| 158 | +// store.Received(1) |
| 159 | +// .WriteCredentials( |
| 160 | +// Arg.Is<TargetUri>(t => t.ActualUri == mockUri), |
| 161 | +// Arg.Is<Credential>(c => c.Username == "token" && c.Password == string.Empty)); |
| 162 | +// } |
| 163 | +// |
| 164 | +// [TestMethod] |
| 165 | +// public void DeleteCredentials_UriNull_DoesNotCallStoreDeleteCredentials() |
| 166 | +// { |
| 167 | +// testSubject.DeleteCredentials(null); |
| 168 | +// |
| 169 | +// store.DidNotReceive().DeleteCredentials(Arg.Any<TargetUri>()); |
| 170 | +// } |
| 171 | +// |
| 172 | +// [TestMethod] |
| 173 | +// public void DeleteCredentials_UriProvided_CallsStoreDeleteCredentials() |
| 174 | +// { |
| 175 | +// testSubject.DeleteCredentials(mockUri); |
| 176 | +// |
| 177 | +// store.Received(1).DeleteCredentials(Arg.Any<TargetUri>()); |
| 178 | +// } |
| 179 | +// |
| 180 | +// private void MockReadCredentials(Uri uri, Credential credentials) => |
| 181 | +// store |
| 182 | +// .ReadCredentials(Arg.Is<TargetUri>(t => t.ActualUri == uri)) |
| 183 | +// .Returns(credentials); |
| 184 | +// } |
| 185 | +// } |
0 commit comments