public void GetAuthenticationHeadersReturnsBasicAuthenticationHeader()
        {
            // Arrange
            var basicAuthInfo = new BasicAuthenticationInformation(USERNAME, PASSWORD);

            // Act
            var authHeaders = basicAuthInfo.GetAuthorizationHeaders();

            // Assert
            Assert.IsNotNull(authHeaders);
            Assert.AreEqual(1, authHeaders.Keys.Count);

            Assert.AreEqual("Basic QXJiaXRyYXJ5VXNlcm5hbWU6QXJiaXRyYXJ5UGFzc3dvcmQ=", authHeaders[Client.Constants.Authentication.AUTHORIZATION_HEADER_KEY]);
        }
        public void LoginWithValidAuthenticationInformationReturnsTrue()
        {
            // Arrange
            var abiquoClient = AbiquoClientFactory.GetByVersion(AbiquoClientFactory.ABIQUO_CLIENT_VERSION_V1);

            var expectedRequestUri = string.Format("{0}{1}", ABIQUO_API_BASE_URI.TrimEnd('/'), AbiquoUriSuffixes.LOGIN);
            var basicAuthInfo      = new BasicAuthenticationInformation(USERNAME, PASSWORD);
            var user = new User()
            {
                Nick = USERNAME
            };

            var responseHeaders = Mock.Create <HttpResponseHeaders>();
            IEnumerable <string> cookieHeaderValues;

            Mock.Arrange(() => responseHeaders.TryGetValues(AbiquoHeaderKeys.SET_COOKIE_HEADER_KEY, out cookieHeaderValues))
            .Returns(new TryGetValuesDelegate((string name, out IEnumerable <string> values) =>
            {
                values = new List <string>()
                {
                    SET_COOKIE_HEADER_VALUE_1, SET_COOKIE_HEADER_VALUE_2
                };

                return(true);
            }))
            .OccursOnce();

            var restCallExecutor = Mock.Create <RestCallExecutor>();

            Mock.Arrange(() => restCallExecutor
                         .Invoke(HttpMethod.Get, expectedRequestUri, basicAuthInfo.GetAuthorizationHeaders(), null))
            .IgnoreInstance()
            .Returns(user.SerializeObject())
            .OccursOnce();

            Mock.Arrange(() => restCallExecutor.GetResponseHeaders())
            .IgnoreInstance()
            .Returns(responseHeaders)
            .OccursOnce();

            // Act
            var loginSucceeded = abiquoClient.Login(ABIQUO_API_BASE_URI, basicAuthInfo);

            // Assert
            Assert.IsTrue(loginSucceeded);

            Mock.Assert(responseHeaders);
            Mock.Assert(restCallExecutor);
        }
        public void LoginWithInvalidAuthenticationInformationReturnsFalse()
        {
            // Arrange
            var abiquoClient = AbiquoClientFactory.GetByVersion(AbiquoClientFactory.ABIQUO_CLIENT_VERSION_V1);

            var expectedRequestUri = string.Format("{0}{1}", ABIQUO_API_BASE_URI.TrimEnd('/'), AbiquoUriSuffixes.LOGIN);
            var basicAuthInfo      = new BasicAuthenticationInformation(USERNAME, PASSWORD);

            var restCallExecutor = Mock.Create <RestCallExecutor>();

            Mock.Arrange(() => restCallExecutor
                         .Invoke(HttpMethod.Get, expectedRequestUri, basicAuthInfo.GetAuthorizationHeaders(), null))
            .IgnoreInstance()
            .Throws <HttpRequestException>()
            .OccursOnce();

            // Act
            var loginSucceeded = abiquoClient.Login(ABIQUO_API_BASE_URI, basicAuthInfo);

            // Assert
            Assert.IsFalse(loginSucceeded);

            Mock.Assert(restCallExecutor);
        }