Authenticate user in Selenium test - django

I'm testing user authentication in Django project with Selenium webdriver, and created a test:
class EditorBlogTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def tearDown(self):
self.browser.quit()
def create_session_store(self):
user = User.objects.create(email='a#b.com', username='TestUser')
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore()
store[SESSION_KEY] = user.pk
store.save()
return store
def test_user_authenticated(self):
store = self.create_session_store()
self.browser.get(self.live_server_url + '/dashboard/')
self.browser.add_cookie({
'name': settings.SESSION_COOKIE_NAME,
'value': store.session_key,
'path': '/',
})
self.browser.get(self.live_server_url + '/dashboard/blog/add/')
hello_box = self.browser.find_element_by_id('hello')
self.assertIsNotNone(self.browser.get_cookie('sessionid'))
self.assertEqual('Hello, TestUser', hello_box.text)
in template there is a line:
<div id="hello">Hello, {{ user }}</div>
But the test failed:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_session_creation (functional_tests.dashboard.test_blog.EditorBlogTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/django/my_project/functional_tests/dashboard/test_blog.py", line 26, in test_user_authenticated
self.assertEqual('Hello, TestUser', hello_box.text)
AssertionError: 'Hello, TestUser' != 'Hello, AnonymousUser'
- Hello, TestUser
+ Hello, AnonymousUser
----------------------------------------------------------------------
Ran 1 test in 2.406s
FAILED (failures=1)
Destroying test database for alias 'default'...
Does anyone have an idea what's wrong with it?

I've made a deeper research of the topic and found that session has nothing to do with RequestContext, that contains {{ user }} variable (as well as the rest context). So, the easiest way to authenticate a user in Selenium test is to open login page on the site and fill the form with test user credentials. After that my test managed to pass.

Related

Select related executes queries

I can't make select_related work with the following configuration:
Models:
class Text(models.Model):
author = models.ForeignKey('authors.Author',
on_delete=models.SET_NULL,
blank=True,
null=True)
Author model:
class Author(models.Model):
name = models.CharField(max_length=200)
def get_absolute_url(self):
kwargs = {'pk': self.pk}
return reverse('author-detail', kwargs=kwargs)
View
In a view, I use the select_related function to avoid hitting the db when querying for text's authors e.g.:mytext.author:
class TextsViewTest(TestCase):
def text_view(request,
pk,
template_name='texts/detail.html'):
source_text = Text.objects.select_related('author').get(pk=pk)
return render(request, template_name,
{
'source': source_text,
})
Test
According to select_related it shouldn't hit the database when accessing the Text.author relationship, but when testing it with:
def test_layout_content_header__uses_prefetched_relationships(self):
author = Author.objects.create(name="foobar")
source_text = Text.objects.create(author=author)
context = {'source': source_text}
with self.assertNumQueries(0):
from django.template.loader import render_to_string
rendered = render_to_string("text/_content_header.html", context)
template
text/content_header.html:
{% if source.author %} by <em>{{source.author.name}}</em>{% endif %}
Output
./manage test texts.test_views shows a hit:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_layout_content_header__uses_prefetched_relationships (author.tests.test_views.TextsViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/.../text/tests/test_views.py", line 1035, in test_layout_content_header__uses_prefetched_relationships
source_text.author
File "/.../lib/python3.6/site-packages/django/test/testcases.py", line 80, in __exit__
'%d. %s' % (i, query['sql']) for i, query in enumerate(self.captured_queries, start=1)
AssertionError: 1 != 0 : 1 queries executed, 0 expected
Captured queries were:
1. SELECT "authors_author"."id", "authors_author"."name", FROM "authors_author" WHERE "authors_author"."id" = 1
----------------------------------------------------------------------
Ran 1 test in 0.489s
FAILED (failures=1)
Destroying test database for alias 'default'...
Any ideas?
It looks like you are not using your view's code inside the test. Try either to copy same query into your test, e.g.:
context = {'source': Text.objects.select_related('author').get(pk=source_text.pk)}
with self.assertNumQueries(0):
from django.template.loader import render_to_string
rendered = render_to_string("text/_content_header.html", context)
Or reuse the view code (it seems to be declared in the Test Case, right?)
with self.assertNumQueries(1):
self.text_view(MagicMock(), source_text.pk)
Although you might need to specify bit more advanced request mock, e.g. using the RequestFactory

Django test client, not creating models (--keepdb option being used)

I'm trying to setup some models in the test database and then post to a custom form which includes a file upload.
Nothing seems to be persisting in the database, and I'm unsure about why the test when I perform the POST is sending back a 200 response? With follow=False, shouldn't it be a 302?
Also, when I try to look for the model in the database, it finds nothing.
And when I look at the database when using the --keepdb option, nothing is there either?
What am I doing wrong?
class ImportTestCase(TestCase):
remote_data_url = "http://test_data.csv"
local_data_path = "test_data.csv"
c = Client()
password = "password"
def setUp(self):
utils.download_file_if_not_exists(self.remote_data_url, self.local_data_path)
self.my_admin = User.objects.create_superuser('jonny', 'jonny#testclient.com', self.password)
self.c.login(username=self.my_admin.username, password=self.password)
def test_create_organisation(self):
self.o = Organization.objects.create(**{'name': 'TEST ORG'})
def test_create_station(self):
self.s = Station.objects.create(**{'name': 'Player', 'organization_id': 1})
def test_create_window(self):
self.w = Window.objects.create(**{'station_id': 1})
def test_create_component(self):
self.c = Component.objects.create(**{
'type': 'Player',
'window_id': 1,
'start': datetime.datetime.now(),
'end': datetime.datetime.now(),
'json': "",
'layer': 0}
)
def test_csv_import(self):
"""Testing that standard csv imports work"""
self.assertTrue(os.path.exists(self.local_data_path))
with open(self.local_data_path) as fp:
response = self.c.post('/schedule/schedule-import/create/', {
'component': self.c,
'csv': fp,
'date_from': datetime.datetime.now(),
'date_to': datetime.datetime.now()
}, follow=False)
self.assertEqual(response.status_code, 200)
def test_record_exists(self):
new_component = Component.objects.all()
self.assertTrue(len(new_component) > 0)
And the test results
Using existing test database for alias 'default'...
.....[]
F
======================================================================
FAIL: test_record_exists (schedule.tests.ImportTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 64, in test_record_exists
self.assertTrue(len(new_component) > 0)
AssertionError: False is not true
----------------------------------------------------------------------
Ran 6 tests in 1.250s
FAILED (failures=1)
The --keepdb option means that the database is kept. That means it's quicker to run the tests again because you don't have to recreate the table.s
However, each test method in a TestCase class is run in a transaction which is rolled back after the method has finished. Using --keepdb doesn't change this.
This means that your object created in test_create_component will not be seen by the test_record_exists test. You can either create the object inside the test_record_exists method, or in the setUp method or setUpTestData classmethod.

Error with Django assertJSONEqual when unit testing view

Similar to this question, I'm having problems testing a view with Django's unit testing framework. My view is very simple: it processes a form, adds an object to the database, and returns a JSONResponse. The test is equally simple, but I keep getting "First argument is not valid JSON: ''. The code actually works in my application; it just doesn't seem to work when unit testing. Any help is appreciated.
EDIT:
Traceback
======================================================================
ERROR: tearDownClass (zoning_intake.tests.AddActionTypeTest)
Traceback (most recent call last):
File "C:\Virtual\Django18\lib\site-packages\django\test\testcases.py", line 96
2, in tearDownClass
cls._rollback_atomics(cls.cls_atomics)
AttributeError: type object 'AddActionTypeTest' has no attribute 'cls_atomics'
======================================================================
FAIL: test_post_add_action_type_succeeds (zoning_intake.tests.AddActionTypeTest)
Traceback (most recent call last):
File "C:\Hg\sdcgis\zoning_intake\tests.py", line 26, in test_post_add_action_t
ype_succeeds
self.assertJSONEqual(response.content,{'result':'Success', 'msg':'Success'})
File "C:\Virtual\Django18\lib\site-packages\django\test\testcases.py", line 68
9, in assertJSONEqual
self.fail("First argument is not valid JSON: %r" % raw)
AssertionError: First argument is not valid JSON: ''
Ran 1 test in 10.757s
FAILED (failures=1, errors=1)
Preserving test database for alias 'default'...
Preserving test database for alias 'other'...
My view:
form = ActionTypeForm(request.POST)
if form.is_valid():
action = form.cleaned_data['action']
new_type = CaseRequestActionType(action=action)
new_type.save()
return JsonResponse({'result':'Success', 'msg':'Success'})
else:
return JsonResponse({'result':'Fail', 'msg':'An unknown error occurred'})
My test:
class AddActionTypeTest(TestCase):
if django.VERSION[:2] >= (1, 7):
# Django 1.7 requires an explicit setup() when running tests in PTVS
#classmethod
def setUpClass(cls):
django.setup()
def test_post_add_action_type_fails(self):
response = self.client.post(reverse('zoning:add_action_type'))
self.assertEqual(response.status_code, 302)
self.assertJSONEqual(force_text(response.content), {'result':'Fail', 'msg':'An unknown error occurred'})
So it turns out that the issue is very simple, and the 302 status code is the key to understanding why I had this issue. I have the #login_required decorator on my view, so when I ran the test WITHOUT having logged in a user, I'm redirected to my login view. Since the login view returns html, not JSON, my response is not valid JSON, and the status code returns 302 instead of the expected 200. I needed to override the setUp method to create a user in the database and then call login in the test itself in order for my test to work properly and my status code to return 200. Thanks to #Shang Wang for assistance
Complete View:
#login_required
def add_action_type(request):
if request.method == 'GET':
...
else:
form = ActionTypeForm(request.POST)
if form.is_valid():
action = form.cleaned_data['action']
new_type = CaseRequestActionType(action=action)
new_type.save()
return JsonResponse({'result':'Success', 'msg':'Success'})
else:
return JsonResponse({'result':'Fail', 'msg':'An unknown error occurred'})
Updated test:
class AddActionTypeTest(TestCase):
#classmethod
def setUp(self):
self.user = User.objects.create_user(username='shawn', email='shawn#...com', password='top_secret')
def test_post_add_action_type_fails(self):
self.client.login(username=self.user.username, password='top_secret')
response = self.client.post(reverse('zoning:add_action_type'))
self.assertJSONEqual(force_text(response.content), {'result':'Fail', 'msg':'An unknown error occurred'})

How to persist data to DB between tests with pytest-django?

How can I persist data to DB when using pytest/pytest-django in a test-run of a Django application?
I run pytest with py.test --nomigrations --reuse-db -s and the Postgres DB test_<configued_db_name> is created as expected, however nothing seems to be persisted to DB between tests and at the end of the test run the DB is empty.
import pytest
from django.contrib.auth.models import User
#pytest.mark.django_db(transaction=False)
def test_insert_user():
user = User.objects.create_user(username="test_user", email="test_user#test.com", password="test")
users = User.objects.all()
assert len(users) > 0
#pytest.mark.django_db(transaction=False)
def test_check_user():
users = User.objects.all()
assert len(users) > 0
The first test passes, the second does not making me wonder if anything is persisted to DB at all. According to the pystest-django documentation #pytest.mark.django_db(transaction=False) will not rollback whatever has been affected by the decorated test.
Thank you,
/David
Another way of prefilling the database with data for each function is like that:
import pytest
from django.contrib.auth.models import User
#pytest.fixture(scope='module')
def django_db_setup(django_db_setup, django_db_blocker):
print('setup')
with django_db_blocker.unblock():
User.objects.create(username='a')
assert set(u.username for u in User.objects.all()) == {'a'}
#pytest.mark.django_db
def test1():
print('test1')
User.objects.create(username='b')
assert set(u.username for u in User.objects.all()) == {'a', 'b'}
#pytest.mark.django_db
def test2():
print('test2')
User.objects.create(username='c')
assert set(u.username for u in User.objects.all()) == {'a', 'c'}
The good thing about this method is that the setup function is only called once:
plugins: django-3.1.2
collected 2 items
mytest.py setup
test1
.test2
.
=================== 2 passed in 1.38 seconds ====================
The bad thing is that 1.38 seconds is a bit too much for such a simple test. --reuse-db is a faster way to do it.
I have solved this problem -- prefill the DB for every function -- by defining a fixture with scope function (i.e. model and session will not work).
Here is the code for testing the views in Django.
# This is used to fill the database more easily
from mixer.backend.django import mixer
import pytest
from django.test import RequestFactory
from inventory import views
from inventory import services
pytestmark = pytest.mark.django_db
#pytest.fixture(scope="function")
def fill_db():
""" Just filling the DB with my data """
for elem in services.Search().get_lookup_data():
mixer.blend('inventory.Enumeration', **elem)
def test_grid_anonymous(fill_db):
request = RequestFactory().get('/grid/')
response = views.items_grid(request)
assert response.status_code == 200, \
"Should be callable by anyone"
def test_list_anonymous(fill_db):
request = RequestFactory().get('/')
response = views.items_list(request)
assert response.status_code == 200, \
"Should be callable by anyone"

Include django logged user in django Traceback error

What is the easist way to include username, first and last name and e-amil in django Traceback error.
I know that the way is create a custom error report:
Create a new class that innherit from django.views.debug.SafeExceptionReporterFilter
Set DEFAULT_EXCEPTION_REPORTER_FILTER
But, what method a should overwrite to receive traceback with also this information?
I would like that my treceback look likes:
Traceback (most recent call last):
File "/usr...o/core/handlers/base.py", line 89, in get_response
response = middleware_method(request)
File "/.../g...ap/utils/middleware.py", line 23,...
if elapsedTime.min > 15:
TypeError: can't compare datetime.timedelta to int
Logged user information:
User: pepito
name: Pepito Grillo
e-mail: grillo#peppeto.com
I did it using Custom Middleware. I'm not sure this is the best answer, but it is how I solved it for my project.
settings.py:
MIDDLEWARE_CLASSES = (
...
'utilities.custom_middleware.CustomMiddleware',
...
)
utilities/custom_middleware.py:
from utilities.request import AddRequestDetails
class CustomMiddleware(object):
"""
Adds user details to request context during request processing, so that they
show up in the error emails. Add to settings.MIDDLEWARE_CLASSES and keep it
outermost(i.e. on top if possible). This allows it to catch exceptions in
other middlewares as well.
"""
def process_exception(self, request, exception):
"""
Process the request to add some variables to it.
"""
# Add other details about the user to the META CGI variables.
try:
if request.user.is_authenticated():
AddRequestDetails(request)
request.META['AUTH_VIEW_ARGS'] = str(view_args)
request.META['AUTH_VIEW_CALL'] = str(view_func)
request.META['AUTH_VIEW_KWARGS'] = str(view_kwargs)
except:
pass
utilities/request.py:
def AddRequestDetails(request):
"""
Adds details about the user to the request, so any traceback will include the
details. Good for troubleshooting; this will be included in the email sent to admins
on error.
"""
if request.user.is_anonymous():
request.META['AUTH_NAME'] = "Anonymous User"
request.META['AUTH_USER'] = "Anonymous User"
request.META['AUTH_USER_EMAIL'] = ""
request.META['AUTH_USER_ID'] = 0
request.META['AUTH_USER_IS_ACTIVE'] = False
request.META['AUTH_USER_IS_SUPERUSER'] = False
request.META['AUTH_USER_IS_STAFF'] = False
request.META['AUTH_USER_LAST_LOGIN'] = ""
else:
request.META['AUTH_NAME'] = str(request.user.first_name) + " " + str(request.user.last_name)
request.META['AUTH_USER'] = str(request.user.username)
request.META['AUTH_USER_EMAIL'] = str(request.user.email)
request.META['AUTH_USER_ID'] = str(request.user.id)
request.META['AUTH_USER_IS_ACTIVE'] = str(request.user.is_active)
request.META['AUTH_USER_IS_SUPERUSER'] = str(request.user.is_superuser)
request.META['AUTH_USER_IS_STAFF'] = str(request.user.is_staff)
request.META['AUTH_USER_LAST_LOGIN'] = str(request.user.last_login)
My trivial solution (works in django 1.5)
settings.py:
MIDDLEWARE_CLASSES = (
...
'utilities.custom_middleware.UserTracebackMiddleware',
...
)
custom_middleware.py:
class UserTracebackMiddleware(object):
"""
Adds user to request context during request processing, so that they
show up in the error emails.
"""
def process_exception(self, request, exception):
if request.user.is_authenticated():
request.META['AUTH_USER'] = unicode(request.user.username)
else:
request.META['AUTH_USER'] = "Anonymous User"
hope it helps