django-tos/tos/tests/test_models.py
Frank Wiles 693d3aeb9d Fixed breaking logins due to initial_data
- Removed initial_data fixture in favor of a post_syncdb
    signal to add the initial 'blank' TOS
2010-11-17 12:52:46 -06:00

91 lines
3.4 KiB
Python

from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.db.models.signals import post_syncdb
from tos.models import TermsOfService, UserAgreement, has_user_agreed_latest_tos
class TestModels(TestCase):
def setUp(self):
self.user1 = User.objects.create_user('user1', 'user1@example.com', 'user1pass')
self.user2 = User.objects.create_user('user2', 'user2@example.com', 'user2pass')
self.user3 = User.objects.create_user('user3', 'user3@example.com', 'user3pass')
self.tos1 = TermsOfService.objects.create(
content = "first edition of the terms of service",
active = True
)
self.tos2 = TermsOfService.objects.create(
content = "second edition of the terms of service",
active = False
)
def test_terms_of_service(self):
tos_objects = TermsOfService.objects.all()
self.assertEquals(TermsOfService.objects.count(),3)
# order is by -created
latest = TermsOfService.objects.latest()
self.assertFalse(latest.active)
# setting a tos to True changes all others to False
latest.active = True
latest.save()
first = TermsOfService.objects.get(id=self.tos1.id)
self.assertFalse(first.active)
# latest is active though
self.assertTrue(latest.active)
def test_terms_of_service_manager(self):
self.assertEquals(TermsOfService.objects.get_current_tos(), self.tos1)
def test_validation_error_all_set_false(self):
""" If you try and set all to false the model will throw a ValidationError """
self.tos1.active=False
self.assertRaises(ValidationError, self.tos1.save)
def test_user_agreement(self):
# simple agreement
UserAgreement.objects.create(
terms_of_service = self.tos1,
user = self.user1
)
self.assertTrue(has_user_agreed_latest_tos(self.user1))
self.assertFalse(has_user_agreed_latest_tos(self.user2))
self.assertFalse(has_user_agreed_latest_tos(self.user3))
# Now set self.tos2.active to True and see what happens
self.tos2.active=True
self.tos2.save()
self.assertFalse(has_user_agreed_latest_tos(self.user1))
self.assertFalse(has_user_agreed_latest_tos(self.user2))
self.assertFalse(has_user_agreed_latest_tos(self.user3))
# add in a couple agreements and try again
UserAgreement.objects.create(
terms_of_service = self.tos2,
user = self.user1
)
UserAgreement.objects.create(
terms_of_service = self.tos2,
user = self.user3
)
self.assertTrue(has_user_agreed_latest_tos(self.user1))
self.assertFalse(has_user_agreed_latest_tos(self.user2))
self.assertTrue(has_user_agreed_latest_tos(self.user3))
class TestSyncDBSignal(TestCase):
"""
Test to ensure our post_syncdb signal is working.
"""
def test_syncdb(self):
found_tos = TermsOfService.objects.filter(active=True)
self.assertEqual(len(found_tos), 1)