|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
2 | 4 | using System.Threading.Tasks;
|
| 5 | +using GitCredentialManager; |
3 | 6 | using GitCredentialManager.Tests.Objects;
|
| 7 | +using Moq; |
4 | 8 | using Xunit;
|
5 | 9 |
|
6 | 10 | namespace Atlassian.Bitbucket.Tests
|
@@ -118,5 +122,113 @@ public async Task BitbucketAuthentication_ShowOAuthRequiredPromptAsync_SucceedsA
|
118 | 122 | Assert.Equal($"Your account has two-factor authentication enabled.{Environment.NewLine}" +
|
119 | 123 | $"To continue you must complete authentication in your web browser.{Environment.NewLine}", context.Terminal.Messages[0].Item1);
|
120 | 124 | }
|
| 125 | + |
| 126 | + [Fact] |
| 127 | + public async Task BitbucketAuthentication_GetCredentialsAsync_AllModes_NoUser_BBCloud_HelperCmdLine() |
| 128 | + { |
| 129 | + var targetUri = new Uri("https://bitbucket.org"); |
| 130 | + |
| 131 | + var helperPath = "/usr/bin/test-helper"; |
| 132 | + var expectedUserName = "jsquire"; |
| 133 | + var expectedPassword = "password"; |
| 134 | + var resultDict = new Dictionary<string, string> |
| 135 | + { |
| 136 | + ["username"] = expectedUserName, |
| 137 | + ["password"] = expectedPassword |
| 138 | + }; |
| 139 | + |
| 140 | + string expectedArgs = $"userpass --show-oauth"; |
| 141 | + |
| 142 | + var context = new TestCommandContext(); |
| 143 | + context.SessionManager.IsDesktopSession = true; // Enable OAuth and UI helper selection |
| 144 | + |
| 145 | + var authMock = new Mock<BitbucketAuthentication>(context) { CallBase = true }; |
| 146 | + authMock.Setup(x => x.TryFindHelperExecutablePath(out helperPath)) |
| 147 | + .Returns(true); |
| 148 | + authMock.Setup(x => x.InvokeHelperAsync(It.IsAny<string>(), It.IsAny<string>(), null, CancellationToken.None)) |
| 149 | + .ReturnsAsync(resultDict); |
| 150 | + |
| 151 | + BitbucketAuthentication auth = authMock.Object; |
| 152 | + CredentialsPromptResult result = await auth.GetCredentialsAsync(targetUri, null, AuthenticationModes.All); |
| 153 | + |
| 154 | + Assert.Equal(AuthenticationModes.Basic, result.AuthenticationMode); |
| 155 | + Assert.Equal(result.Credential.Account, expectedUserName); |
| 156 | + Assert.Equal(result.Credential.Password, expectedPassword); |
| 157 | + |
| 158 | + authMock.Verify(x => x.InvokeHelperAsync(helperPath, expectedArgs, null, CancellationToken.None), |
| 159 | + Times.Once); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public async Task BitbucketAuthentication_GetCredentialsAsync_BasicOnly_User_BBCloud_HelperCmdLine() |
| 164 | + { |
| 165 | + var targetUri = new Uri("https://bitbucket.org"); |
| 166 | + |
| 167 | + var helperPath = "/usr/bin/test-helper"; |
| 168 | + var expectedUserName = "jsquire"; |
| 169 | + var expectedPassword = "password"; |
| 170 | + var resultDict = new Dictionary<string, string> |
| 171 | + { |
| 172 | + ["username"] = expectedUserName, |
| 173 | + ["password"] = expectedPassword |
| 174 | + }; |
| 175 | + |
| 176 | + string expectedArgs = $"userpass --username {expectedUserName}"; |
| 177 | + |
| 178 | + var context = new TestCommandContext(); |
| 179 | + context.SessionManager.IsDesktopSession = true; // Enable UI helper selection |
| 180 | + |
| 181 | + var authMock = new Mock<BitbucketAuthentication>(context) { CallBase = true }; |
| 182 | + authMock.Setup(x => x.TryFindHelperExecutablePath(out helperPath)) |
| 183 | + .Returns(true); |
| 184 | + authMock.Setup(x => x.InvokeHelperAsync(It.IsAny<string>(), It.IsAny<string>(), null, CancellationToken.None)) |
| 185 | + .ReturnsAsync(resultDict); |
| 186 | + |
| 187 | + BitbucketAuthentication auth = authMock.Object; |
| 188 | + CredentialsPromptResult result = await auth.GetCredentialsAsync(targetUri, expectedUserName, AuthenticationModes.Basic); |
| 189 | + |
| 190 | + Assert.Equal(AuthenticationModes.Basic, result.AuthenticationMode); |
| 191 | + Assert.Equal(result.Credential.Account, expectedUserName); |
| 192 | + Assert.Equal(result.Credential.Password, expectedPassword); |
| 193 | + |
| 194 | + authMock.Verify(x => x.InvokeHelperAsync(helperPath, expectedArgs, null, CancellationToken.None), |
| 195 | + Times.Once); |
| 196 | + } |
| 197 | + |
| 198 | + [Fact] |
| 199 | + public async Task BitbucketAuthentication_GetCredentialsAsync_AllModes_NoUser_BBServerDC_HelperCmdLine() |
| 200 | + { |
| 201 | + var targetUri = new Uri("https://example.com/bitbucket"); |
| 202 | + |
| 203 | + var helperPath = "/usr/bin/test-helper"; |
| 204 | + var expectedUserName = "jsquire"; |
| 205 | + var expectedPassword = "password"; |
| 206 | + var resultDict = new Dictionary<string, string> |
| 207 | + { |
| 208 | + ["username"] = expectedUserName, |
| 209 | + ["password"] = expectedPassword |
| 210 | + }; |
| 211 | + |
| 212 | + string expectedArgs = $"userpass --url {targetUri} --show-oauth"; |
| 213 | + |
| 214 | + var context = new TestCommandContext(); |
| 215 | + context.SessionManager.IsDesktopSession = true; // Enable OAuth and UI helper selection |
| 216 | + |
| 217 | + var authMock = new Mock<BitbucketAuthentication>(context) { CallBase = true }; |
| 218 | + authMock.Setup(x => x.TryFindHelperExecutablePath(out helperPath)) |
| 219 | + .Returns(true); |
| 220 | + authMock.Setup(x => x.InvokeHelperAsync(It.IsAny<string>(), It.IsAny<string>(), null, CancellationToken.None)) |
| 221 | + .ReturnsAsync(resultDict); |
| 222 | + |
| 223 | + BitbucketAuthentication auth = authMock.Object; |
| 224 | + CredentialsPromptResult result = await auth.GetCredentialsAsync(targetUri, null, AuthenticationModes.All); |
| 225 | + |
| 226 | + Assert.Equal(AuthenticationModes.Basic, result.AuthenticationMode); |
| 227 | + Assert.Equal(result.Credential.Account, expectedUserName); |
| 228 | + Assert.Equal(result.Credential.Password, expectedPassword); |
| 229 | + |
| 230 | + authMock.Verify(x => x.InvokeHelperAsync(helperPath, expectedArgs, null, CancellationToken.None), |
| 231 | + Times.Once); |
| 232 | + } |
121 | 233 | }
|
122 | 234 | }
|
0 commit comments