当前位置: 首页>>代码示例>>Python>>正文


Python TestClient.logout方法代码示例

本文整理汇总了Python中myjobs.tests.test_views.TestClient.logout方法的典型用法代码示例。如果您正苦于以下问题:Python TestClient.logout方法的具体用法?Python TestClient.logout怎么用?Python TestClient.logout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在myjobs.tests.test_views.TestClient的用法示例。


在下文中一共展示了TestClient.logout方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MySignOn

# 需要导入模块: from myjobs.tests.test_views import TestClient [as 别名]
# 或者: from myjobs.tests.test_views.TestClient import logout [as 别名]
class MySignOn(MyJobsBase):
    def setUp(self):
        super(MySignOn, self).setUp()
        self.user = UserFactory()
        self.auth_callback_url = 'https://secure.my.jobs/account'
        self.auth_callback = '?auth_callback=%s' % self.auth_callback_url
        self.key_qs = '%s&key=%s'

        self.client = TestClient()
        self.client.login_user(self.user)

    def test_anonymous_auth(self):
        """
        Anonymous users must first login before being redirected.

        This redirection happens automatically if JavaScript is disabled. JSON
        is returned and the redirect takes place via JavaScript otherwise.
        """
        login_data = {'username': self.user.email,
                      'password': '[email protected]',
                      'auth_callback': self.auth_callback_url,
                      'action': 'login'}

        self.client.logout()
        self.assertEqual(AuthorizedClient.objects.count(), 0)
        self.assertTrue(self.client.session.get('key') is None)

        response = self.client.post(reverse('sso_authorize'),
                                    login_data)

        self.assertEqual(AuthorizedClient.objects.count(), 1)
        self.assertTrue(self.client.session.get('key') is not None)
        self.assertEqual(response.get('Location'),
                         self.auth_callback_url + '?key=%s' %
                         self.client.session.get('key'))

        self.client.logout()
        response = self.client.post(reverse('sso_authorize'),
                                    login_data,
                                    HTTP_X_REQUESTED_WITH='XMLHttpRequest')

        content = json.loads(response.content)
        self.assertEqual(content['url'], self.auth_callback_url +
                         '?key=%s' % self.client.session['key'])

    def test_authenticated_auth(self):
        """
        Users who are already logged in can simply click a button to authorize
        a given site and are then redirected to the given callback url.

        If a given user has already authorized a given site and that site
        provides the key that it was given, this redirect is automatic.
        """
        self.assertEqual(AuthorizedClient.objects.count(), 0)
        self.assertTrue(self.client.session.get('key') is None)

        response = self.client.post(reverse('sso_authorize'),
                                    {'auth_callback': self.auth_callback_url,
                                     'action': 'authorize'})

        self.assertEqual(self.user.authorizedclient_set.count(), 1)
        self.assertTrue(self.client.session.get('key') is not None)
        self.assertEqual(response.get('Location'),
                         self.auth_callback_url + '?key=%s' %
                         self.client.session.get('key'))

        good_qs = self.key_qs % (self.auth_callback,
                                 self.client.session.get('key'))
        response = self.client.get(reverse('sso_authorize') + good_qs)
        self.assertEqual(response.get('Location'),
                         self.auth_callback_url + '?key=%s' %
                         self.client.session.get('key'))

    def test_bad_key(self):
        """
        Providing a bad key will always cause the user to have to
        log back into their account.

        Bad keys are defined as providing a key that doesn't match the
        user's current key or providing a key when the user doesn't currently
        have a key defined.
        """
        # no key
        no_key = self.key_qs % (self.auth_callback,
                                AuthorizedClient.create_key(self.user))
        response = self.client.get(reverse('sso_authorize') + no_key)
        self.assertEqual(AuthorizedClient.objects.count(), 0)

        # Ensure that user was logged out
        response = self.client.get(reverse('view_profile'))
        path = response.request.get('PATH_INFO')
        self.assertRedirects(response, reverse('home')+'?next='+path)

        # wrong key
        self.client.login_user(self.user)
        session = self.client.session
        session['key'] = AuthorizedClient.create_key(self.user)
        session.save()

        # key is a hex string; we can invalidate it by taking a substring
#.........这里部分代码省略.........
开发者ID:AstroMatchDynamics,项目名称:MyJobs,代码行数:103,代码来源:test_views.py

示例2: MyJobsHelpersTests

# 需要导入模块: from myjobs.tests.test_views import TestClient [as 别名]
# 或者: from myjobs.tests.test_views.TestClient import logout [as 别名]
class MyJobsHelpersTests(MyJobsBase):
    def setUp(self):
        super(MyJobsHelpersTests, self).setUp()
        self.client = TestClient()

        self.login_params = {'username': '[email protected]',
                             'password': '[email protected]',
                             'action': 'login'}

    def test_login_dont_remember_me(self):
        # MyJobsBase logs in a user, so we're clearing that session first
        self.client.logout()
        Session.objects.all().delete()
        self.assertEqual(Session.objects.count(), 0)
        self.client.post(reverse('home'),
                         data=self.login_params)
        self.assertEqual(Session.objects.count(), 1)

        session = Session.objects.all()[0]

        session_dict = session.get_decoded()
        user_id = session_dict['_auth_user_id']
        self.assertEqual(user_id, self.user.id)

        # session.expire_date is tz aware; datetime.datetime.now is naive
        # It probably isn't worth it to bring in pytz just for tests
        now = datetime.datetime.now(session.expire_date.tzinfo)
        diff = session.expire_date - now

        # Due to the delay between the post at the top of this test
        # and reaching this line, this can't be an assertEquals;
        self.assertTrue(880 <= diff.total_seconds() <= 900)

    def test_login_remember_me(self):
        # MyJobsBase logs in a user, so we're clearing that session first
        self.client.logout()
        Session.objects.all().delete()
        self.assertEqual(Session.objects.count(), 0)
        self.login_params['remember_me'] = True
        self.client.post(reverse('home'),
                         data=self.login_params)
        self.assertEqual(Session.objects.count(), 1)

        session = Session.objects.all()[0]

        session_dict = session.get_decoded()
        user_id = session_dict['_auth_user_id']
        self.assertEqual(user_id, self.user.id)

        weeks = (datetime.datetime.now() + datetime.timedelta(days=14))
        # Session expiration should be two weeks from now - comparing number
        # of days should be good enough
        self.assertEqual(session.expire_date.toordinal(), weeks.toordinal())

    def test_extract_value(self):
        """Tests that the proper values are extracted from an object."""

        class Foo(object):
            value = 'foo'


        class Bar(object):
            value = 'bar'
            foo = Foo()

        foo = Foo()
        bar = Bar()

        # single-level object traversal
        self.assertEqual(extract_value(foo, 'value'), 'foo')
        self.assertEqual(extract_value(foo, 'bar'), None)

        # default values
        self.assertEqual(extract_value(foo, 'bar', default='bar'), 'bar')
        self.assertEqual(extract_value(foo, 'value', default='bar'), 'foo')

        # multiple-level object traversal
        self.assertEqual(extract_value(bar, 'foo', 'value'), 'foo')
        self.assertEqual(extract_value(bar, 'foo', 'buz'), None)
开发者ID:DirectEmployers,项目名称:MyJobs,代码行数:81,代码来源:test_helpers.py


注:本文中的myjobs.tests.test_views.TestClient.logout方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。