private LoginService.IClientLoginService MockClientLoginService(LoginService.LoginResponse response)
 {
     return(Mock.Of <LoginService.IClientLoginService>(l =>
                                                       l.LoginAsync(It.IsAny <LoginService.LoginRequest>()) ==
                                                       Task.FromResult(response)
                                                       ));
 }
        public void AuthenticateReturnsBadRequestWhenAuthenticationRequestNull()
        {
            var clientLoginResponse = new LoginService.LoginResponse()
            {
                Status = LoginService.AuthenticationStatus.InvalidPassword
            };
            var service    = MockClientLoginService(clientLoginResponse);
            var controller = new AuthenticationController(service);

            var result = controller.Authenticate(null).Result;

            Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult),
                                    "Authenticate should return bad request when the authentication request is null");
        }
        public void AuthenticateReturnsUnauthorizedWhenAuthenticationFails()
        {
            var clientLoginResponse = new LoginService.LoginResponse()
            {
                Status = LoginService.AuthenticationStatus.InvalidPassword
            };
            var service    = MockClientLoginService(clientLoginResponse);
            var controller = new AuthenticationController(service);

            var result = controller.Authenticate(new Models.AuthenticationRequest()
            {
                Username = "******",
                Password = "******"
            }).Result;

            Assert.IsInstanceOfType(result, typeof(UnauthorizedResult),
                                    "Authenticate should return unauthorized when authentication fails");
        }
        public void AuthenticateBodyContainsClientLoginResponseWhenAuthenticationSucceeds()
        {
            var clientLoginResponse = new LoginService.LoginResponse()
            {
                Status = LoginService.AuthenticationStatus.Success
            };
            var service    = MockClientLoginService(clientLoginResponse);
            var controller = new AuthenticationController(service);

            var result = controller.Authenticate(new Models.AuthenticationRequest()
            {
                Username = "******",
                Password = "******"
            }).Result as OkNegotiatedContentResult <LoginService.LoginResponse>;

            Assert.AreEqual(clientLoginResponse, result.Content,
                            "Authenticate should return the client login response when authentication succeeds");
        }
        public void AuthenticateReturnsOkWhenAuthenticationSucceeds()
        {
            var clientLoginResponse = new LoginService.LoginResponse()
            {
                Status = LoginService.AuthenticationStatus.Success
            };
            var service    = MockClientLoginService(clientLoginResponse);
            var controller = new AuthenticationController(service);

            var result = controller.Authenticate(new Models.AuthenticationRequest()
            {
                Username = "******",
                Password = "******"
            }).Result;

            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult <LoginService.LoginResponse>),
                                    "Authenticate should return ok when authentication succeeds");
        }
        public void AuthenticateCallsClientLoginService()
        {
            var clientLoginResponse = new LoginService.LoginResponse()
            {
                Status = LoginService.AuthenticationStatus.Success
            };
            var service    = MockClientLoginService(clientLoginResponse);
            var controller = new AuthenticationController(service);

            var response = controller.Authenticate(new Models.AuthenticationRequest()
            {
                Username = "******",
                Password = "******"
            }).Result;

            Mock.Get(service).Verify(s =>
                                     s.LoginAsync(It.Is <LoginService.LoginRequest>(lr =>
                                                                                    lr.Username == "username" && lr.Password == "password")
                                                  )
                                     );
        }