public void ShouldNotMakeACallToEndPointWhenLoggingWithAuthTypeNone()
        {
            var request = Substitute.For<IUsergridRequest>();

            var authenticationManager = new AuthenticationManager(request);
            authenticationManager.Login(null, null, AuthType.None);

            request.DidNotReceiveWithAnyArgs().ExecuteJsonRequest<LoginResponse>(Arg.Any<string>(), Arg.Any<Method>(), Arg.Any<object>());
        }
        public void ShouldSetTheAccessToken(string accessToken, AuthType authType)
        {
            IUsergridRequest request = Helpers.InitializeUserGridRequestWithAccessToken(accessToken);

            var authenticationManager = new AuthenticationManager(request);

            authenticationManager.Login(null, null, authType);

            request.Received(1).AccessToken = accessToken;
        }
        public void ShouldNotMakeACallToEndPointWhenLoggingWithAuthTypeNone()
        {
            var request = Substitute.For <IUsergridRequest>();

            var authenticationManager = new AuthenticationManager(request);

            authenticationManager.Login(null, null, AuthType.None);

            request.DidNotReceiveWithAnyArgs().ExecuteJsonRequest <LoginResponse>(Arg.Any <string>(), Arg.Any <Method>(), Arg.Any <object>());
        }
        public void ShouldLoginWithUserCredentialsWithCorrectRequestBodyForAuthTypeUser()
        {
            IUsergridRequest request = Helpers.InitializeUserGridRequestWithAccessToken("accessToken");

            const string clientLoginId = "login";
            const string clientSecret = "secret";

            var authenticationManager = new AuthenticationManager(request);
            authenticationManager.Login(clientLoginId, clientSecret, AuthType.User);

            request
                .Received(1)
                .ExecuteJsonRequest<LoginResponse>(
                    Arg.Any<string>(),
                    Arg.Any<Method>(),
                    Arg.Is<UserLoginPayload>(d => d.GrantType == "password" && d.UserName == clientLoginId && d.Password == clientSecret));
        }
        public void ShouldLoginWithUserCredentialsWithCorrectRequestBodyForAuthTypeUser()
        {
            IUsergridRequest request = Helpers.InitializeUserGridRequestWithAccessToken("accessToken");

            const string clientLoginId = "login";
            const string clientSecret  = "secret";

            var authenticationManager = new AuthenticationManager(request);

            authenticationManager.Login(clientLoginId, clientSecret, AuthType.User);

            request
            .Received(1)
            .ExecuteJsonRequest <LoginResponse>(
                Arg.Any <string>(),
                Arg.Any <Method>(),
                Arg.Is <UserLoginPayload>(d => d.GrantType == "password" && d.UserName == clientLoginId && d.Password == clientSecret));
        }
        public void ShouldTranslateToUserGridErrorAndThrowWhenServiceReturnsBadRequest()
        {
            const string invalidUsernameOrPassword = "******";
            const string invalidGrant = "invalid_grant";

            var restResponseContent = new UsergridError {
                Description = invalidUsernameOrPassword, Error = invalidGrant
            };
            var restResponse = Helpers.SetUpRestResponseWithContent <LoginResponse>(HttpStatusCode.BadRequest, restResponseContent);
            var request      = Helpers.SetUpUsergridRequestWithRestResponse(restResponse);

            var authenticationManager = new AuthenticationManager(request);

            try
            {
                authenticationManager.Login(null, null, AuthType.Organization);
                throw new AssertionException("UserGridException was expected to be thrown here");
            }
            catch (UsergridException e)
            {
                Assert.AreEqual(invalidGrant, e.ErrorCode);
                Assert.AreEqual(invalidUsernameOrPassword, e.Message);
            }
        }
        public void ShouldSetTheAccessToken(string accessToken, AuthType authType)
        {
            IUsergridRequest request = Helpers.InitializeUserGridRequestWithAccessToken(accessToken);

            var authenticationManager = new AuthenticationManager(request);
            authenticationManager.Login(null, null, authType);

            request.Received(1).AccessToken = accessToken;
        }
        public void ShouldTranslateToUserGridErrorAndThrowWhenServiceReturnsBadRequest()
        {
            const string invalidUsernameOrPassword = "******";
            const string invalidGrant = "invalid_grant";

            var restResponseContent = new UsergridError {Description = invalidUsernameOrPassword, Error = invalidGrant};
            var restResponse = Helpers.SetUpRestResponseWithContent<LoginResponse>(HttpStatusCode.BadRequest, restResponseContent);
            var request = Helpers.SetUpUsergridRequestWithRestResponse(restResponse);

            var authenticationManager = new AuthenticationManager(request);
            try
            {
                authenticationManager.Login(null, null, AuthType.Organization);
                throw new AssertionException("UserGridException was expected to be thrown here");
            }
            catch (UsergridException e)
            {
                Assert.AreEqual(invalidGrant, e.ErrorCode);
                Assert.AreEqual(invalidUsernameOrPassword, e.Message);
            }
        }