Ejemplo n.º 1
0
        public void BitbucketHostProvider_IsSupported_FailsForNullInput()
        {
            InputArguments input    = null;
            var            provider = new BitbucketHostProvider(new TestCommandContext());

            Assert.False(provider.IsSupported(input));
        }
Ejemplo n.º 2
0
        // 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);
            }
        }
Ejemplo n.º 3
0
        public void BitbucketHostProvider_IsSupported_FailsForNullHttpResponseMessage()
        {
            HttpResponseMessage httpResponseMessage = null;
            var provider = new BitbucketHostProvider(new TestCommandContext());

            Assert.False(provider.IsSupported(httpResponseMessage));
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
        public void BitbucketHostProvider_IsSupported(string protocol, string host, bool expected)
        {
            var input = new InputArguments(new Dictionary <string, string>
            {
                ["protocol"] = protocol,
                ["host"]     = host,
            });

            var provider = new BitbucketHostProvider(new TestCommandContext());

            Assert.Equal(expected, provider.IsSupported(input));
        }
Ejemplo n.º 7
0
        public void BitbucketHostProvider_IsSupported_HttpResponseMessage(string header, string value, bool expected)
        {
            var input = new HttpResponseMessage();

            if (header != null)
            {
                input.Headers.Add(header, value);
            }

            var provider = new BitbucketHostProvider(new TestCommandContext());

            Assert.Equal(expected, provider.IsSupported(input));
        }
Ejemplo n.º 8
0
        public async Task BitbucketHostProvider_StoreCredentialAsync(string protocol, string host, string username)
        {
            var input = MockInput(protocol, host, username);

            var context = new TestCommandContext();

            var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object);

            Assert.Equal(0, context.CredentialStore.Count);

            await provider.StoreCredentialAsync(input);

            Assert.Equal(1, context.CredentialStore.Count);
        }
Ejemplo n.º 9
0
        public void BitbucketHostProvider_GetSupportedAuthenticationModes(string uriString, string bitbucketAuthModes, AuthenticationModes expectedModes)
        {
            var targetUri = new Uri(uriString);

            var context = new TestCommandContext {
            };

            if (bitbucketAuthModes != null)
            {
                context.Environment.Variables.Add(BitbucketConstants.EnvironmentVariables.AuthenticationModes, bitbucketAuthModes);
            }

            var provider = new BitbucketHostProvider(context, bitbucketAuthentication.Object, bitbucketApi.Object);

            AuthenticationModes actualModes = provider.GetSupportedAuthenticationModes(targetUri);

            Assert.Equal(expectedModes, actualModes);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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);
        }
        // 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);
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
        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);
            }
        }
Ejemplo n.º 17
0
        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);
        }