Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing authentication in Django

Tags:

django

ldap

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 ?

like image 632
LDAPProblems Avatar asked Jun 29 '11 15:06

LDAPProblems


People also ask

What is test client in Django?

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.

What is tests PY in Django?

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.

How do I test a Django project?

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.

How do I authenticate a request in Django?

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.


1 Answers

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.

like image 192
vim Avatar answered Oct 19 '22 00:10

vim