public async Task BitbucketHostProvider_GetCredentialAsync_ValidateTargetUriAsync(string protocol, string host, string username, string password) { var input = MockInput(protocol, host, username); var context = new TestCommandContext(); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); if (protocol.ToLower().Equals("http") && host.ToLower().Equals(BITBUCKET_DOT_ORG_HOST)) { // only fail for http://bitbucket.org await Assert.ThrowsAsync <Exception>(async() => await provider.GetCredentialAsync(input)); } else { MockUserEntersValidBasicCredentials(bitbucketAuthentication, input, password); MockRemoteBasicAuthAccountIsValidRequires2FA(bitbucketApi, input, password); MockRemoteValidRefreshToken(); bitbucketAuthentication.Setup(m => m.ShowOAuthRequiredPromptAsync()).ReturnsAsync(true); bitbucketAuthentication.Setup(m => m.CreateOAuthCredentialsAsync(It.IsAny <Uri>())).ReturnsAsync(new OAuth2TokenResult(MOCK_ACCESS_TOKEN, "access_token")); var userInfo = new UserInfo() { IsTwoFactorAuthenticationEnabled = false }; bitbucketApi.Setup(x => x.GetUserInformationAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>())).ReturnsAsync(new RestApiResult <UserInfo>(System.Net.HttpStatusCode.OK, userInfo)); var credential = await provider.GetCredentialAsync(input); } }
public void BitbucketHostProvider_GetCredentialAsync_Succeeds_ForValidStoredBasicAuthAccount(string protocol, string host, string username, string password) { InputArguments input = MockInput(protocol, host, username); var context = new TestCommandContext(); MockStoredAccount(context, input, password); MockRemoteBasicAuthAccountIsValidNo2FA(bitbucketApi, input, password); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); var credential = provider.GetCredentialAsync(input); //verify bitbucket.org credentials were validated if (BITBUCKET_DOT_ORG_HOST.Equals(host)) { VerifyValidateBasicAuthCredentialsRan(); } else { //verify DC/Server credentials were not validated VerifyValidateBasicAuthCredentialsNeverRan(); } // Stored credentials so don't ask for more VerifyInteractiveBasicAuthFlowNeverRan(password, input, credential); // Valid Basic Auth credentials so don't run Oauth VerifyInteractiveOAuthFlowNeverRan(input, credential); }
// Basic Auth works public void BitbucketHostProvider_GetCredentialAsync_ForcedAuthMode_IsRespected(string protocol, string host, string username, string password, string preconfiguredAuthModes) { var input = MockInput(protocol, host, username); var context = new TestCommandContext(); if (preconfiguredAuthModes != null) { context.Environment.Variables.Add(BitbucketConstants.EnvironmentVariables.AuthenticationModes, preconfiguredAuthModes); } MockUserEntersValidBasicCredentials(bitbucketAuthentication, input, password); MockRemoteBasicAuthAccountIsValidRequires2FA(bitbucketApi, input, password); bitbucketAuthentication.Setup(m => m.ShowOAuthRequiredPromptAsync()).ReturnsAsync(true); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); var credential = provider.GetCredentialAsync(input); Assert.NotNull(credential); if (preconfiguredAuthModes.Contains("basic")) { VerifyInteractiveBasicAuthFlowRan(password, input, credential); VerifyInteractiveOAuthFlowNeverRan(input, credential); } if (preconfiguredAuthModes.Contains("oauth")) { VerifyInteractiveBasicAuthFlowNeverRan(password, input, credential); VerifyInteractiveOAuthFlowRan(password, input, credential); } }
// Basic Auth works public void BitbucketHostProvider_GetCredentialAsync_ForOAuth(string protocol, string host, string username, string password, bool storedAccount, bool?userEntersCredentials, bool expected) { var input = MockInput(protocol, host, username); var context = new TestCommandContext(); if (storedAccount) { MockStoredOAuthAccount(context, input); } if (userEntersCredentials.HasValue && userEntersCredentials.Value) { MockUserEnteredBasicCredentials(bitbucketAuthentication, input, password); } else { MockUserDidNotEnteredBasicCredentials(bitbucketAuthentication); } MockValidRemoteAccount(bitbucketApi, input, password, true); MockRemoteValidRefreshToken(); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); if (userEntersCredentials.HasValue && !userEntersCredentials.Value) { Assert.ThrowsAsync <Exception>(async() => await provider.GetCredentialAsync(input)); return; } var credential = provider.GetCredentialAsync(input); if (expected) { VerifyOAuthFlowRan(password, storedAccount, expected, input, credential, null); } VerifyBasicAuthFlowDidNotRun(password, expected, input, storedAccount, credential, null); }
async Task <ICredential> GetCredentialAsync(Uri uri) { if (credential != null) { return(credential); } var input = new InputArguments(new Dictionary <string, string> { ["protocol"] = "https", ["host"] = "bitbucket.org", ["path"] = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped), }); var provider = new BitbucketHostProvider(new CommandContext(AppPath.Default)); credential = await provider.GetCredentialAsync(input); return(credential); }
public void BitbucketHostProvider_GetCredentialAsync_Succeeds_ForFreshValid2FAAcccount(string protocol, string host, string username, string password) { var input = MockInput(protocol, host, username); var context = new TestCommandContext(); // user is prompted for basic auth credentials MockUserEntersValidBasicCredentials(bitbucketAuthentication, input, password); // basic auth credentials are valid but 2FA is ON MockRemoteBasicAuthAccountIsValidRequires2FA(bitbucketApi, input, password); MockRemoteOAuthAccountIsValid(bitbucketApi, input, password, true); MockRemoteValidRefreshToken(); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); var credential = provider.GetCredentialAsync(input); VerifyOAuthFlowRan(password, false, true, input, credential, null); VerifyBasicAuthFlowNeverRan(password, input, false, null); }
public void BitbucketHostProvider_GetCredentialAsync_Succeeds_ForValidStoredOAuthAccount(string protocol, string host, string username, string token) { InputArguments input = MockInput(protocol, host, username); var context = new TestCommandContext(); MockStoredAccount(context, input, token); MockRemoteOAuthAccountIsValid(bitbucketApi, input, token, false); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); var credential = provider.GetCredentialAsync(input); //verify bitbucket.org credentials were validated VerifyValidateOAuthCredentialsRan(); // Stored credentials so don't ask for more VerifyInteractiveBasicAuthFlowNeverRan(token, input, credential); // Valid Basic Auth credentials so don't run Oauth VerifyInteractiveOAuthFlowNeverRan(input, credential); }
async Task <ICredential> GetCredentialAsync(Uri uri) { if (credential != null) { return(credential); } var input = new InputArguments(new Dictionary <string, string> { ["protocol"] = "https", ["host"] = "bitbucket.org", ["path"] = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped), }); var provider = new BitbucketHostProvider(new CommandContext()); // Currently failing with: // Error converting value {null} to type 'System.Boolean'. Path 'has_2fa_enabled', line 1, position 79. :/ credential = await provider.GetCredentialAsync(input); return(credential); }
public void BitbucketHostProvider_GetCredentialAsync_Succeeds_ForFreshValidBasicAuthAccount(string protocol, string host, string username, string password) { InputArguments input = MockInput(protocol, host, username); var context = new TestCommandContext(); MockUserEntersValidBasicCredentials(bitbucketAuthentication, input, password); if (BITBUCKET_DOT_ORG_HOST.Equals(host)) { MockRemoteOAuthAccountIsValid(bitbucketApi, input, password, true); } MockRemoteBasicAuthAccountIsValidNo2FA(bitbucketApi, input, password); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); var credential = provider.GetCredentialAsync(input); VerifyBasicAuthFlowRan(password, true, input, credential, null); VerifyOAuthFlowDidNotRun(password, true, input, credential); }
// Basic Auth works public void BitbucketHostProvider_GetCredentialAsync_ForOAuth_NoStorage_ForcedAuthMode(string protocol, string host, string username, string password, string preconfiguredAuthModes, bool expected) { var input = MockInput(protocol, host, username); var context = new TestCommandContext(); if (preconfiguredAuthModes != null) { context.Environment.Variables.Add(BitbucketConstants.EnvironmentVariables.AuthenticationModes, preconfiguredAuthModes); } MockUserEnteredBasicCredentials(bitbucketAuthentication, input, password); MockValidRemoteAccount(bitbucketApi, input, password, true); MockRemoteValidRefreshToken(); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); var credential = provider.GetCredentialAsync(input); if (preconfiguredAuthModes == null) { VerifyBasicAuthFlowRan(password, false, expected, input, credential, preconfiguredAuthModes); } if (preconfiguredAuthModes != null && preconfiguredAuthModes.Contains("basic")) { VerifyBasicAuthFlowRan(password, false, expected, input, credential, preconfiguredAuthModes); VerifyOAuthFlowWasNotRun(password, false, expected, input, credential); } if (preconfiguredAuthModes != null && preconfiguredAuthModes.Contains("oauth")) { VerifyOAuthFlowRan(password, false, expected, input, credential, preconfiguredAuthModes); VerifyBasicAuthFlowDidNotRun(password, expected, input, false, credential, preconfiguredAuthModes); } }
public void BitbucketHostProvider_GetCredentialAsync_AlwaysRefreshCredentials_IsRespected(string protocol, string host, string username, string password, string alwaysRefreshCredentials) { var input = MockInput(protocol, host, username); var context = new TestCommandContext(); if (alwaysRefreshCredentials != null) { context.Environment.Variables.Add(BitbucketConstants.EnvironmentVariables.AlwaysRefreshCredentials, alwaysRefreshCredentials); } MockStoredAccount(context, input, password); MockUserEntersValidBasicCredentials(bitbucketAuthentication, input, password); MockRemoteOAuthAccountIsValid(bitbucketApi, input, password, true); MockRemoteBasicAuthAccountIsValidNo2FA(bitbucketApi, input, password); var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object); var credential = provider.GetCredentialAsync(input); var alwaysRefreshCredentialsBool = "1".Equals(alwaysRefreshCredentials) || "on".Equals(alwaysRefreshCredentials) || "true".Equals(alwaysRefreshCredentials) ? true : false; if (alwaysRefreshCredentialsBool) { VerifyBasicAuthFlowRan(password, true, input, credential, null); } else { VerifyBasicAuthFlowNeverRan(password, input, true, null); } VerifyOAuthFlowDidNotRun(password, true, input, credential); }