def test_modelfilterand_ok(self):
        '''This test case ensures compound **and** filter correctly transform the filter into sql alchemy and_ statement.'''

        self._or_invoked = False

        model_filter = ModelFilterAnd(ModelFilter(self._model.id, 1, ModelFilter.EQ),
                                      ModelFilter(self._model.id, 1, ModelFilter.EQ),
                                      ModelFilter(self._model.id, 1, ModelFilter.EQ))

        query = Mock()

        def filter_fn(expr):
            self._or_invoked = True

            return Mock()

        self._model.id.table = Mock()
        query._primary_entity = query
        query.selectable = self._model.id.table
        query.filter = filter_fn

        query_new = model_filter.build(query)

        self.assertTrue(self._or_invoked)
        self.assertIsInstance(query_new, Mock)
Exemple #2
0
    def test_modelfilterand_ok(self):
        '''This test case ensures compound **and** filter correctly transform the filter into sql alchemy and_ statement.'''

        self._or_invoked = False

        model_filter = ModelFilterAnd(
            ModelFilter(self._model.id, 1, ModelFilter.EQ),
            ModelFilter(self._model.id, 1, ModelFilter.EQ),
            ModelFilter(self._model.id, 1, ModelFilter.EQ))

        query = Mock()

        def filter_fn(expr):
            self._or_invoked = True

            return Mock()

        self._model.id.table = Mock()
        query._primary_entity = query
        query.selectable = self._model.id.table
        query.filter = filter_fn

        query_new = model_filter.build(query)

        self.assertTrue(self._or_invoked)
        self.assertIsInstance(query_new, Mock)
    def test_modelfiterand_unhandled_exception(self):
        '''This test case ensures unhandled exceptions raised from ModelFilter are gracefully handled by ModelFilterAnd build.'''

        model_filter = ModelFilter(self._model.id, 1, ModelFilter.EQ)
        model_filter.get_expression = Mock(side_effect=Exception("Unhandled exception"))

        model_filter_and = ModelFilterAnd(model_filter, model_filter, model_filter)

        with self.assertRaises(FantasticoError):
            model_filter_and.build(Mock())
Exemple #4
0
    def test_modelfiterand_unhandled_exception(self):
        '''This test case ensures unhandled exceptions raised from ModelFilter are gracefully handled by ModelFilterAnd build.'''

        model_filter = ModelFilter(self._model.id, 1, ModelFilter.EQ)
        model_filter.get_expression = Mock(
            side_effect=Exception("Unhandled exception"))

        model_filter_and = ModelFilterAnd(model_filter, model_filter,
                                          model_filter)

        with self.assertRaises(FantasticoError):
            model_filter_and.build(Mock())
Exemple #5
0
    def test_modelfilterand_eq_ok(self):
        '''This test case ensures two model filters and equality works as expected.'''

        id_col = Column("id", Integer)
        name_col = Column("name", String(100))

        filter1 = ModelFilterAnd(ModelFilter(id_col, 1, ModelFilter.EQ),
                                 ModelFilter(name_col, "john", ModelFilter.EQ))

        filter2 = ModelFilterAnd(ModelFilter(id_col, 1, ModelFilter.EQ),
                                 ModelFilter(name_col, "john", ModelFilter.EQ))

        self.assertEqual(filter1, filter2)
        self.assertEqual(hash(filter1), hash(filter2))
Exemple #6
0
    def test_modelfilterand_eq_different(self):
        '''This test case ensures compound model filter and is not always equal with another compound model filter and.'''

        id_col = Column("id", Integer)
        name_col = Column("name", String(100))

        filter1 = ModelFilterAnd(ModelFilter(id_col, 1, ModelFilter.EQ),
                                 ModelFilter(id_col, 1, ModelFilter.EQ))

        filter2 = ModelFilterAnd(ModelFilter(id_col, 1, ModelFilter.EQ),
                                 ModelFilter(name_col, "john", ModelFilter.EQ))

        self.assertNotEqual(filter1, filter2)
        self.assertNotEqual(hash(filter1), hash(filter2))
Exemple #7
0
    def test_modelfilterand_eq_differenttype(self):
        '''This test case ensures compound model filter and can not be equal with a simple model filter.'''

        id_col = Column("id", Integer)
        name_col = Column("name", String(100))

        filter1 = ModelFilter(id_col, 1, ModelFilter.EQ)
        filter2 = ModelFilterAnd(filter1,
                                 ModelFilter(name_col, "john", ModelFilter.EQ))

        self.assertNotEqual(filter2, filter1)
        self.assertNotEqual(hash(filter2), hash(filter1))
    def test_retrieve_message_byfilter_and(self):
        '''This test case ensures filtering message using compound **and** works as expected.'''

        model_filter_gt = ModelFilter(ModelFacadeMessage.id, 1, ModelFilter.GT)
        model_filter_like = ModelFilter(ModelFacadeMessage.message,
                                        "%%world 4%%", ModelFilter.LIKE)
        model_filter_and = ModelFilterAnd(model_filter_gt, model_filter_like)

        records = self.model_facade.get_records_paged(
            0, 100, filter_expr=model_filter_and)

        self.assertEqual(1, len(records))
        self.assertEqual(self.last_generated_pk, records[0].id)
        self.assertEqual(self.MESSAGES[-1], records[0].message)
    def _validate_return_url(self, clienturls_facade, return_url):
        '''This test case checks the existence of return url in the list of supported return urls for idp.'''

        qmark_pos = return_url.find("?")

        if qmark_pos > -1:
            return_url = return_url[:qmark_pos]

        client_urls = clienturls_facade.get_records_paged(
                                    start_record=0, end_record=1,
                                    filter_expr=ModelFilterAnd(
                                                    ModelFilter(ClientReturnUrl.client_id, self._idp_client_id, ModelFilter.EQ),
                                                    ModelFilter(ClientReturnUrl.return_url, return_url, ModelFilter.EQ)))

        if len(client_urls) != 1:
            raise OAuth2MissingQueryParamError(self.REDIRECT_PARAM)
