Пример #1
0
    def post(self):
        """Create a new tag.

        Creates a new tag and sets as owner the current user.
        """
        json_data = request.get_json()
        v = RESTValidator(tag_post_schema)
        if v.validate(json_data) is False:
            raise TagValidationError(
                error_msg="Validation error for tag creation",
                status_code=400,
                error_list=v.get_errors())
        uid = current_user.get_id()
        tag_to_create = tags_api.create_tag_for_user(uid, json_data['name'])
        tag_to_return = TagRepresenation(tag_to_create)
        return tag_to_return.marshal(), 201
Пример #2
0
    def post(self):
        """Create a new tag.

        Creates a new tag and sets as owner the current user.
        """
        json_data = request.get_json()
        v = RESTValidator(tag_post_schema)
        if v.validate(json_data) is False:
            raise TagValidationError(
                error_msg="Validation error for tag creation",
                status_code=400,
                error_list=v.get_errors())
        uid = current_user.get_id()
        tag_to_create = tags_api.create_tag_for_user(uid, json_data['name'])
        tag_to_return = TagRepresenation(tag_to_create)
        return tag_to_return.marshal(), 201
Пример #3
0
    def test_pagination_flow(self):
        from invenio.modules.tags import api as tags_api
        # template of tags names
        tag_name_template = "tag{}"
        # endpoint
        endpoint = "/api/testtags/?"
        # links template
        link_template = '<{}per_page={}&page={}>; rel="{}"'
        # create tags
        for i in range(1, 7):
            tag_name = tag_name_template.format(i)
            tags_api.create_tag_for_user(self.user.id, tag_name)

        # request first page
        answer_get = self.client.get(
            url_for('testtagsresource', access_token=self.token.access_token,
                    page=1, per_page=2),
            headers=[('Content-Type', 'application/json')])
        # check to ensure correct results
        tags_names_from_request = [x['name'] for x in answer_get.json]
        links_string = answer_get.headers['Link']
        expected_names = []
        for i in range(1, 3):
            expected_name = tag_name_template.format(i)
            expected_names.append(expected_name)

        first_link = link_template.format(endpoint, 2, 1, "first")
        next_link = link_template.format(endpoint, 2, 2, "next")
        last_link = link_template.format(endpoint, 2, 3, "last")
        expected_links_string = "{0},{1},{2}".format(
            first_link,
            next_link,
            last_link
        )
        self.assertEqual(set(tags_names_from_request), set(expected_names))
        self.assertEqual(links_string, expected_links_string)

        tags_names_from_request = []
        expected_names = []
        # request second page
        answer_get = self.client.get(
            url_for('testtagsresource', access_token=self.token.access_token,
                    page=2, per_page=2),
            headers=[('Content-Type', 'application/json')])
        # check to ensure correct results
        tags_names_from_request = [x['name'] for x in answer_get.json]
        links_string = answer_get.headers['Link']
        # check if names of tags are the expected ones
        expected_names = []
        for i in range(3, 5):
            expected_name = tag_name_template.format(i)
            expected_names.append(expected_name)

        first_link = link_template.format(endpoint, 2, 1, "first")
        prev_link = link_template.format(endpoint, 2, 1, "prev")
        next_link = link_template.format(endpoint, 2, 3, "next")
        last_link = link_template.format(endpoint, 2, 3, "last")
        expected_links_string = "{0},{1},{2},{3}".format(
            first_link,
            prev_link,
            next_link,
            last_link
        )

        self.assertEqual(set(tags_names_from_request), set(expected_names))
        self.assertEqual(links_string, expected_links_string)

        tags_names_from_request = []
        expected_names = []
        # request third(last) page
        answer_get = self.client.get(
            url_for('testtagsresource', access_token=self.token.access_token,
                    page=3, per_page=2),
            headers=[('Content-Type', 'application/json')])
        # check to ensure correct results
        tags_names_from_request = [x['name'] for x in answer_get.json]
        links_string = answer_get.headers['Link']
        # check if names of tags are the expected ones
        expected_names = []
        for i in range(5, 7):
            expected_name = tag_name_template.format(i)
            expected_names.append(expected_name)

        first_link = link_template.format(endpoint, 2, 1, "first")
        prev_link = link_template.format(endpoint, 2, 2, "prev")
        last_link = link_template.format(endpoint, 2, 3, "last")
        expected_links_string = "{0},{1},{2}".format(
            first_link,
            prev_link,
            last_link
        )

        self.assertEqual(set(tags_names_from_request), set(expected_names))
        self.assertEqual(links_string, expected_links_string)

        # delete created tags
        tags_api.delete_all_tags_from_user(self.user.id)
