Code

Opened 6 years ago

Closed 6 years ago

Last modified 4 years ago

#5938 closed (wontfix)

test.Client.login_user(user_instance)

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

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@…> 6 years ago.

Download all attachments as: .zip

Change History (6)

Changed 6 years ago by Thomas Güttler <hv@…>

comment:1 Changed 6 years ago by MichaelBishop

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

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

comment:2 Changed 6 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

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 Changed 6 years ago by guettli

  • Cc hv@… removed

comment:4 Changed 6 years ago by guettli

  • 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 Changed 4 years ago by guettli

New ticket #14350

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.