Opened 17 years ago

Closed 16 years ago

Last modified 14 years ago

#5938 closed (wontfix)

test.Client.login_user(user_instance)

Reported by: Thomas Güttler <hv@…> Owned by: nobody
Component: Testing framework Version: dev
Severity: Keywords:
Cc: hv@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I don't want to use a password in the unittests. This patch adds the method login_user() to
test.Client.

The patch contains updates for the documentation and for the regression tests.

Attachments (1)

testclient_login_without_password.diff (2.5 KB ) - added by Thomas Güttler <hv@…> 17 years ago.

Download all attachments as: .zip

Change History (6)

by Thomas Güttler <hv@…>, 17 years ago

comment:1 by MichaelBishop, 17 years ago

Triage Stage: UnreviewedDesign decision needed

The patch appears complete with documentation. Decision needs to be made if this should be included.

comment:2 by Russell Keith-Magee, 16 years ago

Resolution: wontfix
Status: newclosed

If you have a test user, you can set up the test password to be whatever you want, so the problem isn't putting passwords in plain text. The login() interface isn't that difficult to use, so you can't make a 'shortcut/ease of use argument' either.

Unless someone can provide a particularly compelling reason for adding this method, I'm marking it wontfix.

comment:3 by Thomas Güttler, 16 years ago

Cc: hv@… removed

comment:4 by Thomas Güttler, 16 years ago

Cc: hv@… added

I still think this patch is useful. I have a bunch of read only tests which I run against the production system.
And I don't want to store the password in my code.

Since I try to not modify django code I found this dirty solution:

def fake_authenticate(user):
    assert isinstance(user, User), user
    if not hasattr(user, "backend"): 
        backend=get_backends()[0] 
        user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__) 
    return user

def client_login_user(client, user):
    # Change to django code base was rejected (http://code.djangoproject.com/ticket/5938). 
    # Dirty hack to login user-object without knowing his password.
    # Not threadsafe.
    assert isinstance(user, User), user
    assert isinstance(client, Client), client
    from django.test import client as client_module
    orig_authenticate=client_module.authenticate
    try:
        client_module.authenticate=fake_authenticate
        client.login(user=user)
    finally:
        client_module.authenticate=orig_authenticate
    return user

comment:5 by Thomas Güttler, 14 years ago

New ticket #14350

Note: See TracTickets for help on using tickets.
Back to Top