Пример #1
0
    def process_response(self, request, response):
        if self.should_process_response(response):
            # Get the oauth url fandjango is redirecting the user to
            oauth_url = self.get_oauth_url(response.content,
                                           "window.parent.location =", ';')

            # Update the oauth url so that it's url it goes to after
            # the user authorizes the app is a translated facebook url
            redirect_uri = urlparse.parse_qs(
                urlparse.urlparse(oauth_url).query)['redirect_uri'][0]
            path = urlparse.urlparse(redirect_uri).path
            path = '/' + "/".join(path.split("/")[2:])
            if not path.endswith("/"): path = path + "/"
            new_url = translate_url_to_facebook_url(path)

            # Replace the old url with the new one
            start_token = "redirect_uri="
            start = oauth_url.index(start_token) + len(start_token)
            end = oauth_url.index("&", start)
            new_oauth_url = oauth_url.replace(oauth_url[start:end],
                                              urllib.quote_plus(new_url))
            response.content = response.content.replace(
                oauth_url, new_oauth_url)
            response.status_code = 200

        return response
Пример #2
0
def replacement_URLNode_render_method(self, context):
    """
    Replacement for the URLNode's render method.  It's monkey patched into
    a URLNode instance in the facebook_url tag.
    """
    # NOTE: Pretend this method exists inside the URLNode class (because when it's called it will be)
    url_str = self.old_render(context)
    if url_str:
        url_str = translate_url_to_facebook_url(url_str)
    return url_str
Пример #3
0
 def assert_url_translations(self, expected_url, urls_to_convert):
     values = (
         ("https://apps.facebook.com/django-facetools/", "https://apps.facebook.com/django-facetools"),
         ("http://localhost:8000/canvas/", "http://localhost:8000/canvas")
     )
     for facebook_canvas_page in values[0]:
         for facebook_canvas_url in values[1]:
             for url_to_convert in urls_to_convert:
                 settings.FACEBOOK_CANVAS_PAGE = facebook_canvas_page
                 settings.FACEBOOK_CANVAS_URL = facebook_canvas_url
                 self.assertEquals(expected_url, translate_url_to_facebook_url(url_to_convert))
Пример #4
0
 def process_response(self, request, response):
     if type(response) == HttpResponseRedirect:
         facebook_url = translate_url_to_facebook_url(response['Location'])
         if facebook_url != response['Location']:
             get_params = urllib.urlencode({GET_REDIRECT_PARAM: facebook_url})
             prefix = "?"
             insert_position = len(response['Location'])
             if '?' in response['Location']:
                 prefix = "&"
             if "#" in response['Location']:
                 insert_position = response['Location'].index("#")
             response['Location'] = response['Location'][:insert_position] + \
                                    prefix + get_params + \
                                    response['Location'][insert_position:]
     return response
Пример #5
0
    def process_response(self, request, response):
        if self.should_process_response(response):
            # Get the oauth url fandjango is redirecting the user to
            oauth_url = self.get_oauth_url(response.content, "window.parent.location =", ';')

            # Update the oauth url so that it's url it goes to after
            # the user authorizes the app is a translated facebook url
            redirect_uri = urlparse.parse_qs(urlparse.urlparse(oauth_url).query)['redirect_uri'][0]
            path = urlparse.urlparse(redirect_uri).path
            path = '/' + "/".join(path.split("/")[2:])
            if not path.endswith("/"): path = path + "/"
            new_url = translate_url_to_facebook_url(path)

            # Replace the old url with the new one
            start_token = "redirect_uri="
            start = oauth_url.index(start_token) + len(start_token)
            end = oauth_url.index("&", start)
            new_oauth_url = oauth_url.replace(oauth_url[start:end], urllib.quote_plus(new_url))
            response.content = response.content.replace(oauth_url, new_oauth_url)
            response.status_code = 200

        return response
Пример #6
0
    def test_translate_url_to_facebook_url(self):
        # Test plain urls
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view/",
            ("/canvas/view/", "http://localhost:8000/canvas/view/")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view",
            ("/canvas/view", "http://localhost:8000/canvas/view")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/",
            ("/canvas/", "http://localhost:8000/canvas/")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools",
            ("/canvas", "http://localhost:8000/canvas")
        )
        # Test urls with get parameters
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view/?hi=hey&ho=peter",
            ("/canvas/view/?hi=hey&ho=peter", "http://localhost:8000/canvas/view/?hi=hey&ho=peter")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view?hi=hey&ho=peter",
            ("/canvas/view?hi=hey&ho=peter", "http://localhost:8000/canvas/view?hi=hey&ho=peter")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/?hi=hey&ho=peter",
            ("/canvas/?hi=hey&ho=peter", "http://localhost:8000/canvas/?hi=hey&ho=peter")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools?hi=hey&ho=peter",
            ("/canvas?hi=hey&ho=peter", "http://localhost:8000/canvas?hi=hey&ho=peter")
        )
        # Test urls with fragments
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view/#bells",
            ("/canvas/view/#bells", "http://localhost:8000/canvas/view/#bells")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view#bells",
            ("/canvas/view#bells", "http://localhost:8000/canvas/view#bells")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/#bells",
            ("/canvas/#bells", "http://localhost:8000/canvas/#bells")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools#bells",
            ("/canvas#bells", "http://localhost:8000/canvas#bells")
        )
        # Test urls with get parameters and fragments
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view/?hi=hey&ho=peter#bells",
            ("/canvas/view/?hi=hey&ho=peter#bells", "http://localhost:8000/canvas/view/?hi=hey&ho=peter#bells")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/view?hi=hey&ho=peter#bells",
            ("/canvas/view?hi=hey&ho=peter#bells", "http://localhost:8000/canvas/view?hi=hey&ho=peter#bells")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools/?hi=hey&ho=peter#bells",
            ("/canvas/?hi=hey&ho=peter#bells", "http://localhost:8000/canvas/?hi=hey&ho=peter#bells")
        )
        self.assert_url_translations(
            "https://apps.facebook.com/django-facetools?hi=hey&ho=peter#bells",
            ("/canvas?hi=hey&ho=peter#bells", "http://localhost:8000/canvas?hi=hey&ho=peter#bells")
        )

        # Test that URLS outside of the canvas don't get converted
        url = "http://google.com"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "http://www.google.com"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "https://www.google.com"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "https://www.google.com/whateves"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "https://www.google.com/whateves/"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "https://google.com/whateves"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "https://google.com/whateves/"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "www.google.com"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "www.google.com/hey"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "google.com/yo"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "google.com/loud/noises/"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "/not_canvas/view"
        self.assertEquals(url, translate_url_to_facebook_url(url))
        url = "/not_canvas/view/"
        self.assertEquals(url, translate_url_to_facebook_url(url))

        # Spot check
        url = "/canvas/view/"
        self.assertNotEquals(url, translate_url_to_facebook_url(url))
        url = "/canvas/"
        self.assertNotEquals(url, translate_url_to_facebook_url(url))