def test_follow_post_invalid_request(self):
     """
     Tests to ensure that invalid post requests to the follow action
     return the appropriate error code.
     Invalid requests:
     - not containing a field called 'action', which is set to 'follow' 
     (results in error 404)
     - no user_id supplied (results in error 400)
     - given user_id matches that of the currently logged in user
     - no package_id supplied (results in error 400)
     - package_id not found in database (results in error 404)
     """
     # no action
     self.app.post(url_for('follow'), status = 404)
     # no user_id
     self.app.post(url_for('follow'), 
                   params = {'action': 'follow'}, 
                   status = 400)
     # not logged in:
     self.app.post(url_for('follow'), 
                   params = {'action': 'follow', 'user_id': 'invalid_user'}, 
                   status = 403)
     # logged in but giving a different user_id:
     environ = {'REMOTE_USER': '******'}
     user_id = get_user_id('invalid_user')
     self.app.post(url_for('follow'), 
                   extra_environ = environ,
                   params = {'action': 'follow', 'user_id': user_id}, 
                   status = 403)
     environ = {'REMOTE_USER': '******'}
     user_id = get_user_id('tester')
     self.app.post(url_for('follow'), 
                   extra_environ = environ,
                   params = {'action': 'follow', 'user_id': user_id}, 
                   status = 403)
     # valid user_id, no package_id
     environ = {'REMOTE_USER': '******'}
     self.app.post(url_for('follow'), 
                   extra_environ = environ,
                   params = {'action': 'follow', 'user_id': user_id}, 
                   status = 400)
     # package_id not found
     self.app.post(url_for('follow'), 
                   extra_environ = environ,
                   params = {'action': 'follow', 
                             'user_id': user_id,
                             'package_id': 'invalid_package_id'}, 
                   status = 404)
예제 #2
0
    def filter(self, stream):
        """
        Required to implement IGenshiStreamFilter.

        If this is the package controller with the read action:
        Adds HTML to the document HEAD, the package heading and
        the bottom of the BODY to create the follow/unfollow button
        and the follower count button.

        If this is the user controller with the read action:
        Adds HTML to the BODY to display the currently followed
        packages, if any exist.
        """
        routes = request.environ.get('pylons.routes_dict')

        # if this is the read action of a package, show follower info
        if(routes.get('controller') == 'package' and
           routes.get('action') == 'read' and 
           c.pkg.id):
            # pass data to the javascript file that creates the
            # follower count and follow/unfollow buttons
            user_id = controller.get_user_id(request.environ.get('REMOTE_USER')) or ""
            data = {'package_id': c.pkg.id,
                    'package_name': c.pkg.name,
                    'user_id': user_id}
            # add CSS styles for follower HTML
            stream = stream | Transformer('head').append(HTML(html.HEAD_CODE))
            # add jquery and follower.js links
            stream = stream | Transformer('body')\
                .append(HTML(html.BODY_CODE % data))
            # add the follower DIV to the package title, after the
            # RSS 'subscribe' link
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .append(HTML(html.FOLLOWER_CODE))
            # add the follower error DIV after the H2 tag
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .after(HTML(html.ERROR_CODE))

        # if this is the read action of a user page, show packages being followed
        if(routes.get('controller') == 'user' and
           routes.get('action') == 'read' and 
           c.user):
            user_id = controller.get_user_id(c.user)
            packages = controller.packages_followed_by(user_id)
            if packages:
                packages_html = ""
                for package_number, package in enumerate(packages):
                    # add a link to the package page
                    package_name = controller.get_package_name(package)
                    packages_html += \
                        h.link_to(package_name, 
                                  h.url_for(controller='package', action='read', 
                                            id=package_name))
                    # add comma and space if this isn't the last package in the
                    # list
                    if package_number < len(packages) - 1:
                        packages_html += ", "
                packages_followed = {'packages_followed': packages_html}
                stream = stream | Transformer('body//div[@class="activity"]//ul')\
                    .append(HTML(html.PACKAGES_FOLLOWED_CODE % packages_followed))

        return stream
 def test_follow_post_invalid_request(self):
     """
     Tests to ensure that invalid post requests to the follow action
     return the appropriate error code.
     Invalid requests:
     - not containing a field called 'action', which is set to 'follow' 
     (results in error 404)
     - no user_id supplied (results in error 400)
     - given user_id matches that of the currently logged in user
     - no package_id supplied (results in error 400)
     - package_id not found in database (results in error 404)
     """
     # no action
     self.app.post(url_for('follow'), status=404)
     # no user_id
     self.app.post(url_for('follow'),
                   params={'action': 'follow'},
                   status=400)
     # not logged in:
     self.app.post(url_for('follow'),
                   params={
                       'action': 'follow',
                       'user_id': 'invalid_user'
                   },
                   status=403)
     # logged in but giving a different user_id:
     environ = {'REMOTE_USER': '******'}
     user_id = get_user_id('invalid_user')
     self.app.post(url_for('follow'),
                   extra_environ=environ,
                   params={
                       'action': 'follow',
                       'user_id': user_id
                   },
                   status=403)
     environ = {'REMOTE_USER': '******'}
     user_id = get_user_id('tester')
     self.app.post(url_for('follow'),
                   extra_environ=environ,
                   params={
                       'action': 'follow',
                       'user_id': user_id
                   },
                   status=403)
     # valid user_id, no package_id
     environ = {'REMOTE_USER': '******'}
     self.app.post(url_for('follow'),
                   extra_environ=environ,
                   params={
                       'action': 'follow',
                       'user_id': user_id
                   },
                   status=400)
     # package_id not found
     self.app.post(url_for('follow'),
                   extra_environ=environ,
                   params={
                       'action': 'follow',
                       'user_id': user_id,
                       'package_id': 'invalid_package_id'
                   },
                   status=404)
