2015-01-22 18:29:12 +01:00
|
|
|
from unittest import TestCase
|
2015-02-12 20:57:05 +01:00
|
|
|
from unittest.mock import MagicMock, call, patch
|
2015-01-22 18:29:12 +01:00
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
|
2016-11-04 13:26:44 +01:00
|
|
|
from openslides.users.models import UserManager
|
2015-01-22 18:29:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserManagerTest(TestCase):
|
|
|
|
def test_create_user(self):
|
|
|
|
"""
|
2015-02-12 20:57:05 +01:00
|
|
|
Tests that create_user saves a new user with a set password.
|
2015-01-22 18:29:12 +01:00
|
|
|
"""
|
|
|
|
user = MagicMock()
|
|
|
|
user_manager = UserManager()
|
|
|
|
user_manager.model = MagicMock(return_value=user)
|
2019-01-06 16:22:33 +01:00
|
|
|
user_manager._db = "my_test_db"
|
2015-01-22 18:29:12 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
return_user = user_manager.create_user(
|
|
|
|
"test_username", "test_password", test_kwarg="test_kwarg"
|
|
|
|
)
|
2015-01-22 18:29:12 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
user_manager.model.assert_called_once_with(
|
|
|
|
username="test_username", test_kwarg="test_kwarg"
|
|
|
|
)
|
|
|
|
user.set_password.assert_called_once_with("test_password")
|
|
|
|
user.save.assert_called_once_with(using="my_test_db", skip_autoupdate=False)
|
2015-01-22 18:29:12 +01:00
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
return_user, user, "The returned user is not the created user."
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserManagerGenerateUsername(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the manager method generate_username.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.exists_mock = MagicMock()
|
|
|
|
self.filter_mock = MagicMock(return_value=self.exists_mock)
|
|
|
|
self.manager = UserManager()
|
|
|
|
self.manager.filter = self.filter_mock
|
|
|
|
|
|
|
|
def test_clear_strings(self):
|
|
|
|
self.exists_mock.exists.return_value = False
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.manager.generate_username(
|
|
|
|
"wiaf9eecu9mooJiZ3Lah", "ieHaVe9ci7mooPhe0AuY"
|
|
|
|
),
|
|
|
|
"wiaf9eecu9mooJiZ3Lah ieHaVe9ci7mooPhe0AuY",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
def test_unstripped_strings(self):
|
|
|
|
self.exists_mock.exists.return_value = False
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.manager.generate_username(
|
|
|
|
"ouYeuwai0pheukeeShah ", " Waefa8gahj8ohRaeroca\n"
|
|
|
|
),
|
|
|
|
"ouYeuwai0pheukeeShah Waefa8gahj8ohRaeroca",
|
|
|
|
"The returned value should only have one whitespace between the " "names.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
def test_empty_second_string(self):
|
|
|
|
self.exists_mock.exists.return_value = False
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.manager.generate_username("foobar", ""),
|
|
|
|
"foobar",
|
|
|
|
"The returned value should not have whitespaces at the end.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
def test_empty_first_string(self):
|
|
|
|
self.exists_mock.exists.return_value = False
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.manager.generate_username("", "foobar"),
|
|
|
|
"foobar",
|
|
|
|
"The returned value should not have whitespaces at the beginning.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
def test_two_empty_strings(self):
|
|
|
|
self.exists_mock.exists.return_value = False
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
with self.assertRaises(ValueError, msg="A ValueError should be raised."):
|
|
|
|
self.manager.generate_username("", "")
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
def test_used_username(self):
|
|
|
|
self.exists_mock.exists.side_effect = (True, False)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.manager.generate_username("user", "name"),
|
|
|
|
"user name 1",
|
|
|
|
"If the username already exists, a number should be added to the " "name.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
def test_two_used_username(self):
|
|
|
|
self.exists_mock.exists.side_effect = (True, True, False)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.manager.generate_username("user", "name"),
|
|
|
|
"user name 2",
|
2015-02-12 20:57:05 +01:00
|
|
|
"If the username with a number already exists, a higher number "
|
2019-01-06 16:22:33 +01:00
|
|
|
"should be added to the name.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
def test_umlauts(self):
|
|
|
|
self.exists_mock.exists.return_value = False
|
|
|
|
|
|
|
|
self.assertEqual(
|
2019-01-06 16:22:33 +01:00
|
|
|
self.manager.generate_username("äöü", "ßüäö"),
|
|
|
|
"äöü ßüäö",
|
|
|
|
"The method gen_username has also to work with umlauts.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.users.models.choice")
|
2015-02-12 20:57:05 +01:00
|
|
|
class UserManagerGeneratePassword(TestCase):
|
|
|
|
def test_normal(self, mock_choice):
|
|
|
|
"""
|
|
|
|
Test normal run of the method.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
mock_choice.side_effect = tuple("test_password")
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(UserManager().generate_password(), "test_pas")
|
2015-02-12 20:57:05 +01:00
|
|
|
# choice has to be called 8 times
|
|
|
|
mock_choice.assert_has_calls(
|
2019-01-06 16:22:33 +01:00
|
|
|
[
|
|
|
|
call("abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789")
|
|
|
|
for _ in range(8)
|
|
|
|
]
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.users.models.Permission")
|
|
|
|
@patch("openslides.users.models.Group")
|
2015-02-12 20:57:05 +01:00
|
|
|
class UserManagerCreateOrResetAdminUser(TestCase):
|
2016-08-30 09:16:47 +02:00
|
|
|
def test_add_admin_group(self, mock_group, mock_permission):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2018-10-09 13:44:38 +02:00
|
|
|
Tests that the Group with pk=2 (Admin group) is added to the admin.
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
|
|
|
admin_user = MagicMock()
|
|
|
|
manager = UserManager()
|
2018-11-04 14:02:30 +01:00
|
|
|
manager.get = MagicMock(return_value=(admin_user))
|
2016-08-30 09:16:47 +02:00
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
manager.create_or_reset_admin_user()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
admin_user.groups.add.assert_called_once_with(
|
|
|
|
2
|
|
|
|
) # the admin should be added to the admin group with pk=2
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2016-08-30 09:16:47 +02:00
|
|
|
def test_password_set_to_admin(self, mock_group, mock_permission):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
|
|
|
Tests that the password of the admin is set to 'admin'.
|
|
|
|
"""
|
|
|
|
admin_user = MagicMock()
|
|
|
|
manager = UserManager()
|
2018-11-04 14:02:30 +01:00
|
|
|
manager.get = MagicMock(return_value=(admin_user))
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2016-08-30 09:16:47 +02:00
|
|
|
staff_group = MagicMock(name="Staff")
|
|
|
|
mock_group.objects.get_or_create = MagicMock(return_value=(staff_group, True))
|
|
|
|
mock_permission.get = MagicMock()
|
|
|
|
|
2019-03-19 20:26:12 +01:00
|
|
|
manager.create_or_reset_admin_user(skip_autoupdate=True)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(admin_user.default_password, "admin")
|
2018-11-04 14:02:30 +01:00
|
|
|
admin_user.save.assert_called_once_with(skip_autoupdate=True)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.users.models.User")
|
2016-08-30 09:16:47 +02:00
|
|
|
def test_return_value(self, mock_user, mock_group, mock_permission):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
|
|
|
Tests that the method returns True when a user is created.
|
|
|
|
"""
|
|
|
|
manager = UserManager()
|
2018-11-04 14:02:30 +01:00
|
|
|
manager.get = MagicMock(side_effect=ObjectDoesNotExist)
|
2015-02-12 20:57:05 +01:00
|
|
|
manager.model = mock_user
|
|
|
|
|
2016-08-30 09:16:47 +02:00
|
|
|
staff_group = MagicMock(name="Staff")
|
|
|
|
mock_group.objects.get_or_create = MagicMock(return_value=(staff_group, True))
|
|
|
|
mock_permission.get = MagicMock()
|
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
self.assertEqual(
|
|
|
|
manager.create_or_reset_admin_user(),
|
|
|
|
True,
|
|
|
|
"The method create_or_reset_admin_user should return True when a "
|
2019-01-06 16:22:33 +01:00
|
|
|
"new user is created.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.users.models.User")
|
2016-08-30 09:16:47 +02:00
|
|
|
def test_attributes_of_created_user(self, mock_user, mock_group, mock_permission):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
|
|
|
Tests username and last_name of the created admin user.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
admin_user = MagicMock(username="admin", last_name="Administrator")
|
2015-02-12 20:57:05 +01:00
|
|
|
manager = UserManager()
|
2018-11-04 14:02:30 +01:00
|
|
|
manager.get = MagicMock(side_effect=ObjectDoesNotExist)
|
2015-02-12 20:57:05 +01:00
|
|
|
manager.model = mock_user
|
|
|
|
|
2016-08-30 09:16:47 +02:00
|
|
|
staff_group = MagicMock(name="Staff")
|
|
|
|
mock_group.objects.get_or_create = MagicMock(return_value=(staff_group, True))
|
|
|
|
mock_permission.get = MagicMock()
|
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
manager.create_or_reset_admin_user()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
admin_user.username,
|
2019-01-06 16:22:33 +01:00
|
|
|
"admin",
|
|
|
|
"The username of a new created admin should be 'admin'.",
|
|
|
|
)
|
2015-02-12 20:57:05 +01:00
|
|
|
self.assertEqual(
|
|
|
|
admin_user.last_name,
|
2019-01-06 16:22:33 +01:00
|
|
|
"Administrator",
|
|
|
|
"The last_name of a new created admin should be 'Administrator'.",
|
|
|
|
)
|