Skip to content

Commit bb94c4f

Browse files
committed
bitbucket: add unit test for GetCredAsync helper cmdline
Add unit tests for the BitbucketAuthentication command line for UI helpers.
1 parent e4d4f16 commit bb94c4f

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

src/shared/Atlassian.Bitbucket.Tests/BitbucketAuthenticationTest.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
24
using System.Threading.Tasks;
5+
using GitCredentialManager;
36
using GitCredentialManager.Tests.Objects;
7+
using Moq;
48
using Xunit;
59

610
namespace Atlassian.Bitbucket.Tests
@@ -118,5 +122,113 @@ public async Task BitbucketAuthentication_ShowOAuthRequiredPromptAsync_SucceedsA
118122
Assert.Equal($"Your account has two-factor authentication enabled.{Environment.NewLine}" +
119123
$"To continue you must complete authentication in your web browser.{Environment.NewLine}", context.Terminal.Messages[0].Item1);
120124
}
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+
}
121233
}
122234
}

src/shared/Atlassian.Bitbucket/BitbucketAuthentication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public async Task<OAuth2TokenResult> RefreshOAuthCredentialsAsync(string refresh
245245

246246
#region Private Methods
247247

248-
private bool TryFindHelperExecutablePath(out string path)
248+
protected internal virtual bool TryFindHelperExecutablePath(out string path)
249249
{
250250
return TryFindHelperExecutablePath(
251251
BitbucketConstants.EnvironmentVariables.AuthenticationHelper,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Atlassian.Bitbucket.Tests")]

src/shared/Core/Authentication/AuthenticationBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected Task<IDictionary<string, string>> InvokeHelperAsync(string path, strin
2525
return InvokeHelperAsync(path, args, null, CancellationToken.None);
2626
}
2727

28-
internal protected virtual async Task<IDictionary<string, string>> InvokeHelperAsync(string path, string args,
28+
protected internal virtual async Task<IDictionary<string, string>> InvokeHelperAsync(string path, string args,
2929
IDictionary<string, string> standardInput, CancellationToken ct)
3030
{
3131
var procStartInfo = new ProcessStartInfo(path)

src/shared/Core/InternalsVisibleTo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
[assembly: InternalsVisibleTo("Core.Tests")]
44
[assembly: InternalsVisibleTo("GitHub.Tests")]
5+
[assembly: InternalsVisibleTo("Atlassian.Bitbucket.Tests")]

0 commit comments

Comments
 (0)