Beispiel #1
0
    def test_add_etag(self):
        wrapper = wrappers.add_etag(Mock())
        wrapper.request.response.etag = None
        wrapper(result={'data': [
            {'id': 1, '_version': 1},
            {'id': 2, '_version': 1},
        ]})
        expected1 = '20d135f0f28185b84a4cf7aa51f29500'
        assert wrapper.request.response.etag == expected1

        # Etag is the same when data isn't changed
        wrapper(result={'data': [
            {'id': 1, '_version': 1},
            {'id': 2, '_version': 1},
        ]})
        assert isinstance(wrapper.request.response.etag, basestring)
        assert wrapper.request.response.etag == expected1

        # New object added
        wrapper(result={'data': [
            {'id': 1, '_version': 1},
            {'id': 2, '_version': 1},
            {'id': 3, '_version': 1},
        ]})
        assert isinstance(wrapper.request.response.etag, basestring)
        assert wrapper.request.response.etag != expected1

        # Existing object's version changed
        wrapper(result={'data': [
            {'id': 1, '_version': 1},
            {'id': 2, '_version': 2},
        ]})
        assert isinstance(wrapper.request.response.etag, basestring)
        assert wrapper.request.response.etag != expected1
Beispiel #2
0
 def test_add_etag_no_data(self):
     wrapper = wrappers.add_etag(Mock())
     wrapper.request.response.etag = None
     wrapper(result={'data': []})
     assert wrapper.request.response.etag is None
     wrapper(result={})
     assert wrapper.request.response.etag is None
Beispiel #3
0
    def test_add_etag(self):
        wrapper = wrappers.add_etag(Mock())
        wrapper.request.response.etag = None
        wrapper(result={'data': [
            {'_pk': 1, '_version': 1},
            {'_pk': 2, '_version': 1},
        ]})
        expected1 = '20d135f0f28185b84a4cf7aa51f29500'
        assert wrapper.request.response.etag == expected1

        # Etag is the same when data isn't changed
        wrapper(result={'data': [
            {'_pk': 1, '_version': 1},
            {'_pk': 2, '_version': 1},
        ]})
        assert isinstance(wrapper.request.response.etag, six.string_types)
        assert wrapper.request.response.etag == expected1

        # New object added
        wrapper(result={'data': [
            {'_pk': 1, '_version': 1},
            {'_pk': 2, '_version': 1},
            {'_pk': 3, '_version': 1},
        ]})
        assert isinstance(wrapper.request.response.etag, six.string_types)
        assert wrapper.request.response.etag != expected1

        # Existing object's version changed
        wrapper(result={'data': [
            {'_pk': 1, '_version': 1},
            {'_pk': 2, '_version': 2},
        ]})
        assert isinstance(wrapper.request.response.etag, six.string_types)
        assert wrapper.request.response.etag != expected1
Beispiel #4
0
 def test_add_etag_no_data(self):
     wrapper = wrappers.add_etag(Mock())
     wrapper.request.response.etag = None
     wrapper(result={'data': []})
     assert wrapper.request.response.etag is None
     wrapper(result={})
     assert wrapper.request.response.etag is None
Beispiel #5
0
    def setup_default_wrappers(self):
        """ Setup defaulf wrappers.

        Wrappers are applied when view method does not return instance
        of Response. In this case nefertari renderers call wrappers and
        handle response generation.

        Note: It's important for `add_etag` wrapper be applied before
        `apply_privacy` as later may remove response data that
        is used to generate etag
        """
        # Index
        self._after_calls['index'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
            wrappers.add_etag(self.request),
        ]

        # Show
        self._after_calls['show'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Create
        self._after_calls['create'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Update
        self._after_calls['update'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Replace
        self._after_calls['replace'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Privacy wrappers
        if self._auth_enabled:
            for meth in ('index', 'show', 'create', 'update', 'replace'):
                self._after_calls[meth] += [
                    wrappers.apply_privacy(self.request),
                ]
            for meth in ('update', 'replace', 'update_many'):
                self._before_calls[meth] += [
                    wrappers.apply_request_privacy(self.Model,
                                                   self._json_params),
                ]
Beispiel #6
0
    def setup_default_wrappers(self):
        """ Setup defaulf wrappers.

        Wrappers are applied when view method does not return instance
        of Response. In this case nefertari renderers call wrappers and
        handle response generation.

        Note: It's important for `add_etag` wrapper be applied before
        `apply_privacy` as later may remove response data that
        is used to generate etag
        """
        # Index
        self._after_calls['index'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
            wrappers.add_etag(self.request),
        ]

        # Show
        self._after_calls['show'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Create
        self._after_calls['create'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Update
        self._after_calls['update'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Replace
        self._after_calls['replace'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_object_url(self.request),
        ]

        # Privacy wrappers
        if self._auth_enabled:
            for meth in ('index', 'show', 'create', 'update', 'replace'):
                self._after_calls[meth] += [
                    wrappers.apply_privacy(self.request),
                ]
            for meth in ('update', 'replace', 'update_many'):
                self._before_calls[meth] += [
                    wrappers.apply_request_privacy(
                        self.Model, self._json_params),
                ]
Beispiel #7
0
    def setup_default_wrappers(self):
        self._after_calls['index'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_etag(self.request),
        ]

        self._after_calls['show'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
        ]

        self._after_calls['delete'] = [
            wrappers.add_confirmation_url(self.request)
        ]

        self._after_calls['delete_many'] = [
            wrappers.add_confirmation_url(self.request)
        ]

        self._after_calls['update_many'] = [
            wrappers.add_confirmation_url(self.request)
        ]
Beispiel #8
0
    def setup_default_wrappers(self):
        self._after_calls['index'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
            wrappers.add_etag(self.request),
        ]

        self._after_calls['show'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
        ]

        self._after_calls['delete'] = [
            wrappers.add_confirmation_url(self.request)
        ]

        self._after_calls['delete_many'] = [
            wrappers.add_confirmation_url(self.request)
        ]

        self._after_calls['update_many'] = [
            wrappers.add_confirmation_url(self.request)
        ]
Beispiel #9
0
    def setup_default_wrappers(self):
        root_resource = getattr(self, 'root_resource', None)
        auth_enabled = root_resource and root_resource.auth

        self._after_calls['index'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
        ]
        if auth_enabled:
            self._after_calls['index'] += [
                wrappers.apply_privacy(self.request),
            ]
        self._after_calls['index'] += [
            wrappers.add_etag(self.request),
        ]

        self._after_calls['show'] = [
            wrappers.wrap_in_dict(self.request),
            wrappers.add_meta(self.request),
        ]
        if auth_enabled:
            self._after_calls['show'] += [
                wrappers.apply_privacy(self.request),
            ]

        self._after_calls['delete'] = [
            wrappers.add_confirmation_url(self.request)
        ]

        self._after_calls['delete_many'] = [
            wrappers.add_confirmation_url(self.request)
        ]

        self._after_calls['update_many'] = [
            wrappers.add_confirmation_url(self.request)
        ]