本文整理汇总了Python中lib.auth.backends.MozLdapBackend类的典型用法代码示例。如果您正苦于以下问题:Python MozLdapBackend类的具体用法?Python MozLdapBackend怎么用?Python MozLdapBackend使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MozLdapBackend类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_authenticate_with_ldap_existing_user
def test_authenticate_with_ldap_existing_user(self):
assert not User.objects.all().exists()
user = User.objects.create(
username='foo',
first_name=u'P\xe4ter',
last_name=u'B\xa3ngtsson',
)
user.set_unusable_password()
user.save()
ldap.open = Mock('ldap.open')
ldap.open.mock_returns = Mock('ldap_connection')
ldap.set_option = Mock(return_value=None)
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': self.fake_user,
'ou=groups,dc=mozilla': self.fake_group
}))
backend = MozLdapBackend()
user = backend.authenticate('foo', 'secret')
ok_(user)
_first_name = self.fake_user[0][1]['givenName'][0]
eq_(user.first_name, unicode(_first_name, 'utf-8'))
_last_name = self.fake_user[0][1]['sn'][0]
eq_(user.last_name, unicode(_last_name, 'utf-8'))
eq_(user.email, self.fake_user[0][1]['mail'][0])
user_saved = User.objects.get(username='foo')
eq_(user_saved.first_name, user.first_name)
eq_(user_saved.last_name, user.last_name)
示例2: test_authenticate_with_ldap_existing_user
def test_authenticate_with_ldap_existing_user(self):
assert not User.objects.all().exists()
user = User.objects.create(
username='foo',
email='[email protected]',
first_name='P\xe4ter',
last_name='B\xa3ngtsson',
)
assert user.groups.all().count() == 0
user.set_unusable_password()
user.save()
ldap.open = Mock('ldap.open')
ldap.open.mock_returns = Mock('ldap_connection')
ldap.set_option = Mock(return_value=None)
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': self.fake_user,
'ou=groups,dc=mozilla': self.fake_group
}))
backend = MozLdapBackend()
user = backend.authenticate('[email protected]', 'secret')
self.assertTrue(user)
_first_name = self.fake_user[0][1]['givenName'][0]
self.assertEqual(user.first_name, _first_name.decode('utf-8'))
_last_name = self.fake_user[0][1]['sn'][0]
self.assertEqual(user.last_name, _last_name.decode('utf-8'))
self.assertEqual(user.email, self.fake_user[0][1]['mail'][0])
user_saved = User.objects.get(email='[email protected]')
self.assertEqual(user_saved.first_name, user.first_name)
self.assertEqual(user_saved.last_name, user.last_name)
self.assertTrue(user_saved.groups.filter(name='Localizers').exists())
示例3: test_authenticate_with_ldap_new_user_with_long_email
def test_authenticate_with_ldap_new_user_with_long_email(self):
assert not User.objects.all().exists()
ldap.open = Mock('ldap.open')
ldap.open.mock_returns = Mock('ldap_connection')
ldap.set_option = Mock(return_value=None)
long_email = '[email protected]'
fake_user = [
('mail=%s,...' % long_email,
{'cn': ['Peter Bengtsson'],
'givenName': ['Pet\xc3\xa3r'], # utf-8 encoded
'mail': [long_email],
'sn': ['Bengtss\xc2\xa2n'],
'uid': ['pbengtsson']
})
]
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': fake_user,
'ou=groups,dc=mozilla': self.fake_group
}))
backend = MozLdapBackend()
user = backend.authenticate(long_email, 'secret')
ok_(user)
ok_(User.objects.get(email=long_email))
ok_(len(User.objects.get(email=long_email).username) <= 30)
user = User.objects.get(first_name=u'Pet\xe3r')
eq_(user.last_name, u'Bengtss\xa2n')
ok_(not user.has_usable_password())
ok_(not user.check_password('secret'))
示例4: test_authenticate_with_ldap_existing_user
def test_authenticate_with_ldap_existing_user(self):
assert not User.objects.all().exists()
user = User.objects.create(username="foo", first_name=u"P\xe4ter", last_name=u"B\xa3ngtsson")
assert user.groups.all().count() == 0
user.set_unusable_password()
user.save()
ldap.open = Mock("ldap.open")
ldap.open.mock_returns = Mock("ldap_connection")
ldap.set_option = Mock(return_value=None)
ldap.initialize = Mock(
return_value=MockLDAP({"dc=mozilla": self.fake_user, "ou=groups,dc=mozilla": self.fake_group})
)
backend = MozLdapBackend()
user = backend.authenticate("foo", "secret")
ok_(user)
_first_name = self.fake_user[0][1]["givenName"][0]
eq_(user.first_name, unicode(_first_name, "utf-8"))
_last_name = self.fake_user[0][1]["sn"][0]
eq_(user.last_name, unicode(_last_name, "utf-8"))
eq_(user.email, self.fake_user[0][1]["mail"][0])
user_saved = User.objects.get(username="foo")
eq_(user_saved.first_name, user.first_name)
eq_(user_saved.last_name, user.last_name)
ok_(user_saved.groups.filter(name="Localizers").exists())
示例5: test_authenticate_with_ldap_new_user_with_long_email
def test_authenticate_with_ldap_new_user_with_long_email(self):
assert not User.objects.all().exists()
ldap.open = Mock("ldap.open")
ldap.open.mock_returns = Mock("ldap_connection")
ldap.set_option = Mock(return_value=None)
long_email = "[email protected]"
fake_user = [
(
"mail=%s,..." % long_email,
{
"cn": ["Peter Bengtsson"],
"givenName": ["Pet\xc3\xa3r"], # utf-8 encoded
"mail": [long_email],
"sn": ["Bengtss\xc2\xa2n"],
"uid": ["pbengtsson"],
},
)
]
ldap.initialize = Mock(
return_value=MockLDAP({"dc=mozilla": fake_user, "ou=groups,dc=mozilla": self.fake_group})
)
backend = MozLdapBackend()
user = backend.authenticate(long_email, "secret")
ok_(user)
ok_(User.objects.get(email=long_email))
ok_(len(User.objects.get(email=long_email).username) <= 30)
user = User.objects.get(first_name=u"Pet\xe3r")
eq_(user.last_name, u"Bengtss\xa2n")
ok_(not user.has_usable_password())
ok_(not user.check_password("secret"))
示例6: test_authenticate_with_non_ascii_password
def test_authenticate_with_non_ascii_password(self):
assert not User.objects.all().exists()
ldap.open = Mock('ldap.open')
ldap.open.mock_returns = Mock('ldap_connection')
ldap.set_option = Mock(return_value=None)
email = '[email protected]'
fake_user = [
('mail=%s,...' % email,
{'cn': ['Peter Bengtsson'],
'givenName': ['Peter'],
'mail': [email],
'sn': ['Bengtsson'],
'uid': ['pbengtsson']
})
]
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': fake_user,
'ou=groups,dc=mozilla': self.fake_group
}))
backend = MozLdapBackend()
user = backend.authenticate(email, 's\xc4cret')
self.assertTrue(user)
self.assertTrue(User.objects.get(email=email))
示例7: test_authenticate_with_ldap_wrong_username
def test_authenticate_with_ldap_wrong_username(self):
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': self.fake_user,
'ou=groups,dc=mozilla': self.fake_group
}, credentials={
'some-other-uid': 'secret'
}))
backend = MozLdapBackend()
user = backend.authenticate('foo', 'secret')
eq_(user, None)
示例8: test_authentication_ldap_username_not_recognized
def test_authentication_ldap_username_not_recognized(self):
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': None,
'ou=groups,dc=mozilla': self.fake_group
}, credentials={
self.fake_user[0][0]: 'secret'
}))
backend = MozLdapBackend()
user = backend.authenticate('foo', 'secret')
ok_(not user)
示例9: test_authenticate_without_ldap
def test_authenticate_without_ldap(self):
assert not User.objects.all().exists()
user = User.objects.create(username="foo", email="[email protected]")
user.set_password("secret")
user.save()
backend = MozLdapBackend()
eq_(backend.authenticate("[email protected]", "secret"), user)
eq_(backend.authenticate("foo", "secret"), user)
eq_(backend.authenticate("foo", "JUNK"), None)
示例10: test_authentication_ldap_username_not_recognized
def test_authentication_ldap_username_not_recognized(self):
ldap.initialize = Mock(
return_value=MockLDAP(
{"dc=mozilla": None, "ou=groups,dc=mozilla": self.fake_group},
credentials={self.fake_user[0][0]: "secret"},
)
)
backend = MozLdapBackend()
user = backend.authenticate("foo", "secret")
ok_(not user)
示例11: test_authenticate_with_ldap_wrong_username
def test_authenticate_with_ldap_wrong_username(self):
ldap.initialize = Mock(
return_value=MockLDAP(
{"dc=mozilla": self.fake_user, "ou=groups,dc=mozilla": self.fake_group},
credentials={"some-other-uid": "secret"},
)
)
backend = MozLdapBackend()
user = backend.authenticate("foo", "secret")
eq_(user, None)
示例12: test_authenticate_with_ldap_wrong_password
def test_authenticate_with_ldap_wrong_password(self):
ldap.initialize = Mock(
return_value=MockLDAP(
{"dc=mozilla": self.fake_user, "ou=groups,dc=mozilla": self.fake_group},
credentials={self.fake_user[0][0]: "rightsecret"},
)
)
backend = MozLdapBackend()
user = backend.authenticate("foo", "secret")
eq_(user, None)
示例13: test_authenticate_with_ldap_wrong_password
def test_authenticate_with_ldap_wrong_password(self):
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': self.fake_user,
'ou=groups,dc=mozilla': self.fake_group
}, credentials={
self.fake_user[0][0]: 'rightsecret'
}))
backend = MozLdapBackend()
user = backend.authenticate('foo', 'secret')
eq_(user, None)
示例14: test_lost_group_privileges
def test_lost_group_privileges(self):
# test a user that is part of the `Localizers` group one day but not
# the other
assert not User.objects.all().exists()
ldap.open = Mock('ldap.open')
ldap.open.mock_returns = Mock('ldap_connection')
ldap.set_option = Mock(return_value=None)
groups = [
('cn=scm_l10n,ou=groups,dc=mozilla',
{'cn': ['scm_l10n']}),
('cn=buildteam,ou=groups,dc=mozilla',
{'cn': ['buildteam']})
]
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': self.fake_user,
'ou=groups,dc=mozilla': groups
}))
backend = MozLdapBackend()
user = backend.authenticate('foo', 'secret')
assert user == User.objects.get()
ok_(user.groups.filter(name='Localizers').exists())
ok_(user.groups.filter(name='build').exists())
new_groups = [
('cn=buildteam,ou=groups,dc=mozilla',
{'cn': ['buildteam']})
]
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': self.fake_user,
'ou=groups,dc=mozilla': new_groups
}))
user = backend.authenticate('foo', 'secret')
assert user == User.objects.get()
ok_(not user.groups.filter(name='Localizers').exists())
ok_(user.groups.filter(name='build').exists())
# Now reverse it
new_new_groups = [
('cn=scm_l10n,ou=groups,dc=mozilla',
{'cn': ['scm_l10n']})
]
ldap.initialize = Mock(return_value=MockLDAP({
'dc=mozilla': self.fake_user,
'ou=groups,dc=mozilla': new_new_groups
}))
user = backend.authenticate('foo', 'secret')
assert user == User.objects.get()
ok_(user.groups.filter(name='Localizers').exists())
ok_(not user.groups.filter(name='build').exists())
示例15: test_authenticate_without_ldap
def test_authenticate_without_ldap(self):
assert not User.objects.all().exists()
user = User.objects.create(username='foo', email='[email protected]')
user.set_password('secret')
user.save()
backend = MozLdapBackend()
eq_(backend.authenticate('[email protected]', 'secret'),
user)
eq_(backend.authenticate('foo', 'secret'), user)
eq_(backend.authenticate('foo', 'JUNK'), None)