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


Python UserFactory.refresh_from_db方法代码示例

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


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

示例1: TestAccountDeactivation

# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import refresh_from_db [as 别名]
class TestAccountDeactivation(TestCase):
    """
    Tests the account deactivation endpoint.
    """

    def setUp(self):
        super(TestAccountDeactivation, self).setUp()
        self.test_user = UserFactory()
        self.url = reverse('accounts_deactivation', kwargs={'username': self.test_user.username})

    def build_jwt_headers(self, user):
        """
        Helper function for creating headers for the JWT authentication.
        """
        token = JwtBuilder(user).build_token([])
        headers = {
            'HTTP_AUTHORIZATION': 'JWT ' + token
        }
        return headers

    def assert_activation_status(self, headers, expected_status=status.HTTP_200_OK, expected_activation_status=False):
        """
        Helper function for making a request to the deactivation endpoint, and asserting the status.

        Args:
            expected_status(int): Expected request's response status.
            expected_activation_status(bool): Expected user has_usable_password attribute value.
        """
        self.assertTrue(self.test_user.has_usable_password())  # pylint: disable=no-member
        response = self.client.post(self.url, **headers)
        self.assertEqual(response.status_code, expected_status)
        self.test_user.refresh_from_db()  # pylint: disable=no-member
        self.assertEqual(self.test_user.has_usable_password(), expected_activation_status)  # pylint: disable=no-member

    def test_superuser_deactivates_user(self):
        """
        Verify a user is deactivated when a superuser posts to the deactivation endpoint.
        """
        superuser = SuperuserFactory()
        headers = self.build_jwt_headers(superuser)
        self.assert_activation_status(headers)

    def test_user_with_permission_deactivates_user(self):
        """
        Verify a user is deactivated when a user with permission posts to the deactivation endpoint.
        """
        user = UserFactory()
        permission = PermissionFactory(
            codename='can_deactivate_users',
            content_type=ContentTypeFactory(
                app_label='student'
            )
        )
        user.user_permissions.add(permission)  # pylint: disable=no-member
        headers = self.build_jwt_headers(user)
        self.assertTrue(self.test_user.has_usable_password())  # pylint: disable=no-member
        self.assert_activation_status(headers)

    def test_unauthorized_rejection(self):
        """
        Verify unauthorized users cannot deactivate accounts.
        """
        headers = self.build_jwt_headers(self.test_user)
        self.assert_activation_status(
            headers,
            expected_status=status.HTTP_403_FORBIDDEN,
            expected_activation_status=True
        )

    def test_on_jwt_headers_rejection(self):
        """
        Verify users who are not JWT authenticated are rejected.
        """
        user = UserFactory()
        self.assert_activation_status(
            {},
            expected_status=status.HTTP_401_UNAUTHORIZED,
            expected_activation_status=True
        )
开发者ID:lduarte1991,项目名称:edx-platform,代码行数:81,代码来源:test_views.py


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