def test_anonymous_user(self): "Check the properties of the anonymous user" a = AnonymousUser() self.assertEqual(a.pk, None) self.assertEqual(a.username, '') self.assertEqual(a.get_username(), '') self.assertFalse(a.is_authenticated()) self.assertFalse(a.is_staff) self.assertFalse(a.is_active) self.assertFalse(a.is_superuser) self.assertEqual(a.groups.all().count(), 0) self.assertEqual(a.user_permissions.all().count(), 0)
class AnonymousUserTests(SimpleTestCase): no_repr_msg = "Django doesn't provide a DB representation for AnonymousUser." def setUp(self): self.user = AnonymousUser() def test_properties(self): self.assertIsNone(self.user.pk) self.assertEqual(self.user.username, '') self.assertEqual(self.user.get_username(), '') self.assertIs(self.user.is_anonymous, True) self.assertIs(self.user.is_authenticated, False) self.assertIs(self.user.is_staff, False) self.assertIs(self.user.is_active, False) self.assertIs(self.user.is_superuser, False) self.assertEqual(self.user.groups.all().count(), 0) self.assertEqual(self.user.user_permissions.all().count(), 0) self.assertEqual(self.user.get_user_permissions(), set()) self.assertEqual(self.user.get_group_permissions(), set()) def test_str(self): self.assertEqual(str(self.user), 'AnonymousUser') def test_eq(self): self.assertEqual(self.user, AnonymousUser()) self.assertNotEqual(self.user, User('super', '*****@*****.**', 'super')) def test_hash(self): self.assertEqual(hash(self.user), 1) def test_int(self): msg = ( 'Cannot cast AnonymousUser to int. Are you trying to use it in ' 'place of User?' ) with self.assertRaisesMessage(TypeError, msg): int(self.user) def test_delete(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.delete() def test_save(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.save() def test_set_password(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.set_password('password') def test_check_password(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.check_password('password')
def test_anonymous_user(self): "Check the properties of the anonymous user" a = AnonymousUser() self.assertEqual(a.pk, None) self.assertEqual(a.username, '') self.assertEqual(a.get_username(), '') self.assertFalse(a.is_authenticated()) self.assertFalse(a.is_staff) self.assertFalse(a.is_active) self.assertFalse(a.is_superuser) self.assertEqual(a.groups.all().count(), 0) self.assertEqual(a.user_permissions.all().count(), 0)
class AnonymousUserTests(SimpleTestCase): no_repr_msg = "Django doesn't provide a DB representation for AnonymousUser." def setUp(self): self.user = AnonymousUser() def test_properties(self): self.assertIsNone(self.user.pk) self.assertEqual(self.user.username, '') self.assertEqual(self.user.get_username(), '') self.assertIs(self.user.is_anonymous, True) self.assertIs(self.user.is_authenticated, False) self.assertIs(self.user.is_staff, False) self.assertIs(self.user.is_active, False) self.assertIs(self.user.is_superuser, False) self.assertEqual(self.user.groups.all().count(), 0) self.assertEqual(self.user.user_permissions.all().count(), 0) self.assertEqual(self.user.get_group_permissions(), set()) def test_str(self): self.assertEqual(str(self.user), 'AnonymousUser') def test_eq(self): self.assertEqual(self.user, AnonymousUser()) self.assertNotEqual(self.user, User('super', '*****@*****.**', 'super')) def test_hash(self): self.assertEqual(hash(self.user), 1) def test_int(self): msg = ( 'Cannot cast AnonymousUser to int. Are you trying to use it in ' 'place of User?' ) with self.assertRaisesMessage(TypeError, msg): int(self.user) def test_delete(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.delete() def test_save(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.save() def test_set_password(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.set_password('password') def test_check_password(self): with self.assertRaisesMessage(NotImplementedError, self.no_repr_msg): self.user.check_password('password')
def post_new(request): if request.method == "POST": form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) if request.user.is_authenticated(): post.author = request.user else: post.author = AnonymousUser.get_username() post.published_date = timezone.now() post.save() return redirect('blog.views.post_detail', pk=post.pk) else: raise form.ValidationError("Error") else: form = PostForm() return render(request, 'blog/post_edit.html', {'form':form})