示例#1
0
    def test_create_signed_request(self):
        # test sending only user_id
        signed_request_user_1 = _create_signed_request(settings.FACEBOOK_APPLICATION_SECRET_KEY, user_id=1, issued_at=1254459601, add_default_data=False)
        self.assertEquals(signed_request_user_1, 'Y0ZEAYY9tGklJimbbSGy2dgpYz9qZyVJp18zrI9xQY0=.eyJpc3N1ZWRfYXQiOiAxMjU0NDU5NjAxLCAidXNlcl9pZCI6IDEsICJhbGdvcml0aG0iOiAiSE1BQy1TSEEyNTYifQ==')

        data_user_1 = _parse_signed_request(signed_request_user_1, settings.FACEBOOK_APPLICATION_SECRET_KEY)
        self.assertEquals(sorted(data_user_1.keys()), sorted([u'user_id', u'algorithm', u'issued_at']))
        self.assertEquals(data_user_1['user_id'], 1)
        self.assertEquals(data_user_1['algorithm'], 'HMAC-SHA256')

        # test not sending a user_id which will default to user_id 1
        signed_request_user_2 = _create_signed_request(settings.FACEBOOK_APPLICATION_SECRET_KEY, issued_at=1254459601, add_default_data=False)
        self.assertEquals(signed_request_user_1, signed_request_user_2)

        # test sending each available named argument
        today = datetime.datetime.now()
        tomorrow = today + datetime.timedelta(hours=1)

        signed_request_user_3 = _create_signed_request(
            app_secret = settings.FACEBOOK_APPLICATION_SECRET_KEY,
            user_id = '999',
            issued_at = 1254459600,
            expires = tomorrow,
            oauth_token = '181259711925270|1570a553ad6605705d1b7a5f.1-499729129|8XqMRhCWDKtpG-i_zRkHBDSsqqk',
            app_data = {},
            page = {
                'id': '1',
                'liked': True
            }, add_default_data=False
        )

        data_user_3 = _parse_signed_request(signed_request_user_3, settings.FACEBOOK_APPLICATION_SECRET_KEY)
        self.assertEquals(sorted(data_user_3.keys()), sorted([u'user_id', u'algorithm', u'issued_at', u'expires', u'oauth_token', u'app_data', u'page']))
        self.assertEquals(data_user_3['user_id'], '999')
        self.assertEquals(data_user_3['algorithm'], 'HMAC-SHA256')
        self.assertEquals(data_user_3['issued_at'], 1254459600)
        self.assertEquals(data_user_3['expires'], int(time.mktime(tomorrow.timetuple())))
        self.assertEquals(data_user_3['oauth_token'], '181259711925270|1570a553ad6605705d1b7a5f.1-499729129|8XqMRhCWDKtpG-i_zRkHBDSsqqk')
        self.assertEquals(data_user_3['app_data'], {})
        self.assertEquals(data_user_3['page'], {'id': '1','liked': True})

# ---------------------------------------------------------------------
# End snatched code by Reik Schatz.
# ---------------------------------------------------------------------
示例#2
0
    def _pre_setup(self):
        if self.facebook_test_user:
            if type(self.facebook_test_user) not in [str, unicode]:
                raise Exception("facebook_test_user variable must be a string (found a %s)" % type(self.facebook_test_user))

            app_name = get_app_name_from_test_case(type(self).__module__)
            facetools_fixture_name = _get_facetools_test_fixture_name(app_name)

            if not hasattr(self, 'fixtures'):
                self.fixtures = []
            if facetools_fixture_name not in self.fixtures:
                self.fixtures.append(facetools_fixture_name)
            super(FacebookTestCaseMixin, self)._pre_setup()

            # Make sure anybody that needs to sync their models loaded from fixtures
            # has a chance to do so now that the refreshed user test data is available.
            try:
                for test_user in TestUser.objects.all():
                    sync_facebook_test_user.send(sender=None, test_user=test_user)

                # Allow code to configure the test client so it has a signed request
                # of the specified test user for each request
                self.test_user = TestUser.objects.get(name=self.facebook_test_user)
                setup_facebook_test_client.send(sender=None, client=self.client, signed_request=_create_signed_request(
                    settings.FACEBOOK_APPLICATION_SECRET_KEY,
                    str(self.test_user.facebook_id),
                    oauth_token=self.test_user.access_token,
                ))
            except TestUser.DoesNotExist:
                raise TestUserNotLoaded("Test user %s hasn't been loaded via the %s fixture (did you run sync_facebook_test_users?)" %
                                        (self.facebook_test_user, facetools_fixture_name))
        else:
            super(FacebookTestCaseMixin, self)._pre_setup()
