Beispiel #1
0
    def test_add_admin_group(self, mock_group, mock_permission):
        """
        Tests that the Group with pk=2 (Admin group) is added to the admin.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))

        manager.create_or_reset_admin_user()

        admin_user.groups.add.assert_called_once_with(2)  # the admin should be added to the admin group with pk=2
Beispiel #2
0
    def test_password_set_to_admin(self, mock_group):
        """
        Tests that the password of the admin is set to 'admin'.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))

        manager.create_or_reset_admin_user()

        self.assertEqual(admin_user.default_password, 'admin')
        admin_user.save.assert_called_once_with()
Beispiel #3
0
    def test_return_value(self, mock_user, mock_group):
        """
        Tests that the method returns True when a user is created.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, True))
        manager.model = mock_user

        self.assertEqual(
            manager.create_or_reset_admin_user(), True,
            "The method create_or_reset_admin_user should return True when a "
            "new user is created.")
Beispiel #4
0
    def test_return_value(self, mock_user, mock_group):
        """
        Tests that the method returns True when a user is created.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, True))
        manager.model = mock_user

        self.assertEqual(
            manager.create_or_reset_admin_user(),
            True,
            "The method create_or_reset_admin_user should return True when a "
            "new user is created.")
Beispiel #5
0
    def test_password_set_to_admin(self, mock_group):
        """
        Tests that the password of the admin is set to 'admin'.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))

        manager.create_or_reset_admin_user()

        self.assertEqual(
            admin_user.default_password,
            'admin')
        admin_user.save.assert_called_once_with()
    def test_add_admin_group(self, mock_group, mock_permission):
        """
        Tests that the Group with name='Staff' is added to the admin.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))

        staff_group = MagicMock(name="Staff")
        mock_group.objects.get_or_create = MagicMock(return_value=(staff_group, True))
        mock_permission.get = MagicMock()

        manager.create_or_reset_admin_user()

        admin_user.groups.add.assert_called_once_with(staff_group)
Beispiel #7
0
    def test_attributes_of_created_user(self, mock_user, mock_group):
        """
        Tests username and last_name of the created admin user.
        """
        admin_user = MagicMock(username='******', last_name='Administrator')
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, True))
        manager.model = mock_user

        manager.create_or_reset_admin_user()

        self.assertEqual(
            admin_user.username, 'admin',
            "The username of a new created admin should be 'admin'.")
        self.assertEqual(
            admin_user.last_name, 'Administrator',
            "The last_name of a new created admin should be 'Administrator'.")
Beispiel #8
0
    def test_password_set_to_admin(self, mock_group, mock_permission):
        """
        Tests that the password of the admin is set to 'admin'.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))

        staff_group = MagicMock(name="Staff")
        mock_group.objects.get_or_create = MagicMock(return_value=(staff_group,
                                                                   True))
        mock_permission.get = MagicMock()

        manager.create_or_reset_admin_user()

        self.assertEqual(admin_user.default_password, 'admin')
        admin_user.save.assert_called_once_with()
    def test_return_value(self, mock_user, mock_group, mock_permission):
        """
        Tests that the method returns True when a user is created.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, True))
        manager.model = mock_user

        staff_group = MagicMock(name="Staff")
        mock_group.objects.get_or_create = MagicMock(return_value=(staff_group, True))
        mock_permission.get = MagicMock()

        self.assertEqual(
            manager.create_or_reset_admin_user(),
            True,
            "The method create_or_reset_admin_user should return True when a "
            "new user is created.")
Beispiel #10
0
    def test_password_set_to_admin(self, mock_group, mock_permission):
        """
        Tests that the password of the admin is set to 'admin'.
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))

        staff_group = MagicMock(name="Staff")
        mock_group.objects.get_or_create = MagicMock(return_value=(staff_group, True))
        mock_permission.get = MagicMock()

        manager.create_or_reset_admin_user()

        self.assertEqual(
            admin_user.default_password,
            'admin')
        admin_user.save.assert_called_once_with()
Beispiel #11
0
    def test_get_permissions(self, mock_group, mock_permission):
        """
        Tests if two permissions are get
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, True))

        staff_group = MagicMock(name="Staff")
        mock_group.objects.get_or_create = MagicMock(return_value=(staff_group,
                                                                   True))

        permission_mock = MagicMock(name="test permission")
        mock_permission.objects.get = MagicMock(return_value=permission_mock)

        manager.create_or_reset_admin_user()

        self.assertEqual(mock_permission.objects.get.call_count, 2,
                         "Permission.get should be called two times")
Beispiel #12
0
    def test_attributes_of_created_user(self, mock_user, mock_group):
        """
        Tests username and last_name of the created admin user.
        """
        admin_user = MagicMock(username='******', last_name='Administrator')
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, True))
        manager.model = mock_user

        manager.create_or_reset_admin_user()

        self.assertEqual(
            admin_user.username,
            'admin',
            "The username of a new created admin should be 'admin'.")
        self.assertEqual(
            admin_user.last_name,
            'Administrator',
            "The last_name of a new created admin should be 'Administrator'.")
Beispiel #13
0
    def test_get_permissions(self, mock_group, mock_permission):
        """
        Tests if two permissions are get
        """
        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, True))

        staff_group = MagicMock(name="Staff")
        mock_group.objects.get_or_create = MagicMock(return_value=(staff_group, True))

        permission_mock = MagicMock(name="test permission")
        mock_permission.objects.get = MagicMock(return_value=permission_mock)

        manager.create_or_reset_admin_user()

        self.assertEqual(
            mock_permission.objects.get.call_count,
            2,
            "Permission.get should be called two times")
Beispiel #14
0
    def test_get_admin_group(self, mock_group):
        """
        Tests that the Group with pk=4 is added to the admin.
        """
        def mock_side_effect(pk):
            if pk == 2:
                result = 'mock_registered'
            elif pk == 4:
                result = 'mock_staff'
            else:
                result = ''
            return result

        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))
        mock_group.objects.get.side_effect = mock_side_effect

        manager.create_or_reset_admin_user()

        mock_group.objects.get.assert_called_once_with(pk=4)
        admin_user.groups.add.assert_called_once_with('mock_staff')
Beispiel #15
0
    def test_get_admin_group(self, mock_group):
        """
        Tests that the Group with pk=4 is added to the admin.
        """
        def mock_side_effect(pk):
            if pk == 2:
                result = 'mock_registered'
            elif pk == 4:
                result = 'mock_staff'
            else:
                result = ''
            return result

        admin_user = MagicMock()
        manager = UserManager()
        manager.get_or_create = MagicMock(return_value=(admin_user, False))
        mock_group.objects.get.side_effect = mock_side_effect

        manager.create_or_reset_admin_user()

        mock_group.objects.get.assert_called_once_with(pk=4)
        admin_user.groups.add.assert_called_once_with('mock_staff')