django-tos/tos/tests/test_models.py

107 lines
3.6 KiB
Python
Raw Normal View History

2014-05-07 14:56:04 +00:00
from django.core.exceptions import ValidationError
from django.test import TestCase
2010-06-18 19:00:27 +00:00
2016-03-25 18:31:28 +00:00
from tos.compat import get_runtime_user_model
from tos.models import (
2016-03-27 16:41:41 +00:00
NoActiveTermsOfService,
TermsOfService,
UserAgreement,
has_user_agreed_latest_tos,
)
2014-05-07 14:56:04 +00:00
2014-08-05 21:59:49 +00:00
class TestModels(TestCase):
2010-06-18 19:00:27 +00:00
def setUp(self):
2016-03-25 18:31:28 +00:00
self.user1 = get_runtime_user_model().objects.create_user('user1',
2014-08-05 21:59:49 +00:00
'user1@example.com',
'user1pass')
2016-03-25 18:31:28 +00:00
self.user2 = get_runtime_user_model().objects.create_user('user2',
2014-08-05 21:59:49 +00:00
'user2@example.com',
'user2pass')
2016-03-25 18:31:28 +00:00
self.user3 = get_runtime_user_model().objects.create_user('user3',
2014-08-05 21:59:49 +00:00
'user3@example.com',
'user3pass')
2014-05-07 14:56:04 +00:00
2010-06-18 19:00:27 +00:00
self.tos1 = TermsOfService.objects.create(
2014-05-07 14:56:04 +00:00
content="first edition of the terms of service",
active=True
2010-06-18 19:00:27 +00:00
)
self.tos2 = TermsOfService.objects.create(
2014-05-07 14:56:04 +00:00
content="second edition of the terms of service",
active=False
)
2010-06-18 19:00:27 +00:00
def test_terms_of_service(self):
2014-05-07 14:56:04 +00:00
self.assertEquals(TermsOfService.objects.count(), 2)
2010-06-18 19:00:27 +00:00
# order is by -created
latest = TermsOfService.objects.latest()
self.assertFalse(latest.active)
2014-05-07 14:56:04 +00:00
2010-06-18 19:00:27 +00:00
# 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)
2014-05-07 14:56:04 +00:00
# latest is active though
2014-05-07 14:56:04 +00:00
self.assertTrue(latest.active)
def test_validation_error_all_set_false(self):
2014-08-05 21:59:49 +00:00
"""
If you try and set all to false the model will throw a ValidationError
"""
2014-05-07 14:56:04 +00:00
self.tos1.active = False
self.assertRaises(ValidationError, self.tos1.save)
2010-06-18 19:00:27 +00:00
def test_user_agreement(self):
2014-05-07 14:56:04 +00:00
# simple agreement
UserAgreement.objects.create(
2014-05-07 14:56:04 +00:00
terms_of_service=self.tos1,
user=self.user1
)
self.assertTrue(has_user_agreed_latest_tos(self.user1))
2014-05-07 14:56:04 +00:00
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
2014-05-07 14:56:04 +00:00
self.tos2.active = True
self.tos2.save()
self.assertFalse(has_user_agreed_latest_tos(self.user1))
2014-05-07 14:56:04 +00:00
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(
2014-05-07 14:56:04 +00:00
terms_of_service=self.tos2,
user=self.user1
)
UserAgreement.objects.create(
2014-05-07 14:56:04 +00:00
terms_of_service=self.tos2,
user=self.user3
)
2014-05-07 14:56:04 +00:00
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))
2016-03-27 16:41:41 +00:00
class TestManager(TestCase):
def test_terms_of_service_manager(self):
tos1 = TermsOfService.objects.create(
content="first edition of the terms of service",
active=True
)
self.assertEquals(TermsOfService.objects.get_current_tos(), tos1)
def test_terms_of_service_manager_raises_error(self):
self.assertRaises(NoActiveTermsOfService, TermsOfService.objects.get_current_tos)