Example #1
0
    def test_validate_request_token_not_found(self):
        token_value = "token_value"

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = None

        um = UserManager(mock_db)

        with app.test_request_context('/api', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
 def test_validate_request_if_token_missing(self):
     um = UserManager(None)
     '''more args for app.text_request_context:
              path='/', base_url=None, query_string=None,
              method='GET', input_stream=None, content_type=None,
              content_length=None, errors_stream=None, multithread=False,
              multiprocess=False, run_once=False, headers=None, data=None,
              environ_base=None, environ_overrides=None, charset='utf-8'
     '''
     with app.test_request_context('/', headers=None):
         self.assertFalse("token" in request.headers)
         self.assertFalse(um.validate_request())
Example #3
0
 def test_validate_request_if_token_missing(self):
     um = UserManager(None)
     '''more args for app.text_request_context:
              path='/', base_url=None, query_string=None,
              method='GET', input_stream=None, content_type=None,
              content_length=None, errors_stream=None, multithread=False,
              multiprocess=False, run_once=False, headers=None, data=None,
              environ_base=None, environ_overrides=None, charset='utf-8'
     '''
     with app.test_request_context('/', headers=None):
         self.assertFalse("token" in request.headers)
         self.assertFalse(um.validate_request())
Example #4
0
    def test_validate_request_token_not_found(self):
        token_value = "token_value"

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = None

        um = UserManager(mock_db)

        with app.test_request_context('/api', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_login())
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
Example #5
0
    def test_validate_request_token_expired(self):
        token_value = "token_value"
        token = UserToken(token=token_value, user=None, expire_date=datetime.utcnow() - timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_request())
            self.assertEqual(mock_db.find_first_object_by.call_count, 1)
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
Example #6
0
    def test_validate_request_token_expired(self):
        token_value = "token_value"
        token = UserToken(token=token_value, user=None, expire_date=get_now() - timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertFalse(um.validate_login())
            self.assertEqual(mock_db.find_first_object_by.call_count, 1)
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
Example #7
0
    def test_validate_request_token_valid(self):
        token_value = "token_value"
        user = User(name="test_name")
        token = UserToken(token=token_value, user=user, expire_date=get_now() + timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertTrue(um.validate_login())
            mock_db.find_first_object_by.assert_called_once_with(UserToken, token=token_value)
            self.assertEqual(g.user, user)
    def test_validate_request_token_valid(self):
        token_value = "token_value"
        user = User(name="test_name")
        token = UserToken(token=token_value,
                          user=user,
                          expire_date=datetime.utcnow() +
                          timedelta(seconds=30))

        mock_db = Mock(spec=SQLAlchemyAdapter)
        mock_db.find_first_object_by.return_value = token

        um = UserManager(mock_db)

        with app.test_request_context('/', headers={"token": token_value}):
            self.assertTrue("token" in request.headers)
            self.assertTrue(um.validate_request())
            mock_db.find_first_object_by.assert_called_once_with(
                UserToken, token=token_value)
            self.assertEqual(g.user, user)