I'm trying to implement LDAP authentication in a test Django project. I think I've done all the configuration correctly (I'm using the django_ldap_auth package) and I want to test the authentication without having to create a whole site with log in etc. Is that possible ? FYI, I'm only doing this as a part of making LDAP the main authentication method at my company, and have to plug the functionality into a large project.
I've tried running the server on localhost and ssh-ing through the terminal, but apparently that only logs me onto my own computer, I think :) (not an experienced Linux user).
Any suggestions ?
The test client is a Python class that acts as a dummy web browser, allowing you to test your views and interact with your Django-powered application programmatically.
Django uses the unittest module's built-in test discovery, which will discover tests under the current working directory in any file named with the pattern test*.py. Provided you name the files appropriately, you can use any structure you like.
The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.
If you have an authenticated user you want to attach to the current session - this is done with a login() function. To log a user in, from a view, use login() . It takes an HttpRequest object and a User object. login() saves the user's ID in the session, using Django's session framework.
Go to your Django project folder and Start the python interpreter with
python manage.py shell
and then do,
from django_auth_ldap.backend import LDAPBackend
ldapobj = LDAPBackend()
user = ldapobj.populate_user(<LDAP username of an existing user>)
user.is_anonymous()
if the last function call returns false then it your LDAP auth module works as expected.
edit:
Run the Python interpreter as above and,
import ldap
server = 'ldap://<your server ip>'
user_dn = '<dn for a known user>'
password = '<his or her password>'
con = ldap.initialize(server)
con.simple_bind_s(user_dn, password)
This will return SERVER_DOWN: {'desc': "Can't contact LDAP server"} exception, if you can't connect to the LDAP sever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With