예제 #4
0
    def filter(self, stream):
        """
        Required to implement IGenshiStreamFilter.

        If this is the package controller with the read action:
        Adds HTML to the document HEAD, the package heading and
        the bottom of the BODY to create the follow/unfollow button
        and the follower count button.

        If this is the user controller with the read action:
        Adds HTML to the BODY to display the currently followed
        packages, if any exist.
        """
        routes = request.environ.get('pylons.routes_dict')

        # if this is the read action of a package, show follower info
        if (routes.get('controller') == 'package'
                and routes.get('action') == 'read' and c.pkg.id):
            # pass data to the javascript file that creates the
            # follower count and follow/unfollow buttons
            user_id = controller.get_user_id(
                request.environ.get('REMOTE_USER')) or ""
            data = {
                'package_id': c.pkg.id,
                'package_name': c.pkg.name,
                'user_id': user_id
            }
            # add CSS styles for follower HTML
            stream = stream | Transformer('head').append(HTML(html.HEAD_CODE))
            # add jquery and follower.js links
            stream = stream | Transformer('body')\
                .append(HTML(html.BODY_CODE % data))
            # add the follower DIV to the package title, after the
            # RSS 'subscribe' link
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .append(HTML(html.FOLLOWER_CODE))
            # add the follower error DIV after the H2 tag
            stream = stream | Transformer('body//div[@id="package"]//h2[@class="head"]')\
                .after(HTML(html.ERROR_CODE))

        # if this is the read action of a user page, show packages being followed
        if (routes.get('controller') == 'user'
                and routes.get('action') == 'read' and c.user):
            user_id = controller.get_user_id(c.user)
            packages = controller.packages_followed_by(user_id)
            if packages:
                packages_html = ""
                for package_number, package in enumerate(packages):
                    # add a link to the package page
                    package_name = controller.get_package_name(package)
                    packages_html += \
                        h.link_to(package_name,
                                  h.url_for(controller='package', action='read',
                                            id=package_name))
                    # add comma and space if this isn't the last package in the
                    # list
                    if package_number < len(packages) - 1:
                        packages_html += ", "
                packages_followed = {'packages_followed': packages_html}
                stream = stream | Transformer('body//div[@class="activity"]//ul')\
                    .append(HTML(html.PACKAGES_FOLLOWED_CODE % packages_followed))

        return stream