def test_bug_811751_banned_profile(self): """A banned user's profile should not be viewable""" profile = UserProfile.objects.get(user__username='******') user = profile.user url = reverse('devmo.views.profile_view', args=(user.username,)) # Profile viewable if not banned response = self.client.get(url, follow=True) self.assertNotEqual(response.status_code, 403) # Ban User admin = User.objects.get(username='******') testuser = User.objects.get(username='******') ban = UserBan(user=testuser, by=admin, reason='Banned by unit test.', is_active=True) ban.save() # Profile not viewable if banned response = self.client.get(url, follow=True) self.assertEqual(response.status_code, 403) # Admin can view banned user's profile self.client.login(username='******', password='******') response = self.client.get(url, follow=True) self.assertNotEqual(response.status_code, 403)
def test_ban_middleware(self): """Ban middleware functions correctly.""" client = LocalizingClient() client.login(username='******', password='******') resp = client.get('/') self.assertTemplateNotUsed(resp, 'users/user_banned.html') admin = User.objects.get(username='******') testuser = User.objects.get(username='******') ban = UserBan(user=testuser, by=admin, reason='Banned by unit test.', is_active=True) ban.save() resp = client.get('/') self.assertTemplateUsed(resp, 'users/user_banned.html')
def ban_user(request, user_id): """ Ban a user. """ try: user = User.objects.get(pk=user_id) except User.DoesNotExist: raise Http404 if request.method == 'POST': form = UserBanForm(data=request.POST) if form.is_valid(): ban = UserBan(user=user, by=request.user, reason=form.cleaned_data['reason'], is_active=True) ban.save() return HttpResponseRedirect(user.get_absolute_url()) form = UserBanForm() return render(request, 'users/ban_user.html', {'form': form, 'user': user})
def test_ban_user(self): testuser = User.objects.get(username='******') admin = User.objects.get(username='******') ok_(testuser.is_active) ban = UserBan(user=testuser, by=admin, reason='Banned by unit test') ban.save() testuser_banned = User.objects.get(username='******') ok_(not testuser_banned.is_active) ban.is_active = False ban.save() testuser_unbanned = User.objects.get(username='******') ok_(testuser_unbanned.is_active)