2 | | BASIC_TESTS = """ |
3 | | >>> from django.contrib.auth.models import User, AnonymousUser |
4 | | >>> u = User.objects.create_user('testuser', 'test@example.com', 'testpw') |
5 | | >>> u.has_usable_password() |
6 | | True |
7 | | >>> u.check_password('bad') |
8 | | False |
9 | | >>> u.check_password('testpw') |
10 | | True |
11 | | >>> u.set_unusable_password() |
12 | | >>> u.save() |
13 | | >>> u.check_password('testpw') |
14 | | False |
15 | | >>> u.has_usable_password() |
16 | | False |
17 | | >>> u2 = User.objects.create_user('testuser2', 'test2@example.com') |
18 | | >>> u2.has_usable_password() |
19 | | False |
| 5 | class BasicTestCase(TestCase): |
| 6 | """ |
| 7 | Basic test case class for all basic test cases. |
| 8 | """ |
30 | | >>> a = AnonymousUser() |
31 | | >>> a.is_authenticated() |
32 | | False |
33 | | >>> a.is_staff |
34 | | False |
35 | | >>> a.is_active |
36 | | False |
37 | | >>> a.is_superuser |
38 | | False |
39 | | >>> a.groups.all() |
40 | | [] |
41 | | >>> a.user_permissions.all() |
42 | | [] |
| 16 | def test_user(self): |
| 17 | u = User.objects.create_user('testuser', 'test@example.com', 'testpw') |
| 18 | self.assertTrue(u.has_usable_password()) |
| 19 | self.assertFalse(u.check_password('bad')) |
| 20 | self.assertTrue(u.check_password('testpw')) |
| 21 | u.set_unusable_password() |
| 22 | u.save() |
| 23 | self.assertFalse(u.check_password('testpw')) |
| 24 | self.assertFalse(u.has_usable_password()) |
| 25 | u.set_password(None) |
| 26 | self.assertFalse(u.has_usable_password()) |
| 27 | self.assertTrue(u.is_authenticated()) |
| 28 | self.assertFalse(u.is_staff) |
| 29 | self.assertTrue(u.is_active) |
| 30 | self.assertFalse(u.is_superuser) |
| 31 | |
| 32 | u2 = User.objects.create_user('testuser2', 'test2@example.com') |
| 33 | self.assertFalse(u.has_usable_password()) |
44 | | # superuser tests. |
45 | | >>> super = User.objects.create_superuser('super', 'super@example.com', 'super') |
46 | | >>> super.is_superuser |
47 | | True |
48 | | >>> super.is_active |
49 | | True |
50 | | >>> super.is_staff |
51 | | True |
| 35 | def test_anonymous_user(self): |
| 36 | a = AnonymousUser() |
| 37 | self.assertFalse(a.is_authenticated()) |
| 38 | self.assertFalse(a.is_staff) |
| 39 | self.assertFalse(a.is_active) |
| 40 | self.assertFalse(a.is_superuser) |
| 41 | self.assertEquals(a.groups.all().count(), 0) |
| 42 | self.assertEquals(a.user_permissions.all().count(), 0) |
| 43 | |
| 44 | def test_superuser(self): |
| 45 | super = User.objects.create_superuser('super', 'super@example.com', 'super') |
| 46 | self.assertTrue(super.is_superuser) |
| 47 | self.assertTrue(super.is_active) |
| 48 | self.assertTrue(super.is_staff) |
53 | | # |
54 | | # Tests for createsuperuser management command. |
55 | | # It's nearly impossible to test the interactive mode -- a command test helper |
56 | | # would be needed (and *awesome*) -- so just test the non-interactive mode. |
57 | | # This covers most of the important validation, but not all. |
58 | | # |
59 | | >>> from django.core.management import call_command |
| 50 | def test_createsuperuser_management_command(self): |
| 51 | call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org") |
| 52 | u = User.objects.get(username="joe") |
| 53 | self.assertEquals(u.email, 'joe@somewhere.org') |
| 54 | self.assertEquals(u.password, '!') |
| 55 | call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org") |
| 56 | u = User.objects.get(username="joe+admin@somewhere.org") |
| 57 | self.assertEquals(u.email, 'joe@somewhere.org') |
| 58 | self.assertEquals(u.password, '!') |
61 | | >>> call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org") |
62 | | Superuser created successfully. |
63 | | |
64 | | >>> u = User.objects.get(username="joe") |
65 | | >>> u.email |
66 | | u'joe@somewhere.org' |
67 | | >>> u.password |
68 | | u'!' |
69 | | >>> call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org") |
70 | | Superuser created successfully. |
71 | | |
72 | | >>> u = User.objects.get(username="joe+admin@somewhere.org") |
73 | | >>> u.email |
74 | | u'joe@somewhere.org' |
75 | | >>> u.password |
76 | | u'!' |
77 | | """ |