Newer
Older
from __future__ import division
__copyright__ = "Copyright (C) 2018 Dong Zhuang"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.test import TestCase, override_settings
from django.contrib.auth import get_user_model
from tests.factories import UserFactory
from tests.utils import mock
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class UserModelTest(TestCase):
"""
Different from tests.test_auth, this is testing db operation.
"""
def test_email_uniqueness(self):
UserFactory.create(email="test@example.com")
with self.assertRaises(ValidationError) as error:
UserFactory.create(email="test@example.com")
self.assertEqual(
error.exception.messages,
["That email address is already in use."])
def test_email_uniqueness_case_insensitive(self):
UserFactory.create(email="test@example.com")
with self.assertRaises(ValidationError) as error:
UserFactory.create(email="Test@example.com")
self.assertEqual(
error.exception.messages,
["That email address is already in use."])
def test_institutional_uniqueness(self):
UserFactory.create(institutional_id="1234")
with self.assertRaises(IntegrityError):
UserFactory.create(institutional_id="1234")
def test_save_institutional_id_none(self):
user = UserFactory.create(institutional_id="1234")
users = get_user_model().objects.all()
self.assertTrue(users.count(), 1)
self.assertEqual(users[0].institutional_id, "1234")
user.institutional_id = " "
user.save()
self.assertIsNone(get_user_model().objects.first().institutional_id)
def test_user_must_have_email(self):
UserFactory.create()
with self.assertRaises(IntegrityError):
UserFactory.create(email=None)
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def test_custom_get_full_name_method_failed(self):
"""
Test when RELATE_USER_FULL_NAME_FORMAT_METHOD failed, default method
is used.
"""
user = UserFactory.create(first_name="my_first", last_name="my_last")
default_get_full_name = user.get_full_name()
custom_get_full_name_path = (
"tests.resource.my_customized_get_full_name_method")
get_custom_full_name_method_path = (
"accounts.utils.RelateUserMethodSettingsInitializer"
".get_custom_full_name_method")
with override_settings(
RELATE_USER_FULL_NAME_FORMAT_METHOD=custom_get_full_name_path):
from accounts.utils import relate_user_method_settings
# clear cached value
relate_user_method_settings.__dict__ = {}
# If custom method works, the returned value is different with
# default value.
self.assertNotEqual(default_get_full_name, user.get_full_name())
with mock.patch(get_custom_full_name_method_path) as mock_custom_method:
# clear cached value
relate_user_method_settings.__dict__ = {}
# raise an error when calling custom method
mock_custom_method.side_effect = Exception()
# the value falls back to default value
self.assertEqual(user.get_full_name(), default_get_full_name)
def test_custom_get_full_name_method_is_cached(self):
"""
Test relate_user_method_settings.get_custom_full_name_method is cached.
"""
user = UserFactory.create(first_name="my_first", last_name="my_last")
custom_get_full_name_path = (
"tests.resource.my_customized_get_full_name_method")
get_custom_full_name_check_path = (
"accounts.utils.RelateUserMethodSettingsInitializer"
".check_custom_full_name_method")
with override_settings(
RELATE_USER_FULL_NAME_FORMAT_METHOD=custom_get_full_name_path):
from accounts.utils import relate_user_method_settings
# clear cached value
relate_user_method_settings.__dict__ = {}
user.get_full_name()
with mock.patch(get_custom_full_name_check_path) as mock_check:
user.get_full_name()
self.assertEqual(mock_check.call_count, 0)