Exemple #10
0
    def test_authenticate_invalid_redirecturi(self):
        '''This test case ensures an exception is raised when the return url query parameter is not accepted by idp.'''

        creation_time, expiration_time = self._mock_creationexpiration_time()

        user = User(username="******", password="******")
        user.session = Mock()

        return_url = "/test/url?state=xyz"

        token = Token({
            "client_id": self._IDP_CLIENTID,
            "type": "login",
            "user_id": user.user_id,
            "creation_time": creation_time,
            "expiration_time": expiration_time
        })

        request, user_repo_cls, user_repo, tokens_service_cls, \
            tokens_service, clienturl_facade = self._mock_authenticate_dependencies(token, user, return_url)

        clienturl_facade.get_records_paged = Mock(return_value=[])

        with self.assertRaises(OAuth2MissingQueryParamError):
            self._idp_controller.authenticate(
                request,
                tokens_service_cls=tokens_service_cls,
                user_repo_cls=user_repo_cls)

        return_url = return_url[:return_url.find("?")]
        clienturl_facade.get_records_paged.assert_called_once_with(
            start_record=0,
            end_record=1,
            filter_expr=ModelFilterAnd(
                ModelFilter(ClientReturnUrl.client_id, self._IDP_CLIENTID,
                            ModelFilter.EQ),
                ModelFilter(ClientReturnUrl.return_url, return_url,
                            ModelFilter.EQ)))
    def get_collection(self, request, version, resource_url):
        '''This method provides the route for accessing a resource collection. :doc:`/features/roa/rest_standard` for collections
        are enabled by this method. The typical response format is presented below:

        .. code-block:: javascript

            var response = {"items": [
                                // resources represented as json objects.
                            ],
                            "totalItems": 100}

        If a resource is not found or the resource version does not exist the following response is returned:

        .. code-block:: javascript

            {"error_code": 10000,
             "error_description": "Resource %s version %s does not exist.",
             "error_details": "http://rcosnita.github.io/fantastico/html/features/roa/errors/error_10000.html"}
        '''

        if version != "latest":
            version = float(version)

        params = CollectionParams(request, RoaController.OFFSET_DEFAULT,
                                  RoaController.LIMIT_DEFAULT)

        resource = self._resources_registry.find_by_url(resource_url, version)

        if not resource:
            return self._handle_resource_notfound(version, resource_url)

        self._inject_security_context(request, resource.model)
        access_token = self.validate_security_context(request, "read")

        json_serializer = self._json_serializer_cls(resource)

        filter_expr = self._parse_filter(params.filter_expr, resource.model)

        if resource.user_dependent:
            if filter_expr:
                filter_expr = ModelFilterAnd(
                    filter_expr,
                    ModelFilter(resource.model.user_id, access_token.user_id,
                                ModelFilter.EQ))
            else:
                filter_expr = ModelFilter(resource.model.user_id,
                                          access_token.user_id, ModelFilter.EQ)

        sort_expr = self._parse_sort(params.order_expr, resource.model)

        model_facade = self._model_facade_cls(
            resource.model, self._get_current_connection(request))

        models = model_facade.get_records_paged(start_record=params.offset,
                                                end_record=params.offset +
                                                params.limit,
                                                filter_expr=filter_expr,
                                                sort_expr=sort_expr)
        items = [
            json_serializer.serialize(model, params.fields) for model in models
        ]

        if resource.validator:
            resource.validator().format_collection(items, request)

        models_count = model_facade.count_records(filter_expr=filter_expr)

        body = {"items": items, "totalItems": models_count}

        response = Response(text=json.dumps(body),
                            content_type="application/json",
                            status_code=200)

        self._add_cors_headers(response)

        return response
    def test_modelfilteror_wrongargtype(self):
        '''This test case ensures compound **or** filter works only with ModelFilter arguments.'''

        with self.assertRaises(FantasticoNotSupportedError):
            ModelFilterAnd(Mock(), Mock())
Exemple #13
0
    def test_modelfilterand_notenoughargs(self):
        '''This test case ensures compound **and** filter can not be built with a single argument.'''

        with self.assertRaises(FantasticoNotSupportedError):
            ModelFilterAnd(ModelFilter(self._model.id, 1, ModelFilter.EQ))
Exemple #14
0
    def test_modelfilterand_noargs(self):
        '''This test case ensures compound **and** filter can not be built without arguments.'''

        with self.assertRaises(FantasticoNotSupportedError):
            ModelFilterAnd()