示例#3
0
 def set_client_signed_request(self, facebook_id, access_token):
     """
     Allow code to configure the test client so it has a signed request
     of the specified test user for each request
     """
     setup_facebook_test_client.send(sender=None, client=self.client, signed_request=_create_signed_request(
         settings.FACEBOOK_APPLICATION_SECRET_KEY, facebook_id, oauth_token=access_token))
示例#4
0
    def test_create_signed_request(self):
        # test sending only user_id
        signed_request_user_1 = _create_signed_request(
            settings.FACEBOOK_APPLICATION_SECRET_KEY,
            user_id=1,
            issued_at=1254459601,
            add_default_data=False)
        self.assertEquals(
            signed_request_user_1,
            'Y0ZEAYY9tGklJimbbSGy2dgpYz9qZyVJp18zrI9xQY0=.eyJpc3N1ZWRfYXQiOiAxMjU0NDU5NjAxLCAidXNlcl9pZCI6IDEsICJhbGdvcml0aG0iOiAiSE1BQy1TSEEyNTYifQ=='
        )

        data_user_1 = _parse_signed_request(
            signed_request_user_1, settings.FACEBOOK_APPLICATION_SECRET_KEY)
        self.assertEquals(sorted(data_user_1.keys()),
                          sorted([u'user_id', u'algorithm', u'issued_at']))
        self.assertEquals(data_user_1['user_id'], 1)
        self.assertEquals(data_user_1['algorithm'], 'HMAC-SHA256')

        # test not sending a user_id which will default to user_id 1
        signed_request_user_2 = _create_signed_request(
            settings.FACEBOOK_APPLICATION_SECRET_KEY,
            issued_at=1254459601,
            add_default_data=False)
        self.assertEquals(signed_request_user_1, signed_request_user_2)

        # test sending each available named argument
        today = datetime.datetime.now()
        tomorrow = today + datetime.timedelta(hours=1)

        signed_request_user_3 = _create_signed_request(
            app_secret=settings.FACEBOOK_APPLICATION_SECRET_KEY,
            user_id='999',
            issued_at=1254459600,
            expires=tomorrow,
            oauth_token=
            '181259711925270|1570a553ad6605705d1b7a5f.1-499729129|8XqMRhCWDKtpG-i_zRkHBDSsqqk',
            app_data={},
            page={
                'id': '1',
                'liked': True
            },
            add_default_data=False)

        data_user_3 = _parse_signed_request(
            signed_request_user_3, settings.FACEBOOK_APPLICATION_SECRET_KEY)
        self.assertEquals(
            sorted(data_user_3.keys()),
            sorted([
                u'user_id', u'algorithm', u'issued_at', u'expires',
                u'oauth_token', u'app_data', u'page'
            ]))
        self.assertEquals(data_user_3['user_id'], '999')
        self.assertEquals(data_user_3['algorithm'], 'HMAC-SHA256')
        self.assertEquals(data_user_3['issued_at'], 1254459600)
        self.assertEquals(data_user_3['expires'],
                          int(time.mktime(tomorrow.timetuple())))
        self.assertEquals(
            data_user_3['oauth_token'],
            '181259711925270|1570a553ad6605705d1b7a5f.1-499729129|8XqMRhCWDKtpG-i_zRkHBDSsqqk'
        )
        self.assertEquals(data_user_3['app_data'], {})
        self.assertEquals(data_user_3['page'], {'id': '1', 'liked': True})


# ---------------------------------------------------------------------
# End snatched code by Reik Schatz.
# ---------------------------------------------------------------------