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 |
| 6 | class BasicTestCase(TestCase): |
| 7 | """ |
| 8 | Basic test case class for all basic test cases. |
| 9 | """ |
21 | | >>> u.is_authenticated() |
22 | | True |
23 | | >>> u.is_staff |
24 | | False |
25 | | >>> u.is_active |
26 | | True |
27 | | >>> u.is_superuser |
28 | | False |
| 11 | def test_user(self): |
| 12 | u = User.objects.create_user('testuser', 'test@example.com', 'testpw') |
| 13 | self.assertTrue(u.has_usable_password()) |
| 14 | self.assertFalse(u.check_password('bad')) |
| 15 | self.assertTrue(u.check_password('testpw')) |
| 16 | u.set_unusable_password() |
| 17 | u.save() |
| 18 | self.assertFalse(u.check_password('testpw')) |
| 19 | self.assertFalse(u.has_usable_password()) |
| 20 | u.set_password(None) |
| 21 | self.assertFalse(u.has_usable_password()) |
| 22 | self.assertTrue(u.is_authenticated()) |
| 23 | self.assertFalse(u.is_staff) |
| 24 | self.assertTrue(u.is_active) |
| 25 | self.assertFalse(u.is_superuser) |
| 26 | |
| 27 | u2 = User.objects.create_user('testuser2', 'test2@example.com') |
| 28 | self.assertFalse(u.has_usable_password()) |
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 | | [] |
| 30 | def test_anonymous_user(self): |
| 31 | a = AnonymousUser() |
| 32 | self.assertFalse(a.is_authenticated()) |
| 33 | self.assertFalse(a.is_staff) |
| 34 | self.assertFalse(a.is_active) |
| 35 | self.assertFalse(a.is_superuser) |
| 36 | self.assertEquals(a.groups.all().count(), 0) |
| 37 | self.assertEquals(a.user_permissions.all().count(), 0) |
| 38 | |
| 39 | def test_superuser(self): |
| 40 | super = User.objects.create_superuser('super', 'super@example.com', 'super') |
| 41 | self.assertTrue(super.is_superuser) |
| 42 | self.assertTrue(super.is_active) |
| 43 | self.assertTrue(super.is_staff) |
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 |
| 45 | def test_createsuperuser_management_command(self): |
| 46 | new_io = StringIO() |
| 47 | call_command("createsuperuser", interactive=False, username="joe", email="joe@somewhere.org", stdout=new_io) |
| 48 | command_output = new_io.getvalue().strip() |
| 49 | self.assertEqual(command_output, 'Superuser created successfully.') |
| 50 | u = User.objects.get(username="joe") |
| 51 | self.assertEquals(u.email, 'joe@somewhere.org') |
| 52 | self.assertTrue(u.check_password('')) |
| 53 | new_io = StringIO() |
| 54 | call_command("createsuperuser", interactive=False, username="joe2", email="joe2@somewhere.org", verbosity=0, stdout=new_io) |
| 55 | command_output = new_io.getvalue().strip() |
| 56 | self.assertEqual(command_output, '') |
| 57 | new_io = StringIO() |
| 58 | call_command("createsuperuser", interactive=False, username="joe+admin@somewhere.org", email="joe@somewhere.org", stdout=new_io) |
| 59 | u = User.objects.get(username="joe+admin@somewhere.org") |
| 60 | self.assertEquals(u.email, 'joe@somewhere.org') |
| 61 | self.assertTrue(u.check_password('')) |
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 |
60 | | |
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 | | """ |