public void FailedAuthenticationTest()
        {
            // Given
            // create mocks
            var mockAuthenticationService = MockRepository.GenerateStub<IAuthenticationService>();
            var authenticationResultStub = new AuthenticationResult { AccessToken = null, IsValid = true, Message = Resources.Content.AuthenticationResult_Failed };
            mockAuthenticationService.Stub(x => x.Authenticate("admin", "failing-password")).Return(authenticationResultStub);

            // insert mock in the IoC container
            var bootstrapper = new Bootstrapper();
            var browser = new Browser(bootstrapper);
            bootstrapper.Container.EjectAllInstancesOf<IAuthenticationService>();
            bootstrapper.Container.Inject<IAuthenticationService>(mockAuthenticationService);

            var bodyDyn = new { username = "******", password = "******" };

            // When
            var result = TestHelper.JsonBodyPost(RouterPattern.Security.Authenticate, bodyDyn, bootstrapper, browser);

            // Then
            mockAuthenticationService.AssertWasCalled(x => x.Authenticate("admin", "failing-password"));

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            var authenticationResult = JsonConvert.DeserializeObject<AuthenticationResult>(result.Body.AsString());

            Assert.AreEqual(true, authenticationResult.IsValid);
            Assert.AreEqual(null, authenticationResult.AccessToken);
            Assert.AreEqual(Resources.Content.AuthenticationResult_Failed, authenticationResult.Message);
        }
        public void ReAuthenticationTest()
        {
            var bootstrapper = new Bootstrapper();
            var browser = new Browser(bootstrapper);
            
            var bodyDyn = new { username = "******", password = "******" };

            var result = browser.Post(RouterPattern.Security.Authenticate, with =>
            {
                with.Body(JsonConvert.SerializeObject(bodyDyn), "application/json");
                with.HttpRequest();
            });
            
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            var authenticationResult = JsonConvert.DeserializeObject<AuthenticationResult>(result.Body.AsString());

            Assert.AreEqual(true, authenticationResult.IsValid);
            Assert.AreEqual(Resources.Content.AuthenticationResult_Success, authenticationResult.Message);
        
            // re-authenticate and check whether token and issueAt are the same
            var bodyDyn2 = new { username = "******", password = "******" };

            var result2 = browser.Post(RouterPattern.Security.Authenticate, with =>
            {
                with.Body(JsonConvert.SerializeObject(bodyDyn2), "application/json");
                with.HttpRequest();
            });
            
            Assert.AreEqual(HttpStatusCode.OK, result2.StatusCode);
            var authenticationResult2 = JsonConvert.DeserializeObject<AuthenticationResult>(result2.Body.AsString());

            Assert.AreEqual(authenticationResult.IsValid, authenticationResult2.IsValid);
            Assert.AreEqual(authenticationResult.AccessToken, authenticationResult2.AccessToken);
            Assert.AreEqual(authenticationResult.Message, authenticationResult2.Message);
            Assert.IsTrue(authenticationResult.IssuedAt.Value.SqlEquals(authenticationResult2.IssuedAt.Value));
            
            // authenticate with different user and check whether token and issueAt are different
            var bodyDyn3 = new { username = "******", password = "******" };

            var result3 = browser.Post(RouterPattern.Security.Authenticate, with =>
            {
                with.Body(JsonConvert.SerializeObject(bodyDyn3), "application/json");
                with.HttpRequest();
            });

            Assert.AreEqual(HttpStatusCode.OK, result3.StatusCode);
            var authenticationResult3 = JsonConvert.DeserializeObject<AuthenticationResult>(result3.Body.AsString());

            Assert.AreEqual(true, authenticationResult3.IsValid);
            Assert.AreEqual(Resources.Content.AuthenticationResult_Success, authenticationResult3.Message);
            
            Assert.AreNotEqual(authenticationResult.AccessToken, authenticationResult3.AccessToken);
            Assert.AreNotEqual(authenticationResult.IssuedAt, authenticationResult3.IssuedAt);
            
        }
        public void ReAuthenticationTest()
        {
            var mockAuthenticationService = MockRepository.GenerateStub<IAuthenticationService>();
            var mockAuthenticationService2 = MockRepository.GenerateStub<IAuthenticationService>();
            var authenticationResultStub = new AuthenticationResult { AccessToken = "access-token", IsValid = true, Message = Resources.Content.AuthenticationResult_Success, IssuedAt = new DateTime(2014, 10, 21, 14, 55, 43) };
            var authenticationResultStub2 = new AuthenticationResult { AccessToken = "access-token", IsValid = true, Message = Resources.Content.AuthenticationResult_ExistingToken, IssuedAt = new DateTime(2014, 10, 21, 14, 55, 43) };
            mockAuthenticationService.Stub(x => x.Authenticate("admin", "success-password")).Return(authenticationResultStub);
            mockAuthenticationService2.Stub(x => x.Authenticate("admin", "success-password")).Return(authenticationResultStub2);

            // insert mock in the IoC container
            var bootstrapper = new Bootstrapper();
            var browser = new Browser(bootstrapper);
            bootstrapper.Container.EjectAllInstancesOf<IAuthenticationService>();
            bootstrapper.Container.Inject<IAuthenticationService>(mockAuthenticationService);

            var bodyDyn = new { username = "******", password = "******" };

            // When
            var result = TestHelper.JsonBodyPost(RouterPattern.Security.Authenticate, bodyDyn, bootstrapper, browser);

            // Then
            mockAuthenticationService.AssertWasCalled(x => x.Authenticate("admin", "success-password"));
            
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            var authenticationResult = JsonConvert.DeserializeObject<AuthenticationResult>(result.Body.AsString());

            Assert.AreEqual(true, authenticationResult.IsValid);
            Assert.AreEqual("access-token", authenticationResult.AccessToken);
            Assert.AreEqual(Resources.Content.AuthenticationResult_Success, authenticationResult.Message);
            Assert.AreEqual(new DateTime(2014, 10, 21, 14, 55, 43), authenticationResult.IssuedAt);

            bootstrapper.Container.EjectAllInstancesOf<IAuthenticationService>();
            bootstrapper.Container.Inject<IAuthenticationService>(mockAuthenticationService2);

            var bodyDyn2 = new { username = "******", password = "******" };

            // When
            var result2 = TestHelper.JsonBodyPost(RouterPattern.Security.Authenticate, bodyDyn2, bootstrapper, browser);

            // Then
            mockAuthenticationService2.AssertWasCalled(x => x.Authenticate("admin", "success-password"));
                        
            Assert.AreEqual(HttpStatusCode.OK, result2.StatusCode);
            var authenticationResult2 = JsonConvert.DeserializeObject<AuthenticationResult>(result2.Body.AsString());

            Assert.AreEqual(true, authenticationResult2.IsValid);
            Assert.AreEqual("access-token", authenticationResult2.AccessToken);
            Assert.AreEqual(Resources.Content.AuthenticationResult_ExistingToken, authenticationResult2.Message);
            Assert.AreEqual(new DateTime(2014, 10, 21, 14, 55, 43), authenticationResult2.IssuedAt);
        }
        public void SystemExceptionAuthenticationRequestTest()
        {
            var badRequest = new BadRequest();
            badRequest.Message = "error";
            
            var mockAuthenticationService = MockRepository.GenerateStub<IAuthenticationService>();
            mockAuthenticationService.Stub(x => x.Authenticate("admin", "success-password")).Throw(new Exception());
            mockAuthenticationService.Stub(x => x.ErrorRequest()).Return(badRequest);


            // insert mock in the IoC container
            var bootstrapper = new Bootstrapper();
            var browser = new Browser(bootstrapper);
            bootstrapper.Container.EjectAllInstancesOf<IAuthenticationService>();
            bootstrapper.Container.Inject<IAuthenticationService>(mockAuthenticationService);

            var bodyDyn = new { username = "******", password = "******" };

            // When
            var result = TestHelper.JsonBodyPost(RouterPattern.Security.Authenticate, bodyDyn, bootstrapper, browser);

            // Then
            mockAuthenticationService.AssertWasCalled(x => x.Authenticate("admin", "success-password"));
            mockAuthenticationService.AssertWasCalled(x => x.ErrorRequest());

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
            var badRequestResult = JsonConvert.DeserializeObject<BadRequest>(result.Body.AsString());
            Assert.AreEqual("error", badRequestResult.Message);
        }