public void RequestAccessToken_InvalidClientSecret_InvalidClientSecret()
        {
            var target = new UsernamePasswordAuthenticationFlow(TestConfig.ClientId, "invalid client secret", TestConfig.Username, TestConfig.Password);
            target.TokenRequestEndpointUrl = TestConfig.TokenRequestEndpointUrl;

            ExceptionAssert.IsThrowing(new SalesforceException(SalesforceError.InvalidClient, "invalid client credentials"), () =>
            {
                target.Authenticate();
            });
        }
        public void RequestAccessToken_InvalidPassword_InvalidPassword()
        {
            var target = new UsernamePasswordAuthenticationFlow(TestConfig.ClientId, TestConfig.ClientSecret, TestConfig.Username, "invalid password");
            target.TokenRequestEndpointUrl = TestConfig.TokenRequestEndpointUrl;

            ExceptionAssert.IsThrowing(new SalesforceException(SalesforceError.InvalidPassword, "authentication failure - invalid password"), () =>
            {
                target.Authenticate();
            });
        }
        public void Authenticate_InvalidUsername_AuthenticationFailure()
        {
            var target = new UsernamePasswordAuthenticationFlow(TestConfig.ClientId, TestConfig.ClientSecret, "invalid user name", TestConfig.Password);
            target.TokenRequestEndpointUrl = TestConfig.TokenRequestEndpointUrl;

            ExceptionAssert.IsThrowing(new SalesforceException(SalesforceError.AuthenticationFailure, "authentication failure"), () =>
            {
                target.Authenticate();
            });
        }
        public void RequestAccessToken_ValidCredentials_Authenticated()
        {
            var target = new UsernamePasswordAuthenticationFlow(TestConfig.ClientId, TestConfig.ClientSecret, TestConfig.Username, TestConfig.Password);
            target.TokenRequestEndpointUrl = TestConfig.TokenRequestEndpointUrl;

            var actual = target.Authenticate();
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.AccessToken.Length > 0);
            Assert.IsTrue(actual.InstanceUrl.Length > 0);
            Assert.IsTrue(Uri.IsWellFormedUriString(actual.InstanceUrl, UriKind.Absolute));
        }
        public void Authenticate_Success_AuthenticationInfo()
        {
            var response = MockRepository.GenerateMock<IRestResponse>();
            response.Expect(r => r.Content).Return("{ access_token: 'access token 1', instance_url: 'instance url 2' }");
            response.Expect(r => r.StatusCode).Return(HttpStatusCode.OK);

            var restClient = MockRepository.GenerateMock<IRestClient>();
            restClient.Expect(r => r.BaseUrl).SetPropertyWithArgument("https://login.salesforce.com/services/oauth2/token");
            restClient.Expect(r => r.Execute(null)).IgnoreArguments().Return(response);

            var target = new UsernamePasswordAuthenticationFlow(restClient, "clientId", "clientSecret", "userName", "password");            
            var actual = target.Authenticate();
            Assert.AreEqual("access token 1", actual.AccessToken);
            Assert.AreEqual("instance url 2", actual.InstanceUrl);
        }
        public void Authenticate_Failed_Exception()
        {
            var response = MockRepository.GenerateMock<IRestResponse>();
            response.Expect(r => r.Content).Return("{ error: 'authentication failure', error_description: 'authentication failed' }");
            response.Expect(r => r.StatusCode).Return(HttpStatusCode.BadRequest);

            var restClient = MockRepository.GenerateMock<IRestClient>();
            restClient.Expect(r => r.BaseUrl).SetPropertyWithArgument("tokenUrl");
            restClient.Expect(r => r.Execute(null)).IgnoreArguments().Return(response);

            var target = new UsernamePasswordAuthenticationFlow(restClient, "clientId", "clientSecret", "userName", "password");
            target.TokenRequestEndpointUrl = "tokenUrl";

            ExceptionAssert.IsThrowing(new SalesforceException(SalesforceError.AuthenticationFailure, "authentication failed"), () =>
            {
                target.Authenticate();
            });
        }