Пример #4
0
    def test_pagination_flow(self):
        from invenio.modules.tags import api as tags_api
        # template of tags names
        tag_name_template = "tag{}"
        # endpoint
        endpoint = "/api/testtags/?"
        # links template
        link_template = '<{}per_page={}&page={}>; rel="{}"'
        # create tags
        for i in range(1, 7):
            tag_name = tag_name_template.format(i)
            tags_api.create_tag_for_user(self.user.id, tag_name)

        # request first page
        answer_get = self.client.get(
            url_for('testtagsresource',
                    access_token=self.token.access_token,
                    page=1,
                    per_page=2),
            headers=[('Content-Type', 'application/json')])
        # check to ensure correct results
        tags_names_from_request = [x['name'] for x in answer_get.json]
        links_string = answer_get.headers['Link']
        expected_names = []
        for i in range(1, 3):
            expected_name = tag_name_template.format(i)
            expected_names.append(expected_name)

        first_link = link_template.format(endpoint, 2, 1, "first")
        next_link = link_template.format(endpoint, 2, 2, "next")
        last_link = link_template.format(endpoint, 2, 3, "last")
        expected_links_string = "{0},{1},{2}".format(first_link, next_link,
                                                     last_link)
        self.assertEqual(set(tags_names_from_request), set(expected_names))
        self.assertEqual(links_string, expected_links_string)

        tags_names_from_request = []
        expected_names = []
        # request second page
        answer_get = self.client.get(
            url_for('testtagsresource',
                    access_token=self.token.access_token,
                    page=2,
                    per_page=2),
            headers=[('Content-Type', 'application/json')])
        # check to ensure correct results
        tags_names_from_request = [x['name'] for x in answer_get.json]
        links_string = answer_get.headers['Link']
        # check if names of tags are the expected ones
        expected_names = []
        for i in range(3, 5):
            expected_name = tag_name_template.format(i)
            expected_names.append(expected_name)

        first_link = link_template.format(endpoint, 2, 1, "first")
        prev_link = link_template.format(endpoint, 2, 1, "prev")
        next_link = link_template.format(endpoint, 2, 3, "next")
        last_link = link_template.format(endpoint, 2, 3, "last")
        expected_links_string = "{0},{1},{2},{3}".format(
            first_link, prev_link, next_link, last_link)

        self.assertEqual(set(tags_names_from_request), set(expected_names))
        self.assertEqual(links_string, expected_links_string)

        tags_names_from_request = []
        expected_names = []
        # request third(last) page
        answer_get = self.client.get(
            url_for('testtagsresource',
                    access_token=self.token.access_token,
                    page=3,
                    per_page=2),
            headers=[('Content-Type', 'application/json')])
        # check to ensure correct results
        tags_names_from_request = [x['name'] for x in answer_get.json]
        links_string = answer_get.headers['Link']
        # check if names of tags are the expected ones
        expected_names = []
        for i in range(5, 7):
            expected_name = tag_name_template.format(i)
            expected_names.append(expected_name)

        first_link = link_template.format(endpoint, 2, 1, "first")
        prev_link = link_template.format(endpoint, 2, 2, "prev")
        last_link = link_template.format(endpoint, 2, 3, "last")
        expected_links_string = "{0},{1},{2}".format(first_link, prev_link,
                                                     last_link)

        self.assertEqual(set(tags_names_from_request), set(expected_names))
        self.assertEqual(links_string, expected_links_string)

        # delete created tags
        tags_api.delete_all_tags_from_user(self.user.id)