1

I want to test Urls whether am getting 500 error or not. In normal case where login is not required I get status_code 200 but where login is required, it gives me 302 error. So, how can one test loginrequired and paramterized url in best way.

Thank You

So I am adding this because someone link that question into duplicate but it is not my answer and why it is not my answer because I can login with this method but I want to test that url whose views has loginrequired I can log in but not in that view

c.post('login/', {
'username': 'nitin',
'password': 'qwerty123321'})

if you don't know the answer with url then checkout question on 'views' How to test views with pytest whose views has LoginRequired and some specific user dependencies in that question I am getting user logged in with mixer.blend() and that's fine but in 'views' the user has some more functions related to that just check once. And please help me with that Thank you.

And atleast tell me how should I do this I am very messed with that. Thank you again

urls.py

path('', event_views.dashboard_view, name='event-dashboard'),

views.py

@login_required
def dashboard_view(request):
    # ccd = Org.objects.first()
    # print(ccd.__dict__)
    # print(request.user.)
    org = request.user.profile.org
    week_responses = day_wise_responses(7, org)
    user_org = request.user.profile.org.name
    sms_sent = org.sms_counter
    email_sent = org.email_counter
    today = datetime.today().date()
    responses_one_week = number_of_responses(7, org)
    responses_two_week = number_of_responses(14, org)
    average_rating = org_average_rating(org)
    responses_last_week = responses_two_week - responses_one_week
    if responses_last_week:
        responses_percent_change = (abs(responses_one_week - responses_last_week)/responses_last_week)*100
    else:
        responses_percent_change = responses_one_week*100
    # last n responses
    last_5_responses = last_n_responses(5, org)
    # print(last_5_responses)
    context = {'week_responses': week_responses, 'user_org': user_org, 'today': today,
               'responses_one_week': responses_one_week, 'responses_percent_change': responses_percent_change,
               'last_5_responses': last_5_responses, 'sms_sent': sms_sent, 'email_sent': email_sent,
               'average_rating': average_rating}
    return render(request, 'events/dashboard.html', context)

test_urls.py

@pytest.mark.django_db
class TestUrls(test.TestCase):
def test_event_dashboard(self):
c = Client()
c.post('login/', {
'username': 'nitin',
'password': 'qwerty123321'})
 response = c.get(reverse('event-dashboard'))
 self.assertEqual(response.status_code, 200)


Community
  • 1
  • 1
Nikhil Bhardwaj
  • 324
  • 4
  • 14
  • Can you elaborate why are you expecting 500? 500 is server-side failure. If a view is marked as `@login_required` and current user is not logged in then - yes, it will redirect user to login page. Why would it return 500? – Ivan Starostin Apr 09 '19 at 07:40
  • yaa but I want to test it with login user why not? Threre are some specific situation let me elaborate this if u get that, after user logged in, there are some pages where we are getting 500 error, so to test those pages where we get 500 error we want to use Testing functionality. – Nikhil Bhardwaj Apr 09 '19 at 07:49
  • @IvanStarostin And I know it will redirect me to that login page and if you want check my other question related to that thing, but with Views. So please check that question https://stackoverflow.com/questions/55546688/how-to-test-views-with-pytest-whose-views-has-loginrequired-and-some-specific-us a little help will be appreciated sir.... – Nikhil Bhardwaj Apr 09 '19 at 07:52

1 Answers1

2

The 302 is because your user is being redirected to the login screen.

If you want to test views that require authentication, you'll need to authenticate the user first.

Luckily, this is very easy to do. See the docs.

# Create a new user
User.objects.create_user(
    username='fred',
    password='secret'
)

# Start up a test client
c = Client()

# Authenticate the user on the client
c.login(username='fred', password='secret')

# Do your thing
response = c.get(reverse('event-dashboard'))
self.assertEqual(response.status_code, 200)
Brachamul
  • 1,449
  • 2
  • 13
  • 30
  • can u show me with updated code above – Nikhil Bhardwaj Apr 09 '19 at 00:11
  • Just replace `c.post('login/', {'username': 'nitin', 'password': 'qwerty123321'})` with `c.login(username='nitin', password='qwerty123321')` – Brachamul Apr 09 '19 at 00:16
  • still getting 302 status_code – Nikhil Bhardwaj Apr 09 '19 at 00:19
  • what are you being redirected to @NikhilBhardwaj ? – Brachamul Apr 09 '19 at 10:07
  • after login?? to hompage of our website, actually no one can visit homepage if that person is not logged in, the first page we are showing to a user is login page... and if you want then you can check the view above, the dashboard is our page we are showing to user after log in.. – Nikhil Bhardwaj Apr 09 '19 at 10:12
  • 1
    If you're getting a 302 status_code, you're being redirected somewhere. If you know where, it's easier to debug why you're being redirected. Have you created a user in your test's setup ? Because there's a possibility that the login failed ! Perhaps add `User.objects.create_user(username='fred', password='secret')` if you don't have that yet. – Brachamul Apr 09 '19 at 11:01
  • thanks sir It was a mistake in redirection part. Actually it was redirecting me to some another page, the main problem was redirection from starting and also I forgot to add ```User.objects.create_user(username='fred', password='secret')``` that line. You can write it as answer please. And thanks to give me your precious time. – Nikhil Bhardwaj Apr 09 '19 at 16:48