我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.test.modify_settings()。
def test_header_disappears(self): """ A logged in user is logged out automatically when the REMOTE_USER header disappears during the same browser session. """ # first we must add another authentication backend to settings self.patched_settings = modify_settings( AUTHENTICATION_BACKENDS={'append': 'django.contrib.auth.backends.ModelBackend'}, ) self.patched_settings.enable() self.headers[self.header] = self.known_user.username # Known user authenticates response = self.client.get('/remote_user/', **self.headers) self.assertEqual(response.context['user'].username, 'knownuser') # During the session, the REMOTE_USER header disappears. Should trigger logout. response = self.client.get('/remote_user/') # Django 1.10 and up deprecated is_anonymous() and use the is_anonymous property instead if django_1_10: self.assertTrue(response.context['user'].is_anonymous) else: self.assertTrue(response.context['user'].is_anonymous()) # verify the remoteuser middleware will not remove a user # authenticated via another backend User.objects.create_user(username='modeluser', password='foo') self.client.login(username='modeluser', password='foo') auth.authenticate(username='modeluser', password='foo') response = self.client.get('/remote_user/') self.assertEqual(response.context['user'].username, 'modeluser') if django_1_10: self.assertFalse(response.context['user'].is_anonymous) else: self.assertFalse(response.context['user'].is_anonymous())
def setUp(self): self.patched_settings = modify_settings( AUTHENTICATION_BACKENDS={'append': self.backend}, ) self.patched_settings.enable() self.create_users()
def setUp(self): Group.objects.bulk_create([Group(name=name) for name in ['group1', 'group2', 'group3']]) class GroupSerializer(ModelSerializer): class Meta: model = Group fields = '__all__' class GroupViewSet(ModelViewSet): queryset = Group.objects.all() serializer_class = GroupSerializer permission_classes = [DjangoObjectPermissions] filter_backends = [ChemoPermissionsFilter] self.user = User.objects.create_user(username='testuser', password='test123.') self.perm = Permission.objects.create(content_type=get_content_type(Group), name='Can view group', codename='view_group') self.access_rule = AccessRule.objects.create(ctype_source=get_content_type(User), ctype_target=get_content_type(Group), is_active=True, relation_types=[{'GROUPS': None}]) self.view = GroupViewSet self.patched_settings = modify_settings( AUTHENTICATION_BACKENDS={'append': self.backend} ) self.patched_settings.enable()
def setUp(self): super(ChemoPermissionsBackendTestCase, self).setUp() self.patched_settings = modify_settings( AUTHENTICATION_BACKENDS={'append': self.backend} ) self.patched_settings.enable() clear_neo4j_model_nodes()
def test_0005_group_views(self): module = import_module('boardinghouse.migrations.0005_group_views') # Performing this migration operation when preconditions for table move are not met # should be idempotent, and should not throw an exception. module.move_existing_to_schemata(apps, connection.schema_editor()) # Test for coverage purposes module.noop(apps, connection.schema_editor()) # Test that we get the expected values from the helper method, including when the contrib.groups # app has been installed. self.assertEqual([User.groups.through, User.user_permissions.through], module.private_auth_models(apps)) with modify_settings(PRIVATE_MODELS={'append': ['auth.groups']}): self.assertEqual([ User.groups.through, User.user_permissions.through, Group ], module.private_auth_models(apps)) self.assertEqual([User.groups.through, User.user_permissions.through], module.private_auth_models(apps)) # We need to test that we will move an existing table in public.X_X to <all-schemata>.X-X # Lets get rid of the views that normally get created: module.drop_views(apps, connection.schema_editor()) # And move the template tables into public. with connection.cursor() as cursor: for model in module.private_auth_models(apps): db_table = model._meta.db_table cursor.execute('ALTER TABLE __template__.{0} SET SCHEMA public'.format(db_table)) module.move_existing_to_schemata(apps, connection.schema_editor()) # Now re-create the views. module.create_views(apps, connection.schema_editor()) # Now test that user-group relationships are not stored unless a schema is active. user = User.objects.create_user(username='username', password='password') group = Group.objects.create(name='Group') user.groups.add(group) self.assertEqual(0, user.groups.count()) Schema.objects.create(schema='a', name='a').activate() user.groups.add(group) self.assertEqual(1, user.groups.count())