I have a problem with template testing. Everything has worked fine until I've added a login_required decorator. Now I receive an assertion error when I testing status code and error with template load during Template Used testing. I've tried many things (create superusers, is_active=True and so on) but with no results. What I'm doing wrong?
P.S All solutions included Django's self.client.login(...) does not work in unit tests doesn't work.
test.py
class CityViewTests(TestCase):
def setUp(self):
user = User.objects.create_user(username='test_username',
password='12345', email='[email protected]')
def test_call_view_loads(self):
self.client.login(username='test_username', password='12345')
response = self.client.get('main_view')
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'main_view.html')
url.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^main_view/$', views.main_view, name='main_view'),
url(r'^turn_calculations/$', views.turn_calculations,
name='turn_calculations')
]
views.py
@login_required
def main_view(request):
user = User.objects.get(id=request.user.id)
city_id = City.objects.get(user_id=user.id).id
city = City.objects.get(id=city_id)
profile = Profile.objects.get(user_id=request.user.id)
population = Citizen.objects.filter(city_id=city_id).count()
income =
Citizen.objects.filter(city_id=city_id).aggregate(Sum('income'))
['income__sum']
max_population = Residential.objects.filter(city_id=city_id).aggregate(Sum('max_population'
))['max_population__sum']
house_number = Residential.objects.filter(city_id=city_id).count()
return render(request, 'main_view.html', {'city': city,
'profile': profile,
'population': population,
'max_population': max_population,
'house_number': house_number,
'income': income})