コード例 #1
0
ファイル: signals.py プロジェクト: Princeton-CDH/django-pucas
def cas_login(sender, user, created, **kwargs):
    # Do an LDAP lookup to populate user data. Since CAS authentication
    # succeeded, should be no ambiguity in finding user info.

    # only populate attributes when a new user is created
    if created:
        user_info_from_ldap(user)
コード例 #2
0
    def handle(self, *args, **options):
        User = get_user_model()

        ldap_search = LDAPSearch()
        netids = options['netids']
        admin = options['admin']
        staff = options['staff']
        for netid in netids:
            try:
                # make sure we can find the netid in LDAP first
                ldap_search.find_user(netid)
                user, created = User.objects.get_or_create(username=netid)
                # NOTE: should we re-init data from ldap even if user
                # already exists, or error?
                user_info_from_ldap(user)

                # If admin flag is set, make the user an admin
                if admin or staff:
                    user.is_staff = True
                    if admin:
                        user.is_superuser = True
                    user.save()

                self.stdout.write(
                    self.style.SUCCESS(
                        "%s user '%s'" %
                        ('Created' if created else 'Updated', netid)))

            except LDAPSearchException:
                self.stderr.write(
                    self.style.ERROR("LDAP information for '%s' not found" %
                                     netid))
コード例 #3
0
ファイル: tests.py プロジェクト: Princeton-CDH/django-pucas
    def test_extra_init(self, mock_ldapsearch):
        mockuser = mock.Mock(username='******')

        mock_ldapsearch.return_value.find_user.return_value = mock.Mock()
        user_info_from_ldap(mockuser)
        # check for custom field set by test extra init method
        assert mockuser.extra == 'custom init'
        mockuser.save.assert_called_with()
コード例 #4
0
ファイル: tests.py プロジェクト: Princeton-CDH/django-pucas
    def test_attrs(self, mock_ldapsearch):
        mockuser = mock.Mock(username='******')
        # simulate no user info returned
        mock_ldapsearch.return_value.find_user.return_value = None
        user_info_from_ldap(mockuser)
        # ldap search init should be called with no args
        mock_ldapsearch.assert_called_with()
        # find user should be called with username
        mock_ldapsearch.return_value.find_user.assert_called_with('jdoe')
        # user save should not be called - no data
        mockuser.save.assert_not_called()

        mock_ldapinfo = MockLDAPInfo(eduPerson='*****@*****.**',
                                     givenName='John',
                                     surname='Doe',
                                     mail='*****@*****.**',
                                     extra='foo')
        # first test that list style attributes are set in order, and string
        # attributes are set as given
        mock_ldapsearch.return_value.find_user.return_value = mock_ldapinfo
        user_info_from_ldap(mockuser)
        assert mockuser.first_name == mock_ldapinfo.givenName
        assert mockuser.last_name == mock_ldapinfo.surname
        assert mockuser.email == mock_ldapinfo.mail
        mockuser.save.assert_called_with()

        # second test that should pass over an unset eduPerson attr and
        # set using givenName in list
        # NOTE: recreating mock to clear all assigned attrs
        delattr(mock_ldapinfo, 'mail')
        user_info_from_ldap(mockuser)
        assert mockuser.first_name == mock_ldapinfo.givenName
        assert mockuser.last_name == mock_ldapinfo.surname
        assert mockuser.email == mock_ldapinfo.eduPerson
        mockuser.save.assert_called_with()

        # missing attribute altogether should result in an empty string
        delattr(mock_ldapinfo, 'givenName')
        delattr(mock_ldapinfo, 'surname')
        mockuser = mock.Mock(username='******')
        # set to none to avoid Mock returning a mock and default behavior of
        # getattr
        mockuser.first_name = None
        mockuser.last_name = None
        mock_ldapsearch.return_value.find_user.return_value = mock_ldapinfo
        user_info_from_ldap(mockuser)
        assert mockuser.first_name == ''
        assert mockuser.last_name == ''
        assert mockuser.email == mock_ldapinfo.eduPerson
        mockuser.save.assert_called_with()
コード例 #5
0
ファイル: tests.py プロジェクト: Princeton-CDH/django-pucas
    def test_no_attrs(self, mock_ldapsearch):
        mockuser = mock.Mock()

        user_info_from_ldap(mockuser)
        mock_ldapsearch.assert_not_called()