Example #1
0
    def test_day1_email_ldap_case_b_login_credentials(self) -> None:
        ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}

        ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
        mock_initialize = ldap_patcher.start()
        mock_ldap = MockLDAP()
        mock_initialize.return_value = mock_ldap

        mock_ldap.directory = {
            'uid=newuser,ou=users,dc=zulip,dc=com': {
                'userPassword': ['testing', ],
                'fn': ['full_name'],
                'sn': ['shortname'],
            }
        }

        with self.settings(
                LDAP_APPEND_DOMAIN='zulip.com',
                AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
                AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'):

            self.login_with_return("*****@*****.**", "testing")

            user = UserProfile.objects.get(email="*****@*****.**")
            scheduled_emails = ScheduledEmail.objects.filter(users=user)

            self.assertEqual(len(scheduled_emails), 2)
            email_data = ujson.loads(scheduled_emails[0].data)
            self.assertEqual(email_data["context"]["ldap"], True)
            self.assertEqual(email_data["context"]["ldap_username"], "newuser")
Example #2
0
    def test_change_password_ldap_backend(self) -> None:
        ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}
        ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
        mock_initialize = ldap_patcher.start()
        mock_ldap = MockLDAP()
        mock_initialize.return_value = mock_ldap

        mock_ldap.directory = {
            'uid=hamlet,ou=users,dc=zulip,dc=com': {
                'userPassword': [
                    'ldappassword',
                ],
                'fn': ['New LDAP fullname']
            }
        }

        self.login(self.example_email("hamlet"))
        with self.settings(LDAP_APPEND_DOMAIN="zulip.com",
                           AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password=initial_password(
                        self.example_email("hamlet")),
                    new_password="******",
                ))
            self.assert_json_error(result,
                                   "Your Zulip password is managed in LDAP")

            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password='******',
                    new_password="******",
                ))
            self.assert_json_error(result,
                                   "Your Zulip password is managed in LDAP")

        with self.settings(LDAP_APPEND_DOMAIN="example.com",
                           AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password=initial_password(
                        self.example_email("hamlet")),
                    new_password="******",
                ))
            self.assert_json_success(result)

        with self.settings(LDAP_APPEND_DOMAIN=None,
                           AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password=initial_password(
                        self.example_email("hamlet")),
                    new_password="******",
                ))
            self.assert_json_error(result,
                                   "Your Zulip password is managed in LDAP")
Example #3
0
    def test_day1_email_ldap_case_b_login_credentials(self) -> None:
        ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}

        ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
        mock_initialize = ldap_patcher.start()
        mock_ldap = MockLDAP()
        mock_initialize.return_value = mock_ldap

        mock_ldap.directory = {
            'uid=newuser,ou=users,dc=zulip,dc=com': {
                'userPassword': '******',
                'fn': ['full_name'],
                'sn': ['shortname'],
            }
        }

        with self.settings(
                LDAP_APPEND_DOMAIN='zulip.com',
                AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map,
                AUTH_LDAP_USER_DN_TEMPLATE='uid=%(user)s,ou=users,dc=zulip,dc=com'):

            self.login_with_return("*****@*****.**", "testing")

            user = UserProfile.objects.get(email="*****@*****.**")
            scheduled_emails = ScheduledEmail.objects.filter(user=user)

            self.assertEqual(len(scheduled_emails), 2)
            email_data = ujson.loads(scheduled_emails[0].data)
            self.assertEqual(email_data["context"]["ldap"], True)
            self.assertEqual(email_data["context"]["ldap_username"], "newuser")
Example #4
0
    def test_change_password_ldap_backend(self) -> None:
        ldap_user_attr_map = {'full_name': 'fn', 'short_name': 'sn'}
        ldap_patcher = patch('django_auth_ldap.config.ldap.initialize')
        mock_initialize = ldap_patcher.start()
        mock_ldap = MockLDAP()
        mock_initialize.return_value = mock_ldap

        mock_ldap.directory = {
            'uid=hamlet,ou=users,dc=zulip,dc=com': {
                'userPassword': ['ldappassword', ],
                'fn': ['New LDAP fullname']
            }
        }

        self.login(self.example_email("hamlet"))
        with self.settings(LDAP_APPEND_DOMAIN="zulip.com",
                           AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password=initial_password(self.example_email("hamlet")),
                    new_password="******",
                ))
            self.assert_json_error(result, "Your Zulip password is managed in LDAP")

            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password='******',
                    new_password="******",
                ))
            self.assert_json_error(result, "Your Zulip password is managed in LDAP")

        with self.settings(LDAP_APPEND_DOMAIN="example.com",
                           AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password=initial_password(self.example_email("hamlet")),
                    new_password="******",
                ))
            self.assert_json_success(result)

        with self.settings(LDAP_APPEND_DOMAIN=None,
                           AUTH_LDAP_USER_ATTR_MAP=ldap_user_attr_map):
            result = self.client_patch(
                "/json/settings",
                dict(
                    old_password=initial_password(self.example_email("hamlet")),
                    new_password="******",
                ))
            self.assert_json_error(result, "Your Zulip password is managed in LDAP")