python - Unable to login inside a Django unittest -


how simulate logging in admin inside django unittest? docs make seems simple, when that, 200 response , page saying "please log in again, because session has expired."

my unittest looks like:

def test_login(self):      client = client()     user = user.objects.get(username='jondoe')     user.set_password('password')     user.save()      response = self.client.post(         '/admin/login/',         {'username': 'jondoe', 'password': 'password'})     print(response)     self.assertequal(response.status_code, 200)     self.asserttrue('please log in again' not in str(response)) # fails?! 

my unittest's django settings like:

import os, sys  databases = {     'default':{         'engine': 'django.db.backends.sqlite3',     } } root_urlconf = 'myapp.tests.urls' installed_apps = [     'django.contrib.auth',     'django.contrib.admin',     'django.contrib.contenttypes',     'django.contrib.sessions',     'django.contrib.sites',     'django.contrib.staticfiles',     'django.contrib.messages',      'myapp',     'myapp.tests', ]  south_tests_migrate = false use_tz = true  auth_user_model = 'auth.user'  secret_key = 'secret'  site_id = 1  base_secure_url = 'https://localhost' base_url = 'http://localhost'  template_context_processors = (     'django.contrib.auth.context_processors.auth',     'django.core.context_processors.request',     'django.core.context_processors.i18n',     'django.contrib.messages.context_processors.messages',     'django.core.context_processors.static', )  static_url = '/static/' 

what doing wrong?

in test, you'll either need load fixture has user data need or create new user record, , can:

client.login(username='your-username', password='your-password') 

see: https://docs.djangoproject.com/en/1.6/topics/testing/tools/#django.test.client.client.login

to facilitate further tests client needed, suggest creating client instance in setup method of testcase:

class yourtest(testcase):     def setup(self):         self.client = client() 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -